123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // pages/tabBar/salesallGoods/allGoods.js
- const app = getApp()
- const api = require('../../../request/api');
- const http = require('../../../request/http');
- const storeKeys = require('../../../utils/storageKeys.js');
- const utils = require('../../../utils/util');
- Page({
- data: {
- statusBarHeight: app.globalData.statusBarHeight,
- list: [], //页面数据
- dataObj: {}, //页面对象
- isLoading: true, //是否处于加载中
- sortStyle: 'all', //排序类型,all:综合,sales:销量,price:价格,create_time:新品
- sortSort: true, //排序是否升序
- page: 1, //分页
- },
- onLoad: function (options) {
- this.onPullDownRefresh();
- app.countRecord()
- },
- onShow: function () {
- if(this.data.list.length <= 0 && !this.data.isLoading){
- this.onPullDownRefresh();
- }
- app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl));
- app.countRecordPublic(5);
- },
- onPullDownRefresh: function () {
- wx.stopPullDownRefresh();
- this.data.page = 1;
- this.getPageData();
- },
- onReachBottom: function () {
- if (this.data.dataObj.current_page < this.data.dataObj.last_page){
- this.data.page++;
- this.getPageData();
- }
- },
- //筛选切换
- changeNav: utils.throttle(function (e) {
- let type = e.currentTarget.dataset.type;
- let sortStyle = this.data.sortStyle;
- if(type == sortStyle){
- if(type == "price"){
- let sortSort = !this.data.sortSort
- this.setData({
- sortSort
- })
- }
- }else{
- let sortSort = true;
- if(type == "price"){
- sortSort = false;
- }
- this.setData({
- sortStyle: type,
- sortSort
- })
- }
- this.onPullDownRefresh();
- },500),
-
- navigateTo: utils.throttle(function (e) {
- app.navigateTo(e);
- }),
- //添加购物车
- addCart: utils.throttle(function (e) {
- if (!utils.getStorageSync(storeKeys.TOKEN)) {
- wx.navigateTo({
- url: "/pages/tabBar/login/login"
- })
- return;
- }
- let id = e.currentTarget.dataset.id;
- let stock_total = e.currentTarget.dataset.stock || 0;
- //库存不足
- if(stock_total <= 0){
- utils.toast("当前商品暂时缺货")
- return;
- }
- let staffUserId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || 0;
- let data = {
- "goodsId": id, //商品ID
- "goodsSkuId": "", //商品规格ID
- "goodsNum": 1, //购买数量,不能小于1
- staffUserId: parseInt(staffUserId)
- }
- wx.showLoading({
- title: "加载中"
- });
- http.request({
- url: api.URL + '/api/cart/add',
- method: 'POST',
- data,
- token: utils.getStorageSync(storeKeys.TOKEN),
- success: (res) => {
- wx.hideLoading();
- utils.setStorageSync(storeKeys.CARETOTAl, res.data.data.cartTotal);
- app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl));
- utils.toast(res.data.message)
- },
- error: (res) => {
- wx.hideLoading();
- utils.toast(res.data.message)
- },
- })
- }),
- //获取页面数据
- getPageData(noLoading) {
- wx.showLoading({
- title: "加载中",
- mask: true
- });
- http.request({
- url: api.URL + "/api/goods/list?sortStyle=" + this.data.sortStyle + "&sortSort=" + this.data.sortSort + "&page=" + this.data.page,
- token: utils.getStorageSync(storeKeys.TOKEN),
- method: 'GET',
- success: (res) => {
- let list = [];
- let tmpList = res.data.data.list.data;
- if (this.data.page == 1) {
- list = tmpList;
- } else {
- list = this.data.list.concat(tmpList);
- }
- this.setData({
- list,
- dataObj: res.data.data.list
- });
- setTimeout(() => {
- this.setData({
- isLoading: false
- });
- }, 300)
- wx.hideLoading();
- },
- error: (res) => {
- this.setData({
- isLoading: false
- });
- wx.hideLoading();
- }
- })
- },
- onShareAppMessage: function (e) {
- return {
- title: app.globalData.shareTitle,
- imageUrl: app.globalData.shareImage
- }
- },
- onShareTimeline: function (e) {
- return {
- title: app.globalData.shareTitle,
- imageUrl: app.globalData.shareImage
- }
- }
- })
|