// 全局公共方法 import { wxlogin} from '@/api/user.js' const install = (Vue, vm) => { //拿到全局变量 // 登录操作 const login = (userInfo, redirect) => { vm.$u.vuex('userInfo', userInfo) vm.$u.vuex('accessToken', userInfo.access_token) vm.$u.vuex('refreshToken', userInfo.refresh_token) vm.$u.vuex('userInfo.tokenGeneratedTime', Date.now()) vm.$u.vuex('isLogin', true) if (redirect) { uni.redirectTo({ url: redirect, fail() { uni.switchTab({ url: redirect }) } }) } else uni.switchTab({ url: '/pages/index/index' }) } // 更新个人信息 const updateInfos = (pInfo, redirect) => { vm.$u.vuex('userInfo.detail.phone', pInfo.phone) vm.$u.vuex('userInfo.detail.sfzh', pInfo.sfzh) vm.$u.vuex('userInfo.detail.name', pInfo.name) vm.$u.vuex('userInfo.detail.dz', pInfo.dz) vm.$u.vuex('userInfo.detail.sqName', pInfo.sqmc) if (redirect) { uni.redirectTo({ url: redirect, fail() { uni.switchTab({ url: redirect }) } }) } else{ uni.switchTab({ url: '/pages/my/index' }) } } // 退出登录 const logout = () => { vm.$u.vuex('userInfo', { avatar: '', nick_name: '游客', tenant_id: '暂无' }) vm.$u.vuex('accessToken', '') vm.$u.vuex('isLogin', false) uni.redirectTo({ url: '/pageA/my/login' }) } const checkUpdate = ()=>{ if (vm.userInfo.detail.status==0) { vm.$u.func.showToast({ title: '请完善基础信息'}) uni.navigateTo({ url: '/pageA/my/profile' }) return false } return true } // 检查登录状态 const checkLogin = (e = {}) => { const tokenValidPeriod = 86400000; if (!vm.isLogin) { uni.navigateTo({ url: '/pageA/my/login' }) return false }else if(vm.isLogin && Number(Date.now() - vm.userInfo.tokenGeneratedTime) > Number(tokenValidPeriod)){ // 如果token为空或者已经超过有效期,需要生成新的token vm.$u.vuex('userInfo', { avatar: '', nick_name: '游客', tenant_id: '暂无' }) vm.$u.vuex('accessToken', '') vm.$u.vuex('isLogin', false) console.log('token过期'); uni.navigateTo({ url: '/pageA/my/login' }) return false }else{ return true } } const userLogin =(code)=>{ wxlogin({code:code}).then(res=>{ console.log('接口返回------',res); if(res.data.code=='100'||res.data.code=='500'){ vm.$u.func.showToast({ title: res.data.msg}) uni.navigateTo({ url: '/pageA/my/login='+res.data.data+'&code='+code }); } if(res.data.code=='101'){ vm.$u.func.showToast({ title: res.data.msg}) } if(res.data.code=='102'){ vm.$u.func.showToast({ title: res.data.msg}) } if(res.data.code=='401'){ vm.$u.func.showToast({ title: res.data.msg}) } if(res.data.code=='200'){ //console.log(res,'res') //vm.$u.func.showToast({ title: '登陆成功'}) vm.$u.vuex('accessToken', res.data.data.access_token) vm.$u.vuex('refreshToken', res.data.data.refresh_token) vm.$u.vuex('isLogin', true) vm.$u.vuex('userInfo',res.data.data) } }) } // 跳转路由前检查登录状态 // 跳转路由前检查登录状态 const route = (url) => { if (!vm.isLogin) { uni.showToast({ title: '请先登录', icon: 'none' }) const pages = getCurrentPages() const currentPage = pages[pages.length - 1] setTimeout(() => { uni.navigateTo({ url: `/pageA/my/login?redirect=/${currentPage.route}` }) }, 500) return false } uni.navigateTo({ url: url }) } // URL参数转对象 const paramsToObj = (url) => { if (url.indexOf('?') != -1) { let arr = url.split('?')[1] } let arr = url.split('&') let obj = {} for (let i of arr) { obj[i.split('=')[0]] = i.split('=')[1] } return obj } // 刷新当前页面 const refreshPage = () => { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const path = '/' + currentPage.route + vm.$u.queryParams(currentPage.options) if (vm.$u.test.contains(currentPage.route, 'tabbar')) { uni.reLaunch({ url: path, fail: (err) => { console.log(err) } }) } else { uni.redirectTo({ url: path, fail: (err) => { console.log(err) } }) } } // 提示 const showToast = (data = {}) => { if (typeof data == 'string') { uni.showToast({ title: data, icon: 'none' }) } else { uni.showToast({ title: data.title, icon: data.icon || 'none', image: data.image || '', mask: data.mask || false, position: data.position || 'center', duration: data.duration || 1500, success: () => { setTimeout(() => { if (data.back) return uni.navigateBack() data.success && data.success() }, data.duration || 1500) } }) } } // 刷新token const refreshToken = (data) => { return new Promise((resolve) => { vm.$u.vuex('accessToken', data.access_token) vm.$u.vuex('refreshToken', data.refresh_token) resolve() }) } // 将定义的方法挂载,使用this.$u.func.xx调用 Vue.prototype.$u.func = { login, logout, route, updateInfos, checkLogin, checkUpdate, paramsToObj, refreshPage, showToast, refreshToken } } export default { install }