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

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