Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

request.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {
  2. appId,envs
  3. } from "./api";
  4. import {
  5. encryption
  6. } from "./encryption";
  7. import {
  8. getItem,
  9. StorageKeys
  10. } from "../storage";
  11. import {
  12. sm4Key
  13. } from "../network/api.js";
  14. import {
  15. SM4Util
  16. } from '../util/sm4.js'
  17. const s4 = new SM4Util()
  18. //请求
  19. export function request(code, options = {}) {
  20. //公参
  21. const Common = {
  22. agentId: "52010106004",
  23. channelId: "5201010200601130001",
  24. channelType: "1",
  25. staffId: "54623263cb4d4a289dccbc983b22a4af",
  26. terminalId: "999999999999",
  27. loginSource: "69af303ba2eb4608a099163f0d2a5dbd"
  28. }
  29. //Url 地址
  30. options.url = 'http://'+ envs[process.env.NODE_ENV].baseUrl +'/ifzt/api/interfaceMidGroundIn'
  31. //判断baseUri是否为空
  32. if (options.baseUrl) {
  33. options.url = options.baseUrl
  34. }
  35. //默认json数据格式提交
  36. let contentType = 'application/x-www-form-urlencoded'
  37. //根据type判断数据传输格式
  38. if (options.type && options.type === 2) {
  39. contentType = 'application/json;charset=UTF-8'
  40. }
  41. options.header = {
  42. 'content-type': contentType,
  43. 'Access-Token': getItem(StorageKeys.Token)
  44. }
  45. //默认POST提交
  46. options.method = options.method ? options.method : 'POST'
  47. //设置请求超时时间
  48. options.timeout = 60000
  49. //判断code不为空
  50. if (code) {
  51. options.data = encryption(code, Object.assign(Common, {
  52. ...options.data,
  53. 'appId': appId
  54. }), 2);
  55. }
  56. //是否显示加载中
  57. if (options.showLoading) {
  58. wx.showLoading({
  59. title: '请稍后',
  60. mask: true
  61. });
  62. }
  63. //参数返回
  64. return new Promise((resolve, reject) => {
  65. options.success = (res) => {
  66. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  67. wx.hideLoading()
  68. if (res.data.statusCode !== 0) {
  69. if (res.data.statusCode == 600) {
  70. resolve(res.data)
  71. } else if(res.data.statusCode == 401){
  72. wx.showModal({
  73. title: '提示',
  74. content: res.data.errorMsg,
  75. confirmText:'去登录',
  76. showCancel: false,
  77. success: function(res) {
  78. if (res.confirm) {
  79. uni.navigateTo({
  80. url: '/login/login',
  81. })
  82. }
  83. }
  84. });
  85. }else {
  86. wx.showModal({
  87. title: '提示',
  88. content: res.data.errorMsg,
  89. success: function(res) {
  90. if (res.confirm) {
  91. console.log('用户点击确定');
  92. } else if (res.cancel) {
  93. console.log('用户点击取消');
  94. }
  95. }
  96. });
  97. }
  98. reject(res.data.errorMsg)
  99. return
  100. }
  101. let content = res
  102. // let content = s4.decryptData_CBC(res, sm4Key)
  103. // console.log(content);
  104. // content.data.bizContent = JSON.stringify(content.data.bizContent)
  105. // console.log(content.data);
  106. resolve(content.data)
  107. }
  108. options.fail = (err) => {
  109. wx.hideLoading()
  110. console.log('请求错误', err)
  111. //处理请求错误
  112. reject(err)
  113. }
  114. wx.request(options)
  115. });
  116. }