app.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * tabBar页面路径列表 (用于链接跳转时判断)
  3. * tabBarLinks为常量, 无需修改
  4. */
  5. const tabBarLinks = [
  6. 'pages/index/index',
  7. 'pages/category/index',
  8. 'pages/flow/index',
  9. 'pages/user/index'
  10. ];
  11. // 站点配置文件
  12. import siteinfo from './siteinfo.js';
  13. // 工具类
  14. import util from './utils/util.js';
  15. App({
  16. /**
  17. * 全局变量
  18. */
  19. globalData: {
  20. user_id: null,
  21. },
  22. // api地址
  23. api_root: siteinfo.siteroot + 'index.php?s=/api/',
  24. /**
  25. * 生命周期函数--监听小程序初始化
  26. */
  27. onLaunch(e) {
  28. let _this = this;
  29. // 小程序主动更新
  30. _this.updateManager();
  31. // 小程序启动场景
  32. _this.onStartupScene(e.query);
  33. },
  34. /**
  35. * 小程序启动场景
  36. */
  37. onStartupScene(query) {
  38. // 获取场景值
  39. let scene = this.getSceneData(query);
  40. // 记录推荐人id
  41. let refereeId = query.referee_id ? query.referee_id : scene.uid;
  42. refereeId > 0 && (this.saveRefereeId(refereeId));
  43. },
  44. /**
  45. * 获取商城ID
  46. */
  47. getWxappId() {
  48. return siteinfo.uniacid || 10001;
  49. },
  50. /**
  51. * 记录推荐人id
  52. */
  53. saveRefereeId(refereeId) {
  54. let App = this;
  55. refereeId = parseInt(refereeId);
  56. if (refereeId <= 0 || refereeId == App.getUserId()) {
  57. return false;
  58. }
  59. if (wx.getStorageSync('referee_id')) {
  60. return false;
  61. }
  62. wx.setStorageSync('referee_id', refereeId);
  63. return true;
  64. },
  65. /**
  66. * 获取场景值(scene)
  67. */
  68. getSceneData(query) {
  69. return query.scene ? util.scene_decode(query.scene) : {};
  70. },
  71. /**
  72. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  73. */
  74. onShow(options) {
  75. let App = this;
  76. try {
  77. const livePlayer = requirePlugin('live-player-plugin');
  78. if (options.scene == 1007 || options.scene == 1008 || options.scene == 1044) {
  79. livePlayer.getShareParams()
  80. .then(res => {
  81. // 直播页面的自定义参数
  82. let customParams = res.custom_params;
  83. console.log('get custom params', customParams);
  84. // 记录推荐人ID
  85. if (customParams.hasOwnProperty('referee_id')) {
  86. App.saveRefereeId(customParams['referee_id']);
  87. }
  88. }).catch(err => {
  89. console.log('get share params', err)
  90. });
  91. }
  92. } catch (error) {
  93. }
  94. },
  95. /**
  96. * 执行用户登录
  97. */
  98. doLogin(delta) {
  99. // 保存当前页面
  100. let pages = getCurrentPages();
  101. if (pages.length) {
  102. let currentPage = pages[pages.length - 1];
  103. "pages/login/login" != currentPage.route &&
  104. wx.setStorageSync("currentPage", currentPage);
  105. }
  106. // 跳转授权页面
  107. wx.navigateTo({
  108. url: "/pages/login/login?delta=" + (delta || 1)
  109. });
  110. },
  111. /**
  112. * 当前用户id
  113. */
  114. getUserId() {
  115. return wx.getStorageSync('user_id');
  116. },
  117. /**
  118. * 显示成功提示框
  119. */
  120. showSuccess(msg, callback) {
  121. wx.showToast({
  122. title: msg,
  123. icon: 'success',
  124. mask: true,
  125. duration: 1500,
  126. success() {
  127. callback && (setTimeout(() => {
  128. callback();
  129. }, 1500));
  130. }
  131. });
  132. },
  133. /**
  134. * 显示失败提示框
  135. */
  136. showError(msg, callback) {
  137. wx.showModal({
  138. title: '友情提示',
  139. content: msg,
  140. showCancel: false,
  141. success(res) {
  142. // callback && (setTimeout(() => {
  143. // callback();
  144. // }, 1500));
  145. callback && callback();
  146. }
  147. });
  148. },
  149. /**
  150. * get请求
  151. */
  152. _get(url, data, success, fail, complete, check_login) {
  153. wx.showNavigationBarLoading();
  154. let _this = this;
  155. // 构造请求参数
  156. data = data || {};
  157. data.wxapp_id = _this.getWxappId();
  158. // if (typeof check_login === 'undefined')
  159. // check_login = true;
  160. // 构造get请求
  161. let request = () => {
  162. data.token = wx.getStorageSync('token');
  163. wx.request({
  164. url: _this.api_root + url,
  165. header: {
  166. 'content-type': 'application/json'
  167. },
  168. data: data,
  169. success(res) {
  170. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  171. console.log(res);
  172. _this.showError('网络请求出错');
  173. return false;
  174. }
  175. if (res.data.code === -1) {
  176. // 登录态失效, 重新登录
  177. wx.hideNavigationBarLoading();
  178. _this.doLogin(2);
  179. } else if (res.data.code === 0) {
  180. _this.showError(res.data.msg, () => {
  181. fail && fail(res);
  182. });
  183. return false;
  184. } else {
  185. success && success(res.data);
  186. }
  187. },
  188. fail(res) {
  189. _this.showError(res.errMsg, () => {
  190. fail && fail(res);
  191. });
  192. },
  193. complete(res) {
  194. wx.hideNavigationBarLoading();
  195. complete && complete(res);
  196. },
  197. });
  198. };
  199. // 判断是否需要验证登录
  200. check_login ? _this.doLogin(request) : request();
  201. },
  202. /**
  203. * post提交
  204. */
  205. _post_form(url, data, success, fail, complete, isShowNavBarLoading) {
  206. let _this = this;
  207. isShowNavBarLoading || true;
  208. data.wxapp_id = _this.getWxappId();
  209. data.token = wx.getStorageSync('token');
  210. // 在当前页面显示导航条加载动画
  211. if (isShowNavBarLoading == true) {
  212. wx.showNavigationBarLoading();
  213. }
  214. wx.request({
  215. url: _this.api_root + url,
  216. header: {
  217. 'content-type': 'application/x-www-form-urlencoded',
  218. },
  219. method: 'POST',
  220. data: data,
  221. success(res) {
  222. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  223. _this.showError('网络请求出错');
  224. return false;
  225. }
  226. if (res.data.code === -1) {
  227. // 登录态失效, 重新登录
  228. wx.hideNavigationBarLoading();
  229. _this.doLogin(1);
  230. return false;
  231. } else if (res.data.code === 0) {
  232. _this.showError(res.data.msg, () => {
  233. fail && fail(res);
  234. });
  235. return false;
  236. }
  237. success && success(res.data);
  238. },
  239. fail(res) {
  240. // console.log(res);
  241. _this.showError(res.errMsg, () => {
  242. fail && fail(res);
  243. });
  244. },
  245. complete(res) {
  246. wx.hideNavigationBarLoading();
  247. // wx.hideLoading();
  248. complete && complete(res);
  249. }
  250. });
  251. },
  252. /**
  253. * 验证是否存在user_info
  254. */
  255. validateUserInfo() {
  256. let user_info = wx.getStorageSync('user_info');
  257. return !!wx.getStorageSync('user_info');
  258. },
  259. /**
  260. * 小程序主动更新
  261. */
  262. updateManager() {
  263. if (!wx.canIUse('getUpdateManager')) {
  264. return false;
  265. }
  266. const updateManager = wx.getUpdateManager();
  267. updateManager.onCheckForUpdate(res => {
  268. // 请求完新版本信息的回调
  269. // console.log(res.hasUpdate)
  270. });
  271. updateManager.onUpdateReady(() => {
  272. wx.showModal({
  273. title: '更新提示',
  274. content: '新版本已经准备好,即将重启应用',
  275. showCancel: false,
  276. success(res) {
  277. if (res.confirm) {
  278. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  279. updateManager.applyUpdate()
  280. }
  281. }
  282. });
  283. });
  284. updateManager.onUpdateFailed(() => {
  285. // 新的版本下载失败
  286. wx.showModal({
  287. title: '更新提示',
  288. content: '新版本下载失败',
  289. showCancel: false
  290. })
  291. });
  292. },
  293. /**
  294. * 获取tabBar页面路径列表
  295. */
  296. getTabBarLinks() {
  297. return tabBarLinks;
  298. },
  299. /**
  300. * 跳转到指定页面
  301. * 支持tabBar页面
  302. */
  303. navigationTo(url) {
  304. if (!url || url.length == 0) {
  305. return false;
  306. }
  307. let tabBarLinks = this.getTabBarLinks();
  308. // tabBar页面
  309. if (tabBarLinks.indexOf(url) > -1) {
  310. wx.switchTab({
  311. url: '/' + url
  312. });
  313. } else {
  314. // 普通页面
  315. wx.navigateTo({
  316. url: '/' + url
  317. });
  318. }
  319. },
  320. /**
  321. * 记录formId
  322. * (因微信模板消息已下线,所以formId取消不再收集)
  323. */
  324. saveFormId(formId) {
  325. return true;
  326. // let _this = this;
  327. // console.log('saveFormId');
  328. // if (formId === 'the formId is a mock one') {
  329. // return false;
  330. // }
  331. // _this._post_form('wxapp.formId/save', {
  332. // formId: formId
  333. // }, null, null, null, false);
  334. },
  335. /**
  336. * 生成转发的url参数
  337. */
  338. getShareUrlParams(params) {
  339. let _this = this;
  340. return util.urlEncode(Object.assign({
  341. referee_id: _this.getUserId()
  342. }, params));
  343. },
  344. /**
  345. * 发起微信支付
  346. */
  347. wxPayment(option) {
  348. let options = Object.assign({
  349. payment: {},
  350. success: () => {},
  351. fail: () => {},
  352. complete: () => {},
  353. }, option);
  354. wx.requestPayment({
  355. timeStamp: options.payment.timeStamp,
  356. nonceStr: options.payment.nonceStr,
  357. package: 'prepay_id=' + options.payment.prepay_id,
  358. signType: 'MD5',
  359. paySign: options.payment.paySign,
  360. success(res) {
  361. options.success(res);
  362. },
  363. fail(res) {
  364. options.fail(res);
  365. },
  366. complete(res) {
  367. options.complete(res);
  368. }
  369. });
  370. },
  371. /**
  372. * 验证登录
  373. */
  374. checkIsLogin() {
  375. return wx.getStorageSync('token') != '' && wx.getStorageSync('user_id') != '';
  376. },
  377. /**
  378. * 授权登录
  379. */
  380. getUserInfo(userInfo, callback) {
  381. let App = this;
  382. wx.showLoading({
  383. title: "正在登录",
  384. mask: true
  385. });
  386. // 执行微信登录
  387. wx.login({
  388. success(res) {
  389. // 发送用户信息
  390. App._post_form('user/login', {
  391. code: res.code,
  392. user_info: JSON.stringify(userInfo),
  393. referee_id: wx.getStorageSync('referee_id')
  394. }, result => {
  395. // 记录token user_id
  396. wx.setStorageSync('token', result.data.token);
  397. wx.setStorageSync('user_id', result.data.user_id);
  398. // 执行回调函数
  399. callback && callback();
  400. }, false, () => {
  401. wx.hideLoading();
  402. });
  403. }
  404. });
  405. },
  406. /**
  407. * 记录购物车商品总数量
  408. * @param {*} value
  409. */
  410. setCartTotalNum(value) {
  411. wx.setStorageSync('cartTotalNum', Number(value))
  412. },
  413. /**
  414. * 设置购物车tabbar的角标
  415. */
  416. setCartTabBadge() {
  417. const number = wx.getStorageSync('cartTotalNum')
  418. if (number > 0) {
  419. wx.setTabBarBadge({
  420. index: 2,
  421. text: `${number}`
  422. })
  423. } else {
  424. wx.removeTabBarBadge({
  425. index: 2
  426. })
  427. }
  428. return
  429. }
  430. });