登录调整
Showing
4 changed files
with
113 additions
and
110 deletions
| ... | @@ -13,7 +13,6 @@ export default { | ... | @@ -13,7 +13,6 @@ export default { |
| 13 | firstload = true | 13 | firstload = true |
| 14 | 14 | ||
| 15 | this.globalData.baseUrl_api = config.baseUrl_api; | 15 | this.globalData.baseUrl_api = config.baseUrl_api; |
| 16 | |||
| 17 | let userName = uni.getStorageSync('userName') | 16 | let userName = uni.getStorageSync('userName') |
| 18 | if (userName) { | 17 | if (userName) { |
| 19 | this.globalData.isLogin = true; | 18 | this.globalData.isLogin = true; | ... | ... |
| ... | @@ -89,16 +89,22 @@ function loginByPhone(phonenumber, code) { | ... | @@ -89,16 +89,22 @@ function loginByPhone(phonenumber, code) { |
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | // 获取用户详细信息 | 91 | // 获取用户详细信息 |
| 92 | function getInfo() { | 92 | function getInfo() { |
| 93 | let app | ||
| 93 | return request({ | 94 | return request({ |
| 94 | url: `${config.baseUrl_api}/getInfoForPc`, | 95 | url: `${config.baseUrl_api}/getInfoForPc`, |
| 95 | method: 'get' | 96 | method: 'get' |
| 96 | }).then(res => { | 97 | }).then(res => { |
| 97 | const app = getApp() | 98 | app = getApp() |
| 98 | const user = res.data.user | 99 | const user = res.data.user |
| 99 | uni.setStorageSync('userName', user.userName) | 100 | uni.setStorageSync('userName', user.userName) |
| 100 | uni.setStorageSync('perId', aes.encrypt(user.userId)) | 101 | uni.setStorageSync('perId', aes.encrypt(user.userId)) |
| 101 | app.globalData.userInfo = user | 102 | app.globalData.userInfo = user |
| 103 | }).catch(() => { | ||
| 104 | app = getApp() | ||
| 105 | uni.removeStorageSync('token') | ||
| 106 | uni.removeStorageSync('userName') | ||
| 107 | app.globalData.isLogin = false | ||
| 102 | }) | 108 | }) |
| 103 | } | 109 | } |
| 104 | 110 | ... | ... |
| 1 | // import config from '@/config.js' | 1 | // import config from '@/config.js' |
| 2 | import { | 2 | import _ from 'lodash' |
| 3 | h5LoginAuto | ||
| 4 | } from './login' | ||
| 5 | 3 | ||
| 6 | const excludeUrls = ['pages/index/login', 'pages/index/register'] | 4 | const excludeUrls = ['getMemberCountInfo', 'getInfoForPc'] |
| 7 | 5 | ||
| 8 | // 获取Token | 6 | // 获取Token |
| 9 | function getToken() { | 7 | function getToken() { |
| 10 | try { | 8 | try { |
| 11 | const token = uni.getStorageSync('token') | 9 | const token = uni.getStorageSync('token') |
| 12 | if (token) { | 10 | if (token) { |
| 13 | return token | 11 | return token |
| 14 | } else { | 12 | } else { |
| 15 | return '' | 13 | return '' |
| 16 | } | 14 | } |
| 17 | } catch (e) { | 15 | } catch (e) { |
| 18 | console.log(e) | 16 | console.log(e) |
| 19 | } | 17 | } |
| 20 | } | 18 | } |
| 21 | 19 | ||
| 22 | // 获取请求头 | 20 | // 获取请求头 |
| 23 | function getHeaders() { | 21 | function getHeaders() { |
| 24 | const token = getToken() | 22 | const token = getToken() |
| 25 | const header = { | 23 | const header = { |
| 26 | 'Authorization': token, | 24 | 'Authorization': token, |
| 27 | 'Content-Type': 'application/json', // 根据自己的数据类型 | 25 | 'Content-Type': 'application/json', // 根据自己的数据类型 |
| 28 | // "Content-Type":"application/x-www-form-urlencoded", | 26 | // "Content-Type":"application/x-www-form-urlencoded", |
| 29 | 'Ztx-Per-Id': uni.getStorageSync('perId') || '-1' | 27 | 'Ztx-Per-Id': uni.getStorageSync('perId') || '-1' |
| 30 | } | 28 | } |
| 31 | return header | 29 | return header |
| 32 | } | 30 | } |
| 33 | 31 | ||
| 34 | const request = function(req) { | 32 | const request = function(req) { |
| 35 | req.method = req.method.toUpperCase() | 33 | req.method = req.method.toUpperCase() |
| 36 | if (!['GET', 'POST', 'PUT', 'DELETE'].includes(req.method)) { | 34 | if (!['GET', 'POST', 'PUT', 'DELETE'].includes(req.method)) { |
| 37 | uni.showToast({ | 35 | uni.showToast({ |
| 38 | title: `暂不支持的请求方式: ${req.method}`, | 36 | title: `暂不支持的请求方式: ${req.method}`, |
| 39 | icon: 'none' | 37 | icon: 'none' |
| 40 | }) | 38 | }) |
| 41 | return | 39 | return |
| 42 | } | 40 | } |
| 43 | 41 | ||
| 44 | // if (req.method === 'GET') { | 42 | // if (req.method === 'GET') { |
| 45 | // if (!req.params) { | 43 | // if (!req.params) { |
| 46 | // req.params = {} | 44 | // req.params = {} |
| 47 | // } | 45 | // } |
| 48 | // req.params.pageNum = req.params.pageNum || 1 | 46 | // req.params.pageNum = req.params.pageNum || 1 |
| 49 | // req.params.pageSize = req.params.pageSize || 50 | 47 | // req.params.pageSize = req.params.pageSize || 50 |
| 50 | // } | 48 | // } |
| 51 | 49 | ||
| 52 | // if (req.method == 'POST' && !req.hideLoding) { | 50 | // if (req.method == 'POST' && !req.hideLoding) { |
| 53 | // uni.showLoading({ | 51 | // uni.showLoading({ |
| 54 | // title: '提交中...' | 52 | // title: '提交中...' |
| 55 | // }) | 53 | // }) |
| 56 | // } | 54 | // } |
| 57 | 55 | ||
| 58 | return new Promise((resolve, reject) => { | 56 | return new Promise((resolve, reject) => { |
| 59 | uni.request({ | 57 | uni.request({ |
| 60 | url: req.url, | 58 | url: req.url, |
| 61 | method: req.method, | 59 | method: req.method, |
| 62 | data: req.params, | 60 | data: req.params, |
| 63 | header: getHeaders() | 61 | header: getHeaders() |
| 64 | }).then(res => { | 62 | }).then(res => { |
| 65 | switch (res.statusCode) { | 63 | switch (res.statusCode) { |
| 66 | case 200: | 64 | case 200: |
| 67 | const data = res.data || {} | 65 | const data = res.data || {} |
| 68 | if (!data || data.code === 0 || data.code === 200 || data.pageData?.code === | 66 | if (!data || data.code === 0 || data.code === 200 || data.pageData?.code === 200) { |
| 69 | 200) { | 67 | resolve(data) |
| 70 | resolve(data) | 68 | } else if (_.some(excludeUrls, (url) => req.url.indexOf(url) > -1)) { |
| 71 | } else if (req.url.indexOf('getMemberCountInfo') > -1) { | 69 | resolve(data) |
| 72 | resolve(data) | 70 | } else { |
| 73 | } else { | 71 | if (data.msg) { |
| 74 | if (data.msg) { | 72 | uni.showToast({ |
| 75 | uni.showToast({ | 73 | title: data.msg, |
| 76 | title: data.msg, | 74 | icon: 'none', |
| 77 | icon: 'none', | 75 | duration: 2000 |
| 78 | duration: 2000 | 76 | }) |
| 79 | }) | 77 | } |
| 80 | } | ||
| 81 | 78 | ||
| 82 | // 登录超时 | 79 | // 登录超时 |
| 83 | if (data.code === 60002 || data.code === 60001) { | 80 | // if (data.code === 60002 || data.code === 60001) { |
| 84 | uni.redirectTo({ | 81 | // uni.redirectTo({ |
| 85 | url: '/pages/index/login' | 82 | // url: '/pages/index/login' |
| 86 | }) | 83 | // }) |
| 87 | } else if (data.code === 401) { | 84 | // } else if (data.code === 401) { |
| 88 | h5LoginAuto() | 85 | // h5LoginAuto() |
| 89 | .then(() => { | 86 | // .then(() => { |
| 90 | uni.hideLoading() | 87 | // uni.hideLoading() |
| 91 | uni.redirectTo({ | 88 | // uni.redirectTo({ |
| 92 | url: getCurrentPages()[getCurrentPages() | 89 | // url: getCurrentPages()[getCurrentPages() |
| 93 | .length - 1].$page.fullPath | 90 | // .length - 1].$page.fullPath |
| 94 | }) | 91 | // }) |
| 95 | }) | 92 | // }) |
| 96 | .catch(() => { | 93 | // .catch(() => { |
| 97 | uni.showToast({ | 94 | // debugger |
| 98 | title: '服务异常,请稍后重试', | 95 | // uni.showToast({ |
| 99 | icon: 'none' | 96 | // title: '服务异常,请稍后重试', |
| 100 | }) | 97 | // icon: 'none' |
| 101 | }) | 98 | // }) |
| 102 | } | 99 | // }) |
| 100 | // } | ||
| 103 | 101 | ||
| 104 | reject(res) | 102 | reject(res) |
| 105 | } | 103 | } |
| 106 | break | 104 | break |
| 107 | default: | 105 | default: |
| 108 | reject(res) | 106 | reject(res) |
| 109 | } | 107 | } |
| 110 | }).catch(res => { | 108 | }).catch(res => { |
| 111 | reject(res) | 109 | reject(res) |
| 112 | }).finally(() => { | 110 | }).finally(() => { |
| 113 | // if (req.method == 'POST' && !req.hideLoding) { | 111 | // if (req.method == 'POST' && !req.hideLoding) { |
| 114 | // uni.hideLoading() | 112 | // uni.hideLoading() |
| 115 | // } | 113 | // } |
| 116 | }) | 114 | }) |
| 117 | }) | 115 | }) |
| 118 | } | 116 | } |
| 119 | 117 | ||
| 120 | export default request | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 118 | export default request | ... | ... |
-
Please register or sign in to post a comment