app.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. const util = require('./utils/util.js');
  2. const api = require('./config/api');
  3. const bayCbs = []
  4. ! function () {
  5. var PageTmp = Page;
  6. Page = function (pageConfig) {
  7. // 设置全局默认分享
  8. let tempShare = pageConfig.onShareAppMessage
  9. pageConfig.onShareAppMessage = function(...args) {
  10. let pages = getCurrentPages()
  11. let path = pages[pages.length - 1].route;
  12. let defconfig = {
  13. title: '【好友推荐】场景还原,实现线上选座',
  14. desc: '【好友推荐】场景还原,实现线上选座',
  15. path: path,
  16. imageUrl: '/static/images/share.png',
  17. };
  18. let currConfig = tempShare && tempShare.call(this, ...args)
  19. let config = defconfig
  20. if (currConfig) {
  21. config = {
  22. ...defconfig,
  23. ...currConfig,
  24. }
  25. }
  26. return config
  27. }
  28. let onShow = pageConfig.onShow
  29. pageConfig.onShow = function(...args) {
  30. getApp().setLoginProps(true)
  31. return onShow && onShow.call(this, ...args)
  32. }
  33. PageTmp(pageConfig);
  34. };
  35. }();
  36. const voiceCbs = []
  37. const voiceProps = {
  38. noMute: false,
  39. pullUrls: [],
  40. pushUrl: false,
  41. }
  42. const loginCbs = []
  43. const loginProps = {
  44. isLogin: true
  45. };
  46. App({
  47. appHideCb: '',
  48. onHide() {
  49. this.appHideCb && this.appHideCb()
  50. },
  51. onShareAppMessage() {
  52. console.log('--------------------')
  53. },
  54. onLaunch: function () {
  55. //获取小程序更新机制兼容
  56. if (wx.canIUse('getUpdateManager')) {
  57. const updateManager = wx.getUpdateManager()
  58. updateManager.onCheckForUpdate(function (res) {
  59. // 请求完新版本信息的回调
  60. if (res.hasUpdate) {
  61. updateManager.onUpdateReady(function () {
  62. wx.showModal({
  63. title: '更新提示',
  64. content: '新版本已经准备好,是否重启应用?',
  65. success: function (res) {
  66. if (res.confirm) {
  67. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  68. updateManager.applyUpdate()
  69. }
  70. }
  71. })
  72. })
  73. updateManager.onUpdateFailed(function () {
  74. // 新的版本下载失败
  75. wx.showModal({
  76. title: '已经有新版本了哟~',
  77. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  78. })
  79. })
  80. }
  81. })
  82. } else {
  83. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  84. wx.showModal({
  85. title: '提示',
  86. content: '当前微信版本过低,无法更好体验程序,请升级到最新微信版本后重试。'
  87. })
  88. }
  89. this.onGetSysInfo()
  90. },
  91. globalData: {
  92. voiceProps,
  93. loginProps,
  94. userInfo: {
  95. nickName: 'Hi,游客',
  96. userName: '点击去登录',
  97. avatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
  98. },
  99. token: '',
  100. userCoupon: 'NO_USE_COUPON', //默认不适用优惠券
  101. courseCouponCode: {}, //购买课程的时候优惠券信息
  102. isiPhoneX: false,
  103. isLogin: false
  104. },
  105. addVoicePropsListener(cb) {
  106. voiceCbs.push(cb)
  107. },
  108. removeVoicePropsListener(cb) {
  109. voiceCbs.splice(voiceCbs.indexOf(cb), 1)
  110. },
  111. setVoiceProps(props) {
  112. console.log(props, 'props')
  113. Object.keys(props).forEach(k => {
  114. voiceProps[k] = props[k]
  115. })
  116. voiceCbs.forEach(cb => cb(props))
  117. },
  118. addLoginListener(cb) {
  119. loginCbs.push(cb)
  120. },
  121. removeLoginListener(cb) {
  122. loginCbs.splice(loginCbs.indexOf(cb), 1)
  123. },
  124. setLoginProps(props) {
  125. loginProps.isLogin = props
  126. loginCbs.forEach(cb => cb())
  127. },
  128. onGetSysInfo() {
  129. // 先缓存获取
  130. let isiPhoneX = wx.getStorageSync('isiPhoneX') || false
  131. // 缓存没有 再获取
  132. if (!isiPhoneX) {
  133. wx.getSystemInfo({
  134. success: res => {
  135. // 手机品牌
  136. let modelmes = res.model;
  137. console.log('mode---------------lmes', modelmes)
  138. // 如果是 X,XS,XR,XS MAX 均可适配
  139. if (modelmes.indexOf('iPhone X') != -1) {
  140. // 存储型号
  141. this.globalData.isiPhoneX = true
  142. wx.setStorageSync('isiPhoneX', true)
  143. // 加入回调
  144. this.sysCallback && this.sysCallback()
  145. }
  146. },
  147. })
  148. } else {
  149. this.globalData.isiPhoneX = isiPhoneX
  150. }
  151. },
  152. // 下拉刷新
  153. onPullDownRefresh: function () {
  154. // 显示顶部刷新图标
  155. wx.showNavigationBarLoading();
  156. var that = this;
  157. // 隐藏导航栏加载框
  158. wx.hideNavigationBarLoading();
  159. // 停止下拉动作
  160. wx.stopPullDownRefresh();
  161. this.checkNetStatu()
  162. },
  163. // 更新小程序
  164. updateManager: function () {
  165. //获取系统信息 客户端基础库
  166. wx.getSystemInfo({
  167. success: function (res) {
  168. //基础库版本比较,版本更新必须是1.9.90以上
  169. const v = util.compareVersion(res.SDKVersion, '1.9.90');
  170. if (v > 0) {
  171. const manager = wx.getUpdateManager();
  172. manager.onCheckForUpdate(function (res) {
  173. // 请求完新版本信息的回调
  174. //console.log(res.hasUpdate);
  175. });
  176. manager.onUpdateReady(function () {
  177. wx.showModal({
  178. title: '更新提示',
  179. content: '新版本已经准备好,是否重启应用?',
  180. success: function (res) {
  181. if (res.confirm) {
  182. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  183. manager.applyUpdate();
  184. }
  185. }
  186. })
  187. });
  188. manager.onUpdateFailed(function () {
  189. // 新的版本下载失败
  190. });
  191. } else {
  192. // wx.showModal({
  193. // title: '温馨提示',
  194. // content: '当前微信版本过低,无法更好体验程序,请升级到最新微信版本后重试。'
  195. // })
  196. }
  197. },
  198. })
  199. },
  200. getCurrentPages: function () {
  201. var pages = getCurrentPages(); //获取加载的页面
  202. var currentPage = pages[pages.length - 1]; //获取当前页面的对象
  203. var url = currentPage.route; //当前页面url
  204. var options = currentPage.options; //获取url中所带的参数
  205. //拼接url的参数
  206. var currentPage = url + '?';
  207. for (var key in options) {
  208. var value = options[key]
  209. currentPage += key + '=' + value + '&';
  210. }
  211. currentPage = currentPage.substring(0, currentPage.length - 1);
  212. return currentPage;
  213. },
  214. orderBuyCbs(cb) {
  215. bayCbs.push(cb)
  216. },
  217. callOrderBuyCbs(...args) {
  218. while(bayCbs.length) {
  219. bayCbs.shift()(...args)
  220. }
  221. },
  222. async checkNetStatu() {
  223. if (!(await util.checkNetStatu())) {
  224. return wx.redirectTo({
  225. url: '/pages/offline/offline?url=' + decodeURI(this.getCurrentPages()),
  226. })
  227. }
  228. },
  229. updateCardCount() {
  230. return;
  231. util.request(api.CartList).then((res) => {
  232. //显示红字,badge
  233. if (!res.data || !res.data.cartTotal) {
  234. return
  235. }
  236. // global.setTabBarBadge({
  237. // index: 3,
  238. // text: res.data.cartTotal.goodsCount ,
  239. // });
  240. if (res.data.cartTotal.goodsCount) {
  241. wx.setTabBarBadge({
  242. index: 3,
  243. text: res.data.cartTotal.goodsCount + '',
  244. });
  245. } else {
  246. wx.removeTabBarBadge({
  247. index: 3,
  248. });
  249. }
  250. })
  251. },
  252. onLaunch() {
  253. wx.setStorageSync('lastSceneNum', null)
  254. }
  255. })