app.js 9.1 KB

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