app.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (C) 2018-2019
  3. * All rights reserved, Designed By www.joolun.com
  4. * 注意:
  5. * 本软件为www.joolun.com开发研制,项目使用请保留此说明
  6. */
  7. /**
  8. * <version>3.3.2</version>
  9. */
  10. import api from './utils/api'
  11. import __config from './config/env'
  12. App({
  13. api: api,
  14. globalData: {
  15. thirdSession: null,
  16. wxUser: null,
  17. config: __config
  18. },
  19. onLaunch: function () {
  20. //检测新版本
  21. this.updateManager()
  22. wx.getSystemInfo({
  23. success: e => {
  24. this.globalData.StatusBar = e.statusBarHeight;
  25. let custom = wx.getMenuButtonBoundingClientRect();
  26. this.globalData.Custom = custom;
  27. this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  28. }
  29. })
  30. },
  31. updateManager(){
  32. const updateManager = wx.getUpdateManager()
  33. updateManager.onUpdateReady(function () {
  34. wx.showModal({
  35. title: '更新提示',
  36. content: '新版本已经准备好,是否重启应用?',
  37. success(res) {
  38. if (res.confirm) {
  39. updateManager.applyUpdate()
  40. }
  41. }
  42. })
  43. })
  44. },
  45. //获取购物车数量
  46. shoppingCartCount() {
  47. this.api.shoppingCartCount()
  48. .then(res => {
  49. let shoppingCartCount = res.data
  50. this.globalData.shoppingCartCount = shoppingCartCount + ''
  51. wx.setTabBarBadge({
  52. index: 2,
  53. text: this.globalData.shoppingCartCount + ''
  54. })
  55. })
  56. },
  57. //初始化,供每个页面调用
  58. initPage: function () {
  59. let that = this
  60. return new Promise((resolve, reject) => {
  61. if (!that.globalData.thirdSession) {//无thirdSession,进行登录
  62. that.doLogin()
  63. .then(res => {
  64. resolve("success")
  65. })
  66. } else {//有thirdSession,说明已登录,返回初始化成功
  67. wx.checkSession({//检查登录态是否过期
  68. success () {
  69. //session_key 未过期,并且在本生命周期一直有效
  70. console.log('session_key 未过期')
  71. resolve("success")
  72. },
  73. fail () {
  74. // session_key 已经失效,需要重新执行登录流程
  75. console.log('session_key 已经失效')
  76. that.doLogin()
  77. .then(res => {
  78. resolve("success")
  79. })
  80. }
  81. })
  82. }
  83. })
  84. },
  85. doLogin(){
  86. wx.showLoading({
  87. title: '登录中',
  88. })
  89. let that = this
  90. return new Promise((resolve, reject) => {
  91. wx.login({
  92. success: function (res) {
  93. if (res.code) {
  94. api.login({
  95. jsCode: res.code
  96. })
  97. .then(res => {
  98. wx.hideLoading()
  99. let wxUser = res.data
  100. that.globalData.thirdSession = wxUser.sessionKey
  101. that.globalData.wxUser = wxUser
  102. resolve("success")
  103. //获取购物车数量
  104. that.shoppingCartCount()
  105. })
  106. }
  107. }
  108. })
  109. })
  110. },
  111. //获取当前页面带参数的url
  112. getCurrentPageUrlWithArgs(){
  113. const pages = getCurrentPages()
  114. const currentPage = pages[pages.length - 1]
  115. const url = currentPage.route
  116. const options = currentPage.options
  117. let urlWithArgs = `/${url}?`
  118. for (let key in options) {
  119. const value = options[key]
  120. urlWithArgs += `${key}=${value}&`
  121. }
  122. urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
  123. return urlWithArgs
  124. }
  125. })