Browse Source

把token放进请求拦截器header

master
蟑螂恶霸 3 years ago
parent
commit
6e004bd733
  1. 4
      src/http/http.js
  2. 62
      src/store/Common.js
  3. 4
      src/views/Home.vue
  4. 2
      src/views/login.vue

4
src/http/http.js

@ -1,8 +1,8 @@
import axios from 'axios'; import axios from 'axios';
import { $globalStore } from "../common/globalStore"
// import router from '@/router/home'; // import router from '@/router/home';
// import {cache} from '@/store/store'; // import {cache} from '@/store/store';
axios.defaults.withCredentials = true axios.defaults.withCredentials = true
const { useCommon } = $globalStore;
const http = axios.create({ const http = axios.create({
traditional: true, traditional: true,
contentType : "application/json", contentType : "application/json",
@ -14,8 +14,8 @@ http.interceptors.request.use(
config => { config => {
if(!config.headers['Content-Type']){ if(!config.headers['Content-Type']){
config.headers['Content-Type'] = 'application/json' config.headers['Content-Type'] = 'application/json'
config.headers['Authorization'] = 'Bearer '+ useCommon.token
} }
config.headers['token'] = $globalStore.useCommon.token
return config return config
}, },
err => { err => {

62
src/store/Common.js

@ -6,17 +6,17 @@ export default defineStore('useCommon', {
subMsgKey: "", // 定义通知(由于路由不再支持parmas传参丢失) subMsgKey: "", // 定义通知(由于路由不再支持parmas传参丢失)
goods_card_num: 0, goods_card_num: 0,
is_reload_card: false, is_reload_card: false,
id:"", id:"",
//企业统一信用代码 //企业统一信用代码
enterpriseNumber:"", enterpriseNumber:"",
//企业名称 //企业名称
enterpriseName:"", enterpriseName:"",
//当前登录用户ID //当前登录用户ID
userId:"", userId:"",
// token // token
token: "", token: "",
//当前登录用户的手机号 //当前登录用户的手机号
phonenumber:"" phonenumber:""
}), }),
getters: {}, getters: {},
actions: { actions: {
@ -32,26 +32,26 @@ export default defineStore('useCommon', {
SET_RELAOD_SHOP(data) { SET_RELAOD_SHOP(data) {
this.is_reload_card = data; this.is_reload_card = data;
}, },
//修改用户关联的企业纳税识别号 //修改用户关联的企业纳税识别号
SET_USER_ENTERPRISE_NUMBER(data){ SET_USER_ENTERPRISE_NUMBER(data){
this.enterpriseNumber = data; this.enterpriseNumber = data;
}, },
//修改用户关联的企业纳税识别号 //修改用户关联的企业纳税识别号
SET_USER_ENTERPRISE_NAME(data){ SET_USER_ENTERPRISE_NAME(data){
this.enterpriseName = data; this.enterpriseName = data;
}, },
//设置用户ID //设置用户ID
SET_USER_ID(data){ SET_USER_ID(data){
this.userId = data; this.userId = data;
}, },
//设置用户token //设置用户token
SET_TOKEN(data){ SET_TOKEN(data){
this.token = data; this.token = data;
}, },
//设置用户手机号码 //设置用户手机号码
SET_USER_PHONENUMBER(data){ SET_USER_PHONENUMBER(data){
this.phonenumber = data; this.phonenumber = data;
}, },
// 获取多语言数据 // 获取多语言数据
async getLang(params) { async getLang(params) {
let url = $globalRequestUrl({ let url = $globalRequestUrl({

4
src/views/Home.vue

@ -138,17 +138,13 @@ onMounted(() => {
const access_secret = '434fec85aa5846e88bbeb8fac6cf6d7f'; const access_secret = '434fec85aa5846e88bbeb8fac6cf6d7f';
const nonce = generateRandomNumber(); const nonce = generateRandomNumber();
const timestamp = `${Date.now()}`; const timestamp = `${Date.now()}`;
console.log(nonce);
console.log(timestamp);
const str = [access_secret, nonce, timestamp] const str = [access_secret, nonce, timestamp]
.sort() .sort()
.reduce((str, item) => str += item, ''); .reduce((str, item) => str += item, '');
console.log(str);
// 最终还需要转成全小写 // 最终还需要转成全小写
const signature = sha1(str).toString().toLowerCase(); const signature = sha1(str).toString().toLowerCase();
console.log(signature);
//深i企鉴权 //深i企鉴权
w6s.config({ w6s.config({
access_key: access_key, access_key: access_key,

2
src/views/login.vue

@ -38,7 +38,7 @@ const onSubmit = async (values) => {
console.log(values); console.log(values);
// await getGoodsnum(); // await getGoodsnum();
// await getLang(); // await getLang();
values.password = Encrypt(values.password); values.secretKey = Encrypt(values.password);
// return; // return;
userLogin(values,success=>{ userLogin(values,success=>{
console.log(success); console.log(success);

Loading…
Cancel
Save