request.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import sysconfig from '../sysconfig/sysconfig.js'
  2. import crypt from './crypt.js'
  3. let Fly = require("flyio")//引入fly包
  4. let fly = new Fly()
  5. //接口地址
  6. fly.config.baseURL = sysconfig.baseURL
  7. //请求拦截,config是请求对象
  8. fly.interceptors.request.use((config) => {
  9. //组织请求参数密钥
  10. const aesKey = sysconfig.copyright + sysconfig.app.snCode
  11. //同步读取本地token
  12. const token = wx.getStorageSync(sysconfig.user.tokenKey)
  13. if (token) {
  14. //判断当前请求api是否在白名单内,在白名单内的api不需要添加token
  15. let apiName = ""
  16. const urlList = config.url.split("/")
  17. const apiWhiteList = sysconfig.apiWhiteList
  18. if (urlList.length > 1) {
  19. apiName = urlList[1]
  20. } else {
  21. apiName = urlList[0]
  22. }
  23. //不在白名单内,则添加token,并且对token进行加密
  24. if (!apiWhiteList.includes(apiName)) {
  25. let item = {}
  26. item["rnd"] = Math.random().toString()
  27. item["token"] = token
  28. let plaintext = JSON.stringify(item)
  29. //对token进行加密
  30. const ciphertext = crypt.aesEncode(plaintext, aesKey)
  31. config.headers.Authorization = ciphertext
  32. }
  33. }
  34. //对get参数进行加密
  35. if (config.params && (Object.keys(config.params).length) > 0) {
  36. const params = config.params
  37. const paramsText = JSON.stringify(params)
  38. if (paramsText != "{}") {
  39. const ciphertext = crypt.aesEncode(params, aesKey)
  40. config.params = {}
  41. config.params = ciphertext
  42. }
  43. }
  44. //对post参数进行加密
  45. if (config.body && (Object.keys(config.body).length) > 0) {
  46. const plaintext = JSON.stringify(config.body)
  47. const ciphertext = crypt.aesEncode(plaintext, aesKey)
  48. config.body = {}
  49. config.body = ciphertext
  50. }
  51. return config
  52. }, error => {
  53. return Promise.reject(error)
  54. })
  55. export default fly