二 uniapp 请求封装 +header 接口安全 次用aes

319 阅读1分钟
import { EncryptMd5,Encrypt } from "@/utils/aes/aes.js"
export default {
        common: {
                baseUrl: 'http://testapp.xuexiluxian.cn',
                dataType: 'json',
                method: 'GET',
                data: {},
                header: {
                        "Content-Type": "application/json",
                        "Source": ""
                }
        },
        /*
         url: '/slider/getSliders',
        */
        request(options = {}) {
                // 时间戳-接口安全
                let dataTime = new Date().getTime();
                this.common.header.Source = Encrypt("http://testapp.xuexiluxian.cn::"+dataTime+"::" + EncryptMd5("http://testapp.xuexiluxian.cn"+ dataTime))
                let headers = {
                        ...this.common.header,
                        ...options.header
                } 
                options.header = headers
                // 拼接完整url
                let url = options.url
                options.url = this.common.baseUrl + options.url
                // 参数是否存在
                options.data = options.data || this.common.data;
                // dataType
                options.dataType = options.dataType || this.common.dataType

                uni.showLoading({
                        title: '加载中'
                })
                // console.log(options)
                return new Promise((res, req) => {
                        uni.request({
                                ...options,
                                success: result => {
                                        // console.log(result)
                                        // 10006 登录成功的参数
                                        // 登录成功
                                        if (result.data.meta.code === '10006') {
                                                setTimeout(function() {
                                                        uni.hideLoading();
                                                }, 100)
                                                return res(result.data)
                                        } else if (result.data.meta.code === '50002') {
                                                // token过期
                                                // localstorage 
                                                uni.removeStorageSync('token')
                                        }
                                        // 请求接口失败
                                        if (result.data.meta.code !== '200') {
                                                setTimeout(function() {
                                                        uni.hideLoading();
                                                }, 100)
                                                return req(result.data);
                                        } else {
                                                // 请求接口成功
                                                setTimeout(function() {
                                                        uni.hideLoading();
                                                }, 100)
                                                return res(result.data);
                                        }
                                },
                                fail: err => {
                                        // console.log(err)
                                        setTimeout(function() {
                                                uni.hideLoading();
                                        }, 100)
                                        return req(err.data);
                                }
                        })
                })

        }
}