排队支付小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
14 KiB

  1. //app.js
  2. var util = require('./utils/utils.js');
  3. var api = require('./api.js');
  4. App({
  5. is_on_launch: true,
  6. onLaunch: function () {
  7. console.log(wx.getSystemInfoSync());
  8. // this.getStoreData();
  9. this.getCatList();
  10. var access_token = wx.getStorageSync("access_token");
  11. console.log('token:' + access_token);
  12. wx.setStorageSync('token', "");
  13. this.autoUpdate();
  14. wx.login({
  15. success: function (res) {
  16. if (res.code) {
  17. var code = res.code;
  18. console.log("7777777")
  19. console.log(res)
  20. getApp().loginrequest({
  21. url: api.passport.quick_login,
  22. method: "post",
  23. data: {
  24. code: code
  25. },
  26. success: function (res) {
  27. console.log("quickLoginSuccessful")
  28. console.log(res)
  29. wx.hideLoading();
  30. if (res.code == 200) {
  31. const tokenStr = res.data.tokenHead + res.data.token
  32. wx.setStorageSync('loginToken', tokenStr)
  33. wx.setStorageSync('token', res.data.token);
  34. wx.setStorageSync("access_token", res.data.userId);
  35. wx.setStorageSync("user_info", {
  36. avatar_url: res.data.img,
  37. nickname: res.data.username,
  38. avatar_url: res.data.icon,
  39. // parent: res.data.parent,
  40. id: res.data.id
  41. });
  42. wx.setStorageSync("my_info", {
  43. supplyId: res.data.myInfo.supplyId,
  44. supplyState: res.data.myInfo.supplyState,
  45. myMoney: res.data.myInfo.myMoney,
  46. brokerage: res.data.myInfo.brokerage,
  47. brokerageTixian: res.data.myInfo.brokerageTixian,
  48. moneyTotal: res.data.myInfo.moneyTotal,
  49. buyTotal: res.data.myInfo.buyTotal,
  50. icon: res.data.myInfo.icon,
  51. nickname: res.data.myInfo.nickname,
  52. memberLevelId: res.data.myInfo.memberLevelId,
  53. id: res.data.myInfo.id,
  54. city: res.data.myInfo.city,
  55. gender: res.data.myInfo.gender,
  56. job: res.data.myInfo.job,
  57. phone: res.data.myInfo.phone,
  58. birthday: res.data.myInfo.birthday,
  59. // parent: res.data.parent,
  60. });
  61. // if (wx.reLaunch) {
  62. // wx.reLaunch({
  63. // url: '/pages/index/index'
  64. // })
  65. // } else {
  66. // this.location('/pages/index/index');
  67. // }
  68. } else {
  69. wx.setStorageSync('token', '');
  70. wx.setStorageSync("access_token", '');
  71. }
  72. }
  73. });
  74. }
  75. }
  76. });
  77. // if (access_token) {
  78. // if (wx.reLaunch) {
  79. // wx.reLaunch({
  80. // url: '/pages/index/index'
  81. // })
  82. // } else {
  83. // this.location('/pages/index/index');
  84. // }
  85. // } else {
  86. // wx.reLaunch({
  87. // url: '/pages/allow/allow'
  88. // })
  89. // }
  90. // if (!access_token)
  91. // this.login();
  92. },
  93. //获取小程序更新机制的兼容,由于更新的功能基础库要1.9.90以上版本才支持,所以此处要做低版本的兼容处理
  94. autoUpdate: function () {
  95. let _this = this
  96. // 获取小程序更新机制的兼容,由于更新的功能基础库要1.9.90以上版本才支持,所以此处要做低版本的兼容处理
  97. if (wx.canIUse('getUpdateManager')) {
  98. // wx.getUpdateManager接口,可以获知是否有新版本的小程序、新版本是否下载好以及应用新版本的能力,会返回一个UpdateManager实例
  99. const updateManager = wx.getUpdateManager()
  100. // 检查小程序是否有新版本发布,onCheckForUpdate:当小程序向后台请求完新版本信息,会通知这个版本告知检查结果
  101. updateManager.onCheckForUpdate(function (res) {
  102. // 请求完新版本信息的回调
  103. if (res.hasUpdate) {
  104. // 检测到新版本,需要更新,给出提示
  105. wx.showModal({
  106. title: '更新提示',
  107. content: '检测到新版本,是否下载新版本并重启小程序',
  108. success: function (res) {
  109. if (res.confirm) {
  110. // 用户确定更新小程序,小程序下载和更新静默进行
  111. _this.downLoadAndUpdate(updateManager)
  112. } else if (res.cancel) {
  113. // 若用户点击了取消按钮,二次弹窗,强制更新,如果用户选择取消后不需要进行任何操作,则以下内容可忽略
  114. wx.showModal({
  115. title: '提示',
  116. content: '本次版本更新涉及到新功能的添加,旧版本将无法正常使用',
  117. showCancel: false, // 隐藏取消按钮
  118. confirmText: '确认更新', // 只保留更新按钮
  119. success: function (res) {
  120. if (res.confirm) {
  121. // 下载新版本,重启应用
  122. _this.downLoadAndUpdate(updateManager)
  123. }
  124. }
  125. })
  126. }
  127. }
  128. })
  129. }
  130. })
  131. } else {
  132. // 在最新版本客户端上体验小程序
  133. wx.showModal({
  134. title: '提示',
  135. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试',
  136. })
  137. }
  138. },
  139. // 下载小程序最新版本并重启
  140. downLoadAndUpdate: function (updateManager) {
  141. wx.showLoading()
  142. // 静默下载更新小程序新版本,onUpdateReady:当新版本下载完成回调
  143. updateManager.onUpdateReady(function () {
  144. wx.hideLoading()
  145. // applyUpdate:强制当前小程序应用上新版本并重启
  146. updateManager.applyUpdate()
  147. })
  148. // onUpdateFailed:当新版本下载失败回调
  149. updateManager.onUpdateFailed(function () {
  150. // 下载新版本失败
  151. wx.showModal({
  152. title: '已有新版本',
  153. content: '新版本已经上线了,请删除当前小程序,重新搜索打开',
  154. })
  155. })
  156. }
  157. ,
  158. getCatList: function () {
  159. this.request({
  160. url: api.default.cat_list,
  161. data: {
  162. limit: 15
  163. },
  164. success: function (res) {
  165. if (res.code == 200) {
  166. var cat_list = res.data || [];
  167. wx.setStorageSync("cat_list", cat_list);
  168. }
  169. }
  170. });
  171. },
  172. login: function () {
  173. var pages = getCurrentPages();
  174. var page = pages[(pages.length - 1)];
  175. wx.login({
  176. success: function (res) {
  177. if (res.code) {
  178. var code = res.code;
  179. getApp().request({
  180. url: api.passport.quick_login,
  181. method: "post",
  182. data: {
  183. code: code
  184. },
  185. success: function (res) {
  186. console.log("quickLoginSuccessful")
  187. console.log(res)
  188. wx.hideLoading();
  189. if (res.code == 200) {
  190. const tokenStr = res.data.tokenHead+res.data.token
  191. wx.setStorageSync('loginToken', tokenStr)
  192. wx.setStorageSync('token', res.data.token);
  193. wx.setStorageSync("access_token", res.data.userId);
  194. wx.setStorageSync("user_info", {
  195. avatar_url: res.data.img,
  196. nickname: res.data.username,
  197. avatar_url: res.data.icon,
  198. // parent: res.data.parent,
  199. id: res.data.id
  200. });
  201. wx.setStorageSync("my_info", {
  202. supplyId: res.data.myInfo.supplyId,
  203. supplyState: res.data.myInfo.supplyState,
  204. myMoney: res.data.myInfo.myMoney,
  205. icon: res.data.myInfo.icon,
  206. nickname: res.data.myInfo.nickname,
  207. // parent: res.data.parent,
  208. });
  209. var parent_id = wx.getStorageSync("parent_id");
  210. var p = getCurrentPages();
  211. // var parent_id = 0;
  212. if (p[0].options.user_id != undefined) {
  213. parent_id = p[0].options.user_id;
  214. }
  215. else if (p[0].options.scene != undefined) {
  216. parent_id = p[0].options.scene;
  217. }
  218. console.log('parentid:' + parent_id, p[0].options.scene, p[0].options.user_id);
  219. if (page == undefined) {
  220. return;
  221. }
  222. wx.redirectTo({
  223. url: "/" + page.route + "?" + util.objectToUrlParams(page.options),
  224. fail: function () {
  225. wx.switchTab({
  226. url: "/" + page.route,
  227. });
  228. },
  229. });
  230. } else {
  231. // wx.reLaunch({
  232. // url: '/pages/allow/allow'
  233. // })
  234. // wx.showToast({
  235. // title: res.msg
  236. // });
  237. }
  238. }
  239. });
  240. }
  241. }
  242. });
  243. },
  244. request: function (object) {
  245. var access_token = wx.getStorageSync("access_token");
  246. console.log('token:' + access_token);
  247. if (access_token) {
  248. if (!object.data)
  249. object.data = {};
  250. // object.data.access_token = access_token;
  251. }
  252. wx.request({
  253. url: object.url,
  254. header: object.header || {
  255. 'content-type': 'application/x-www-form-urlencoded',
  256. 'Authorization':wx.getStorageSync('loginToken') // 让每个请求携带自定义token 请根据实际情况自行修改
  257. },
  258. data: object.data || {},
  259. method: object.method || "GET",
  260. dataType: object.dataType || "json",
  261. success: function (res) {
  262. if (res.data.code == -1) {
  263. getApp().login();
  264. } else {
  265. if (object.success)
  266. object.success(res.data);
  267. }
  268. },
  269. fail: function (res) {
  270. var app = getApp();
  271. if (app.is_on_launch) {
  272. app.is_on_launch = false;
  273. wx.showModal({
  274. title: "网络请求出错",
  275. content: res.errMsg,
  276. showCancel: false,
  277. success: function (res) {
  278. if (res.confirm) {
  279. if (object.fail)
  280. object.fail(res);
  281. }
  282. }
  283. });
  284. } else {
  285. wx.showToast({
  286. title: res.errMsg,
  287. image: "/images/icon-warning.png",
  288. });
  289. if (object.fail)
  290. object.fail(res);
  291. }
  292. },
  293. complete: function (res) {
  294. if (object.complete)
  295. object.complete(res);
  296. }
  297. });
  298. },
  299. loginrequest: function (object) {
  300. var access_token = wx.getStorageSync("access_token");
  301. console.log('token:' + access_token);
  302. if (access_token) {
  303. if (!object.data)
  304. object.data = {};
  305. // object.data.access_token = access_token;
  306. }
  307. wx.request({
  308. url: object.url,
  309. header: object.header || {
  310. 'content-type': 'application/x-www-form-urlencoded'
  311. },
  312. data: object.data || {},
  313. method: object.method || "GET",
  314. dataType: object.dataType || "json",
  315. success: function (res) {
  316. if (res.data.code == -1) {
  317. getApp().login();
  318. } else {
  319. if (object.success)
  320. object.success(res.data);
  321. }
  322. },
  323. fail: function (res) {
  324. var app = getApp();
  325. if (app.is_on_launch) {
  326. app.is_on_launch = false;
  327. wx.showModal({
  328. title: "网络请求出错",
  329. content: res.errMsg,
  330. showCancel: false,
  331. success: function (res) {
  332. if (res.confirm) {
  333. if (object.fail)
  334. object.fail(res);
  335. }
  336. }
  337. });
  338. } else {
  339. wx.showToast({
  340. title: res.errMsg,
  341. image: "/images/icon-warning.png",
  342. });
  343. if (object.fail)
  344. object.fail(res);
  345. }
  346. },
  347. complete: function (res) {
  348. if (object.complete)
  349. object.complete(res);
  350. }
  351. });
  352. },
  353. saveFormId: function (form_id) {
  354. this.request({
  355. url: api.user.save_form_id,
  356. data: {
  357. form_id: form_id,
  358. }
  359. });
  360. },
  361. });