https.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // @ts-ignore
  2. import {fetchToken} from "@/utils/auth"
  3. declare const process: any;
  4. declare const __wxConfig: any;
  5. let isDevelopment = false;
  6. // #ifdef MP-WEIXIN
  7. const e =
  8. typeof __wxConfig !== "undefined"
  9. ? __wxConfig.envVersion || "release"
  10. : "release";
  11. console.log("env", e);
  12. isDevelopment = e === "develop" || e === "trial";
  13. // #endif
  14. let env: string = isDevelopment ? "dev" : "prd";
  15. const apis = {
  16. dev: {
  17. serverUrl: "http://192.168.5.7:9099/api",
  18. fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/"
  19. },
  20. prd: {
  21. serverUrl: "https://dev-wash.kuaiyuman.cn/api",
  22. fileUrl: "https://dev-wash.kuaiyuman.cn",
  23. },
  24. };
  25. // @ts-ignore
  26. // @ts-ignore
  27. const cfg = {
  28. key: {
  29. token: 'wash.token'
  30. },
  31. env: env,
  32. api: {
  33. serverUrl: apis[env]?.serverUrl,
  34. fileUrl: apis[env].fileUrl,
  35. uploadUrl: apis[env].serverUrl + "file/upload"
  36. }
  37. };
  38. const serverUrl = cfg.api.serverUrl;
  39. const fileUrl = cfg.api.fileUrl;
  40. const isEmptyOrNull = function (exp: any) {
  41. return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || Object.keys(exp).length===0;
  42. };
  43. /**
  44. * get请求封装
  45. * @param url
  46. * @param param
  47. */
  48. const get = (url: string, param = {}) => {
  49. let token = fetchToken() || ""
  50. if (!isEmptyOrNull(param)) {
  51. let params = [];
  52. for (let key in param) {
  53. // @ts-ignore
  54. params.push(encodeURIComponent(key) + "=" + encodeURIComponent(param[key]));
  55. }
  56. param = params.join("&")
  57. }
  58. let options = {
  59. url: fillUrl(url) + (isEmptyOrNull(param) ? "" : "?" + param),
  60. data: param,
  61. method: 'GET',
  62. header: {
  63. "Accept": "application/json",
  64. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  65. "satoken": token,
  66. },
  67. };
  68. return request(options)
  69. };
  70. const post = (url: string, param = {}) => {
  71. let token = fetchToken() || ""
  72. let options = {
  73. url: fillUrl(url),
  74. data: param,
  75. method: 'POST',
  76. header: {
  77. 'X-Requested-With': 'XMLHttpRequest',
  78. "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8;",
  79. "satoken": token,
  80. },
  81. dataType: 'json'
  82. };
  83. return request(options);
  84. };
  85. const body = (url: string, param = {}) => {
  86. let token = fetchToken() || ""
  87. let options = {
  88. url: fillUrl(url),
  89. data: param,
  90. method: 'POST',
  91. header: {
  92. 'X-Requested-With': 'XMLHttpRequest',
  93. "Accept": "application/json",
  94. "Content-Type": "application/json; charset=UTF-8",
  95. "satoken": token,
  96. },
  97. dataType: 'json'
  98. };
  99. return request(options);
  100. };
  101. const request = (options: any) => {
  102. return new Promise((resolve, reject) => {
  103. uni.request({
  104. url: options.url,
  105. data: options.data,
  106. method: options.method,
  107. header: options.header,
  108. dataType: options.dataType
  109. }).then((res:any) => {
  110. // @ts-ignore
  111. let response = res.data;
  112. if (response.code !== 200) {
  113. if (response.code == 92213 || response.code == 92305) {
  114. uni.showToast({
  115. title: response.msg,
  116. icon: 'none'
  117. });
  118. setTimeout(() => {
  119. uni.navigateTo({
  120. url: `/pages/login/login`
  121. })
  122. }, 300)
  123. } else {
  124. let errMsg = response.msg || '网络异常,请稍后重试';
  125. uni.showToast({
  126. title: errMsg,
  127. icon: 'none'
  128. });
  129. reject(errMsg);
  130. }
  131. } else {
  132. resolve(response.data);
  133. }
  134. }).catch(error => {
  135. uni.hideLoading();
  136. uni.showToast({
  137. title: '网络异常,请稍后重试',
  138. icon: 'none'
  139. });
  140. console.error("error=>", error);
  141. reject(error.msg);
  142. })
  143. });
  144. };
  145. const upload = (url: string, param: any = {}) => {
  146. let token = fetchToken() || ""
  147. console.log("upload", url, param, token)
  148. return new Promise((resolve, reject) => {
  149. uni.uploadFile({
  150. url: fillUrl(url),
  151. filePath: param.filePath||param.url,
  152. name: 'file',
  153. header: {
  154. "satoken":token
  155. },
  156. success: function (res) {
  157. console.log("upload file=>", res)
  158. let response = JSON.parse(res.data);
  159. if (response.code === 200) {
  160. resolve(response);
  161. }else if (response.code === 500) {
  162. uni.showToast({
  163. title: '系统繁忙,请稍后重试',
  164. icon: 'none'
  165. });
  166. reject();
  167. } else {
  168. console.error()
  169. uni.showToast({
  170. title: response.message
  171. });
  172. reject();
  173. }
  174. },
  175. fail: function (e) {
  176. console.error(e)
  177. uni.showToast({
  178. title: '请稍后重试'
  179. });
  180. reject();
  181. }
  182. });
  183. })
  184. }
  185. const uploadV2 = (opt: any) => {
  186. opt = opt || {};
  187. opt.url = opt.url || '';
  188. opt.filePath = opt.filePath || null;//要上传文件资源的路径。
  189. opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
  190. opt.filePath = opt.filePath || null;
  191. opt.success = opt.success || function () {
  192. };
  193. uni.uploadFile({
  194. url: fillUrl(opt.url),
  195. filePath: opt.filePath,
  196. name: opt.name,
  197. success: function (res) {
  198. console.log("upload file=>", res)
  199. opt.success(res);
  200. },
  201. fail: function () {
  202. uni.showToast({
  203. title: '请稍后重试'
  204. });
  205. }
  206. })
  207. };
  208. const fillUrl = function (url: string) {
  209. if (url.indexOf("http") === 0) {
  210. return url;
  211. } else {
  212. return serverUrl + url;
  213. }
  214. };
  215. const formatUrl = (v: string) => {
  216. if (v == null || v == "") {
  217. return "/static/missing-face.png";
  218. }
  219. if (v.indexOf("http") === 0) {
  220. return v;
  221. }
  222. return fileUrl + v.replace(/\\/g, "/");
  223. };
  224. export {
  225. get,post, body, upload, uploadV2, cfg, serverUrl, fileUrl, formatUrl
  226. }