list.js 5.5 KB

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