1

写文章的故事:给整宕机了

第一步:定义请求文件,request.js

import encrypt from '@/util/encrypt'
import {
    settings
} from '@/settings.js'

const BASE_URL = settings.BASE_URL
const encryptKey = settings.ENCRYPT_KEY
const whiteList = settings.WHITE_LIST

export const request = (options) => {

    const token = uni.getStorageSync('shopToken')
    const head = {
        'Authorization': '',
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
    }
    
    if (!options.header || !options.header['Content-Type']) {
        options.header = head
    }
    
    if (whiteList.indexOf(options.url) === -1) {
        if (!token) {
            uni.showToast({
                icon: "none",
                title: "请先登录"
            })
            uni.removeStorageSync("shopToken")
            
            return Promise.reject("请先登录")
        } else {
            options.header.Authorization = token
        }
    }
    
    if (options.data) {
        options.data['sign'] = encrypt.encryptMd5(options.data, encryptKey)
    }
        
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + options.url,
            method: options.method || 'GET',
            data: options.data || {},
            header: {
                'Authorization': options.header.Authorization,
                'Content-Type': options.header['Content-Type']
            },
            success: (res) => {
                if (res.data.code !== 200) {
                    if (res.data.code === 1000 || res.datacode === 1001 || res.datacode ===
                        1002 || res.data.code === 1010 || res.data.code === 500) { 
                        uni.showModal({
                            title: '提示',
                            content: '长时间未操作请重新登录',
                            showCancel: false,
                            success: function(d) {
                                uni.removeStorageSync('shopToken')
                                
                                uni.navigateTo({
                                   url: '/pages/login/register',
                                })
                            }
                        });
                    } else if (res.data.msg != 'token不匹配') {
                    } else if (res.data.msg == 'token不匹配') {
                        res.data.msg = "登录状态过期请重新登录"
                    }
                    
                    uni.showToast({ icon: "none", title: res.data.msg })
                    reject(res.data.msg)
                } else {
                    resolve(res.data)
                }
            },
            fail(err) {
                reject(err)
            }
        })
    })
}

第二步:settings.js

export const settings = { 
    // #ifdef MP-WEIXIN
    BASE_URL: 'http://192.168.101.35', // 访问接口 
    // #endif
    
    // #ifdef H5
    BASE_URL: '', // 访问接口 
    // #endif
    
    // BASE_URL: '', // 访问接口
    ENCRYPT_KEY: '', // 前端加密key
    WHITE_LIST: ["/api/lifeMerchant/baseInfo/selectSortByDistance", "/api/middle/sendSms", "/api/lifeMerchant/account/merchantRegisterLogin"] , // 接口过滤白名单 ,'/alipay/credit','/alipay/pay',"/alipay/tradeRefund","/alipay/unFreeze"
}

ThinkPHP
4 声望3 粉丝