request.js 4.1 KB

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