Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. if (options.noCommon) {
  61. // 不携带公参
  62. options.data = encryption(code, {
  63. ...options.data,
  64. 'appId': appId
  65. }, 2);
  66. } else {
  67. options.data = encryption(code, Object.assign(Common, {
  68. ...options.data,
  69. 'appId': appId
  70. }), 2);
  71. }
  72. }
  73. //是否显示加载中
  74. if (!options.showLoading) {
  75. uni.showLoading({
  76. title: options.showTitle ? options.showTitle : '加载中',
  77. mask: true
  78. });
  79. }
  80. //参数返回
  81. return new Promise((resolve, reject) => {
  82. options.success = (res) => {
  83. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  84. uni.hideLoading()
  85. if (res.data.statusCode !== 0) {
  86. uni.showModal({
  87. title: '提示',
  88. content: res.data.errorMsg,
  89. success: function(res) {
  90. if (res.confirm) {} else if (res.cancel) {}
  91. }
  92. });
  93. }
  94. if (import.meta.env.VITE_APP_TYPE === 'production') {
  95. res = s4.decryptData_CBC(res, sm4Key)
  96. }
  97. //请求成功
  98. resolve(res.data)
  99. }
  100. options.fail = (err) => {
  101. uni.hideLoading()
  102. console.log('请求错误', err)
  103. //处理请求错误
  104. reject(err)
  105. }
  106. uni.request(options)
  107. });
  108. }
  109. // jsonp请求-用于获取公网地址
  110. export function myJsonp(options) {
  111. /* 1.首先动态创建script标签 */
  112. var script = document.createElement("script");
  113. //1.1 把数据对象转换为查询字符串
  114. // var qs = resolveData(options.data);
  115. // 1.2动态生成函数名
  116. var jsonpName = 'ipCallback';
  117. //1.3将当前文件名放到window里
  118. window[jsonpName] = function(res) {
  119. //把结果回掉出去
  120. options.success && options.success(res);
  121. //删除window中的函数名
  122. delete window[jsonpName];
  123. };
  124. /* 2.给src设置url和参数 */
  125. script.src = options.url;
  126. /* 3.添加到html */
  127. document.querySelector("head").appendChild(script);
  128. /* 4.加载完毕后删除script标签 使用监听 */
  129. script.onload = function() {
  130. document.querySelector("head").removeChild(script);
  131. };
  132. }