You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

request.js 3.5KB

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