utils.js 4.9 KB

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