From 2ef633a0c826f243b00ca6f52d729f8c73cc72c8 Mon Sep 17 00:00:00 2001 From: aixianling Date: Wed, 29 Jun 2022 16:41:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- src/components/utils/http.js | 29 ++++++++++++ src/components/utils/modules.js | 66 ++++++++++++++++++++++++++ src/main.js | 4 +- src/pages/home.vue | 22 ++++----- src/store/index.js | 84 +++------------------------------ src/utils/axios.js | 62 +++++++----------------- 7 files changed, 133 insertions(+), 138 deletions(-) create mode 100644 src/components/utils/http.js create mode 100644 src/components/utils/modules.js diff --git a/package.json b/package.json index 96c19b4..565e014 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": false, "author": "Kubbo", "scripts": { - "dev": "node bin/pages.js&&cross-env NODE_ENV=development VUE_APP_CW_MODE=dev UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch --minimize", + "dev": "node bin/pages.js&&cross-env NODE_ENV=development VUE_APP_CW_MODE=dev UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch", "pages": "node bin/pages.js", "lib": "node bin/lib.js&&npm unpublish --force&&npm publish&&node bin/clean.js", "lib:all": "node src/project/build.js&&npm unpublish --workspaces --force&&npm publish --workspaces&&node bin/clean.js", @@ -39,10 +39,10 @@ "@dcloudio/uni-stat": "^2.0.1-33920220208001", "@vue/shared": "^3.2.31", "axios": "^0.19.2", + "axios-miniprogram-adapter": "^0.3.2", "core-js": "^3.7.0", "dayjs": "^1.9.5", "dvcp-wui": "^1.0.1", - "flyio": "^0.6.14", "query-string": "^7.1.1", "regenerator-runtime": "^0.12.1", "uview-ui": "^1.7.8", diff --git a/src/components/utils/http.js b/src/components/utils/http.js new file mode 100644 index 0000000..6e85df0 --- /dev/null +++ b/src/components/utils/http.js @@ -0,0 +1,29 @@ +import axios from 'axios' +import adapter from 'axios-miniprogram-adapter' + +const instance = axios.create({ + timeout: 600000, + withCredentials: true, + adapter +}) +const getToken = () => { + let vuex = uni.getStorageSync("vuex") + return !!vuex ? JSON.parse(vuex).token : null +} +const source = axios.CancelToken.source(); +instance.interceptors.request.use(config => { + if (config.withoutToken) { + return config + } else if (getToken()) { + config.headers["Authorization"] = getToken() + } else { + config.cancelToken = source.token + source.cancel("用户未验证,取消请求:" + config.url) + } + return config +}, err => { + console.error(err) + return Promise.reject(err) +}) + +export default instance diff --git a/src/components/utils/modules.js b/src/components/utils/modules.js new file mode 100644 index 0000000..787ed5c --- /dev/null +++ b/src/components/utils/modules.js @@ -0,0 +1,66 @@ +import http from "./http"; +import Vue from "vue"; + +/** + * 用户登录信息 + */ +export const user = { + state: () => ({}), + mutations: { + setUser(state, user) { + for (const key in user) { + Vue.set(state, key, user[key]) + } + }, + }, + actions: { + getUserInfo({commit}) { + //获取企业微信后台账号信息 + //党员认证状态 partyStatusForWX:0、未认证 1、认证中 2、已认证 + return http.post("/app/appwechatuser/check").then(res => { + if (res?.code == 0) { + commit('setUser', res.data) + return Promise.all([]) + } + }) + }, + getCode({dispatch}, count = 0) { + if (count > 3) { + return Promise.reject("无法获取code") + } else return new Promise((resolve, reject) => { + uni.login({ + success: res => { + if (res?.code) { + resolve(res.code); + } else { + reject(res); + } + }, + fail: () => resolve(dispatch("getCode", ++count)) + }) + }) + }, + getToken({commit}, code) { + if (code) { + return http.post("/auth/wechat-con/token", {code}, { + headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="}, + withoutToken: true + }).then(res => { + if (res?.access_token) { + const token = [res?.token_type, res?.access_token].join(" ").trim() + commit("setToken", token) + return token + } else { + uni.showToast({title: res?.msg}) + return Promise.resolve(res?.msg) + } + }) + } else return Promise.reject("缺少登录code") + }, + autoLogin({dispatch}, count = 0) { + if (count <= 3) { + return dispatch("getCode").then(code => dispatch("getToken", code).catch(() => dispatch("autoLogin", ++count))) + } else return Promise.reject("登录失败,请联系管理员") + } + } +} diff --git a/src/main.js b/src/main.js index 242acab..27ac2eb 100644 --- a/src/main.js +++ b/src/main.js @@ -9,13 +9,13 @@ import 'uview-ui/theme.scss' Vue.use(vui) Vue.config.productionTip = false -Vue.prototype.$instance = axios.instance +Vue.prototype.$instance = axios Vue.prototype.$areaId = config.areaId; Vue.prototype.$areaName = config.areaName; Vue.prototype.$cdn = 'https://cdn.cunwuyun.cn/' App.mpType = 'app' Object.keys(utils).map(e => Vue.prototype[e] = utils[e]) -utils.$dict.init(axios.instance) +utils.$dict.init(axios) const app = new Vue({ store, diff --git a/src/pages/home.vue b/src/pages/home.vue index 4dc5053..7862707 100644 --- a/src/pages/home.vue +++ b/src/pages/home.vue @@ -30,11 +30,10 @@
-