Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

request.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. appId
  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. // 引入 sm-crypto 库
  15. import {
  16. SM4Util
  17. } from '../util/sm4.js'
  18. const s4 = new SM4Util()
  19. /**
  20. * 配置信息,针对不同的平台进行配置
  21. */
  22. const envs = {
  23. //开发环境配置
  24. development: {
  25. baseUrl: "http://192.168.100.63:8087/ifzt/api/interfaceMidGroundIn",
  26. },
  27. //生产环境配置 https://testzeus.etcjz.cn/api/common/transmit/v1/send/zt
  28. production: {
  29. baseUrl: "https://testzeus.etcjz.cn/api/common/transmit/v1/send/zt",
  30. }
  31. }
  32. //https://etcfile.etcjz.cn 文件服务器
  33. //公参
  34. export function request(code, options = {}) {
  35. //公参
  36. const Common = {
  37. agentId: "52010106004",
  38. channelId: "5201010200601130001",
  39. channelType: "1",
  40. staffId: "54623263cb4d4a289dccbc983b22a4af",
  41. terminalId: "999999999999",
  42. loginSource: "69af303ba2eb4608a099163f0d2a5dbd"
  43. }
  44. //Url 地址
  45. //options.url = envs[process.env.NODE_ENV].baseUrl || '' + options.url
  46. options.url = 'http://192.168.100.63:8087/ifzt/api/interfaceMidGroundIn'
  47. // options.url = 'http://222.85.144.89:19002/ifzt/api/interfaceMidGroundIn'
  48. //判断baseUri是否为空
  49. if (options.baseUrl) {
  50. options.url = options.baseUrl
  51. }
  52. //默认json数据格式提交
  53. let contentType = 'application/x-www-form-urlencoded'
  54. //根据type判断数据传输格式
  55. if (options.type && options.type === 2) {
  56. contentType = 'application/json;charset=UTF-8'
  57. }
  58. options.header = {
  59. 'content-type': contentType,
  60. 'Access-Token': getItem(StorageKeys.Token)
  61. }
  62. //默认POST提交
  63. options.method = options.method ? options.method : 'POST'
  64. //设置请求超时时间
  65. options.timeout = 60000
  66. //判断code不为空
  67. if (code) {
  68. options.data = encryption(code, Object.assign(Common, {
  69. ...options.data,
  70. 'appId': appId
  71. }), 2);
  72. }
  73. //是否显示加载中
  74. if (options.showLoading) {
  75. wx.showLoading({
  76. title: '请稍后',
  77. mask: true
  78. });
  79. }
  80. //参数返回
  81. return new Promise((resolve, reject) => {
  82. options.success = (res) => {
  83. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  84. wx.hideLoading()
  85. if (res.data.statusCode !== 0 && res.data.statusCode !== 200) {
  86. if (res.data.statusCode == 600) {
  87. resolve(res.data)
  88. } else {
  89. wx.showModal({
  90. title: '提示',
  91. content: res.data.errorMsg,
  92. success: function(res) {
  93. if (res.confirm) {
  94. console.log('用户点击确定');
  95. } else if (res.cancel) {
  96. console.log('用户点击取消');
  97. }
  98. }
  99. });
  100. }
  101. reject(res.data.errorMsg)
  102. return
  103. }
  104. let content = s4.decryptData_CBC(res, sm4Key)
  105. content.data.bizContent = JSON.stringify(content.data.bizContent)
  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. }