request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. export const request = (url, d = {}, method = "GET") => {
  34. let { showToast = true, ...data } = d;
  35. // 获取登录标识
  36. let userLogin = uni.getStorageSync("userLogin");
  37. let shopId = uni.getStorageSync("shopId");
  38. // 合并参数
  39. if (userLogin && userLogin.authcode)
  40. data = Object.assign({ authcode: userLogin.authcode }, data);
  41. if (shopId) data = Object.assign({ shop_id: shopId }, data);
  42. if (app_id) data = Object.assign({ app_id: app_id }, data);
  43. // 封装
  44. return new Promise((resolve, reject) => {
  45. uni.request({
  46. url: domain + url,
  47. method: method,
  48. data: data,
  49. success: (res) => {
  50. // 登录提示
  51. if (res.data.code == "no_login") {
  52. // 清空登录标识
  53. uni.setStorageSync("userLogin", null);
  54. // 清空用户信息
  55. uni.setStorageSync("userInfo", null);
  56. // 前去登录
  57. uni.showModal({
  58. title: "请登录",
  59. success(res) {
  60. if (res.confirm) {
  61. // 用户点击确定按钮
  62. router.push({
  63. url: "/pages/login/index",
  64. });
  65. }
  66. },
  67. });
  68. reject("请登录");
  69. return;
  70. }
  71. if (res.data.code !== "success" && showToast) {
  72. uni.showToast({
  73. title: res.data.msg,
  74. icon: "none",
  75. });
  76. }
  77. // 返回结果
  78. resolve(res.data);
  79. },
  80. fail: (err) => {
  81. reject(err);
  82. },
  83. });
  84. });
  85. };
  86. const fileupload = (url, filePath, data = {}) => {
  87. // 获取登录标识
  88. let userLogin = uni.getStorageSync("userLogin");
  89. // 合并参数
  90. if (userLogin && userLogin.authcode)
  91. data = Object.assign({ authcode: userLogin.authcode }, data);
  92. if (app_id) data = Object.assign({ app_id: app_id }, data);
  93. // 封装
  94. return new Promise((resolve, reject) => {
  95. uni.uploadFile({
  96. url: domain + url, // 你的上传接口地址
  97. filePath: filePath,
  98. name: "file", // 必须填写,为了后端接收文件流的参数名字
  99. formData: data, // 其他要上传的参数
  100. success: (res) => {
  101. // 登录提示
  102. if (res.data.code == "no_login") {
  103. // 前去登录
  104. uni.showModal({
  105. title: "请登录",
  106. success(res) {
  107. if (res.confirm) {
  108. // 用户点击确定按钮
  109. uni.navigateTo({
  110. url: "/pages/login/index",
  111. });
  112. }
  113. },
  114. });
  115. }
  116. // 转json,php返回可能会带bom头需要先替换
  117. let resdata = JSON.parse(res.data.replace("\uFEFF", ""));
  118. resolve(resdata);
  119. },
  120. fail: (err) => {
  121. reject(err);
  122. },
  123. });
  124. });
  125. };
  126. // 字符串键值对转参数对象
  127. const strToParam = (str, separator = "&") => {
  128. // 先转码
  129. str = decodeURIComponent(str);
  130. let pairs = str.split(separator);
  131. let result = {};
  132. pairs.forEach((pair) => {
  133. let [key, value] = pair.split("=");
  134. result[key] = value;
  135. });
  136. // 返回结果
  137. return result;
  138. };
  139. // 模块导出,{name:object} 为对象式,导出多个使用
  140. // export default {request:request,request1:request1};
  141. // 单个对象,直接导出如下
  142. export default {
  143. request: request,
  144. fileupload: fileupload,
  145. strToParam: strToParam,
  146. };