Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

request.js 4.2KB

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