index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const api = require('../../config/api')
  2. const util = require('../../utils/util')
  3. const check = require('../../utils/check')
  4. let app = getApp()
  5. Page({
  6. data: {
  7. show: false,
  8. nickname: "",
  9. mobile: "",
  10. banner: [],
  11. channel: [],
  12. categoryId: -2, // 底部商品分类id
  13. category: [
  14. { id: -2, name: '推荐' },
  15. { id: -1, name: '新品' }
  16. ],
  17. floorGoods: [],
  18. page: 1,
  19. totalPages: 1
  20. },
  21. onLoad() {
  22. const token = wx.getStorageSync('token'), userInfo = wx.getStorageSync('userInfo')
  23. if (token && userInfo && (!userInfo.mobile || userInfo.mobile.length != 11)) this.setData({ show: true })
  24. this.getIndexData()
  25. },
  26. onPullDownRefresh() {
  27. this.getIndexData()
  28. wx.stopPullDownRefresh()
  29. },
  30. onReachBottom() {
  31. if (this.data.totalPages > this.data.page) {
  32. this.setData({ page: this.data.page + 1 })
  33. this.getfloorGoods()
  34. }
  35. },
  36. getIndexData() {
  37. util.showLoad('加载中')
  38. util.request(api.IndexUrl).then(res => {
  39. util.hideLoad()
  40. if (res.errno === 0) {
  41. res.data.floorGoodsList.forEach(g => g.picUrl = g.picUrl.split(','))
  42. this.setData({
  43. banner: res.data.banner,
  44. floorGoods: res.data.floorGoodsList,
  45. totalPages: res.data.totalPages
  46. })
  47. if (res.data.channel && res.data.channel.length) this.setData({ channel: res.data.channel.splice(0, 8) })
  48. } else util.showErrorToast(res.errmsg)
  49. }).catch(() => {
  50. util.hideLoad()
  51. util.showErrorToast('网络连接失败')
  52. })
  53. },
  54. // 获取底部 分类商品
  55. getfloorGoods(e) {
  56. const { page, categoryId } = this.data
  57. util.showLoad('加载中')
  58. util.request(api.GetfloorGoods, { page, categoryId }).then(res => {
  59. util.hideLoad()
  60. if (res.errno === 0) {
  61. res.data.goodsList.forEach(g => g.picUrl = g.picUrl.split(','))
  62. const floorGoods = e ? res.data.goodsList : this.data.floorGoods.concat(res.data.goodsList)
  63. this.setData({ totalPages: res.data.totalPages, floorGoods })
  64. } else util.showErrorToast(res.errmsg)
  65. }).catch(() => {
  66. util.hideLoad()
  67. util.showErrorToast('网络连接失败')
  68. })
  69. },
  70. // 切换底部分类
  71. changeCategory({ currentTarget }) {
  72. const { id } = currentTarget.dataset
  73. this.setData({ categoryId: id, page: 1 })
  74. this.getfloorGoods('reset')
  75. },
  76. toCatelog({ currentTarget }) {
  77. const { id } = currentTarget.dataset
  78. app.globalData.menuId = id
  79. wx.switchTab({ url: '/pages/catalog/catalog' })
  80. },
  81. // 商品详情
  82. toDetail({ currentTarget }) {
  83. const { item } = currentTarget.dataset
  84. wx.navigateTo({ url: `/packageGoods/detail/detail?obj=${JSON.stringify({ id: item.id })}` })
  85. },
  86. hidePopup() {
  87. this.setData({ show: false, nickname: "", mobile: "" })
  88. },
  89. nameInput({ detail }) {
  90. this.setData({ nickname: detail })
  91. },
  92. mobileInput({ detail }) {
  93. this.setData({ mobile: detail })
  94. },
  95. updateUser() {
  96. const { nickname, mobile } = this.data
  97. const { id } = wx.getStorageSync('userInfo')
  98. if (!check.validNickName(nickname)) return util.showToast('请输入2-10个字符')
  99. if (!check.validPhone(mobile)) return util.showToast('请输入正确手机号')
  100. util.showLoad('加载中')
  101. util.request(api.UserUpdate, { id, nickname, mobile }, 'POST').then(res => {
  102. util.hideLoad()
  103. if (res.errno === 0) {
  104. wx.setStorageSync('userInfo', { ...wx.getStorageSync('userInfo'), nickname, mobile })
  105. this.hidePopup()
  106. } else util.showErrorToast(res.errmsg)
  107. }).catch(() => {
  108. util.hideLoad()
  109. util.showErrorToast('网络连接失败')
  110. })
  111. },
  112. onShareAppMessage() {}
  113. })