整合小程序库
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
"private": false,
|
"private": false,
|
||||||
"author": "Kubbo",
|
"author": "Kubbo",
|
||||||
"scripts": {
|
"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",
|
"pages": "node bin/pages.js",
|
||||||
"lib": "node bin/lib.js&&npm unpublish --force&&npm publish&&node bin/clean.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",
|
"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",
|
"@dcloudio/uni-stat": "^2.0.1-33920220208001",
|
||||||
"@vue/shared": "^3.2.31",
|
"@vue/shared": "^3.2.31",
|
||||||
"axios": "^0.19.2",
|
"axios": "^0.19.2",
|
||||||
|
"axios-miniprogram-adapter": "^0.3.2",
|
||||||
"core-js": "^3.7.0",
|
"core-js": "^3.7.0",
|
||||||
"dayjs": "^1.9.5",
|
"dayjs": "^1.9.5",
|
||||||
"dvcp-wui": "^1.0.1",
|
"dvcp-wui": "^1.0.1",
|
||||||
"flyio": "^0.6.14",
|
|
||||||
"query-string": "^7.1.1",
|
"query-string": "^7.1.1",
|
||||||
"regenerator-runtime": "^0.12.1",
|
"regenerator-runtime": "^0.12.1",
|
||||||
"uview-ui": "^1.7.8",
|
"uview-ui": "^1.7.8",
|
||||||
|
|||||||
29
src/components/utils/http.js
Normal file
29
src/components/utils/http.js
Normal file
@@ -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
|
||||||
66
src/components/utils/modules.js
Normal file
66
src/components/utils/modules.js
Normal file
@@ -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("登录失败,请联系管理员")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,13 +9,13 @@ import 'uview-ui/theme.scss'
|
|||||||
|
|
||||||
Vue.use(vui)
|
Vue.use(vui)
|
||||||
Vue.config.productionTip = false
|
Vue.config.productionTip = false
|
||||||
Vue.prototype.$instance = axios.instance
|
Vue.prototype.$instance = axios
|
||||||
Vue.prototype.$areaId = config.areaId;
|
Vue.prototype.$areaId = config.areaId;
|
||||||
Vue.prototype.$areaName = config.areaName;
|
Vue.prototype.$areaName = config.areaName;
|
||||||
Vue.prototype.$cdn = 'https://cdn.cunwuyun.cn/'
|
Vue.prototype.$cdn = 'https://cdn.cunwuyun.cn/'
|
||||||
App.mpType = 'app'
|
App.mpType = 'app'
|
||||||
Object.keys(utils).map(e => Vue.prototype[e] = utils[e])
|
Object.keys(utils).map(e => Vue.prototype[e] = utils[e])
|
||||||
utils.$dict.init(axios.instance)
|
utils.$dict.init(axios)
|
||||||
|
|
||||||
const app = new Vue({
|
const app = new Vue({
|
||||||
store,
|
store,
|
||||||
|
|||||||
@@ -30,11 +30,10 @@
|
|||||||
<div class="appName fill" v-text="app.name"/>
|
<div class="appName fill" v-text="app.name"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<AiLogin ref="login" @success="getAuth()"/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import {mapMutations, mapState} from 'vuex'
|
import {mapActions, mapState} from 'vuex'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "home",
|
name: "home",
|
||||||
@@ -57,18 +56,18 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapMutations(['getUserInfo']),
|
...mapActions(['getUserInfo', 'autoLogin']),
|
||||||
handleLogin() {
|
handleLogin() {
|
||||||
if (!this.token) {
|
if (!this.token) {
|
||||||
this.$refs.login.show();
|
this.autoLogin();
|
||||||
}
|
} else this.$u.toast("已登录,无需重新登录!")
|
||||||
},
|
},
|
||||||
handleGotoApp(app) {
|
handleGotoApp(app) {
|
||||||
uni.navigateTo({url: `${app.path}`})
|
uni.navigateTo({url: `${app.path}`})
|
||||||
},
|
},
|
||||||
getApps() {
|
getApps() {
|
||||||
this.$instance.post("/node/wechatapps/list", null, {
|
this.$instance.post("/node/wechatapps/list", null, {
|
||||||
baseURL: "http://192.168.1.87:12525", params: {size: 999, type: 'mp'}
|
baseURL: "http://192.168.1.87:12525", params: {size: 999, type: 'mp'}, withoutToken: true
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res?.data) {
|
if (res?.data) {
|
||||||
this.apps = res.data.records.map(e => {
|
this.apps = res.data.records.map(e => {
|
||||||
@@ -80,14 +79,13 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
getAuth() {
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.getUserInfo()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onShow() {
|
onShow() {
|
||||||
this.getAuth();
|
if (this.token) {
|
||||||
|
this.getUserInfo();
|
||||||
|
} else {
|
||||||
|
this.autoLogin().then(() => this.getUserInfo())
|
||||||
|
}
|
||||||
this.getApps()
|
this.getApps()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,97 +1,25 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
import perState from 'vuex-persistedstate'
|
import perState from 'vuex-persistedstate'
|
||||||
import http from "../utils/axios";
|
import * as modules from "dvcp-wui/utils/modules"
|
||||||
|
|
||||||
Vue.use(Vuex)
|
Vue.use(Vuex)
|
||||||
|
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
state: {
|
state: {
|
||||||
/**
|
token: ""
|
||||||
* 是否需要强制登录
|
|
||||||
*/
|
|
||||||
forcedLogin: false,
|
|
||||||
token: "",
|
|
||||||
userName: "",
|
|
||||||
user: {
|
|
||||||
username: "",
|
|
||||||
avatarUrl: "",
|
|
||||||
nickName: "",
|
|
||||||
gender: "",
|
|
||||||
token: "",
|
|
||||||
phoneNumber: ''
|
|
||||||
},
|
|
||||||
global: {
|
|
||||||
areaId: ""
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
getToken(state, params) {
|
|
||||||
if (params?.code) {
|
|
||||||
http.instance.post("/auth/wechat-con/token", params, {
|
|
||||||
headers: {"Authorization": "Basic d2VjaGF0OndlY2hhdA=="},
|
|
||||||
withoutToken: true
|
|
||||||
}).then(res => {
|
|
||||||
if (res?.access_token) {
|
|
||||||
store.commit("setToken", res.token_type + ' ' + res.access_token)
|
|
||||||
params?.then?.(true)
|
|
||||||
} else {
|
|
||||||
uni.showToast({title: res?.msg})
|
|
||||||
params?.then?.(false)
|
|
||||||
}
|
|
||||||
}).finally(err => console.error(err))
|
|
||||||
} else params?.then(false)
|
|
||||||
},
|
|
||||||
setToken(state, token) {
|
setToken(state, token) {
|
||||||
state.token = token
|
state.token = token
|
||||||
},
|
},
|
||||||
setUserInfo(state, user) {
|
logout(state, showToast) {
|
||||||
state.user = user
|
|
||||||
},
|
|
||||||
getUserInfo(state, cb) {
|
|
||||||
http.instance.post("/app/appwechatuser/check").then(res => {
|
|
||||||
if (res?.data) {
|
|
||||||
store.commit("setUserInfo", res.data)
|
|
||||||
cb && cb(true)
|
|
||||||
} else {
|
|
||||||
cb && cb(false)
|
|
||||||
}
|
|
||||||
}).catch(err => {
|
|
||||||
console.error(err)
|
|
||||||
cb && cb(false)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
login(state, ploy) {
|
|
||||||
state.userName = ploy.userName || '新用户';
|
|
||||||
state.token = ploy.token
|
|
||||||
},
|
|
||||||
logout(state) {
|
|
||||||
state.userName = "";
|
|
||||||
state.token = ""
|
state.token = ""
|
||||||
uni.setStorageSync('userInfo', {})
|
state.user = {}
|
||||||
uni.setStorageSync('vuex', {})
|
showToast && uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
|
||||||
state.user = {
|
|
||||||
username: "",
|
|
||||||
avatarUrl: "",
|
|
||||||
nickName: "",
|
|
||||||
gender: "",
|
|
||||||
token: "",
|
|
||||||
phoneNumber: ''
|
|
||||||
}
|
|
||||||
uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
|
|
||||||
},
|
},
|
||||||
setUser(state, info) {
|
|
||||||
state.user = info
|
|
||||||
},
|
|
||||||
getUser(state, ploy) {
|
|
||||||
state.user.nickName = ploy.nickName;
|
|
||||||
state.user.avatarUrl = ploy.avatarUrl;
|
|
||||||
state.user.gender = ploy.gender;
|
|
||||||
},
|
|
||||||
getUserPhoneNumber(state, ploy) {
|
|
||||||
state.user.phoneNumber = ploy.phoneNumber;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
modules,
|
||||||
plugins: [perState({
|
plugins: [perState({
|
||||||
storage: {
|
storage: {
|
||||||
getItem: key => uni.getStorageSync(key),
|
getItem: key => uni.getStorageSync(key),
|
||||||
|
|||||||
@@ -1,52 +1,26 @@
|
|||||||
import config from '@/utils/config';
|
import setting from './config';
|
||||||
import util from 'dvcp-wui/utils/util';
|
import util from 'dvcp-wui/utils/util';
|
||||||
import store from "../store"
|
import store from "../store"
|
||||||
|
import instance from "dvcp-wui/utils/http";
|
||||||
const Fly = require('flyio/dist/npm/wx');
|
|
||||||
let instance = new Fly();
|
|
||||||
const baseURL = config.baseUrl;
|
|
||||||
let count = 0;
|
|
||||||
instance.config.timeout = 50000;
|
|
||||||
instance.config.baseURL = baseURL;
|
|
||||||
|
|
||||||
const getToken = () => {
|
|
||||||
if (store.state.token) return store.state.token;
|
|
||||||
else return '';
|
|
||||||
};
|
|
||||||
|
|
||||||
instance.interceptors.request.use((config) => {
|
instance.interceptors.request.use((config) => {
|
||||||
if (!config.withoutToken) {
|
config.baseURL = config.baseURL || setting.baseUrl
|
||||||
config.headers['Authorization'] = getToken();
|
|
||||||
}
|
|
||||||
return config;
|
return config;
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(response => {
|
||||||
function (response) {
|
|
||||||
util.$hideLoading();
|
util.$hideLoading();
|
||||||
if (response.data.code === 1) {
|
if (response.data.code === 1) {
|
||||||
util.$toast({title: response.data.msg, duration: 3000});
|
util.$toast({title: response.data.msg, duration: 3000});
|
||||||
} else if (response.data.code == 2) {
|
} else if (response.data.code == 2) {
|
||||||
//首次静默登录异常不做任何返回
|
//首次静默登录异常不做任何返回
|
||||||
} else if (response.data.code == 401) {
|
} else if (response.data.code == 401) {
|
||||||
if (count > 3) {
|
return store.commit('logout')
|
||||||
return;
|
|
||||||
}
|
|
||||||
count++;
|
|
||||||
return util.$getLoginCode().then((res) => {
|
|
||||||
store.commit("getToken", {code: res.code})
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
count = 0;
|
|
||||||
return response.data;
|
return response.data;
|
||||||
}
|
}
|
||||||
},
|
}, err => {
|
||||||
function (err) {
|
console.error(err);
|
||||||
console.log(err);
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export default {
|
export default instance
|
||||||
instance,
|
|
||||||
baseURL
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user