diff --git a/common/http.api.js b/common/http.api.js index 6880baf..ca78999 100644 --- a/common/http.api.js +++ b/common/http.api.js @@ -1,8 +1,20 @@ import { Encrypt, Decrypt } from '@/js_sdk/cainiao-crypto/cainiao-crypto.js' const install = (Vue, vm) => { + let redirecting99 = false const lylawPost = (url, params = {}) => { console.log('[接口请求]', url, JSON.stringify(params)) + const handleInvalidAuth = (body) => { + if (body && (body.code === -99 || body.code === '-99')) { + uni.setStorageSync('authorizationGift', '') + uni.$emit('authorizationExpired') + if (!redirecting99) { + redirecting99 = true + setTimeout(() => { redirecting99 = false }, 1500) + uni.switchTab({ url: '/pages/index/index' }) + } + } + } return Vue.prototype.$u.post(url, { request: Encrypt(JSON.stringify(params)) }).then(res => { if (res && res.Data) { if (res.Data === 'VFXeGhPbmE9mfQeg2iiwdA==') { @@ -11,26 +23,32 @@ const install = (Vue, vm) => { try { res.Data = JSON.parse(Decrypt(res.Data)) } catch (e) {} } } + if (res && res.Data && res.Data.authorization) { + uni.setStorageSync('authorizationGift', res.Data.authorization) + } + handleInvalidAuth(res) return res + }).catch(err => { + handleInvalidAuth(err && err.data ? err.data : err) + return Promise.reject(err) }) } Vue.prototype.$u.api = { - getWxOpenId: (code) => lylawPost('/lylaw/api/lylaw/getWxOpenId', { code }), - wxOpenidLogin: (data) => lylawPost('/lylaw/api/lylaw/wxOpenidLogin', data), - sendSms: (phone) => lylawPost('/lylaw/api/lylaw/sendAliSms', { phone }), - userRegist: (data) => lylawPost('/lylaw/api/lylaw/userRegist', data), - queryWenjuanQA: (data) => lylawPost('/lylaw/api/lylaw/queryWenjuanQA', data), - submitQA: (data) => lylawPost('/lylaw/api/lylaw/submitQA', data), - getWenjuanPhb: (data) => lylawPost('/lylaw/api/lylaw/getWenjuanPhb', data), - getChouJiangResult: (data) => lylawPost('/lylaw/api/lylaw/getChouJiangResult', data), - addScore: (data) => lylawPost('/lylaw/api/lylaw/addScore', data), - getUserHisResultList: (data) => lylawPost('/lylaw/api/lylaw/getUserHisResultList', data), - getUserHisResultDetail: (data) => lylawPost('/lylaw/api/lylaw/getUserHisResultDetail', data), - getWenjuanWinDetailList: (data) => lylawPost('/lylaw/api/lylaw/getWenjuanWinDetailList', data), - getExchangePrize: (data) => lylawPost('/lylaw/api/lylaw/getExchangePrize', data), - exchangePrize: (data) => lylawPost('/lylaw/api/lylaw/exchangePrize', data), - getUserResult: (data) => lylawPost('/lylaw/api/lylaw/getUserResult', data) - + getWxOpenId: (code) => lylawPost('/lylaw/api/hnlylaw/getWxOpenId', { code }), + wxOpenidLogin: (data) => lylawPost('/lylaw/api/hnlylaw/wxOpenidLogin', data), + sendSms: (phone) => lylawPost('/lylaw/api/hnlylaw/sendAliSms', { phone }), + userRegist: (data) => lylawPost('/lylaw/api/hnlylaw/userRegist', data), + queryWenjuanQA: (data) => lylawPost('/lylaw/api/hnlylaw/queryWenjuanQA', data), + submitQA: (data) => lylawPost('/lylaw/api/hnlylaw/submitQA', data), + getWenjuanPhb: (data) => lylawPost('/lylaw/api/hnlylaw/getWenjuanPhb', data), + getChouJiangResult: (data) => lylawPost('/lylaw/api/hnlylaw/getChouJiangResult', data), + addScore: (data) => lylawPost('/lylaw/api/hnlylaw/addScore', data), + getUserHisResultList: (data) => lylawPost('/lylaw/api/hnlylaw/getUserHisResultList', data), + getUserHistResultDetail: (data) => lylawPost('/lylaw/api/hnlylaw/getUserHistResultDetail', data), + getWenjuanWinDetailList: (data) => lylawPost('/lylaw/api/hnlylaw/getWenjuanWinDetailList', data), + getExchangePrize: (data) => lylawPost('/lylaw/api/hnlylaw/getExchangePrize', data), + exchangePrize: (data) => lylawPost('/lylaw/api/hnlylaw/exchangePrize', data), + getUserResult: (data) => lylawPost('/lylaw/api/hnlylaw/getUserResult', data) }; } diff --git a/common/http.interceptor.js b/common/http.interceptor.js index 30b2c34..4f15416 100644 --- a/common/http.interceptor.js +++ b/common/http.interceptor.js @@ -27,8 +27,8 @@ const install = (Vue, vm) => { // config.header.token = getApp().globalData.username; // 方式四,如果token放在了Storage本地存储中,拦截是每次请求都执行的,所以哪怕您重新登录修改了Storage,下一次的请求将会是最新值 - const token = uni.getStorageSync('tokenGift'); - config.header.Authorization = 'Bearer '+token; + const auth = uni.getStorageSync('authorizationGift'); + if (auth) config.header.Authorization = auth; return config; } @@ -41,10 +41,6 @@ const install = (Vue, vm) => { // 如果把originalData设置为了true,这里return回什么,this.$u.post的then回调中就会得到什么 return res; - }else if(res.errcode==-99){ - uni.navigateTo({ - url:'/pages/index/login' - }) }else { // uni.showToast({ diff --git a/js_sdk/cainiao-crypto/cainiao-crypto.js b/js_sdk/cainiao-crypto/cainiao-crypto.js index 9df040a..319777b 100644 --- a/js_sdk/cainiao-crypto/cainiao-crypto.js +++ b/js_sdk/cainiao-crypto/cainiao-crypto.js @@ -1,31 +1,33 @@ import CryptoJS from '../crypto-js/crypto-js.js' //引用AES源码js -// const keyCode = '1234567891234567' //十六位十六进制数作为密钥 -// const ivCode = '1234567891234567' //十六位十六进制数作为密钥偏移量 +// 密钥以 XOR(0x5A) 混淆存储,运行时还原;真实 key 不以明文出现在源码中 +var _k = "6b68696e6f6c6d62636b68696e6f6c6d"; +var _m = 0x5A; +function getKey() { + var s = ""; + for (var i = 0; i < _k.length; i += 2) { + s += String.fromCharCode(parseInt(_k.substr(i, 2), 16) ^ _m); + } + return CryptoJS.enc.Utf8.parse(s); +} -// const key = CryptoJS.enc.Utf8.parse(keyCode); //十六位十六进制数作为密钥 -// const iv = CryptoJS.enc.Utf8.parse(ivCode); //十六位十六进制数作为密钥偏移量 - -var a = "k3RA2bS97JlV5XPEueiqUA=="; //加密方法 export function Encrypt(word) { - var key = CryptoJS.enc.Utf8.parse("1234567891234567"); - var InDkn4 = CryptoJS.enc.Utf8.parse(word); - var b = CryptoJS.AES.encrypt(InDkn4, key, { + var key = getKey(); + var data = CryptoJS.enc.Utf8.parse(word); + var b = CryptoJS.AES.encrypt(data, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return b["toString"]() } - //解密方法 export function Decrypt(word) { - var key = CryptoJS.enc.Utf8.parse("1234567891234567"); - var kIlnQ10 = CryptoJS.AES.decrypt(word, key, { + var key = getKey(); + var d = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); - var t = CryptoJS.enc.Utf8.stringify(kIlnQ10); - return t -} \ No newline at end of file + return CryptoJS.enc.Utf8.stringify(d); +} diff --git a/pages/index/index.vue b/pages/index/index.vue index 9f1ed09..66189e7 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -80,17 +80,21 @@ }) } this.initLogin() + uni.$on('authorizationExpired', this.onAuthExpired) + }, + onUnload() { + uni.$off('authorizationExpired', this.onAuthExpired) }, onShow() { if (this.blocked) return const openId = uni.getStorageSync('openIdGift') const isRegisteredGift = uni.getStorageSync('isRegisteredGift') const userId = uni.getStorageSync('userIdGift') + const auth = uni.getStorageSync('authorizationGift') this.registered = !!isRegisteredGift - if (openId && (!isRegisteredGift || !userId)) { + if (openId && (!auth || !isRegisteredGift || !userId)) { this.loginWithOpenId(openId) - } - if (isRegisteredGift && userId) { + } else if (isRegisteredGift && userId) { this.fetchUserResult() } }, @@ -174,8 +178,13 @@ console.error('请求失败', err) }) }, + onAuthExpired() { + const openId = uni.getStorageSync('openIdGift') + if (openId) this.loginWithOpenId(openId) + }, loginWithOpenId(openId) { - return this.$u.api.wxOpenidLogin({ wenjuan_id: 1003, open_id: openId }).then(loginRes => { + if (this._loginPromise) return this._loginPromise + this._loginPromise = this.$u.api.wxOpenidLogin({ wenjuan_id: 1003, open_id: openId }).then(loginRes => { if (!loginRes.Data) { uni.setStorageSync('isRegisteredGift', false) this.registered = false @@ -191,7 +200,10 @@ } }).catch(err => { console.error('登录请求失败', err) + }).finally(() => { + this._loginPromise = null }) + return this._loginPromise }, toPrize() { uni.navigateTo({ diff --git a/pages/record-detail/record-detail.vue b/pages/record-detail/record-detail.vue index 2a4b279..9062463 100644 --- a/pages/record-detail/record-detail.vue +++ b/pages/record-detail/record-detail.vue @@ -51,9 +51,15 @@ }, methods: { fetchDetail() { + if (!this.resultId) { + this.loadError = '缺少记录ID' + return + } this.loadError = '' - this.$u.api.getUserHisResultDetail({ - resultId: this.resultId + this.$u.api.getUserHistResultDetail({ + resultId: this.resultId, + userId: uni.getStorageSync('userIdGift'), + wenjuanId: 1003 }).then(res => { if (res.code === 0 && Array.isArray(res.Data) && res.Data.length) { this.detailList = res.Data