diff --git a/common/js/globalData.js b/common/js/globalData.js new file mode 100644 index 0000000..b134ea7 --- /dev/null +++ b/common/js/globalData.js @@ -0,0 +1,5 @@ +export default { + deptId: 1, + initPageNum: 1, + initPageSize: 5 +} \ No newline at end of file diff --git a/common/js/request.js b/common/js/request.js new file mode 100644 index 0000000..3f59cea --- /dev/null +++ b/common/js/request.js @@ -0,0 +1,114 @@ +import globalData from '@/common/js/globalData.js'; + +export default { + // 异步接口拦截 + addInterceptor() { + uni.addInterceptor('request', { + invoke(args) { + // request 触发前拼接 url + args.url = 'http://www.opsoul.com' + args.url; + // console.log("停止触发"); + // return false; + }, + success(res) { + if (res.data.code !== 0) { + uni.showToast({ + title: '系统开小差啦T_T,请稍后重试', + icon: 'error' + }) + } + }, + fail(err) { + uni.showToast({ + title: '系统开小差啦T_T,请稍后重试', + icon: 'error' + }) + }, + complete(res) { + console.log('interceptor-complete', res) + } + }) + }, + checkAndAuth() { + let _this = this; + // 通过 wx.getSetting 先查询一下用户是否授权了 "scope.userInfo" 这个 scope + wx.getSetting({ + success(res) { + if (!res.authSetting['scope.userInfo']) { + // 用户授权 + wx.authorize({ + scope: 'scope.userInfo', + success() { + // 用户已经同意, 后续调用此接口不会弹窗询问 + _this.login(); + }, + fail() { + // 用户已经拒绝过授权 + wx.openSetting({ + success(res) { + if (res['scope.userInfo']) { + _this.checkAndAuth(); + } + } + }) + } + }) + } else { + _this.login(); + } + } + }) + }, + async login() { + // 从缓存中获取登录信息 + let userInfo = uni.getStorageSync('userProfile'); + if (userInfo) { + return true; + } + + // TODO:调用小程序服务端确认是否是授权登录过的用户 + let loginRes = { + logined: false, + userInfo: {} + }; + // 未登录过的获取微信用户信息 + if (!loginRes.logined) { + userInfo = await wx.getUserProfile({ + desc: '用于小程序登录' + }); + // 获取微信登录凭证 + const wxLoginRes = await wx.login(); + console.log(wxLoginRes) + // 再次请求小程序服务端存储用户,服务端添加附加用户信息后返回 + loginRes = { + logined: true, + userInfo: { + ...userInfo, + userId: 1, + wxLoginCode: wxLoginRes.code + } + }; + + const wxAuthRes = await uni.request({ + url: '/wx/auth', + header: { + code: wxLoginRes.code + } + }) + console.log(wxAuthRes) + loginRes.userInfo.openId = wxAuthRes[1].data.data.openid; + } + userInfo = loginRes.userInfo; + + if (!userInfo) { + uni.showToast({ + icon: 'error', + title: '微信用户信息获取失败,请退出小程序重试' + }) + return false; + } + // 页面存储用户登录有效信息,以便其他页面调用 + uni.setStorageSync('userProfile', userInfo); + return true; + } +} diff --git a/main.js b/main.js index 01e0b6e..bbc7fd7 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,8 @@ import Data from './common/js/data.js'; import globalFun from './common/js/glogalFun.js'; import validate from './common/js/validate.js'; import dateUtil from './common/js/dateUtil.js'; -import commonFun from './common/js/commonFun.js'; +import commonFun from './common/js/commonFun.js'; +import request from './common/js/request.js'; Vue.component('cu-custom', CuCustom) Vue.component('confirm-modal', ConfirmModal); @@ -16,7 +17,8 @@ const data = type => { return new Promise(resolve => { resolve(Data[type]); }) -} +} +request.addInterceptor(); Vue.prototype.$api = { data } @@ -24,6 +26,7 @@ Vue.prototype.$globalFun = globalFun; Vue.prototype.$validate = validate; Vue.prototype.$dateUtil = dateUtil; Vue.prototype.$commonFun = commonFun; +Vue.prototype.$request = request; Vue.config.productionTip = false diff --git a/pages/index/index-home.vue b/pages/index/index-home.vue index 4e852b8..d5f2e7b 100644 --- a/pages/index/index-home.vue +++ b/pages/index/index-home.vue @@ -168,6 +168,7 @@ methods: { async loadData() { this.curUserInfo = uni.getStorageSync('userProfile'); + console.log(this.curUserInfo) this.isAuthWxLoginModal = this.curUserInfo ? false : true; if (!this.isAuthWxLoginModal) { this.myInfo = await this.$api.data('myInfo'); @@ -201,7 +202,7 @@ }) }, authWxLogin() { - this.$commonFun.login().then(res => { + this.$request.login().then(res => { if (res) { this.loadData(); }