選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

request.js 2.8KB

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