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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {
  2. encryption
  3. } from "./encryption"
  4. import {
  5. getToken,
  6. getItem,
  7. StorageKeys
  8. } from '@/utils/storage'
  9. import {
  10. sm4Key,
  11. } from "../network/api.js";
  12. import {
  13. SM4Util
  14. } from '../util/sm4.js'
  15. const s4 = new SM4Util()
  16. /**
  17. * 配置信息,针对不同的平台进行配置
  18. */
  19. export function request(code, options = {}) {
  20. // console.log('接口请求参数:', options)
  21. //公参
  22. const Common = {
  23. // loginSource: '69af303ba2eb4608a099163f0d2a5dbd',
  24. loginSource: '431bfdc8b9c645e0b293b85d6ce1948n',
  25. orderSource: 'H5'
  26. }
  27. //Url 地址
  28. // options.url = 'http://' + envs['development'].baseUrl + '/ifzt/api/interfaceMidGroundIn'
  29. // options.url = import.meta.env.VITE_APP_BASE_URL + '/api/interfaceMidGroundIn'
  30. options.url = import.meta.env.VITE_APP_BASE_URL + import.meta.env.VITE_APP_TYPE_URL + '/api/interfaceMidGroundIn'
  31. // options.url = 'https://' + 'skx.mynatapp.cc' + '/ifzt/api/interfaceMidGroundIn'
  32. //判断baseUri是否为空
  33. if (options.baseUrl) {
  34. options.url = options.baseUrl
  35. }
  36. //默认json数据格式提交`
  37. let contentType = 'application/json;charset=UTF-8'
  38. //根据type判断数据传输格式
  39. if (options.type && options.type === 2) {
  40. contentType = 'application/x-www-form-urlencoded '
  41. } else if (options.type && options.type === 3) {
  42. contentType = 'multipart/form-data'
  43. }
  44. options.header = {
  45. 'content-type': 'application/json'
  46. }
  47. const token = getToken();
  48. if (token) {
  49. options.header["Access-Token"] = token;
  50. }
  51. //默认POST提交
  52. options.method = options.method ? options.method : 'POST'
  53. //设置请求超时时间
  54. options.timeout = 10000
  55. //判断code不为空
  56. if (code) {
  57. let appId = ''
  58. if (import.meta.env.VITE_APP_TYPE === 'production') {
  59. appId = '52030131'
  60. } else {
  61. appId = 'admin001'
  62. }
  63. //是否显示加载中
  64. if (!options.showLoading) {
  65. uni.showLoading({
  66. title: options.showTitle ? options.showTitle : '加载中',
  67. mask: true
  68. });
  69. }
  70. console.time()
  71. if (options.noCommon) {
  72. // 不携带公参
  73. options.data = encryption(code, {
  74. ...options.data,
  75. 'appId': appId
  76. }, 2);
  77. } else {
  78. options.data = encryption(code, Object.assign(Common, {
  79. ...options.data,
  80. 'appId': appId
  81. }), 2);
  82. }
  83. console.timeEnd()
  84. }
  85. //参数返回
  86. return new Promise((resolve, reject) => {
  87. options.success = (res) => {
  88. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  89. // uni.hideLoading()
  90. if (res.data.statusCode !== 0) {
  91. if (!options.hideShowModal && res.data.statusCode !== options.statusCode)
  92. uni.showModal({
  93. title: '提示',
  94. content: res.data.errorMsg,
  95. success: function(res) {
  96. if (res.confirm) {} else if (res.cancel) {}
  97. }
  98. });
  99. }
  100. if (import.meta.env.VITE_APP_TYPE === 'production' && res.data.statusCode === 0) {
  101. // let cssObj = {
  102. // data: {
  103. // bizContent: "ePn4Q6MBw3gYi+IWhpvEUrV7iQg7cW/OtBGMWKUif9QWFJnOBKhxFY4jcRxRrpiyw9uiwGtZHD46Qoq2a1H6UhTrzz2RX8qwbuREi61si+mkIVFJBMTkZTiv6XraZ0j1TsIt8a7fB+eHri3HQisHEdPoPAFCHRmeOSTLh2ivpkcG3LzF0gut1An38vXCnMPQZo/5LThK2PZUwp/qRprde75T8k74p/ACS9YFWXr5RpEkEATqrkH0v0A8F4evxxwiwIvwfNJjrcCXWdARxwNVjThMxB4nkUKcL3R8SCAXcFQc+64YKxUKi82OOyhdOtkEZbOKo4QRpP6H9CCJ22CmwUPRr73oM9IngpVeYwUnh1YH/HC4WXIl90wGLDA8hh9bmIU6o+1BMb5vY8SWPj/GqGq96kRYd+5dnyJ+Vdnlm5nD4QXft/2E+GkfAN/kq8qu2LghR2RN1XmsI8el2jcmmpBRcMy2dZ1/vxjw0bqzIRpN4lFFN9bDU4wAL3xn/15vN8l1VZbQmRKpPtsaUkgqg35tHY0N11pWZrLgGhuRxSAQWccZ/ViVx6J64j3w4j3YUBT/FQFOZ/DySq2LA2sDwC1mB/BGyDiuM1PqywhfGW3mw5shGmz2CBRwGNjbumeI8gbSiqx0UTrcBXu5SFQQyndCGKdvJmXTWgj/K/qieNHUxzC95Cpy3BvJCnD8y7QKHXR/HLhKD9HU6FMaggE7Fw=="
  104. // }
  105. // }
  106. // const dataObjs = s4.decryptData_CBC(cssObj, sm4Key)
  107. // console.log(cssObj.data.bizContent, s4.decryptData_CBC(cssObj, sm4Key))
  108. // console.log(JSON.parse(dataObjs.data.bizContent))
  109. res = s4.decryptData_CBC(res, sm4Key)
  110. }
  111. //请求成功
  112. resolve(res.data)
  113. }
  114. options.fail = (err) => {
  115. uni.hideLoading()
  116. console.log('请求错误', err)
  117. //处理请求错误
  118. uni.showModal({
  119. title: '提示',
  120. content: '请求错误,请检查网络环境',
  121. showCancel: false,
  122. success: (res) => {
  123. reject(err)
  124. }
  125. });
  126. }
  127. options.complete = () => {
  128. uni.hideLoading()
  129. }
  130. uni.request(options)
  131. });
  132. }
  133. // jsonp请求-用于获取公网地址
  134. export function myJsonp(options) {
  135. /* 1.首先动态创建script标签 */
  136. var script = document.createElement("script");
  137. //1.1 把数据对象转换为查询字符串
  138. // var qs = resolveData(options.data);
  139. // 1.2动态生成函数名
  140. var jsonpName = 'ipCallback';
  141. //1.3将当前文件名放到window里
  142. window[jsonpName] = function(res) {
  143. //把结果回掉出去
  144. options.success && options.success(res);
  145. //删除window中的函数名
  146. delete window[jsonpName];
  147. };
  148. /* 2.给src设置url和参数 */
  149. script.src = options.url;
  150. /* 3.添加到html */
  151. document.querySelector("head").appendChild(script);
  152. /* 4.加载完毕后删除script标签 使用监听 */
  153. script.onload = function() {
  154. document.querySelector("head").removeChild(script);
  155. };
  156. }
  157. //请求
  158. export function requestNew(code, options = {}, start = false) {
  159. const token = getToken();
  160. //公参
  161. const Common = {
  162. opId: getItem(StorageKeys.OpenId),
  163. openId: getItem(StorageKeys.OpenId),
  164. orderSource: 'H5',
  165. loginSource: '431bfdc8b9c645e0b293b85d6ce1948n',
  166. accessToken:token
  167. }
  168. options.url = import.meta.env.VITE_APP_BASE_URL + import.meta.env.VITE_APP_TYPE_URL + code
  169. // options.url = import.meta.env.VITE_APP_BASE_URL + code
  170. //默认json数据格式提交`
  171. let contentType = 'application/json'
  172. //判断baseUri是否为空
  173. if (options.baseUrl) {
  174. options.url = options.baseUrl
  175. }
  176. //根据type判断数据传输格式
  177. if (options.type && options.type === 2) {
  178. contentType = 'application/json;charset=UTF-8'
  179. }
  180. //默认POST提交
  181. options.method = options.method ? options.method : 'POST'
  182. //设置请求超时时间
  183. options.timeout = 60000
  184. options.header = {
  185. 'content-type': contentType,
  186. }
  187. if (token) {
  188. options.header["Access-Token"] = token;
  189. }
  190. if (!start) {
  191. //判断code不为空
  192. if (code) {
  193. options.data = Object.assign(Common, {
  194. ...options.data
  195. });
  196. }
  197. }
  198. console.log('code', code, options.data)
  199. //是否显示加载中
  200. if (options.showLoading) {
  201. uni.showLoading({
  202. title: '请稍后',
  203. mask: true
  204. });
  205. }
  206. //参数返回
  207. return new Promise((resolve, reject) => {
  208. options.success = (res) => {
  209. // 请求返回后,隐藏loading(如果请求返回快的话,可能会没有loading)
  210. if (options.showLoading) {
  211. uni.hideLoading()
  212. }
  213. console.log("返回结果" + code, res)
  214. if (res.data.code === 0) {
  215. resolve(res.data)
  216. } else {
  217. uni.showModal({
  218. title: '提示',
  219. content: res.data.message,
  220. success: function(res) {
  221. if (res.confirm) {
  222. console.log('用户点击确定2');
  223. } else if (res.cancel) {
  224. console.log('用户点击取消2');
  225. }
  226. }
  227. });
  228. reject(res.data.message)
  229. return
  230. }
  231. }
  232. options.fail = (err) => {
  233. uni.hideLoading()
  234. console.log('请求错误', err)
  235. //处理请求错误
  236. reject(err)
  237. }
  238. uni.getNetworkType({
  239. success: function(res) {
  240. if (res.networkType == 'none') {
  241. uni.showModal({
  242. title: '提示',
  243. content: "网络异常",
  244. success: function(res) {
  245. if (res.confirm) {
  246. console.log('用户点击确定');
  247. } else if (res.cancel) {
  248. console.log('用户点击取消');
  249. }
  250. }
  251. });
  252. } else {
  253. uni.request(options)
  254. }
  255. console.log("res.networkType", res.networkType);
  256. }
  257. });
  258. });
  259. }