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