home.js 7.5 KB

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