This commit is contained in:
liuye
2022-06-29 18:10:55 +08:00
10 changed files with 173 additions and 173 deletions

View File

@@ -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",

View File

@@ -11,6 +11,7 @@
<script>
import tools from '../utils/util'
import {mapActions} from "vuex"
export default {
name: 'AiLogin',
@@ -22,6 +23,7 @@ export default {
}
},
methods: {
...mapActions(['autoLogin']),
show() {
tools.$getUserProfile().then(data => {
this.userData = data.userInfo;
@@ -45,7 +47,7 @@ export default {
}, {withoutToken: true}).then(d => {
if (d.data) {
let data = {...this.userData, phone: d.data};
this.$autoLogin(data, this.$store).then(res => {
this.autoLogin(data, this.$store).then(res => {
this.$emit("success", res);
});
}

View File

@@ -87,8 +87,8 @@ export default {
this.$emit('list', this.fileList)
},
upload(wait) {
let count = this.limit - (this.fileList?.length||0)
if(count>0){
let count = this.limit - (this.fileList?.length || 0)
if (count > 0) {
let params = {
count,
sizeType: ['compressed'],
@@ -115,7 +115,7 @@ export default {
} else {
uni.chooseFile(params)
}
}else{
} else {
this.$u.toast(`不能超过${this.limit}`)
}
},
@@ -128,8 +128,9 @@ export default {
this.$emit('manual', img)
uni.hideLoading()
} else {
console.log(this.$instance.defaults)
uni.uploadFile({
url: this.$instance.config.baseURL + this.action,
url: this.$instance.defaults.baseURL + this.action,
filePath: img.path,
name: 'file',
header: {
@@ -137,7 +138,6 @@ export default {
Authorization: uni.getStorageSync('token'),
},
success: response => {
uni.hideLoading()
const res = JSON.parse(response.data)
if (res?.data) {
this.$emit('data', res.data)
@@ -160,9 +160,9 @@ export default {
}
},
fail: err => {
console.log(err)
uni.hideLoading()
}
console.error(err)
},
complete: () => uni.hideLoading()
})
}

View 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

View File

@@ -0,0 +1,97 @@
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("登录失败,请联系管理员")
},
authCheck({state, dispatch}, {checkType, modulePath}) {
//用于进入应用的权限判断
//checkType 1、登录认证 2、居民认证 3、党员认证
//判断是否需要校验认证信息
let {token, user: userInfo} = state
if (!checkType) {
//如果需要校验认证信息,必定要先验证是否登录
uni.navigateTo({url: modulePath});
} else if (checkType == 1) {
if (!token) {
return dispatch('autoLogin');
}
uni.navigateTo({url: modulePath});
} else if (checkType == 2) {
if (!token) {
return dispatch('autoLogin');
}
if (!(userInfo.residentId && userInfo.status == 2)) {
return uni.navigateTo({url: '/mods/AppAuth/AppAuth'});
}
uni.navigateTo({url: modulePath});
} else if (checkType == 3) {
if (!token) {
return dispatch('autoLogin');
}
if (!userInfo.partyId) {
return uni.showToast({title: "您还不是党员,暂时无法使用该功能", icon: "none"});
}
uni.navigateTo({url: modulePath});
}
}
}
}

View File

@@ -328,30 +328,6 @@ const $getLoginCode = () => {
});
};
const $autoLogin = (params, store) => {
params = params ? params : {};
return new Promise(function (resolve, reject) {
$getLoginCode().then((res) => {
let body = {...params, code: res.code};
store.commit('getToken', {
...body,
then: (v1) => {
if (v1) {
store.commit('getUserInfo', (v2) => {
if (v2) {
resolve(v2);
} else {
reject(v2);
}
});
} else {
reject(v1);
}
}
});
});
});
};
export default {
$toast,
$loading,
@@ -366,6 +342,5 @@ export default {
$dayjs,
$dict,
$getLoginCode,
$autoLogin,
$qs
};

View File

@@ -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,

View File

@@ -30,11 +30,10 @@
<div class="appName fill" v-text="app.name"/>
</div>
</div>
<AiLogin ref="login" @success="getAuth()"/>
</div>
</template>
<script>
import {mapMutations, mapState} from 'vuex'
import {mapActions, mapState} from 'vuex'
export default {
name: "home",
@@ -57,18 +56,18 @@ export default {
},
},
methods: {
...mapMutations(['getUserInfo']),
...mapActions(['getUserInfo', 'autoLogin']),
handleLogin() {
if (!this.token) {
this.$refs.login.show();
}
this.autoLogin();
} else this.$u.toast("已登录,无需重新登录!")
},
handleGotoApp(app) {
uni.navigateTo({url: `${app.path}`})
},
getApps() {
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 => {
if (res?.data) {
this.apps = res.data.records.map(e => {
@@ -80,14 +79,13 @@ export default {
}
})
},
getAuth() {
this.$nextTick(() => {
this.getUserInfo()
})
}
},
onShow() {
this.getAuth();
if (this.token) {
this.getUserInfo();
} else {
this.autoLogin().then(() => this.getUserInfo())
}
this.getApps()
},
}

View File

@@ -1,97 +1,25 @@
import Vue from 'vue'
import Vuex from 'vuex'
import perState from 'vuex-persistedstate'
import http from "../utils/axios";
import * as modules from "dvcp-wui/utils/modules"
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
/**
* 是否需要强制登录
*/
forcedLogin: false,
token: "",
userName: "",
user: {
username: "",
avatarUrl: "",
nickName: "",
gender: "",
token: "",
phoneNumber: ''
},
global: {
areaId: ""
}
token: ""
},
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) {
state.token = token
},
setUserInfo(state, user) {
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 = "";
logout(state, showToast) {
state.token = ""
uni.setStorageSync('userInfo', {})
uni.setStorageSync('vuex', {})
state.user = {
username: "",
avatarUrl: "",
nickName: "",
gender: "",
token: "",
phoneNumber: ''
}
uni.showToast({title: `登录失效,请重新登录`, duration: 2000, icon: 'none'})
state.user = {}
showToast && 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({
storage: {
getItem: key => uni.getStorageSync(key),

View File

@@ -1,52 +1,23 @@
import config from '@/utils/config';
import setting from './config';
import util from 'dvcp-wui/utils/util';
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;
instance.defaults.baseURL = setting.baseUrl
const getToken = () => {
if (store.state.token) return store.state.token;
else return '';
};
instance.interceptors.request.use((config) => {
if (!config.withoutToken) {
config.headers['Authorization'] = getToken();
instance.interceptors.response.use(response => {
util.$hideLoading();
if (response.data.code === 1) {
util.$toast({title: response.data.msg, duration: 3000});
} else if (response.data.code == 2) {
//首次静默登录异常不做任何返回
} else if (response.data.code == 401) {
return store.commit('logout')
} else {
return response.data;
}
return config;
}, err => {
console.error(err);
});
instance.interceptors.response.use(
function (response) {
util.$hideLoading();
if (response.data.code === 1) {
util.$toast({title: response.data.msg, duration: 3000});
} else if (response.data.code == 2) {
//首次静默登录异常不做任何返回
} else if (response.data.code == 401) {
if (count > 3) {
return;
}
count++;
return util.$getLoginCode().then((res) => {
store.commit("getToken", {code: res.code})
});
} else {
count = 0;
return response.data;
}
},
function (err) {
console.log(err);
}
);
export default {
instance,
baseURL
};
export default instance