request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const { api } = require('../config/env.js')
  2. const { DEBUG, NOT_LOGIN } = require('../config/index.js')
  3. const { isUndef, getStorageSync, getLoginUserInfo } = require('./util.js')
  4. const md5 = require('./md5.min.js')
  5. export const commonParams = () => {
  6. let systemInfo = getStorageSync('systemInfo')
  7. if (!systemInfo) {
  8. try {
  9. systemInfo = wx.getSystemInfoSync()
  10. if (systemInfo) {
  11. wx.setStorageSync('systemInfo', systemInfo)
  12. }
  13. } catch (error) {}
  14. }
  15. let deviceId = 'weapp'
  16. let systemVersion = 'weapp'
  17. if (systemInfo) {
  18. let {
  19. brand,
  20. model,
  21. system
  22. } = systemInfo
  23. if (brand || model) {
  24. deviceId = brand + model
  25. }
  26. if (system && 'string' === typeof system) {
  27. systemVersion = system
  28. }
  29. }
  30. const params = {
  31. cityId: '254', //城市ID默认为2
  32. language: 'cn', //语言参数
  33. program: 'liuxue', // 小程序名称
  34. deviceType: 'weapp', //接口调用来源参数
  35. deviceId: deviceId, //设备唯一标识码
  36. apiVersion: '1.0.0', //api版本号
  37. systemVersion: systemVersion //操作系统及版本号
  38. }
  39. const loginUserInfo = getLoginUserInfo()
  40. if (loginUserInfo && loginUserInfo.token) {
  41. params.token = loginUserInfo.token //用户登录凭证token
  42. }
  43. let citySelectObj = getStorageSync('citySelectObj')
  44. if (citySelectObj) {
  45. params.cityId = citySelectObj.cityId //用户选择的城市id
  46. }
  47. return params
  48. }
  49. export const paramsSign = (params, url) => {
  50. /*参数签名*/
  51. let key = 'd9ce9fe030776727e85442d033d1be84'
  52. if (url && url.indexOf('/common/') !== -1) {
  53. key = 'b15681f74461af9642ab5356fba7962b'
  54. }
  55. const tempParams = { ...params }
  56. const paramsString = Object.keys(tempParams).sort().map(key => {
  57. return `${key}=${tempParams[key]}`
  58. }).concat(`key=${key}`).join('&')
  59. return md5(paramsString).toLowerCase()
  60. }
  61. export const request = (params) => {
  62. return new Promise((resolve, reject) => {
  63. const temp = params
  64. // 过滤空值
  65. for (const key in temp.data) {
  66. if (isUndef(temp.data[key])) {
  67. delete temp.data[key]
  68. }
  69. }
  70. temp.data = { ...commonParams(), ...temp.data }
  71. temp.data.signToken = paramsSign(temp.data, temp.url)
  72. wx.request({
  73. url: api + temp.url,
  74. data: temp.data,
  75. header: {
  76. 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  77. ...temp.header
  78. },
  79. method: temp.method || 'POST',
  80. dataType: temp.dataType || 'json',
  81. success: res => {
  82. const response = res.data
  83. DEBUG && console.log(`Request for ${temp.url} exception.--->${response.msg}`)
  84. if (response && response.code !== 0) {
  85. // 未登录
  86. if (NOT_LOGIN === response.code) {
  87. wx.clearStorage()
  88. wx.showToast({
  89. title: '您的用户信息已失效,请重新登录操作',
  90. icon: 'none',
  91. duration: 2000,
  92. success: () => {
  93. wx.navigateTo({
  94. url: '/pages/login/login'
  95. })
  96. }
  97. })
  98. } else {
  99. wx.showToast({
  100. title: response.msg || 'error',
  101. icon: 'none',
  102. duration: 2000
  103. })
  104. reject(response)
  105. }
  106. } else {
  107. resolve(response)
  108. }
  109. },
  110. fail: err => {
  111. DEBUG && console.log(`Request for ${temp.url} fail.--->`, err)
  112. reject(err)
  113. },
  114. complete: () => {
  115. wx.stopPullDownRefresh()
  116. }
  117. })
  118. })
  119. }