選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

request.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {
  2. encryption
  3. } from "./encryption"
  4. import {
  5. getToken
  6. } from '@/utils/storage'
  7. import {
  8. sm4Key,
  9. } from "../network/api.js";
  10. import {
  11. SM4Util
  12. } from '../util/sm4.js'
  13. const s4 = new SM4Util()
  14. /**
  15. * 配置信息,针对不同的平台进行配置
  16. */
  17. export function request(code, options = {}) {
  18. // console.log('接口请求参数:', options)
  19. //公参
  20. const Common = {
  21. loginSource: '69af303ba2eb4608a099163f0d2a5dbd',
  22. orderSource: 'H5'
  23. }
  24. //Url 地址
  25. // options.url = 'http://' + envs['development'].baseUrl + '/ifzt/api/interfaceMidGroundIn'
  26. // options.url = import.meta.env.VITE_APP_BASE_URL + '/api/interfaceMidGroundIn'
  27. options.url = '/api/interfaceMidGroundIn'
  28. // options.url = 'https://' + 'skx.mynatapp.cc' + '/ifzt/api/interfaceMidGroundIn'
  29. //判断baseUri是否为空
  30. if (options.baseUrl) {
  31. options.url = options.baseUrl
  32. }
  33. //默认json数据格式提交`
  34. let contentType = 'application/json;charset=UTF-8'
  35. //根据type判断数据传输格式
  36. if (options.type && options.type === 2) {
  37. contentType = 'application/x-www-form-urlencoded '
  38. } else if (options.type && options.type === 3) {
  39. contentType = 'multipart/form-data'
  40. }
  41. options.header = {
  42. 'content-type': contentType
  43. }
  44. const token = getToken();
  45. if (token) {
  46. options.header["Access-Token"] = token;
  47. }
  48. //默认POST提交
  49. options.method = options.method ? options.method : 'POST'
  50. //设置请求超时时间
  51. options.timeout = 60000
  52. //判断code不为空
  53. if (code) {
  54. let appId = ''
  55. if (import.meta.env.VITE_APP_TYPE === 'production') {
  56. appId = '52030131'
  57. } else {
  58. appId = 'admin001'
  59. }
  60. //是否显示加载中
  61. if (!options.showLoading) {
  62. uni.showLoading({
  63. title: options.showTitle ? options.showTitle : '加载中',
  64. mask: true
  65. });
  66. }
  67. console.time()
  68. if (options.noCommon) {
  69. // 不携带公参
  70. options.data = encryption(code, {
  71. ...options.data,
  72. 'appId': appId
  73. }, 2);
  74. } else {
  75. options.data = encryption(code, Object.assign(Common, {
  76. ...options.data,
  77. 'appId': appId
  78. }), 2);
  79. }
  80. console.timeEnd()
  81. }
  82. //参数返回
  83. return new Promise((resolve, reject) => {
  84. options.success = (res) => {
  85. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  86. uni.hideLoading()
  87. if (res.data.statusCode !== 0) {
  88. if(!options.hideShowModal && res.data.statusCode !== options.statusCode)
  89. uni.showModal({
  90. title: '提示',
  91. content: res.data.errorMsg,
  92. success: function(res) {
  93. if (res.confirm) {} else if (res.cancel) {}
  94. }
  95. });
  96. }
  97. if (import.meta.env.VITE_APP_TYPE === 'production') {
  98. res = s4.decryptData_CBC(res, sm4Key)
  99. }
  100. //请求成功
  101. resolve(res.data)
  102. }
  103. options.fail = (err) => {
  104. uni.hideLoading()
  105. console.log('请求错误', err)
  106. //处理请求错误
  107. reject(err)
  108. }
  109. uni.request(options)
  110. });
  111. }
  112. // jsonp请求-用于获取公网地址
  113. export function myJsonp(options) {
  114. /* 1.首先动态创建script标签 */
  115. var script = document.createElement("script");
  116. //1.1 把数据对象转换为查询字符串
  117. // var qs = resolveData(options.data);
  118. // 1.2动态生成函数名
  119. var jsonpName = 'ipCallback';
  120. //1.3将当前文件名放到window里
  121. window[jsonpName] = function(res) {
  122. //把结果回掉出去
  123. options.success && options.success(res);
  124. //删除window中的函数名
  125. delete window[jsonpName];
  126. };
  127. /* 2.给src设置url和参数 */
  128. script.src = options.url;
  129. /* 3.添加到html */
  130. document.querySelector("head").appendChild(script);
  131. /* 4.加载完毕后删除script标签 使用监听 */
  132. script.onload = function() {
  133. document.querySelector("head").removeChild(script);
  134. };
  135. }