giftBagSize.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // subPackagesE/pages/giftBagSize/giftBagSize.js
  2. import {
  3. get,
  4. post
  5. } from '../../../utils/http'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. id: '', //礼盒ID
  12. giftBagList: [], //礼包分配列表
  13. selectGiftList: [], //选中的列表
  14. giftBagListOne: [], //不用分配列表
  15. selectSum: '', //选中的次数
  16. num: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad(options) {
  22. this.setData({
  23. id: options.id
  24. })
  25. // 获取礼包列表
  26. this.getGiftBagList()
  27. },
  28. /**
  29. * 生命周期函数--监听页面初次渲染完成
  30. */
  31. onReady() {
  32. },
  33. /**
  34. * 生命周期函数--监听页面显示
  35. */
  36. onShow() {
  37. },
  38. /**
  39. * 生命周期函数--监听页面隐藏
  40. */
  41. onHide() {
  42. },
  43. /**
  44. * 生命周期函数--监听页面卸载
  45. */
  46. onUnload() {
  47. },
  48. /**
  49. * 页面相关事件处理函数--监听用户下拉动作
  50. */
  51. onPullDownRefresh() {
  52. },
  53. /**
  54. * 页面上拉触底事件的处理函数
  55. */
  56. onReachBottom() {
  57. },
  58. /**
  59. * 用户点击右上角分享
  60. */
  61. onShareAppMessage() {
  62. },
  63. // 获取礼包列表
  64. getGiftBagList() {
  65. get('v2/api/package/info', {
  66. id: this.data.id
  67. }, (res => {
  68. let giftBagList = [] //分配
  69. let giftBagListOne = [] //不分配
  70. res.data.items.forEach(item => {
  71. if (item.type == 2) {
  72. // 添加选择次数
  73. item.selectNum = 0
  74. item.goods_list.forEach(item1 => {
  75. // 添加选择状态
  76. item1.status = false
  77. // 添加每个产品的数量
  78. item1.num = 0
  79. })
  80. giftBagList.push(item)
  81. } else {
  82. giftBagListOne.push(item)
  83. }
  84. })
  85. this.setData({
  86. giftBagList,
  87. giftBagListOne,
  88. giftBagInfo: res.data
  89. })
  90. // 将不用分配的添加到数组里
  91. let selectGiftList = this.data.selectGiftList
  92. this.data.giftBagListOne.forEach(item => {
  93. item.goods_list.forEach(itemA => {
  94. let list = {}
  95. list.item_id = item.id
  96. list.goods_id = itemA.id
  97. list.num = 1
  98. list.goods_name = itemA.goods_name
  99. list.cover_url = itemA.cover_url
  100. selectGiftList.push(list)
  101. this.setData({
  102. selectGiftList
  103. })
  104. })
  105. })
  106. }))
  107. },
  108. // 选择规格
  109. selectGift(e) {
  110. let itemA = e.currentTarget.dataset.itema //单个产品的信息
  111. let item = e.currentTarget.dataset.item //整个商品分类的信息
  112. // 判断是否是多选
  113. if(item.num>1){
  114. return
  115. }
  116. let indexA = e.currentTarget.dataset.indexa //产品index
  117. let index = e.currentTarget.dataset.index //分类index
  118. let selectGiftList = this.data.selectGiftList //选中的商品ID列表
  119. let itemSum = item.num //商品分类可选的最大次数
  120. // 超过最大次数返回
  121. if (item.selectNum == itemSum && !itemA.status) {
  122. wx.showToast({
  123. title: `你已选择${itemSum}次`,
  124. icon: 'none'
  125. })
  126. return
  127. }
  128. // 选中改变商品状态
  129. let giftBagStatus = `giftBagList[${index}].goods_list[${indexA}].status`
  130. this.setData({
  131. [giftBagStatus]: !this.data.giftBagList[index].goods_list[indexA].status
  132. })
  133. // 计算是否超过选择的最大次数
  134. let selectNum = `giftBagList[${index}].selectNum`
  135. let sum = 0
  136. this.data.giftBagList[index].goods_list.forEach(itemB => {
  137. if (itemB.status) {
  138. sum = ++sum
  139. }
  140. })
  141. this.setData({
  142. [selectNum]: sum
  143. })
  144. //添加选中的ID到列表中
  145. let list = {}
  146. let flag = false
  147. let selectIndex = ''
  148. selectGiftList.forEach((selectItem, Index) => {
  149. if (selectItem.goods_id == itemA.id) {
  150. flag = true
  151. selectIndex = Index
  152. }
  153. })
  154. if (!flag) {
  155. list.item_id = item.id
  156. list.goods_id = itemA.id
  157. list.num = 1
  158. list.goods_name = itemA.goods_name
  159. list.cover_url = itemA.cover_url
  160. selectGiftList.push(list)
  161. } else {
  162. selectGiftList.splice(selectIndex, 1)
  163. }
  164. this.setData({
  165. selectGiftList
  166. })
  167. },
  168. // 加产品
  169. add(e) {
  170. let index = e.currentTarget.dataset.index //分类的下标A
  171. let indexA = e.currentTarget.dataset.indexa //产品的下标
  172. let itemSum = e.currentTarget.dataset.item.num //每个分类的可选总次数
  173. let itemSelectNum = e.currentTarget.dataset.item.selectNum //每个分类已经选择的次数
  174. // 如果加购产品数量大于总次数就返回
  175. if (itemSelectNum + 1 > itemSum) {
  176. return
  177. }
  178. let giftBagNum = `giftBagList[${index}].goods_list[${indexA}].num`
  179. let selectNum = `giftBagList[${index}].selectNum`
  180. this.setData({
  181. [giftBagNum]: this.data.giftBagList[index].goods_list[indexA].num+1,
  182. [selectNum]:this.data.giftBagList[index].selectNum+1
  183. })
  184. let selectGiftList =[]
  185. this.data.giftBagList.forEach(item=>{
  186. item.goods_list.forEach(itemA=>{
  187. if(itemA.num>0){
  188. let list = {}
  189. list.item_id = item.id
  190. list.goods_id = itemA.id
  191. list.num = itemA.num
  192. list.goods_name = itemA.goods_name
  193. list.cover_url = itemA.cover_url
  194. selectGiftList.push(list)
  195. }
  196. })
  197. })
  198. this.setData({
  199. selectGiftList
  200. })
  201. },
  202. // 减产品
  203. reduce(e) {
  204. let index = e.currentTarget.dataset.index //分类的下标A
  205. let indexA = e.currentTarget.dataset.indexa //产品的下标
  206. let itemSelectNum = e.currentTarget.dataset.item.selectNum //每个分类已经选择的次数
  207. if ( this.data.giftBagList[index].goods_list[indexA].num == 0) {
  208. return
  209. }
  210. let giftBagNum = `giftBagList[${index}].goods_list[${indexA}].num`
  211. let selectNum = `giftBagList[${index}].selectNum`
  212. this.setData({
  213. [giftBagNum]: this.data.giftBagList[index].goods_list[indexA].num-1,
  214. [selectNum]:this.data.giftBagList[index].selectNum-1
  215. })
  216. },
  217. //确认选择
  218. affrimSelect() {
  219. // 判断是否分配完毕
  220. let flag = false
  221. this.data.giftBagList.forEach(item => {
  222. if (item.selectNum != item.num) {
  223. flag = true
  224. }
  225. })
  226. if (flag) {
  227. wx.showToast({
  228. title: '您还有商品没有选择完毕',
  229. icon: 'none'
  230. })
  231. return
  232. }
  233. wx.navigateTo({
  234. url: `/subPackagesE/pages/giftBagOrder/giftBagOrder?productList=${JSON.stringify(this.data.selectGiftList)}&&giftID=${this.data.id}`,
  235. })
  236. }
  237. })