您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

request.js 3.6KB

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