addGood.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. tabs: [{
  8. id: 0,
  9. name: '社区商品'
  10. },
  11. {
  12. id: 1,
  13. name: '仅快递'
  14. }
  15. ],
  16. currentIdx: 0,
  17. list: [],
  18. loadText: "加载中...",
  19. noData: 0,
  20. loadMore: true,
  21. checkedAll: false,
  22. checkedCount: 0
  23. },
  24. page: 1,
  25. keyword: '',
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function(options) {
  30. let currentIdx = options.type || 0;
  31. let that = this;
  32. this.setData({
  33. currentIdx
  34. }, ()=>{
  35. that.getData();
  36. })
  37. },
  38. initFn: function() {
  39. this.page = 1;
  40. this.setData({
  41. list: [],
  42. loadText: "加载中...",
  43. noData: 0,
  44. loadMore: true,
  45. checkedAll: false,
  46. checkedCount: 0
  47. },()=>{
  48. this.getData();
  49. })
  50. },
  51. /**
  52. * 切换导航
  53. */
  54. changeTabs: function(e) {
  55. let that = this;
  56. let currentIdx = e.currentTarget.dataset.type || 0;
  57. this.page = 1;
  58. this.setData({
  59. currentIdx,
  60. list: [],
  61. noData: 0,
  62. showEmpty: false,
  63. loadMore: true,
  64. loadOver: false
  65. }, () => {
  66. that.getData();
  67. })
  68. },
  69. /**
  70. * 获取列表
  71. */
  72. getData: function() {
  73. let that = this;
  74. let keyword = this.keyword || '';
  75. const token = wx.getStorageSync('token');
  76. app.util.request({
  77. url: 'entry/wxapp/index',
  78. data: {
  79. controller: 'solitaire.search_head_goodslist',
  80. token: token,
  81. page: this.page,
  82. is_only_express: this.data.currentIdx,
  83. keyword,
  84. is_soli: 1
  85. },
  86. dataType: 'json',
  87. success: function(res) {
  88. wx.hideLoading();
  89. if (res.data.code == 0) {
  90. let h = {};
  91. let list = res.data.data;
  92. if (list.length < 20) h.noMore = true;
  93. let oldList = that.data.list;
  94. list = oldList.concat(list);
  95. that.page++;
  96. that.setData({ list, ...h, checkedAll: false })
  97. } else if (res.data.code == 1) {
  98. // 无数据
  99. if (that.page == 1) that.setData({ noData: 1 })
  100. that.setData({ loadMore: false, noMore: false, loadText: "没有更多记录了~" })
  101. } else if (res.data.code == 2) {
  102. app.util.message('您还未登录', 'switchTo:/lionfish_comshop/pages/index/index', 'error');
  103. return;
  104. } else {
  105. app.util.message(res.data.msg, 'switchTo:/lionfish_comshop/pages/index/index', 'error');
  106. return;
  107. }
  108. }
  109. })
  110. },
  111. /**
  112. * selType: 0单选 1多选
  113. */
  114. selectGoods: function(t){
  115. let selType = t.currentTarget.dataset.type || 0;
  116. let currentIdx = this.data.currentIdx;
  117. var pages = getCurrentPages();
  118. var prevPage = pages[pages.length - 2]; //上一个页面
  119. let goods = prevPage.data.goods || [];
  120. let goodsItem = t.detail;
  121. if (selType==0) {
  122. if(goods.length>0) {
  123. let idx = goods.findIndex(item => { return (item.gid == goodsItem.gid) })
  124. if (idx === -1) goods.push(goodsItem);
  125. } else {
  126. goods.push(goodsItem);
  127. }
  128. } else {
  129. let list = this.data.list || [];
  130. let selGoods = list.filter(item => item.checked==1 );
  131. let newGoods = goods.concat(selGoods);
  132. let uniq = new Map()
  133. // 去重合并
  134. for (let i = 0; i < newGoods.length; i++) {
  135. let gid = newGoods[i].gid, val = newGoods[i];
  136. if (uniq.has(gid)) uniq.set(gid, val)
  137. else uniq.set(gid, val)
  138. }
  139. let res = [];
  140. // 放入数组
  141. for (let comb of uniq) {
  142. console.log(comb[1])
  143. res.push(comb[1])
  144. }
  145. goods = res;
  146. }
  147. prevPage.setData({
  148. goods,
  149. type: currentIdx
  150. })
  151. wx.navigateBack({
  152. delta: 1
  153. })
  154. },
  155. /**
  156. * 勾选
  157. */
  158. checkboxChange: function (e) {
  159. var type = e.currentTarget.dataset.type,
  160. idx = e.currentTarget.dataset.index,
  161. list = this.data.list,
  162. checkedAll = this.data.checkedAll;
  163. if ("all" === type) {
  164. let ck = 0;
  165. if (checkedAll) {
  166. list.forEach(function (item) {
  167. item.checked = 0;
  168. })
  169. } else {
  170. list.forEach(function (item) {
  171. item.checked = 1;
  172. })
  173. ck = list.length;
  174. }
  175. this.setData({
  176. checkedCount: ck,
  177. list,
  178. checkedAll: !checkedAll
  179. })
  180. } else if ("item" === type) {
  181. list.forEach(function (item, t) {
  182. if (idx == t) {
  183. if (item.checked) {
  184. item.checked = 0
  185. } else {
  186. item.checked = 1
  187. }
  188. }
  189. })
  190. var ck = 0;
  191. list.forEach(function (item) {
  192. if (item.checked) {
  193. ck++;
  194. }
  195. })
  196. this.setData({
  197. checkedCount: ck,
  198. list,
  199. checkedAll: ck == list.length ? true : false
  200. })
  201. }
  202. },
  203. goResult: function(e){
  204. let keyword = e.detail.value || '';
  205. (this.keyword = keyword), this.initFn();
  206. },
  207. /**
  208. * 页面相关事件处理函数--监听用户下拉动作
  209. */
  210. onPullDownRefresh: function() {
  211. },
  212. /**
  213. * 页面上拉触底事件的处理函数
  214. */
  215. onReachBottom: function() {
  216. if (!this.data.loadMore) return false;
  217. this.getData();
  218. }
  219. })