utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. user:'kuaiyuman.user',
  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. console.log("resp>>>",res)
  85. let response = res.data;
  86. if (response.code !== 200) {
  87. if (response.code == 10001) {
  88. uni.showToast({
  89. title: response.message,
  90. icon: 'none'
  91. });
  92. setTimeout(() => {
  93. uni.navigateTo({
  94. url: `/pages/login/login`
  95. })
  96. }, 300)
  97. } else {
  98. let errMsg = response.message || '网络异常,请稍后重试';
  99. uni.showToast({
  100. title: errMsg,
  101. icon: 'none'
  102. });
  103. reject(errMsg);
  104. }
  105. } else {
  106. resolve(response.data);
  107. }
  108. }).catch(error => {
  109. uni.hideLoading();
  110. uni.showToast({
  111. title: '网络异常,请稍后重试',
  112. icon: 'none'
  113. });
  114. console.error("error=>", error);
  115. reject(error.msg);
  116. })
  117. });
  118. };
  119. const upload = opt => {
  120. opt = opt || {};
  121. opt.url = opt.url || '';
  122. opt.filePath = opt.filePath || null;//要上传文件资源的路径。
  123. opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
  124. opt.filePath = opt.filePath || null;
  125. opt.success = opt.success || function () {
  126. };
  127. uni.uploadFile({
  128. url: fillUrl(opt.url),
  129. filePath: opt.filePath,
  130. name: opt.name,
  131. success: function (res) {
  132. console.log("upload file=>", res)
  133. opt.success(res);
  134. },
  135. fail: function () {
  136. uni.showToast({
  137. title: '请稍后重试'
  138. });
  139. }
  140. })
  141. };
  142. const fillUrl = function (url) {
  143. if (url.indexOf("http") === 0) {
  144. return url;
  145. } else {
  146. return serverUrl + url;
  147. }
  148. };
  149. const formatUrl = v => {
  150. if (v == null || v == "") {
  151. return "/static/missing-face.png";
  152. }
  153. if (v.indexOf("http") === 0) {
  154. return v;
  155. }
  156. return fileUrl + v.replace(/\\/g, "/");
  157. };
  158. const msg = (title, icon = 'none', duration = 1800, mask = false) => {
  159. //统一提示方便全局修改
  160. if (Boolean(title) === false) {
  161. return;
  162. }
  163. uni.showToast({
  164. title,
  165. duration,
  166. mask,
  167. icon
  168. });
  169. };
  170. export {
  171. get, post, upload, cfg, serverUrl, fileUrl,formatUrl,msg
  172. }