allGoods.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // pages/tabBar/salesallGoods/allGoods.js
  2. const app = getApp()
  3. const api = require('../../../request/api');
  4. const http = require('../../../request/http');
  5. const storeKeys = require('../../../utils/storageKeys.js');
  6. const utils = require('../../../utils/util');
  7. Page({
  8. data: {
  9. statusBarHeight: app.globalData.statusBarHeight,
  10. list: [], //页面数据
  11. dataObj: {}, //页面对象
  12. isLoading: true, //是否处于加载中
  13. sortStyle: 'all', //排序类型,all:综合,sales:销量,price:价格,create_time:新品
  14. sortSort: true, //排序是否升序
  15. page: 1, //分页
  16. },
  17. onLoad: function (options) {
  18. this.onPullDownRefresh();
  19. app.countRecord()
  20. },
  21. onShow: function () {
  22. if(this.data.list.length <= 0 && !this.data.isLoading){
  23. this.onPullDownRefresh();
  24. }
  25. app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl));
  26. app.countRecordPublic(5);
  27. },
  28. onPullDownRefresh: function () {
  29. wx.stopPullDownRefresh();
  30. this.data.page = 1;
  31. this.getPageData();
  32. },
  33. onReachBottom: function () {
  34. if (this.data.dataObj.current_page < this.data.dataObj.last_page){
  35. this.data.page++;
  36. this.getPageData();
  37. }
  38. },
  39. //筛选切换
  40. changeNav: utils.throttle(function (e) {
  41. let type = e.currentTarget.dataset.type;
  42. let sortStyle = this.data.sortStyle;
  43. if(type == sortStyle){
  44. if(type == "price"){
  45. let sortSort = !this.data.sortSort
  46. this.setData({
  47. sortSort
  48. })
  49. }
  50. }else{
  51. let sortSort = true;
  52. if(type == "price"){
  53. sortSort = false;
  54. }
  55. this.setData({
  56. sortStyle: type,
  57. sortSort
  58. })
  59. }
  60. this.onPullDownRefresh();
  61. },500),
  62. navigateTo: utils.throttle(function (e) {
  63. app.navigateTo(e);
  64. }),
  65. //添加购物车
  66. addCart: utils.throttle(function (e) {
  67. if (!utils.getStorageSync(storeKeys.TOKEN)) {
  68. wx.navigateTo({
  69. url: "/pages/tabBar/login/login"
  70. })
  71. return;
  72. }
  73. let id = e.currentTarget.dataset.id;
  74. let stock_total = e.currentTarget.dataset.stock || 0;
  75. //库存不足
  76. if(stock_total <= 0){
  77. utils.toast("当前商品暂时缺货")
  78. return;
  79. }
  80. let staffUserId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || 0;
  81. let data = {
  82. "goodsId": id, //商品ID
  83. "goodsSkuId": "", //商品规格ID
  84. "goodsNum": 1, //购买数量,不能小于1
  85. staffUserId: parseInt(staffUserId)
  86. }
  87. wx.showLoading({
  88. title: "加载中"
  89. });
  90. http.request({
  91. url: api.URL + '/api/cart/add',
  92. method: 'POST',
  93. data,
  94. token: utils.getStorageSync(storeKeys.TOKEN),
  95. success: (res) => {
  96. wx.hideLoading();
  97. utils.setStorageSync(storeKeys.CARETOTAl, res.data.data.cartTotal);
  98. app.changeTabBarBadge(utils.getStorageSync(storeKeys.CARETOTAl));
  99. utils.toast(res.data.message)
  100. },
  101. error: (res) => {
  102. wx.hideLoading();
  103. utils.toast(res.data.message)
  104. },
  105. })
  106. }),
  107. //获取页面数据
  108. getPageData(noLoading) {
  109. wx.showLoading({
  110. title: "加载中",
  111. mask: true
  112. });
  113. http.request({
  114. url: api.URL + "/api/goods/list?sortStyle=" + this.data.sortStyle + "&sortSort=" + this.data.sortSort + "&page=" + this.data.page,
  115. token: utils.getStorageSync(storeKeys.TOKEN),
  116. method: 'GET',
  117. success: (res) => {
  118. let list = [];
  119. let tmpList = res.data.data.list.data;
  120. if (this.data.page == 1) {
  121. list = tmpList;
  122. } else {
  123. list = this.data.list.concat(tmpList);
  124. }
  125. this.setData({
  126. list,
  127. dataObj: res.data.data.list
  128. });
  129. setTimeout(() => {
  130. this.setData({
  131. isLoading: false
  132. });
  133. }, 300)
  134. wx.hideLoading();
  135. },
  136. error: (res) => {
  137. this.setData({
  138. isLoading: false
  139. });
  140. wx.hideLoading();
  141. }
  142. })
  143. },
  144. onShareAppMessage: function (e) {
  145. return {
  146. title: app.globalData.shareTitle,
  147. imageUrl: app.globalData.shareImage
  148. }
  149. },
  150. onShareTimeline: function (e) {
  151. return {
  152. title: app.globalData.shareTitle,
  153. imageUrl: app.globalData.shareImage
  154. }
  155. }
  156. })