home.js 8.2 KB

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