home.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. const pages = require('../../mixin/pages')
  2. const { getProductCategoryList } = require('../../api/common')
  3. const { getLunboList } = require('./api/index')
  4. const app = getApp()
  5. const tmplIds = 'uc9ztDw9ZscpNkMr52XUcB7YrDBk5AzjrRtNSu8Dq-4'
  6. let leftHeight = 0
  7. let rightHeight = 0
  8. let query = wx.createSelectorQuery()
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. ...pages.data(),
  15. listUrl: '/api/user/home/product/list',
  16. searchForm: {
  17. key_words: '',
  18. category_id: ''
  19. },
  20. lunbos: [],
  21. lunboMsg: [],
  22. headerNav: [
  23. {
  24. icon: 'partner@2x.png',
  25. name: '供应商',
  26. path: 'business'
  27. },
  28. {
  29. icon: 'guide@2x.png',
  30. name: '全部产品',
  31. path: 'goods'
  32. },
  33. {
  34. icon: 'collection@2x.png',
  35. name: '收藏',
  36. path: 'collection'
  37. },
  38. {
  39. icon: 'news.png',
  40. name: '选购指南',
  41. path: 'guide'
  42. }
  43. ],
  44. categoryList: [],
  45. leftList: [],
  46. rightList: [],
  47. shop_status: 0
  48. },
  49. ...pages.methods,
  50. /**
  51. * 生命周期函数--监听页面加载
  52. */
  53. async onLoad(options) {
  54. this.fetchLunboList()
  55. await this.fetchProductCategoryList()
  56. if (this.data.searchForm.category_id) {
  57. this.fetchOrderList()
  58. }
  59. },
  60. /**
  61. * 生命周期函数--监听页面初次渲染完成
  62. */
  63. onReady() {
  64. },
  65. /**
  66. * 生命周期函数--监听页面显示
  67. */
  68. onShow() {
  69. const { shop_status } = app.globalData.userInfo
  70. this.getTabBar().init()
  71. if (shop_status === 1 && this.getTabBar().data.list.findIndex(item => item.pagePath === 'pages/news/news') === -1) {
  72. this.getTabBar().setData({ list: app.globalData.tabBarList })
  73. }
  74. this.setData({
  75. shop_status: shop_status === 1 ? shop_status : 0
  76. })
  77. },
  78. /**
  79. * 生命周期函数--监听页面隐藏
  80. */
  81. onHide() {
  82. },
  83. /**
  84. * 生命周期函数--监听页面卸载
  85. */
  86. onUnload() {
  87. },
  88. /**
  89. * 页面相关事件处理函数--监听用户下拉动作
  90. */
  91. onPullDownRefresh() {
  92. if (this.data.freshing) {
  93. return
  94. }
  95. this.setData({
  96. freshing: true
  97. })
  98. this.bindCallBack()
  99. },
  100. /**
  101. * 页面上拉触底事件的处理函数
  102. */
  103. onReachBottom() {
  104. this.fetchOrderList()
  105. },
  106. /**
  107. * 用户点击右上角分享
  108. */
  109. onShareAppMessage() {
  110. },
  111. bindCallBack() {
  112. this.refreshOrderList()
  113. this.fetchLunboList()
  114. },
  115. async fetchLunboList() {
  116. try {
  117. const { status, data, msg } = await getLunboList()
  118. if (status) {
  119. const { lunbos, lunbo_msg } = data
  120. this.setData({
  121. lunbos: lunbos,
  122. lunboMsg: lunbo_msg
  123. })
  124. } else {
  125. wx.showToast({
  126. title: msg,
  127. icon: 'none'
  128. })
  129. }
  130. } catch (err) {}
  131. },
  132. async fetchProductCategoryList() {
  133. try {
  134. const { status, data, msg } = await getProductCategoryList()
  135. if (status && Array.isArray(data) && data.length > 0) {
  136. const temp = data.map(item => {
  137. return {
  138. ...item,
  139. text: item.category_name
  140. }
  141. })
  142. temp.unshift({
  143. category_img_url: 'https://lanman-shining.oss-cn-hangzhou.aliyuncs.com/images/user/bashi635cd28a12481.png',
  144. category_name: '全部',
  145. id: 0
  146. })
  147. this.setData({
  148. categoryList: temp,
  149. 'searchForm.category_id': temp.length > 0 ? temp[0].id : ''
  150. })
  151. } else {
  152. wx.showToast({
  153. title: msg,
  154. icon: 'none'
  155. })
  156. }
  157. } catch (err) {}
  158. },
  159. handleTab(event) {
  160. const { name } = event.detail
  161. this.setData({
  162. 'searchForm.category_id': name
  163. }, () => {
  164. this.refreshOrderList()
  165. })
  166. },
  167. async isLeft(list) {
  168. const {
  169. leftList,
  170. rightList
  171. } = this.data
  172. for (const item of list) {
  173. if (leftHeight < rightHeight) {
  174. leftList.push(item)
  175. } else if (leftHeight === rightHeight) {
  176. leftList.length <= rightList ? leftList.push(item) : rightList.push(item)
  177. } else {
  178. rightList.push(item)
  179. }
  180. await this.getBoxHeight(leftList, rightList)
  181. }
  182. },
  183. getBoxHeight(leftList, rightList) {
  184. return new Promise((resolve, reject) => {
  185. this.setData({
  186. leftList,
  187. rightList
  188. }, () => {
  189. query.select('.waterfall-left').boundingClientRect()
  190. query.select('.waterfall-right').boundingClientRect()
  191. query.exec((res) => {
  192. leftHeight = res[0].height
  193. rightHeight = res[1].height
  194. resolve()
  195. })
  196. })
  197. })
  198. },
  199. jumpLeavingAMessage() {
  200. wx.navigateTo({
  201. url: '/pages/leavingAMessage/leavingAMessage'
  202. })
  203. },
  204. handleSwiperClick(e) {
  205. const isTab = app.globalData.tabBarList.map(item => item.pagePath)
  206. const { item } = e.currentTarget.dataset
  207. if (Object.prototype.toString.call(item) === '[object Object]') {
  208. const { lunbo_link_url } = item
  209. if (/^http/.test(lunbo_link_url)) {
  210. wx.navigateTo({
  211. url: '/pages/h5/h5?url=' + lunbo_link_url
  212. })
  213. } else if (/^\/pages/.test(lunbo_link_url)) {
  214. if (isTab.findIndex(item => lunbo_link_url.indexOf(item) > -1) > -1) {
  215. wx.switchTab({
  216. url: lunbo_link_url
  217. })
  218. } else {
  219. wx.navigateTo({
  220. url: lunbo_link_url
  221. })
  222. }
  223. }
  224. }
  225. },
  226. jumpMessageDetail(e) {
  227. const { item } = e.currentTarget.dataset
  228. wx.setStorageSync(app.globalData.messageDetailStorageKey, JSON.stringify(item))
  229. wx.navigateTo({
  230. url: '/pages/messageDetail/messageDetail'
  231. })
  232. },
  233. handleNav(item) {
  234. if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
  235. const path = item.path
  236. if (!path) {
  237. return
  238. }
  239. if (path === 'news' || path === 'partner') {
  240. wx.switchTab({
  241. url: `/pages/${path}/${path}`
  242. })
  243. return
  244. }
  245. wx.navigateTo({
  246. url: `/pages/${path}/${path}`
  247. })
  248. }
  249. },
  250. openMarketing(e) {
  251. const { page } = e.currentTarget.dataset
  252. wx.navigateTo({
  253. url: `/pages/${page}/${page}`
  254. })
  255. },
  256. async handleSubscribeMessage(e) {
  257. const { item } = e.currentTarget.dataset
  258. const that = this
  259. try {
  260. const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true })
  261. if (errMsg === 'getSetting:ok') {
  262. // 用户打开了订阅消息总开关
  263. if (subscriptionsSetting.mainSwitch) {
  264. // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  265. if (subscriptionsSetting.itemSettings != null) {
  266. const moIdState = subscriptionsSetting.itemSettings[tmplIds]
  267. if (moIdState === 'accept') {
  268. // 接受了消息推送
  269. that.handleNav(item)
  270. } else if (moIdState === 'reject') {
  271. // 拒绝消息推送
  272. that.handleNav(item)
  273. } else if (moIdState === 'ban') {
  274. // 已被后台封禁
  275. that.handleNav(item)
  276. } else {
  277. that.handleSubscribeMessageBridge(tmplIds, item)
  278. }
  279. } else {
  280. that.handleSubscribeMessageBridge(tmplIds, item)
  281. }
  282. } else {
  283. // 订阅消息未开启
  284. that.handleSubscribeMessageBridge(tmplIds, item)
  285. }
  286. }
  287. } catch (err) {}
  288. },
  289. handleSubscribeMessageBridge(tmplIds, item) {
  290. const that = this
  291. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  292. wx.showModal({
  293. title: '提示',
  294. content: '请授权开通服务通知',
  295. showCancel: true,
  296. success: function (res) {
  297. if (res.confirm) {
  298. wx.requestSubscribeMessage({
  299. tmplIds: [tmplIds],
  300. success(res) {
  301. that.handleNav(item)
  302. },
  303. fail(err) {
  304. that.handleNav(item)
  305. }
  306. })
  307. }
  308. }
  309. })
  310. }
  311. })