https.ts 6.2 KB

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