diff --git a/src/App.vue b/src/App.vue index 5335f6f..65974ff 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,9 +3,8 @@ import util from './utils/util'; export default { onLaunch: function() { - // #ifndef H5 + this.$store.commit('setLoginState', { loginState: false }); this.updateManager(); - // #endif }, methods: { isLogin(){ diff --git a/src/js/server.js b/src/js/server.js index c671b75..dc191c7 100644 --- a/src/js/server.js +++ b/src/js/server.js @@ -1,5 +1,6 @@ import util from '../utils/util'; // import { app as vm } from '../main'; +import { app as vm } from '../main'; const islog = true; @@ -27,6 +28,9 @@ export class Server { method: method, data: res, }) + + handleUserExceptions({ res }); + if(isDefaultGet){ if(failMsg == '')throw Error('默认回调,失败提示不能为空 key -> failMsg'); defaultGet({ @@ -70,6 +74,14 @@ export class Server { reject({url,res,data}); } } + // 用户信息异常 + function handleUserExceptions({ res }){ + if(res?.data?.code === 401&&res?.data?.message?.indexOf('用户') !== -1){ + console.log('用户信息异常,请重新登录'); + const _store = vm.$store; + _store.commit('setLoginState', { loginState: false }); + } + } } get({url,data={},header={},isDefaultGet=true,failMsg=''}){ return this.request(url,data,'GET',header,isDefaultGet,failMsg); diff --git a/src/main.js b/src/main.js index 38f2407..049f478 100644 --- a/src/main.js +++ b/src/main.js @@ -6,8 +6,9 @@ Vue.config.productionTip = false App.mpType = 'app' -const app = new Vue({ +export const app = new Vue({ ...App, store }) app.$mount() + diff --git a/src/pages.json b/src/pages.json index 5e5e3a7..e1bf9d5 100644 --- a/src/pages.json +++ b/src/pages.json @@ -4,7 +4,8 @@ "path": "pages/index/index", "style": { "componentPlaceholder": { - "bottom-logo": "view" + "bottom-logo": "view", + "authorization-login": "view" } } }, @@ -969,6 +970,17 @@ } } ] + }, + { + "root": "subpackage/authorization", + "pages": [ + { + "path": "pages/index", + "style" : { + "navigationBarTitleText": "" + } + } + ] } ], "globalStyle": { diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 848b883..6751c16 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -2,22 +2,22 @@ - + {{indexData.brand_name || '-'}}(共{{indexData.stadium_num || '0'}}家) 点击登陆 今日总收入 - {{loginStatus?'¥':''}}{{loginStatus?(indexData.amount || '0'):'***'}} + {{loginState?'¥':''}}{{loginState?(indexData.amount || '0'):'***'}} 收款笔数 - {{loginStatus?(indexData.in_count || '0'):'**'}} + {{loginState?(indexData.in_count || '0'):'**'}} 退款笔数 - {{loginStatus?(indexData.out_count || '0'):'**'}} + {{loginState?(indexData.out_count || '0'):'**'}} @@ -52,33 +52,9 @@ - - - 微信授权 - 您的信息和数据将受到保护 - - - + - - - - - - - + 核销 @@ -91,7 +67,9 @@ import util from '../../utils/util'; import { servers } from '../../js/server'; import { API } from '../../js/api'; + import { mapGetters } from 'vuex'; import bottomLogo from "@/subpackage/menu/components/bottom_logo.vue"; + import authorizationLogin from "@/subpackage/authorization/components/login.vue"; const tabList = [ { id: 0, @@ -176,57 +154,34 @@ ]; const app = getApp(); - - // #ifndef H5 - - const uniGetSetting = util.promisify(uni.getSetting); - const uniLogin = util.promisify(uni.login); - const uniGetUserInfo= util.promisify(uni.getUserInfo); const APPID = uni.getAccountInfoSync().miniProgram.appId; - - // #endif - - // #ifdef H5 - const APPID = "wx7106e84614cf0060" //TODO - // #endif export default { - components: { bottomLogo }, + components: { bottomLogo, authorizationLogin }, computed: { - isProfile: _=>util.isProfile(), - // loginStatus(){ - // return app.isLogin(); - // } + ...mapGetters([ 'loginState' ]), }, data() { return { tabList, - isShowAuthorModal: false, indexData: {}, - loginStatus: false, } }, async onLoad(options) { - try{ - util.showLoad(); + let _loginRes = {}; // 20230829 新增平台账户不绑定,不检验,每次进来走重新登陆绑定流程 - if(options.role !== 'ADMIN-PLATFORM')await this.checkUserAuthor(); - - util.hideLoad(); - this.loginStatus = app.isLogin(); - if(!!app.isLogin())this.getIndexInfo(); + if(options.role !== 'ADMIN-PLATFORM')_loginRes = await this.$store.dispatch('checkUserAuthor'); + // 20240325 新增openid绑定账号检测 + if(_loginRes?.data === '')this.webBrandUserCheck(); + if(_loginRes?.data)this.getIndexInfo(); }catch(err){ - util.hideLoad(); - uni.removeStorageSync('token'); - // util.showNone(err.message || '身份校验失败!'); - console.log('onLoad err',err); - this.loginStatus = app.isLogin(); + console.warn('pages index onLoad err--->', err); } }, onShow(){ - let { indexData } = this; - if(JSON.stringify(indexData)!='{}'&&!!app.isLogin())this.getIndexInfo(); + let { indexData, loginState, getIndexInfo } = this; + if(JSON.stringify(indexData)!='{}'&&loginState)getIndexInfo(); }, methods: { // 账号有开启了权限的才显示菜单,否则不显示【ID1000840】 @@ -238,13 +193,13 @@ return false; }, toNoticeList(){ - if(!app.isLogin())return this.showAuthor(); + if(!this.loginState)return this.showAuthor(); util.routeTo(`/pages/message/list/list`,'nT'); }, toPageInfo(tabInfo){ - let { indexData } = this; + let { indexData, loginState } = this; let _permission = indexData.permission || {}; - if(!app.isLogin())return this.showAuthor(); + if(!loginState)return this.showAuthor(); if(!tabInfo.path)return util.showNone('暂未开放!'); //if(tabInfo.id == 2)return util.routeTo(tabInfo.path,'nT'); //测试 if(!_permission[tabInfo.serverKey])return util.showNone('暂无权限,请联系管理员开启!') @@ -259,7 +214,6 @@ if([ 10, 11, 4 ].includes(tabInfo.id))return util.routeTo(tabInfo.path + `?brand_id=${indexData.brand.id}`,'nT'); if(tabInfo.id === 12){ - // if(!indexData.brand.mini_wechat_appid)return util.showNone('暂无appid!') let url = tabInfo.path + `?appid=${indexData.brand.mini_wechat_appid}` console.log(222,url) return util.routeTo(url,'nT'); @@ -280,137 +234,15 @@ this.$store.commit('setBrandInfo',res); }) }, - // 看是否授权,清除token - async checkUserAuthor(){ - try{ - let loginRes ="" - // #ifndef H5 - loginRes = await uniLogin(); - // #endif - // #ifdef H5 - loginRes = {errMsg: "login:ok", code: "0c3xEi0w3kr1t23zcU3w3ZQR3w3xEi0h"} //TODO 设置H5测试数据 - // #endif - console.log(loginRes) - return servers.get({ - url: API.wechatMiniAppLoginAndSync, - data: { - code: loginRes.code, - appid: APPID, - // #ifdef H5 - token:"3d2b0092-e761-11ee-8a66-5254005df464", //TODO - // #endif - }, - isDefaultGet: false, - }) - .then(res=>{ - util.hideLoad(); - let _data = res.data; - if(_data.code == 0){ - // let _data = res.data.data; - - if(_data.data == ''){ - uni.removeStorageSync('token'); - this.loginStatus = app.isLogin(); - // 20240325 新增openid绑定账号检测 - this.webBrandUserCheck(); - - // return util.routeTo(`/pages/login/login`,'rL'); - } - uni.setStorageSync('token',_data.data); - return _data; - }else{ - util.showNone(_data.message || '校验身份失败!'); - throw res.data || {}; - } - - }) - .catch(err=>{ - throw err; - }) - }catch(err){ - throw err; - } - }, showAuthor(){ - this.isShowAuthorModal = true - }, - closeAuthor(){ - this.isShowAuthorModal = false - }, - cancelAuthor(){ - this.closeAuthor(); - }, - getLoginQuery({ - userInfo, - loginRes - }){ - return { - appid: APPID, - code: loginRes.code, - encryptedData: userInfo.encryptedData, - // is_details: 1, - // 后端解密错误,直接传用户信息 - user_info: userInfo.userInfo, - user_raw_data: userInfo.rawData, - ...userInfo.userInfo, - } - }, - // 新获取用户信息 - profileConfirm(){ - uni.getUserProfile({ - lang: 'zh_CN', desc: '授权登陆', - success: res => { - this.confirmAuthor({detail: {...res}}); + this.$refs?.authorizationLogin?.alert?.({ + success: ()=>{ + setTimeout(this.getIndexInfo, 1000); }, - fail: function(err) { - util.showNone('获取用户信息失败!请重试'); - console.warn('getUserProfile Err', err) + fail: err =>{ + // console.warn('pages index showAuthor authorizationLogin Err ->', err) } }) - }, - - async confirmAuthor(userRes){ - if(!userRes.detail.userInfo){ - this.closeAuthor(); - return util.showNone('获取用户信息失败!请稍后重试'); - } - let loginRes = await uniLogin(); - - if(!loginRes.code){ - this.closeAuthor(); - return util.showNone('获取登陆凭证失败!稍后重试'); - } - - servers.post({ - url: API.wechatMiniAppLoginAndSync, - data: this.getLoginQuery({ - userInfo: userRes.detail, - loginRes - }), - isDefaultGet: false, - }) - .then(res=>{ - util.hideLoad(); - let _data = res.data || {}; - if(_data.code == 0){ - if(_data.data == '')return util.routeTo(`/pages/login/login`,'rL'); - util.showNone(_data.message || '登陆成功!'); - // let _data = res.data.data; - // if(_data.user.role == '')return util.routeTo(`/pages/merchant_login/merchant_login`,'rL'); - - uni.setStorageSync('token',_data.data); - setTimeout(_=>{ - this.getIndexInfo(); - this.closeAuthor(); - this.loginStatus = app.isLogin(); - }, 1200); - }else{ - util.showNone(_data.message || '后台登陆失败!'); - setTimeout(_=>this.closeAuthor(), 1200); - } - - }).catch(util.hideLoad) - }, webBrandUserCheck(){ @@ -456,8 +288,8 @@ }) }, toStoreList(){ - if(!app.isLogin())return this.showAuthor(); - let { indexData } = this; + let { indexData, loginState } = this; + if(!loginState)return this.showAuthor(); util.routeTo(`/pages/store_list/store_list?brand_id=${indexData.brand.id}`,'nT'); }, bindUserOpenid(){ @@ -687,57 +519,6 @@ } } - .ic-author-modal{ - position: absolute; - left: 50%; - top: 50%; - transform: translate(-50%, -50%); - padding-top: 60upx; - width: 662upx; - height: 884upx; - border-radius: 10upx; - background-color: #fff; - .iam-title{ - margin-bottom: 22upx; - text-align: center; - line-height: 60upx; - font-size: 44upx; - font-weight: 500; - color: #1a1a1a; - } - .iam-tip{ - margin-bottom: 52upx; - line-height: 40upx; - text-align: center; - font-size: 28upx; - color: #9c9c9f; - } - .iam-pic{ - margin: 0 auto 62upx; - display: block; - width: 488upx; - height: 416upx; - } - .iam-btns{ - @include centerFlex(center); - >button{ - margin: 0 20upx; - width: 240upx; - height: 92upx; - line-height: 88upx; - text-align: center; - border-radius: 46upx; - border: 2upx solid $themeColor; - font-size: 32upx; - color: $themeColor; - &+button{ - background-color: $themeColor; - color: #fff; - } - } - } - - } // fly_btn .fly_btn{ diff --git a/src/store/actions.js b/src/store/actions.js index 2babf68..c5ebddc 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -1,7 +1,7 @@ // 异步方法 import { servers } from '../js/server'; import { API } from '../js/api'; -import { showLoad, hideLoad, showModal } from '../utils/util'; +import { showLoad, hideLoad, showModal, promisify } from '../utils/util'; export default { getBrandInfo({commit, state}){ @@ -56,4 +56,45 @@ export default { // return Promise.reject(err); }) }, + // 看是否授权,清除token 也可以理解自动登陆 + async checkUserAuthor({ commit, state }){ + let loginRes = {}; + try{ + const uniLogin = promisify(uni.login); + loginRes = await uniLogin(); + }catch(err){ + console.warn('store actions checkUserAuthor login err', err); + showModal({ content: '获取登陆凭证失败!请稍后重新登录!' }); + return Promise.reject(err); + } + showLoad(); + return servers.post({ + url: API.wechatMiniAppLoginAndSync, + data: { + code: loginRes.code, + appid: state.APPID, + }, + isDefaultGet: false, + }) + .then(res => { + hideLoad(); + let _data = res?.data || {}; + if(_data.code === 0){ + if(_data.data === ''){ + commit('setLoginState', { loginState: false, token: '' }); + return _data; + } + commit('setLoginState', { loginState: true, token: _data.data }); + return _data; + }else{ + return Promise.reject(_data); + } + }) + .catch(err=>{ + console.warn('store actions checkUserAuthor wechatMiniAppLoginAndSync err --->', err); + hideLoad(); + showModal({ content: err?.message || '静默登录失败!请稍后重试!' }); + return Promise.reject(err); + }) + }, } \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js index 38ef2f5..6c1f777 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -54,7 +54,8 @@ export default new Vuex.Store({ typeInfo: {}, // 球场类型 venueList: [], // 选择场地列表 orderInfo: {}, // 订单信息 // 20230620 新增挂账需求,用于回显确认订单 - } + }, + isLogin: false, // 登陆状态 }, mutations, actions, @@ -69,6 +70,12 @@ export default new Vuex.Store({ } console.warn(_arr.length) return _arr; + }, + loginState: state =>{ + let _isLogin = state.isLogin; + let _token = uni.getStorageSync('token'); + // 只是想获得一个响应的token状态 + if(_isLogin || !_isLogin)return !!_token; } } }); diff --git a/src/store/mutations.js b/src/store/mutations.js index 0918706..4d9a91d 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -10,5 +10,18 @@ export default { // 场地占用信息 setOccupyInfo(state, _occupyInfo){ state.occupyInfo = _occupyInfo; + }, + + // + setLoginState(state, { loginState = false, token = '' }){ + console.log(loginState, token); + if(loginState&&token){ + uni.setStorageSync('token', token); + state.isLogin = loginState; + }else{ + uni.removeStorageSync('token', token); + state.isLogin = false; + } + } } \ No newline at end of file diff --git a/src/subpackage/authorization/components/login.vue b/src/subpackage/authorization/components/login.vue new file mode 100644 index 0000000..635810e --- /dev/null +++ b/src/subpackage/authorization/components/login.vue @@ -0,0 +1,190 @@ + + + + + \ No newline at end of file diff --git a/src/subpackage/authorization/js/api.js b/src/subpackage/authorization/js/api.js new file mode 100644 index 0000000..5b8b559 --- /dev/null +++ b/src/subpackage/authorization/js/api.js @@ -0,0 +1,7 @@ +import { ORIGIN } from '@/js/api'; + +export const AUTHOR_API = { + wechatMiniAppLoginAndSync: `${ORIGIN}/assistant/WechatMiniAppGetToken`, // 小程序授权获取token,为空就登录 +} + +export default AUTHOR_API; \ No newline at end of file diff --git a/src/subpackage/authorization/js/server.js b/src/subpackage/authorization/js/server.js new file mode 100644 index 0000000..ac1b071 --- /dev/null +++ b/src/subpackage/authorization/js/server.js @@ -0,0 +1,10 @@ +import { Server } from '@/js/server'; + +class _Server extends Server { + constructor(props){ + super(props) + } +} + + +export default new _Server(); \ No newline at end of file diff --git a/src/subpackage/authorization/pages/index.vue b/src/subpackage/authorization/pages/index.vue new file mode 100644 index 0000000..b3f8694 --- /dev/null +++ b/src/subpackage/authorization/pages/index.vue @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/src/subpackage/authorization/static/images/author_modal.png b/src/subpackage/authorization/static/images/author_modal.png new file mode 100644 index 0000000..8e36123 Binary files /dev/null and b/src/subpackage/authorization/static/images/author_modal.png differ