net.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. const {
  2. DEBUG,
  3. CODE_NOT_LOGIN
  4. } = require('./c.js');
  5. const {
  6. api_get_openid,
  7. api_get_unionid,
  8. api_wx_login
  9. } = require('./api.js');
  10. const md5 = require('./md5.js');
  11. const util = require('./util.js');
  12. /**
  13. * 网络请求对象
  14. */
  15. const net = {
  16. commonParams: () => {
  17. let systemInfo = util.getStorageSync('systemInfo');
  18. if (!systemInfo) {
  19. systemInfo = wx.getSystemInfoSync();
  20. if (!systemInfo) {
  21. wx.setStorageSync('systemInfo', systemInfo);
  22. }
  23. }
  24. let deviceId = 'weapp';
  25. let systemVersion = 'weapp';
  26. if (systemInfo) {
  27. let {
  28. brand,
  29. model,
  30. system
  31. } = systemInfo;
  32. if (brand || model) {
  33. deviceId = brand + model;
  34. }
  35. if (system && 'string' === typeof (system)) {
  36. systemVersion = system;
  37. }
  38. }
  39. let params = {
  40. language: 'cn', //语言参数
  41. deviceType: 'weapp', //接口调用来源参数
  42. deviceId: deviceId, //设备唯一标识码
  43. apiVersion: '1.0.3', //api版本号
  44. systemVersion: systemVersion //操作系统及版本号
  45. };
  46. let loginUserInfo = util.getStorageSync('userInfo');
  47. if (loginUserInfo) {
  48. let userToken = loginUserInfo.token;
  49. if (userToken) {
  50. params.token = userToken; //用户登录凭证token
  51. }
  52. }
  53. let citySelectObj = util.getStorageSync('citySelectObj');
  54. if (citySelectObj) {
  55. citySelectObj.cityId && (params.cityId = citySelectObj.cityId) //用户选择的城市id
  56. citySelectObj.schoolId && JSON.stringify(citySelectObj.schoolId).length && (params.schoolId = citySelectObj.schoolId) //用户选择的学校id
  57. }
  58. return params;
  59. },
  60. paramsSign: function (params, url) {
  61. /*参数签名*/
  62. let key = 'd9ce9fe030776727e85442d033d1be84';
  63. if (url && url.indexOf('/common/') !== -1) {
  64. key = 'b15681f74461af9642ab5356fba7962b';
  65. }
  66. let allKeys = [],
  67. paramsLower = {},
  68. sArr = [],
  69. res = '';
  70. for (let k in params) {
  71. let kLower = k;
  72. allKeys.push(kLower);
  73. paramsLower[kLower] = params[k];
  74. }
  75. allKeys.sort();
  76. allKeys.forEach((v, i, a) => {
  77. sArr.push(`${v}=${paramsLower[v]}`);
  78. });
  79. sArr.push(`key=${key}`);
  80. res = sArr.join('&');
  81. res = md5(res);
  82. return res.toLowerCase();
  83. },
  84. //isDealFail是否手动处理异常,true or false
  85. req: function (options, isDealFail) { //get或post请求
  86. const params = {
  87. ...options
  88. }
  89. if (!params.url) {
  90. wx.showToast({
  91. title: '请设置请求url',
  92. icon: 'none',
  93. duration: 2000
  94. });
  95. return Promise.reject();
  96. }
  97. // 过滤空值
  98. for (const key in params.data) {
  99. if (util.isUndef(params.data[key])) {
  100. delete params.data[key]
  101. }
  102. }
  103. return new global.Promise((resolve, reject) => {
  104. params.data = Object.assign({}, net.commonParams(), params.data);
  105. params.data.signToken = net.paramsSign(params.data, params.url);
  106. wx.request({
  107. url: params.url,
  108. data: params.data || null,
  109. header: {
  110. 'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
  111. },
  112. method: params.method || 'POST',
  113. dataType: params.dataType || 'json',
  114. success: function (res) {
  115. const {
  116. code,
  117. msg,
  118. data
  119. } = res.data;
  120. if (0 === code) { //ok
  121. return resolve(data);
  122. } else if (CODE_NOT_LOGIN === code) { //未登录
  123. DEBUG && console.log(`Request for ${params.url} exception.--->${JSON.stringify(msg)}`);
  124. wx.clearStorage();
  125. wx.showToast({
  126. title: '您的用户信息已失效,请重新登录操作',
  127. icon: 'none',
  128. duration: 3000,
  129. success: () => {
  130. wx.clearStorageSync();
  131. wx.reLaunch({
  132. url: '/pages/index/index'
  133. });
  134. },
  135. });
  136. } else {
  137. DEBUG && console.log(`Request for ${params.url} exception.--->${JSON.stringify(msg)}`);
  138. if (isDealFail !== true) {
  139. wx.showToast({
  140. title: msg || 'error',
  141. icon: 'none',
  142. duration: 2000
  143. });
  144. }
  145. return reject(res.data);
  146. }
  147. },
  148. fail: function (e) {
  149. DEBUG && console.log(`Request for ${params.url} fail.--->${JSON.stringify(e)}`);
  150. if (isDealFail !== true) {
  151. wx.showToast({
  152. title: (e && e.errMsg) || '',
  153. icon: 'none',
  154. duration: 2000
  155. });
  156. }
  157. reject(e);
  158. },
  159. complete: function (res) {
  160. DEBUG && console.log(`Request for ${params.url} complete.--->${JSON.stringify(res)}`);
  161. }
  162. });
  163. })
  164. },
  165. getOpenid: function (code, next) { //获取openid
  166. let params = {};
  167. params.url = api_get_openid;
  168. params.data = {
  169. code: code,
  170. program: "hooliHome"
  171. };
  172. net.req(params).then((res) => {
  173. let {
  174. openid
  175. } = res;
  176. wx.setStorageSync('openid', openid);
  177. next && next(openid);
  178. });
  179. },
  180. getUnionid: function (openid, encryptedData, iv, rawData, signature, next) { //获取unionid
  181. let params = {};
  182. params.url = api_get_unionid;
  183. params.data = {
  184. openid,
  185. encryptedData,
  186. iv,
  187. rawData,
  188. signature,
  189. program: 'hooliHome'
  190. };
  191. net.req(params, true).then((res) => {
  192. let {
  193. unionid
  194. } = res;
  195. if (unionid) {
  196. wx.setStorageSync('unionid', unionid);
  197. next && next(res);
  198. } else {
  199. wx.showToast({
  200. title: '网络不稳定,请重新操作',
  201. icon: 'none',
  202. duration: 2000
  203. });
  204. }
  205. }, (e) => {
  206. wx.showToast({
  207. title: '网络不稳定,请重新操作',
  208. icon: 'none',
  209. duration: 2000
  210. });
  211. });
  212. },
  213. wxLogin: function (next) { //点击注册登录操作
  214. wx.checkSession({
  215. success: () => { //session_key 未过期,并且在本生命周期一直有效
  216. let openid = util.getStorageSync('openid');
  217. if (openid) {
  218. next && next(openid);
  219. } else {
  220. wx.login({
  221. success: (res) => {
  222. let {
  223. code
  224. } = res;
  225. net.getOpenid(code, (openid) => {
  226. next && next(openid);
  227. });
  228. }
  229. });
  230. }
  231. },
  232. fail: () => { //session_key 已经失效,需要重新执行登录流程,让服务端获取新的session_key
  233. wx.login({
  234. success: (res) => {
  235. let {
  236. code
  237. } = res;
  238. net.getOpenid(code, (openid) => {
  239. next && next(openid);
  240. });
  241. }
  242. });
  243. }
  244. });
  245. },
  246. login: function (unionid, countryId, mobile, smsCode, next) { //微信绑定手机号注册登录
  247. let params = {};
  248. params.url = api_wx_login;
  249. params.data = {
  250. unionid,
  251. countryId,
  252. mobile,
  253. smsCode
  254. };
  255. net.req(params).then((res) => {
  256. wx.setStorageSync('userInfo', res.userInfo);
  257. next && next(res);
  258. });
  259. }
  260. }
  261. module.exports = net;