net.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const md5 = require("./md5.js");
  2. const util = require("./util.js");
  3. /**
  4. * 网络请求对象
  5. */
  6. const net = {
  7. commonParams: () => {
  8. let systemInfo = util.getStorageSync("systemInfo");
  9. if (!systemInfo) {
  10. systemInfo = wx.getSystemInfoSync();
  11. if (!systemInfo) {
  12. wx.setStorageSync("systemInfo", systemInfo);
  13. }
  14. }
  15. let deviceId = "weapp";
  16. let systemVersion = "weapp";
  17. if (systemInfo) {
  18. let { brand, model, system } = systemInfo;
  19. if (brand || model) {
  20. deviceId = brand + model;
  21. }
  22. if (system && "string" === typeof system) {
  23. systemVersion = system;
  24. }
  25. }
  26. let params = {
  27. language: "cn", //语言参数
  28. deviceType: "weapp", //weapp 接口调用来源参数
  29. deviceId: deviceId, //设备唯一标识码
  30. apiVersion: "1.0.0", //api版本号
  31. systemVersion: systemVersion, //操作系统及版本号
  32. };
  33. let loginUserInfo = util.getStorageSync("USERINFO");
  34. if (loginUserInfo) {
  35. let userToken = loginUserInfo.token;
  36. if (userToken) {
  37. params.token = userToken; //用户登录凭证token
  38. }
  39. }
  40. return params;
  41. },
  42. paramsSign: function (params, url) {
  43. /*参数签名*/
  44. let key = "d9ce9fe030776727e85442d033d1be84";
  45. if (url && url.indexOf("/common/") !== -1) {
  46. key = "b15681f74461af9642ab5356fba7962b";
  47. }
  48. let allKeys = [],
  49. paramsLower = {},
  50. sArr = [],
  51. res = "";
  52. for (let k in params) {
  53. let kLower = k;
  54. allKeys.push(kLower);
  55. paramsLower[kLower] = params[k];
  56. }
  57. allKeys.sort();
  58. allKeys.forEach((v, i, a) => {
  59. sArr.push(`${v}=${paramsLower[v]}`);
  60. });
  61. sArr.push(`key=${key}`);
  62. res = sArr.join("&");
  63. res = md5(res);
  64. return res.toLowerCase();
  65. },
  66. //isDealFail是否手动处理异常,true or false
  67. req: function (options, isDealFail) {
  68. //get或post请求
  69. const params = {
  70. ...options,
  71. };
  72. if (!params.url) {
  73. wx.showToast({
  74. title: "请设置请求url",
  75. icon: "none",
  76. duration: 2000,
  77. });
  78. return Promise.reject();
  79. }
  80. // 过滤空值
  81. for (const key in params.data) {
  82. if (util.isUndef(params.data[key])) {
  83. delete params.data[key];
  84. }
  85. }
  86. return new Promise((resolve, reject) => {
  87. params.data = Object.assign({}, net.commonParams(), params.data);
  88. params.data.signToken = net.paramsSign(params.data, params.url);
  89. wx.request({
  90. url: params.url,
  91. data: params.data || null,
  92. header: {
  93. "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  94. },
  95. method: params.method || "POST",
  96. dataType: params.dataType || "json",
  97. success: function (res) {
  98. const { code, msg, data } = res.data;
  99. if (0 === code) {
  100. //ok
  101. return resolve(data);
  102. } else if (3 === code) {
  103. //未登录
  104. wx.clearStorage();
  105. wx.navigateTo({
  106. url: "/pages/login/login",
  107. });
  108. return reject(res.data);
  109. } else {
  110. if (isDealFail !== true && msg) {
  111. wx.showToast({
  112. title: msg || "",
  113. icon: "none",
  114. duration: 2000,
  115. });
  116. }
  117. return reject(res.data);
  118. }
  119. },
  120. fail: function (e) {
  121. if (isDealFail !== true) {
  122. wx.showToast({
  123. title: (e && e.errMsg) || "",
  124. icon: "none",
  125. duration: 2000,
  126. });
  127. }
  128. reject(e);
  129. },
  130. complete: function (res) {},
  131. });
  132. });
  133. },
  134. uploadFile: function (params, isDealFail) {
  135. //get或post请求
  136. if (!params) {
  137. wx.showToast({
  138. title: "请设置请求参数",
  139. icon: "none",
  140. duration: 2000,
  141. });
  142. return new Promise((resolve, reject) => {
  143. reject();
  144. });
  145. }
  146. if (!params.url) {
  147. wx.showToast({
  148. title: "请设置请求url",
  149. icon: "none",
  150. duration: 2000,
  151. });
  152. return new Promise((resolve, reject) => {
  153. reject();
  154. });
  155. }
  156. if (!params.filePath || !params.filePath.length) {
  157. wx.showToast({
  158. title: "请设置文件资源的路径",
  159. icon: "none",
  160. duration: 2000,
  161. });
  162. return new Promise((resolve, reject) => {
  163. reject();
  164. });
  165. }
  166. let promise = new Promise((resolve, reject) => {
  167. params.data = Object.assign({}, net.commonParams(), params.data);
  168. params.data.signToken = net.paramsSign(params.data, params.url);
  169. wx.uploadFile({
  170. url: params.url,
  171. filePath: params.filePath,
  172. name: "files[]",
  173. formData: params.data,
  174. header: {
  175. "content-type": "multipart/form-data",
  176. },
  177. method: "POST",
  178. success: function (res) {
  179. let data = res.data;
  180. if (typeof data == "string" && data.constructor == String) {
  181. data = JSON.parse(data);
  182. }
  183. let { code, msg } = data;
  184. if (0 === parseInt(code)) {
  185. //ok
  186. resolve(data);
  187. } else if (3 === code) {
  188. //未登录
  189. //未登录
  190. wx.clearStorage();
  191. wx.navigateTo({
  192. url: "/pages/login/login",
  193. });
  194. return reject(res.data);
  195. } else {
  196. if (isDealFail) {
  197. reject(data);
  198. } else {
  199. wx.showToast({
  200. title: msg || "error",
  201. icon: "none",
  202. duration: 2000,
  203. });
  204. }
  205. }
  206. },
  207. fail: function (e) {
  208. if (isDealFail) {
  209. reject(e);
  210. } else {
  211. wx.showToast({
  212. title: (e && e.errMsg) || "",
  213. icon: "none",
  214. duration: 2000,
  215. });
  216. }
  217. },
  218. complete: function (res) {},
  219. });
  220. });
  221. return promise;
  222. },
  223. };
  224. module.exports = net;