home.js 7.8 KB

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