list-vop.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. listType: 1, // 1为1个商品一行,2为2个商品一行
  9. name: '', // 搜索关键词
  10. orderBy: '', // 排序规则
  11. page: 1 // 读取第几页
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function (options) {
  17. this.setData({
  18. name: options.name,
  19. cid1: options.cid1,
  20. cid2: options.cid2
  21. })
  22. this.search()
  23. },
  24. /**
  25. * 生命周期函数--监听页面初次渲染完成
  26. */
  27. onReady: function () {
  28. },
  29. /**
  30. * 生命周期函数--监听页面显示
  31. */
  32. onShow: function () {
  33. },
  34. async search(){
  35. if (!this.data.cid1 && !this.data.cid2) {
  36. return
  37. }
  38. wx.showLoading({
  39. title: '加载中',
  40. })
  41. const _data = {
  42. sortType: this.data.orderBy,
  43. page: this.data.page,
  44. pageSize: 20,
  45. }
  46. if (this.data.name) {
  47. _data.keyword = this.data.name
  48. }
  49. if (this.data.cid1) {
  50. _data.cid1 = this.data.cid1
  51. }
  52. if (this.data.cid2) {
  53. _data.cid2 = this.data.cid2
  54. }
  55. const res = await WXAPI.jdvopGoodsList(_data)
  56. wx.hideLoading()
  57. if (res.code == 0) {
  58. res.data.result.forEach(ele => {
  59. ele.pic = res.data.imageDomain + ele.pic
  60. })
  61. if (this.data.page == 1) {
  62. this.setData({
  63. goods: res.data.result,
  64. })
  65. } else {
  66. this.setData({
  67. goods: this.data.goods.concat(res.data.result),
  68. })
  69. }
  70. } else {
  71. if (this.data.page == 1) {
  72. this.setData({
  73. goods: null,
  74. })
  75. } else {
  76. wx.showToast({
  77. title: '没有更多了',
  78. icon: 'none'
  79. })
  80. }
  81. }
  82. },
  83. /**
  84. * 生命周期函数--监听页面隐藏
  85. */
  86. onHide: function () {
  87. },
  88. /**
  89. * 生命周期函数--监听页面卸载
  90. */
  91. onUnload: function () {
  92. },
  93. /**
  94. * 页面相关事件处理函数--监听用户下拉动作
  95. */
  96. onPullDownRefresh: function () {
  97. },
  98. /**
  99. * 页面上拉触底事件的处理函数
  100. */
  101. onReachBottom: function () {
  102. this.setData({
  103. page: this.data.page + 1
  104. });
  105. this.search()
  106. },
  107. changeShowType(){
  108. if (this.data.listType == 1) {
  109. this.setData({
  110. listType: 2
  111. })
  112. } else {
  113. this.setData({
  114. listType: 1
  115. })
  116. }
  117. },
  118. bindinput(e){
  119. this.setData({
  120. name: e.detail.value
  121. })
  122. },
  123. bindconfirm(e){
  124. this.setData({
  125. page: 1,
  126. name: e.detail.value
  127. })
  128. this.search()
  129. },
  130. filter(e){
  131. this.setData({
  132. page: 1,
  133. orderBy: e.currentTarget.dataset.val
  134. })
  135. this.search()
  136. },
  137. async addShopCar(e) {
  138. const curGood = this.data.goods.find(ele => {
  139. return ele.id == e.currentTarget.dataset.id
  140. })
  141. if (!curGood) {
  142. return
  143. }
  144. if (curGood.stores <= 0) {
  145. wx.showToast({
  146. title: '已售罄~',
  147. icon: 'none'
  148. })
  149. return
  150. }
  151. this.addShopCarCheck({
  152. goodsId: curGood.id,
  153. buyNumber: 1,
  154. sku: []
  155. })
  156. },
  157. async addShopCarCheck(options) {
  158. AUTH.checkHasLogined().then(isLogined => {
  159. this.setData({
  160. wxlogin: isLogined
  161. })
  162. if (isLogined) {
  163. // 处理加入购物车的业务逻辑
  164. this.addShopCarDone(options)
  165. }
  166. })
  167. },
  168. async addShopCarDone(options) {
  169. const res = await WXAPI.shippingCarInfoAddItem(wx.getStorageSync('token'), options.goodsId, options.buyNumber, options.sku)
  170. if (res.code == 30002) {
  171. // 需要选择规格尺寸
  172. const skuCurGoodsRes = await WXAPI.goodsDetail(options.goodsId)
  173. if (skuCurGoodsRes.code != 0) {
  174. wx.showToast({
  175. title: skuCurGoodsRes.msg,
  176. icon: 'none'
  177. })
  178. return
  179. }
  180. const skuCurGoods = skuCurGoodsRes.data
  181. skuCurGoods.basicInfo.storesBuy = 1
  182. this.setData({
  183. skuCurGoods
  184. })
  185. return
  186. }
  187. if (res.code != 0) {
  188. wx.showToast({
  189. title: res.msg,
  190. icon: 'none'
  191. })
  192. return
  193. }
  194. wx.showToast({
  195. title: '加入成功',
  196. icon: 'success'
  197. })
  198. this.setData({
  199. skuCurGoods: null
  200. })
  201. },
  202. storesJia() {
  203. const skuCurGoods = this.data.skuCurGoods
  204. if (skuCurGoods.basicInfo.storesBuy < skuCurGoods.basicInfo.stores) {
  205. skuCurGoods.basicInfo.storesBuy++
  206. this.setData({
  207. skuCurGoods
  208. })
  209. }
  210. },
  211. storesJian() {
  212. const skuCurGoods = this.data.skuCurGoods
  213. if (skuCurGoods.basicInfo.storesBuy > 1) {
  214. skuCurGoods.basicInfo.storesBuy--
  215. this.setData({
  216. skuCurGoods
  217. })
  218. }
  219. },
  220. closeSku() {
  221. this.setData({
  222. skuCurGoods: null
  223. })
  224. wx.showTabBar()
  225. },
  226. skuSelect(e) {
  227. const pid = e.currentTarget.dataset.pid
  228. const id = e.currentTarget.dataset.id
  229. // 处理选中
  230. const skuCurGoods = this.data.skuCurGoods
  231. const property = skuCurGoods.properties.find(ele => { return ele.id == pid })
  232. property.childsCurGoods.forEach(ele => {
  233. if (ele.id == id) {
  234. ele.active = true
  235. } else {
  236. ele.active = false
  237. }
  238. })
  239. this.setData({
  240. skuCurGoods
  241. })
  242. },
  243. addCarSku() {
  244. const skuCurGoods = this.data.skuCurGoods
  245. const propertySize = skuCurGoods.properties.length // 有几组SKU
  246. const sku = []
  247. skuCurGoods.properties.forEach(p => {
  248. const o = p.childsCurGoods.find(ele => { return ele.active })
  249. if (!o) {
  250. return
  251. }
  252. sku.push({
  253. optionId: o.propertyId,
  254. optionValueId: o.id
  255. })
  256. })
  257. if (sku.length != propertySize) {
  258. wx.showToast({
  259. title: '请选择规格',
  260. icon: 'none'
  261. })
  262. return
  263. }
  264. const options = {
  265. goodsId: skuCurGoods.basicInfo.id,
  266. buyNumber: skuCurGoods.basicInfo.storesBuy,
  267. sku
  268. }
  269. this.addShopCarDone(options)
  270. },
  271. })