123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import sysconfig from '../sysconfig/sysconfig.js'
- import crypt from './crypt.js'
- let Fly = require("flyio/dist/npm/wx")//引入fly包
- let fly = new Fly()
- //本地
- fly.config.baseURL = "http://127.0.0.1:8080/msapi"
- //远程
- fly.config.baseURL = "http://www.locaty.com.cn/msapi"
- //请求拦截,config是请求对象
- fly.interceptors.request.use((config) => {
- //组织请求参数密钥
- const aesKey = sysconfig.copyright + sysconfig.app.snCode
- //同步读取本地token
- const token = wx.getStorageSync(sysconfig.user.tokenKey)
- if (token) {
- //判断当前请求api是否在白名单内,在白名单内的api不需要添加token
- let apiName = ""
- const urlList = config.url.split("/")
- const apiWhiteList = sysconfig.apiWhiteList
- if (urlList.length > 1) {
- apiName = urlList[1]
- } else {
- apiName = urlList[0]
- }
- //不在白名单内,则添加token,并且对token进行加密
- if (!apiWhiteList.includes(apiName)) {
- let item = {}
- item["rnd"] = Math.random().toString()
- item["token"] = token
- let plaintext = JSON.stringify(item)
- //对token进行加密
- const ciphertext = crypt.aesEncode(plaintext, aesKey)
- config.headers.Authorization = ciphertext
- }
- }
- //对get参数进行加密
- if (config.params && (Object.keys(config.params).length) > 0) {
- const params = config.params
- const paramsText = JSON.stringify(params)
- if (paramsText != "{}") {
- const ciphertext = crypt.aesEncode(params, aesKey)
- config.params = {}
- config.params = ciphertext
- }
- }
- //对post参数进行加密
- if (config.body && (Object.keys(config.body).length) > 0) {
- const plaintext = JSON.stringify(config.body)
- const ciphertext = crypt.aesEncode(plaintext, aesKey)
- config.body = {}
- config.body = ciphertext
- }
- return config
- }, error => {
- return Promise.reject(error)
- })
- export default fly
|