home.js 7.3 KB

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