123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- const WXAPI = require('apifm-wxapi')
- const AUTH = require('../../utils/auth')
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- listType: 1, // 1为1个商品一行,2为2个商品一行
- name: '', // 搜索关键词
- orderBy: '', // 排序规则
- page: 1 // 读取第几页
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.setData({
- name: options.name,
- categoryId: options.categoryId
- })
- this.search()
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- async search(){
- wx.showLoading({
- title: '加载中',
- })
- const _data = {
- orderBy: this.data.orderBy,
- page: this.data.page,
- pageSize: 20,
- }
- if (this.data.name) {
- _data.k = this.data.name
- }
- if (this.data.categoryId) {
- _data.categoryId = this.data.categoryId
- }
- const res = await WXAPI.goods(_data)
- wx.hideLoading()
- if (res.code == 0) {
- if (this.data.page == 1) {
- this.setData({
- goods: res.data,
- })
- } else {
- this.setData({
- goods: this.data.goods.concat(res.data),
- })
- }
- } else {
- if (this.data.page == 1) {
- this.setData({
- goods: null,
- })
- } else {
- wx.showToast({
- title: '没有更多了',
- icon: 'none'
- })
- }
- }
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- onReachBottom() {
- this.setData({
- page: this.data.page + 1
- });
- this.search()
- },
- changeShowType(){
- if (this.data.listType == 1) {
- this.setData({
- listType: 2
- })
- } else {
- this.setData({
- listType: 1
- })
- }
- },
- bindinput(e){
- this.setData({
- name: e.detail.value
- })
- },
- bindconfirm(e){
- this.setData({
- page: 1,
- name: e.detail.value
- })
- this.search()
- },
- filter(e){
- this.setData({
- page: 1,
- orderBy: e.currentTarget.dataset.val
- })
- this.search()
- },
- async addShopCar(e) {
- const curGood = this.data.goods.find(ele => {
- return ele.id == e.currentTarget.dataset.id
- })
- if (!curGood) {
- return
- }
- if (curGood.stores <= 0) {
- wx.showToast({
- title: '已售罄~',
- icon: 'none'
- })
- return
- }
- this.addShopCarCheck({
- goodsId: curGood.id,
- buyNumber: 1,
- sku: []
- })
- },
- async addShopCarCheck(options) {
- AUTH.checkHasLogined().then(isLogined => {
- this.setData({
- wxlogin: isLogined
- })
- if (isLogined) {
- // 处理加入购物车的业务逻辑
- this.addShopCarDone(options)
- }
- })
- },
- async addShopCarDone(options) {
- const res = await WXAPI.shippingCarInfoAddItem(wx.getStorageSync('token'), options.goodsId, options.buyNumber, options.sku)
- if (res.code == 30002) {
- // 需要选择规格尺寸
- const skuCurGoodsRes = await WXAPI.goodsDetail(options.goodsId)
- if (skuCurGoodsRes.code != 0) {
- wx.showToast({
- title: skuCurGoodsRes.msg,
- icon: 'none'
- })
- return
- }
- const skuCurGoods = skuCurGoodsRes.data
- skuCurGoods.basicInfo.storesBuy = 1
- this.setData({
- skuCurGoods
- })
- return
- }
- if (res.code != 0) {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- return
- }
- wx.showToast({
- title: '加入成功',
- icon: 'success'
- })
- this.setData({
- skuCurGoods: null
- })
- },
- storesJia() {
- const skuCurGoods = this.data.skuCurGoods
- if (skuCurGoods.basicInfo.storesBuy < skuCurGoods.basicInfo.stores) {
- skuCurGoods.basicInfo.storesBuy++
- this.setData({
- skuCurGoods
- })
- }
- },
- storesJian() {
- const skuCurGoods = this.data.skuCurGoods
- if (skuCurGoods.basicInfo.storesBuy > 1) {
- skuCurGoods.basicInfo.storesBuy--
- this.setData({
- skuCurGoods
- })
- }
- },
- closeSku() {
- this.setData({
- skuCurGoods: null
- })
- wx.showTabBar()
- },
- skuSelect(e) {
- const pid = e.currentTarget.dataset.pid
- const id = e.currentTarget.dataset.id
- // 处理选中
- const skuCurGoods = this.data.skuCurGoods
- const property = skuCurGoods.properties.find(ele => { return ele.id == pid })
- property.childsCurGoods.forEach(ele => {
- if (ele.id == id) {
- ele.active = true
- } else {
- ele.active = false
- }
- })
- this.setData({
- skuCurGoods
- })
- },
- addCarSku() {
- const skuCurGoods = this.data.skuCurGoods
- const propertySize = skuCurGoods.properties.length // 有几组SKU
- const sku = []
- skuCurGoods.properties.forEach(p => {
- const o = p.childsCurGoods.find(ele => { return ele.active })
- if (!o) {
- return
- }
- sku.push({
- optionId: o.propertyId,
- optionValueId: o.id
- })
- })
- if (sku.length != propertySize) {
- wx.showToast({
- title: '请选择规格',
- icon: 'none'
- })
- return
- }
- const options = {
- goodsId: skuCurGoods.basicInfo.id,
- buyNumber: skuCurGoods.basicInfo.storesBuy,
- sku
- }
- this.addShopCarDone(options)
- },
- })
|