request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import ext from "./ext";
  2. import { router } from "./router";
  3. // 请求域名
  4. var domain = "https://openwork.dfwy.tech/";
  5. var app_id = "";
  6. var port = "";
  7. console.log(ext);
  8. // #ifdef MP-WEIXIN
  9. // 'http://127.0.0.1:8000/';
  10. //domain = uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? 'https://openwork.dfwy.tech/' : 'https://openwork.dfwy.tech/';
  11. domain =
  12. uni.getAccountInfoSync().miniProgram.envVersion == "release"
  13. ? ext.getExtStoreId("release_host_url")
  14. : ext.getExtStoreId("host_url");
  15. app_id = ext.getExtStoreId("app_id") ? ext.getExtStoreId("app_id") : "";
  16. // #endif
  17. // #ifdef WEB
  18. port = "web";
  19. domain =
  20. window.location.hostname == "h5.kailin.com.cn"
  21. ? "https://api.kailin.com.cn/"
  22. : "https://openwork.dfwy.tech/";
  23. // #endif
  24. // #ifdef H5
  25. domain =
  26. window.location.hostname == "h5.kailin.com.cn"
  27. ? "https://api.kailin.com.cn/"
  28. : "https://openwork.dfwy.tech/";
  29. port = "wap";
  30. // #endif
  31. // domain = "https://openwork.dfwy.tech/";
  32. // 发送网络请求的函数
  33. let modal = null;
  34. export const request = (url, d = {}, method = "GET") => {
  35. let { showToast = true, ...data } = d;
  36. // 获取登录标识
  37. let userLogin = uni.getStorageSync("userLogin");
  38. let shopId = uni.getStorageSync("shopId");
  39. // 合并参数
  40. if (userLogin && userLogin.authcode)
  41. data = Object.assign({ authcode: userLogin.authcode }, data);
  42. if (shopId) data = Object.assign({ shop_id: shopId }, data);
  43. if (app_id) data = Object.assign({ app_id: app_id }, data);
  44. // 封装
  45. return new Promise(async (resolve, reject) => {
  46. await uni.request({
  47. url: domain + url,
  48. method: method,
  49. data: data,
  50. success: (res) => {
  51. // 登录提示
  52. if (res.data.code == "no_login") {
  53. // 清空登录标识
  54. uni.setStorageSync("userLogin", null);
  55. // 清空用户信息
  56. uni.setStorageSync("userInfo", null);
  57. // 前去登录
  58. if (!modal) {
  59. modal = true;
  60. uni.showModal({
  61. title: "请登录",
  62. success(res) {
  63. if (res.confirm) {
  64. // 用户点击确定按钮
  65. router.push({
  66. url: "/pages/login/index",
  67. });
  68. }
  69. },
  70. complete() {
  71. setTimeout(() => {
  72. modal = false;
  73. }, 1000);
  74. },
  75. });
  76. }
  77. reject("请登录");
  78. return;
  79. }
  80. if (res.data.code !== "success" && showToast) {
  81. uni.showToast({
  82. title: res.data.msg,
  83. icon: "none",
  84. });
  85. }
  86. // 返回结果
  87. resolve(res.data);
  88. },
  89. fail: (err) => {
  90. reject(err);
  91. },
  92. });
  93. });
  94. };
  95. const fileupload = (url, filePath, data = {}) => {
  96. // 获取登录标识
  97. let userLogin = uni.getStorageSync("userLogin");
  98. // 合并参数
  99. if (userLogin && userLogin.authcode)
  100. data = Object.assign({ authcode: userLogin.authcode }, data);
  101. if (app_id) data = Object.assign({ app_id: app_id }, data);
  102. // 封装
  103. return new Promise((resolve, reject) => {
  104. uni.uploadFile({
  105. url: domain + url, // 你的上传接口地址
  106. filePath: filePath,
  107. name: "file", // 必须填写,为了后端接收文件流的参数名字
  108. formData: data, // 其他要上传的参数
  109. success: (res) => {
  110. // 登录提示
  111. if (res.data.code == "no_login") {
  112. // 前去登录
  113. uni.showModal({
  114. title: "请登录",
  115. success(res) {
  116. if (res.confirm) {
  117. // 用户点击确定按钮
  118. uni.navigateTo({
  119. url: "/pages/login/index",
  120. });
  121. }
  122. },
  123. });
  124. }
  125. // 转json,php返回可能会带bom头需要先替换
  126. let resdata = JSON.parse(res.data.replace("\uFEFF", ""));
  127. resolve(resdata);
  128. },
  129. fail: (err) => {
  130. reject(err);
  131. },
  132. });
  133. });
  134. };
  135. // 字符串键值对转参数对象
  136. const strToParam = (str, separator = "&") => {
  137. // 先转码
  138. str = decodeURIComponent(str);
  139. let pairs = str.split(separator);
  140. let result = {};
  141. pairs.forEach((pair) => {
  142. let [key, value] = pair.split("=");
  143. result[key] = value;
  144. });
  145. // 返回结果
  146. return result;
  147. };
  148. // 模块导出,{name:object} 为对象式,导出多个使用
  149. // export default {request:request,request1:request1};
  150. // 单个对象,直接导出如下
  151. export default {
  152. request: request,
  153. fileupload: fileupload,
  154. strToParam: strToParam,
  155. };