123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //用BASEURL来表示域名地址
- // const BASE_URL = 'http://192.168.110.153:9002'
- // const BASE_URL = 'https://zswl.api.jpy.wang/zswl-cloud-bdb'
- // const BASE_URL = 'https://api.dev.zonelife.cn'
- import {
- BASE_URL
- } from '@/utils/config.js';
- //封装请求方法
- export let request = (options) => {
- return new Promise((resolve, reject) => {
- //请求路径拼接,,其中options.url就是通过下面方法myRequest获取到接口部分的url
- //method--请求方法,不是method的post就是get
- //请求的参数,当没有参数的时候就是空对象
- request.requestTask = uni.request({
- url: options.url.indexOf('http') == -1 ? (BASE_URL + options.url) : options.url,
- method: options.method || 'GET',
- data: options.data || {},
- header: {
- accessToken: uni.getStorageSync('token'),
- code: 1
- },
- success: (res) => {
- if (res.statusCode !== 200) {
- return uni.showToast({
- title: '获取失败',
- icon: 'error',
- duration: 5000
- })
- // uni.hideLoading()
- } else if (res.data.msg != '成功') { //接口返回报错
- let strUrl = '/vipserver/group/user/activation'
- let isContained = options.url.indexOf(strUrl) !== -1;
- if (!isContained) {
- setTimeout(() => {
- uni.showToast({
- title: res.data.content || res.data.msg || (res.data.exception && res.data.exception.message) ||'请求失败',
- icon: 'none',
- duration: 3000
- })
- })
- }
- console.error('报错接口:', BASE_URL + options.url, '报错信息:', res.data);
- // token过期或出了问题
- if (res.data.exception && res.data.exception.type ==
- 'AuthenticationCredentialsNotFoundException') {
- uni.showToast({
- title: '登录过期',
- icon: 'error',
- duration: 5000
- })
- if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token')) {
- // 刷新token
- uni.showLoading({
- title: '刷新登录中',
- duration: 5000
- })
- uni.request({
- url: BASE_URL + '/zswl-cloud-bdb/user/refreshToken',
- method: 'GET',
- data: {
- refreshToken: uni.getStorageSync('refreshToken')
- },
- success(r) {
- uni.hideLoading()
- if (r.data.content == '刷新令牌错误') {
- setTimeout(() => {
- uni.showToast({
- title: '登录失效',
- icon: 'fail'
- })
- }, 0)
- uni.clearStorageSync()
- uni.reLaunch({
- url: '/pages/index/index'
- })
- } else {
- uni.setStorageSync('token', r.data
- .content) //登录状态
- // 刷新页面
- var pages = getCurrentPages();
- var url = '/' + pages[pages.length - 1].route
- uni.reLaunch({
- url
- })
- }
- }
- })
- } else {
- uni.hideLoading()
- console.log(111);
- uni.showModal({
- title: '请登录',
- confirmText: '去登录',
- success(res) {
- if (res.confirm) {
- var pages = getCurrentPages()
- let parmas = pages[pages.length - 1].options
- console.log(11111, parmas);
- let str = ''
- for (let key in parmas) {
- str += `&${key}=${parmas[key]}`
- }
- console.log(2222222, str);
- var url = '/' + pages[pages.length - 1].route
- console.log(333333, url);
- uni.reLaunch({
- url: '/login/login/login?redirect=' +
- url + str
- })
- } else if (res.cancel) {
- console.log('拒绝登录,跳转首页');
- uni.switchTab({
- url: '/pages/index/index'
- })
- }
- }
- })
- }
- }
- }
- resolve(res.data)
- },
- //请求失败
- fail: (err) => {
- setTimeout(() => {
- uni.showToast({
- title: '请求接口失败',
- icon: "none",
- duration: 5000
- })
- }, 0)
- reject(err)
- }
- })
- })
- }
|