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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. appId
  3. } from "./api";
  4. import {
  5. encryption
  6. } from "./encryption";
  7. import {
  8. getItem,
  9. StorageKeys
  10. } from "../storage";
  11. /**
  12. * 配置信息,针对不同的平台进行配置
  13. */
  14. const envs = {
  15. //开发环境配置
  16. development: {
  17. baseUrl: "http://192.168.100.63:8087/ifzt/api/interfaceMidGroundIn",
  18. },
  19. //生产环境配置 https://testzeus.etcjz.cn/api/common/transmit/v1/send/zt
  20. production: {
  21. baseUrl: "https://testzeus.etcjz.cn/api/common/transmit/v1/send/zt",
  22. }
  23. }
  24. //222.85.144.89:19003
  25. //https://etcfile.etcjz.cn 文件服务器
  26. //公参
  27. export function request(code, options = {}) {
  28. //工参
  29. const Common = {
  30. agentId: "52010106004",
  31. channelId: "5201010200601130001",
  32. channelType: "1",
  33. staffId: "54623263cb4d4a289dccbc983b22a4af",
  34. terminalId: "999999999999"
  35. }
  36. //Url 地址
  37. //options.url = envs[process.env.NODE_ENV].baseUrl || '' + options.url
  38. options.url = 'http://192.168.100.63:8087/ifzt/api/interfaceMidGroundIn'
  39. // options.url = 'http://222.85.144.89:19002/ifzt/api/interfaceMidGroundIn'
  40. //判断baseUri是否为空
  41. if (options.baseUrl) {
  42. options.url = options.baseUrl
  43. }
  44. //默认json数据格式提交
  45. let contentType = 'application/x-www-form-urlencoded'
  46. //根据type判断数据传输格式
  47. if (options.type && options.type === 2) {
  48. contentType = 'application/json;charset=UTF-8'
  49. }
  50. options.header = {
  51. 'content-type': contentType,
  52. 'Access-Token': getItem(StorageKeys.Token)
  53. }
  54. //默认POST提交
  55. options.method = options.method ? options.method : 'POST'
  56. //设置请求超时时间
  57. options.timeout = 60000
  58. // if (!options.data.tel) {
  59. // options.data.tel = uni.getStorageSync('operatorId')
  60. // }
  61. // options.data.orderNo = uni.getStorageSync('orderNo')
  62. // options.data.operatorId = uni.getStorageSync('operatorId') //操作人编号
  63. // options.data.applySourceType = '6' //申请来源 6 H5
  64. //判断code不为空
  65. if (code) {
  66. options.data = encryption(code, Object.assign(Common, {
  67. ...options.data,
  68. 'appId': appId
  69. }), 2);
  70. }
  71. console.log('======请求参数======', options.data)
  72. //是否显示加载中
  73. if (options.showLoading) {
  74. wx.showLoading({
  75. title: '请稍后',
  76. mask: true
  77. });
  78. }
  79. //参数返回
  80. return new Promise((resolve, reject) => {
  81. options.success = (res) => {
  82. console.log('请求返回结果:======', 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. // console.log("请求错误")
  102. reject(res.data.errorMsg)
  103. return
  104. }
  105. console.log("请求成功")
  106. //请求成功
  107. resolve(res.data)
  108. }
  109. options.fail = (err) => {
  110. wx.hideLoading()
  111. console.log('请求错误', err)
  112. //处理请求错误
  113. reject(err)
  114. }
  115. wx.request(options)
  116. });
  117. }