审查视图

pages/classify/classify.js 9.7 KB
倪静楠 authored
1
// pages/classify/classify.js
倪静楠 authored
2 3
let method = require("../../utils/reuqest.js");
const util = require("../../utils/util.js");
倪静楠 authored
4 5
Page({
  data: {
倪静楠 authored
6
    Authorization: '', //判断用户是否登录
倪静楠 authored
7
    integral: '', //用户积分
倪静楠 authored
8
    page: 1,
倪静楠 authored
9 10 11 12
    classifyId: "", //侧边分类id
    activeKey: 0,
    lableArray: [], //侧边导航
    list: [], //商品列表
倪静楠 authored
13 14
    idx: 0, //商品规格索引
    check: false, //商品规格选择
倪静楠 authored
15 16 17
    goodsData: '', //商品信息
    stockNum: '', //获取商品库存
    keyword: '', //搜索
倪静楠 authored
18
    id: '',
倪静楠 authored
19 20
    count: 1, //加购数量
    location: '', //地址解析
倪静楠 authored
21 22
    latitude: '', //维度
    longitude: '', //经度
倪静楠 authored
23
    bottomHint: false //触底提示
倪静楠 authored
24
  },
倪静楠 authored
25
  //获取位置信息
倪静楠 authored
26 27
  location() {
    let that = this
倪静楠 authored
28
    console.log(that.data.latitude, '5555')
倪静楠 authored
29
    let str = that.data.latitude + ',' + that.data.longitude;
倪静楠 authored
30
    console.log(str, 'str')
倪静楠 authored
31 32 33
    method.getRequest("/goods/geocoder/v1/" + str, data => {
      if (data.statusCode == 0) {
        that.setData({
倪静楠 authored
34
          location: data.data.address.split('市')[1]
倪静楠 authored
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
        })
      }
    })
  },
  //搜索框
  getKeyWord(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  search() {
    if (this.data.keyword == '') {
      wx.showToast({
        title: '请输入商品名称',
        icon: 'none'
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/searchRequest/searchRequest?keyword=' + this.data.keyword,
    })
  },
倪静楠 authored
57 58 59 60 61 62 63 64 65 66 67 68
  //头部导航
  pullDown() {
    this.setData({
      show: !this.data.show
    })
  },
  onPageScroll(e) {
    this.setData({
      show: false
    })
  },
  // 侧边导航菜单
倪静楠 authored
69 70 71 72 73 74 75 76 77 78 79 80 81 82
  catalog() {
    method.getRequest('/category/getCategoryList', data => {
      if (data.statusCode == 0) {
        this.setData({
          lableArray: data.data
        })
        this.onData()
      }
    })
  },
  onChange(e) {
    let that = this;
    let list = that.data.lableArray;
    let id = e.currentTarget.dataset.id;
倪静楠 authored
83 84 85
    this.setData({
      classifyId: id
    })
倪静楠 authored
86 87 88
    that.dataRequest(id)
  },
  //第一次数据请求
倪静楠 authored
89
  onData(id) {
倪静楠 authored
90
    let postData = {
倪静楠 authored
91
      categoryId: this.data.classifyId,
倪静楠 authored
92
      page: this.data.page,
倪静楠 authored
93 94 95
      size: 10
    }
    method.postRequest('/goods/list', postData, data => {
倪静楠 authored
96
      let lists = this.data.page == 1 ? data.data : this.data.list.concat(data.data)
倪静楠 authored
97 98
      if (data.statusCode == 0) {
        this.setData({
倪静楠 authored
99
          list: lists
倪静楠 authored
100
        })
倪静楠 authored
101
      }
倪静楠 authored
102
    })
倪静楠 authored
103 104 105
  },
  //分类数据请求
  dataRequest: function (id) {
倪静楠 authored
106
    this.setData({
倪静楠 authored
107
      page: 1
倪静楠 authored
108
    })
倪静楠 authored
109 110
    let postData = {
      categoryId: id,
倪静楠 authored
111
      page: this.data.page,
倪静楠 authored
112 113 114
      size: 10
    }
    method.postRequest('/goods/list', postData, data => {
倪静楠 authored
115 116
      wx.stopPullDownRefresh()
      let lists = this.data.page == 1 ? data.data : this.data.list.concat(data.data)
倪静楠 authored
117 118
      if (data.statusCode == 0) {
        this.setData({
倪静楠 authored
119
          list: lists
倪静楠 authored
120
        })
倪静楠 authored
121
      }
倪静楠 authored
122
    })
倪静楠 authored
123
  },
倪静楠 authored
124
  //加入购物车
倪静楠 authored
125
  addCart(e) {
倪静楠 authored
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    if (this.data.Authorization) {
      let id = e.currentTarget.dataset.id
      this.setData({
        showMask: true
      })
      method.getRequest("/goods/" + id, data => {
        if (data.statusCode == 0) {
          this.setData({
            goodsData: data.data,
            stockNum: data.data.list[0].goodsStock
          })
        } else {

        }
      })
    } else {
      wx.showToast({
        title: '请先登录!',
        icon: 'none'
      })
      setTimeout(() => {
        util.getUser()
      }, 2000)
    }
倪静楠 authored
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  },
  closeMask() {
    this.setData({
      showMask: false
    })
  },
  confrimCart() {
    let postData = {
      goodsSkuId: this.data.skuId,
      num: this.data.count
    }
    method.postRequest("/cart", postData, data => {
      if (data.statusCode == 0) {
        this.setData({
          showMask: false
        })
        wx.showToast({
          title: "添加购物车成功",
          icon: 'success',
          duration: 1000
        })
      } else {
        wx.showToast({
          title: '请选择商品规格',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  decNum() {
    let that = this
    let count = that.data.count
    if (that.data.count > 1) {
      count--
    } else {
      wx.showToast({
        title: '已经是最少了哦~',
        icon: 'none'
      })
    }
    that.setData({
      count
    })
  },
  addNum(e) {
    let that = this
    let count = that.data.count
    let stock = that.data.stockNum
    if (count < stock) {
      count++
    } else {
      wx.showToast({
        title: '不能超过库存哦~',
        icon: 'none'
      })
    }
    that.setData({
      count
    })
  },
  check(e) {
    let list = this.data.goodsData.list;
    let skuId = e.currentTarget.dataset.id;
    this.setData({
      idx: e.currentTarget.dataset.index,
      skuId: skuId
    })
  },
  //兑换
  exchange(e) {
倪静楠 authored
222 223 224 225 226 227 228 229 230
    if (this.data.Authorization) {
      let jifenNum = e.currentTarget.dataset.integral;
      let defaultSku = e.currentTarget.dataset.defaultSku;
      wx.setStorageSync('defaultSku', e.currentTarget.dataset.defaultsku)
      //获取用户积分
      method.getRequest("/myUser/queryUserInfo", data => {
        if (data.statusCode == 0) {
          this.setData({
            integral: data.data.integral
倪静楠 authored
231
          })
倪静楠 authored
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          if (jifenNum > this.data.integral) {
            wx.showToast({
              title: '您的积分目前不够兑换该商品',
              icon: 'none'
            })
            return false
          } else {
            wx.navigateTo({
              url: '/pages/integral-order/integral-order',
            })
          }
        } else {}
      })
    } else {
      wx.showToast({
        title: '请先登录!',
        icon: 'none'
      })
      setTimeout(() => {
        util.getUser()
      }, 2000)
    }
倪静楠 authored
254 255 256 257 258 259 260 261 262 263

  },
  //商品详情
  goGoodsDetail(e) {
    let goodsId = e.currentTarget.dataset.id;
    wx.setStorageSync('goodsId', goodsId)
    wx.navigateTo({
      url: '/pages/product-detail/product-detail',
    })
  },
倪静楠 authored
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  //再次申请获取位置信息
  openConfirm: function () {
    wx.showModal({
      content: '检测到您没打开定位权限,有些功能无法使用,是否去设置打开?',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        //点击“确认”时打开设置页面
        if (res.confirm) {
          console.log('用户点击确认')
          wx.openSetting({
            success: (res) => {}
          })
        } else {
          console.log('用户点击取消')
        }
      }
    });
  },
倪静楠 authored
284 285 286 287
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
倪静楠 authored
288 289
    this.catalog();
    if (wx.getStorageSync('categoryId')) {
倪静楠 authored
290
      let id = wx.getStorageSync('categoryId')
倪静楠 authored
291 292 293 294 295 296 297 298 299 300 301 302
      this.setData({
        classifyId: id
      })
      this.dataRequest(this.data.classifyId);
    } else {
      setTimeout(() => {
        this.setData({
          classifyId: this.data.lableArray[0].categoryId
        })
      }, 500)
      this.dataRequest(this.data.classifyId);
    }
倪静楠 authored
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    wx.removeStorageSync('categoryId')
    wx.removeStorageSync('index')
    this.catalog();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let categoryId = wx.getStorageSync('categoryId')
    let index = wx.getStorageSync('classifyIndex')
倪静楠 authored
321
    if (index) {
倪静楠 authored
322 323 324
      this.setData({
        activeKey: index
      });
倪静楠 authored
325
    } else {
倪静楠 authored
326
      this.setData({
倪静楠 authored
327
        activeKey: 0
倪静楠 authored
328 329 330
      })
    }
    wx.removeStorageSync("classifyIndex")
倪静楠 authored
331
    let that = this;
倪静楠 authored
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    //判断用户是否登录
    if (this.data.Authorization) {
      // wx.getLocation({
      //   type: 'gcj02',
      //   success(res) {
      //     that.setData({
      //       latitude: res.latitude,
      //       longitude: res.longitude
      //     })
      //     that.location();
      //     const speed = res.speed
      //     const accuracy = res.accuracy
      //   },
      //   fail(errInfo) {
      //     console.info(errInfo)
      //   }
      // })
    }
    //判断用户是否登录
    if (wx.getStorageSync('Authorization') === '') {
      this.setData({
        Authorization: false
      })
    } else {
      this.setData({
        Authorization: true
      })
      //位置
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          that.setData({
            longitude: res.longitude,
            latitude: res.latitude
          })
          that.location()
        }
      })
      //判断是否获得了用户地理位置授权
      if (that.data.latitude == undefined) {
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation'])
              that.openConfirm()
          }
倪静楠 authored
377 378
        })
      }
倪静楠 authored
379 380 381 382 383 384 385 386

    }
    if (that.data.latitude !== undefined) {
      setTimeout(() => {
        that.location()
      }, 1000)
    }
倪静楠 authored
387
  },
倪静楠 authored
388
  open() {
倪静楠 authored
389
倪静楠 authored
390
  },
倪静楠 authored
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
倪静楠 authored
408 409 410 411 412 413 414
  onRefresh() {
    this.setData({
      page: 1,
      list: []
    })
    this.dataRequest(this.data.classifyId);
  },
倪静楠 authored
415
  onPullDownRefresh: function () {
倪静楠 authored
416
    this.onRefresh()
倪静楠 authored
417 418 419 420 421 422
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
倪静楠 authored
423 424 425 426 427 428 429 430 431 432 433 434
    this.setData({
      page: Number(this.data.page) + 1
    })
    this.dataRequest(this.data.classifyId);
    this.setData({
      bottomHint: true
    })
    setTimeout(() => {
      this.setData({
        bottomHint: false
      })
    }, 2000)
倪静楠 authored
435 436 437 438 439 440 441 442 443
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})