utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const env = process.env.NODE_ENV === "development" ? "dev" : "prd";
  2. let apis = {
  3. dev: {
  4. serverUrl: "https://cloud.kuaiyuman.cn/admin/",
  5. fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/"
  6. },
  7. prd: {
  8. serverUrl: "https://cloud.kuaiyuman.cn/admin/",
  9. fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/",
  10. },
  11. };
  12. const cfg = {
  13. key: {
  14. token: 'kuaiyuman.token',
  15. user: 'kuaiyuman.user',
  16. },
  17. env: env,
  18. api: {
  19. serverUrl: apis[env].serverUrl,
  20. fileUrl: apis[env].fileUrl,
  21. uploadUrl: apis[env].serverUrl + "file/upload"
  22. }
  23. };
  24. const serverUrl = cfg.api.serverUrl;
  25. const fileUrl = cfg.api.fileUrl;
  26. const isEmptyOrNull = function (exp) {
  27. return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || JSON.stringify(exp) === "{}";
  28. };
  29. /**
  30. * get请求封装
  31. * @param url
  32. * @param param
  33. */
  34. const get = (url, param = {}) => {
  35. let token = uni.getStorageSync(cfg.key.token) || "";
  36. if (!isEmptyOrNull(param)) {
  37. var params = [];
  38. for (var key in param) {
  39. params.push(encodeURIComponent(key) + "=" + encodeURIComponent(param[key]));
  40. }
  41. param = params.join("&")
  42. }
  43. let options = {
  44. url: fillUrl(url) + (isEmptyOrNull(param) ? "" : "?" + param),
  45. data: param,
  46. method: 'GET',
  47. header: {
  48. "Accept": "application/json",
  49. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  50. "satoken": token,
  51. },
  52. };
  53. return request(options)
  54. };
  55. const post = (url, param = {}) => {
  56. let token = uni.getStorageSync(cfg.key.token) || "";
  57. let options = {
  58. url: fillUrl(url),
  59. data: param,
  60. method: 'POST',
  61. header: {
  62. 'X-Requested-With': 'XMLHttpRequest',
  63. "Accept": "application/json",
  64. "Content-Type": "application/json; charset=UTF-8",
  65. "satoken": token,
  66. },
  67. dataType: 'json'
  68. };
  69. return request(options);
  70. };
  71. const request = (options) => {
  72. return new Promise((resolve, reject) => {
  73. uni.request({
  74. url: options.url,
  75. data: options.data,
  76. method: options.method,
  77. header: options.header,
  78. dataType: options.dataType
  79. }).then(res => {
  80. console.log("resp>>>", res)
  81. let response = res.data;
  82. if (response.code !== 200) {
  83. if (response.code == 10001) {
  84. uni.showToast({
  85. title: response.message,
  86. icon: 'none'
  87. });
  88. setTimeout(() => {
  89. uni.navigateTo({
  90. url: `/pages/login/index`
  91. })
  92. }, 300)
  93. } else {
  94. let errMsg = response.message || '网络异常,请稍后重试';
  95. uni.showToast({
  96. title: errMsg,
  97. icon: 'none'
  98. });
  99. reject(errMsg);
  100. }
  101. } else {
  102. resolve(response.data);
  103. }
  104. }).catch(error => {
  105. uni.hideLoading();
  106. uni.showToast({
  107. title: '网络异常,请稍后重试',
  108. icon: 'none'
  109. });
  110. console.error("error=>", error);
  111. reject(error.msg);
  112. })
  113. });
  114. };
  115. const upload = opt => {
  116. opt = opt || {};
  117. opt.url = opt.url || '';
  118. opt.filePath = opt.filePath || null;//要上传文件资源的路径。
  119. opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
  120. opt.filePath = opt.filePath || null;
  121. opt.success = opt.success || function () {
  122. };
  123. uni.uploadFile({
  124. url: fillUrl(opt.url),
  125. filePath: opt.filePath,
  126. name: opt.name,
  127. success: function (res) {
  128. console.log("upload file=>", res)
  129. opt.success(res);
  130. },
  131. fail: function () {
  132. uni.showToast({
  133. title: '请稍后重试'
  134. });
  135. }
  136. })
  137. };
  138. const fillUrl = function (url) {
  139. if (url.indexOf("http") === 0) {
  140. return url;
  141. } else {
  142. return serverUrl + url;
  143. }
  144. };
  145. const fmtUrl = v => {
  146. if (v == null || v == "") {
  147. return "/static/missing-face.png";
  148. }
  149. if (v.indexOf("http") === 0) {
  150. return v;
  151. }
  152. return fileUrl + v.replace(/\\/g, "/");
  153. };
  154. const fmtMoney = function (money) {
  155. if (!money) {
  156. return "0.00";
  157. }
  158. return (money / 100).toFixed(2);
  159. }
  160. const fmtDate = function (time, format) {
  161. time = time ? new Date(time) : new Date()
  162. format = format || 'YYYY-MM-DD'
  163. function tf(i) {
  164. return (i < 10 ? '0' : '') + i
  165. }
  166. // @ts-ignore
  167. return format.replace(/YYYY|MM|DD|hh|mm|ss|WW/g, function (a) {
  168. switch (a) {
  169. case 'YYYY':
  170. return tf(time.getFullYear())
  171. case 'MM':
  172. return tf(time.getMonth() + 1)
  173. case 'DD':
  174. return tf(time.getDate())
  175. case 'mm':
  176. return tf(time.getMinutes())
  177. case 'hh':
  178. return tf(time.getHours())
  179. case 'ss':
  180. return tf(time.getSeconds())
  181. case 'WW':
  182. return ['日', '一', '二', '三', '四', '五', '六'][time.getDay()]
  183. }
  184. })
  185. }
  186. const msg = (title, icon = 'none', duration = 1800, mask = false) => {
  187. //统一提示方便全局修改
  188. if (Boolean(title) === false) {
  189. return;
  190. }
  191. uni.showToast({
  192. title,
  193. duration,
  194. mask,
  195. icon
  196. });
  197. };
  198. export {
  199. get, post, upload, cfg, serverUrl, fileUrl, fmtUrl, fmtMoney, msg, fmtDate
  200. }