From 6f29bdc89f470d3abe9bb3d84114b6169831bd64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=98=8E=E6=98=8E?= <970899069@qq.com> Date: Mon, 26 Jul 2021 12:05:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=91=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/index.js | 116 + apis/interfaces/auth.js | 22 + pages/index/index.vue | 22 +- store/index.js | 9 +- .../cloudfunctions/phone-login/index.js | 46 +- unpackage/dist/dev/app-plus/app-service.js | 7004 +++++++++-------- 6 files changed, 3706 insertions(+), 3513 deletions(-) create mode 100644 apis/index.js create mode 100644 apis/interfaces/auth.js diff --git a/apis/index.js b/apis/index.js new file mode 100644 index 0000000..143c5f7 --- /dev/null +++ b/apis/index.js @@ -0,0 +1,116 @@ + +/** + * Web唐明明 + * 匆匆数载恍如梦,岁月迢迢华发增。 + * 碌碌无为枉半生,一朝惊醒万事空。 + */ + +import store from '@/store' + +// 基础配置 +const config = { + apiUrl : 'https://e-chain.cnskl.com/api/', + timeout : 60000 +} + +let loginHintState = false + +// 网络请求 +const request = (parameter) => { + // 检查url配置 + if(parameter.url === 'undefined' || parameter.url === ''){ + uni.showToast({ + title: '请求地址不能为空', + icon : 'none' + }) + return + } + // 注入header + config.header = { + 'Accept': 'application/json', + 'Authorization': store.getters.getToken || '' + } + // 请求实例 + return new Promise((resolve, reject) => { + uni.request({ + url : config.apiUrl + parameter.url, + timeout : config.timeout, + header : config.header || {}, + data : parameter.data || {}, + method : parameter.method || 'GET', + success : res => { + if (res.header.Authorization){ + updateToken('token', res.header.Authorization) + } + if(res.statusCode === 200){ + const resolveData = res.data + if(resolveData.status_code === 200) { + resolve(resolveData.data) + return + } + if(resolveData.status_code === 401) { + loginHint() + return + } + reject(resolveData) + return + } + errToast(res.statusCode) + } + }) + }) +} + +// 处理一些http请求错误提示 +const errToast = (code) => { + console.log(code) + switch (code){ + case 404: + uni.showToast({ + title: code + '接口不存在,请联系系统管理员', + icon : 'none' + }) + break; + case 405: + uni.showToast({ + title: code + '请检查接口请求方式错误', + icon : 'none' + }) + break; + case 500: + uni.showToast({ + title: code + '服务端错误,请检查服务器信息', + icon : 'none' + }) + break; + case 401: + console.log('重新登录') + break; + } +} + +// 更新token +const updateToken = (token) => { + store.commit('setToken', token) +} + +// 处理登录提示 +const loginHint = () => { + if( loginHintState ) return + if(!loginHintState) loginHintState = true + updateToken('') + uni.showModal({ + title: '登录提示', + content: '您的登录信息已过期,请重新登录', + confirmColor: '#009B69', + showCancel:false, + success: res=> { + loginHintState = false + if (res.confirm) uni.reLaunch({ + url: '/pages/auth/login?type=overdue' + }) + } + }) +} + +export default request diff --git a/apis/interfaces/auth.js b/apis/interfaces/auth.js new file mode 100644 index 0000000..26aef3a --- /dev/null +++ b/apis/interfaces/auth.js @@ -0,0 +1,22 @@ + +/** + * Web唐明明 + * 匆匆数载恍如梦,岁月迢迢华发增。 + * 碌碌无为枉半生,一朝惊醒万事空。 + * moduleName: 鉴权 + */ + +import request from '../index' + +// 一键登录 +const auth = (data) => { + return request({ + url: 'user/socialite/login/unicloud/app', + method: 'POST', + data: data + }) +} + +export { + auth +} diff --git a/pages/index/index.vue b/pages/index/index.vue index 806574e..4300177 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -5,7 +5,8 @@ -