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

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