category.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../utils/auth')
  3. const TOOLS = require('../../utils/tools.js') // TOOLS.showTabBarBadge();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. categories: [],
  10. activeCategory: 0,
  11. categorySelected: {
  12. name: '',
  13. id: ''
  14. },
  15. currentGoods: [],
  16. onLoadStatus: true,
  17. scrolltop: 0,
  18. skuCurGoods: undefined,
  19. page: 1,
  20. pageSize: 20
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function(options) {
  26. wx.showShareMenu({
  27. withShareTicket: true
  28. })
  29. this.categories();
  30. },
  31. async categories() {
  32. wx.showLoading({
  33. title: '',
  34. })
  35. const res = await WXAPI.goodsCategory()
  36. wx.hideLoading()
  37. let activeCategory = 0
  38. let categorySelected = this.data.categorySelected
  39. if (res.code == 0) {
  40. const categories = res.data
  41. categories.forEach(p => {
  42. p.childs = categories.filter(ele => {
  43. return p.id == ele.pid
  44. })
  45. })
  46. const firstCategories = categories.filter(ele => { return ele.level == 1 })
  47. if (this.data.categorySelected.id) {
  48. activeCategory = firstCategories.findIndex(ele => {
  49. return ele.id == this.data.categorySelected.id
  50. })
  51. categorySelected = firstCategories[activeCategory]
  52. } else {
  53. categorySelected = firstCategories[0]
  54. }
  55. this.setData({
  56. page: 1,
  57. activeCategory,
  58. categories,
  59. firstCategories,
  60. categorySelected
  61. })
  62. this.getGoodsList()
  63. }
  64. },
  65. async getGoodsList() {
  66. wx.showLoading({
  67. title: '',
  68. })
  69. // secondCategoryId
  70. let categoryId = ''
  71. if (this.data.secondCategoryId) {
  72. categoryId = this.data.secondCategoryId
  73. } else if(this.data.categorySelected.id) {
  74. categoryId = this.data.categorySelected.id
  75. }
  76. const res = await WXAPI.goods({
  77. categoryId,
  78. page: this.data.page,
  79. pageSize: this.data.pageSize
  80. })
  81. wx.hideLoading()
  82. if (res.code == 700) {
  83. if (this.data.page == 1) {
  84. this.setData({
  85. currentGoods: null
  86. });
  87. } else {
  88. wx.showToast({
  89. title: '没有更多了',
  90. icon: 'none'
  91. })
  92. }
  93. return
  94. }
  95. if (res.code != 0) {
  96. wx.showToast({
  97. title: res.msg,
  98. icon: 'none'
  99. })
  100. return
  101. }
  102. if (this.data.page == 1) {
  103. this.setData({
  104. currentGoods: res.data
  105. })
  106. } else {
  107. this.setData({
  108. currentGoods: this.data.currentGoods.concat(res.data)
  109. })
  110. }
  111. },
  112. onCategoryClick(e) {
  113. const idx = e.target.dataset.idx
  114. if (idx == this.data.activeCategory) {
  115. this.setData({
  116. scrolltop: 0,
  117. })
  118. return
  119. }
  120. this.setData({
  121. page: 1,
  122. secondCategoryId: '',
  123. activeCategory: idx,
  124. categorySelected: this.data.firstCategories[idx],
  125. scrolltop: 0
  126. });
  127. this.getGoodsList();
  128. },
  129. onSecondCategoryClick(e) {
  130. const idx = e.detail.index
  131. let secondCategoryId = ''
  132. if (idx) {
  133. // 点击了具体的分类
  134. secondCategoryId = this.data.categorySelected.childs[idx-1].id
  135. }
  136. this.setData({
  137. page: 1,
  138. secondCategoryId
  139. });
  140. this.getGoodsList();
  141. },
  142. bindconfirm(e) {
  143. this.setData({
  144. inputVal: e.detail
  145. })
  146. wx.navigateTo({
  147. url: '/pages/goods/list?name=' + this.data.inputVal,
  148. })
  149. },
  150. onShareAppMessage() {
  151. return {
  152. title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
  153. path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid')
  154. }
  155. },
  156. onShow() {
  157. AUTH.checkHasLogined().then(isLogined => {
  158. if (isLogined) {
  159. this.setData({
  160. wxlogin: isLogined
  161. })
  162. TOOLS.showTabBarBadge() // 获取购物车数据,显示TabBarBadge
  163. }
  164. })
  165. const _categoryId = wx.getStorageSync('_categoryId')
  166. wx.removeStorageSync('_categoryId')
  167. if (_categoryId) {
  168. this.data.categorySelected.id = _categoryId
  169. this.categories();
  170. }
  171. },
  172. async addShopCar(e) {
  173. const curGood = this.data.currentGoods.find(ele => {
  174. return ele.id == e.currentTarget.dataset.id
  175. })
  176. if (!curGood) {
  177. return
  178. }
  179. if (curGood.stores <= 0) {
  180. wx.showToast({
  181. title: '已售罄~',
  182. icon: 'none'
  183. })
  184. return
  185. }
  186. this.addShopCarCheck({
  187. goodsId: curGood.id,
  188. buyNumber: 1,
  189. sku: []
  190. })
  191. },
  192. async addShopCarCheck(options){
  193. AUTH.checkHasLogined().then(isLogined => {
  194. this.setData({
  195. wxlogin: isLogined
  196. })
  197. if (isLogined) {
  198. // 处理加入购物车的业务逻辑
  199. this.addShopCarDone(options)
  200. } else {
  201. AUTH.openLoginDialog()
  202. }
  203. })
  204. },
  205. async addShopCarDone(options){
  206. const res = await WXAPI.shippingCarInfoAddItem(wx.getStorageSync('token'), options.goodsId, options.buyNumber, options.sku)
  207. if (res.code == 30002) {
  208. // 需要选择规格尺寸
  209. const skuCurGoodsRes = await WXAPI.goodsDetail(options.goodsId)
  210. if (skuCurGoodsRes.code != 0) {
  211. wx.showToast({
  212. title: skuCurGoodsRes.msg,
  213. icon: 'none'
  214. })
  215. return
  216. }
  217. wx.hideTabBar()
  218. const skuCurGoods = skuCurGoodsRes.data
  219. skuCurGoods.basicInfo.storesBuy = 1
  220. this.setData({
  221. skuCurGoods,
  222. skuGoodsPic: skuCurGoods.basicInfo.pic,
  223. selectSizePrice: skuCurGoods.basicInfo.minPrice,
  224. selectSizeOPrice: skuCurGoods.basicInfo.originalPrice,
  225. skuCurGoodsShow: true
  226. })
  227. return
  228. }
  229. if (res.code != 0) {
  230. wx.showToast({
  231. title: res.msg,
  232. icon: 'none'
  233. })
  234. return
  235. }
  236. wx.showToast({
  237. title: '加入成功',
  238. icon: 'success'
  239. })
  240. this.setData({
  241. skuCurGoods: null,
  242. skuCurGoodsShow: false
  243. })
  244. wx.showTabBar()
  245. TOOLS.showTabBarBadge() // 获取购物车数据,显示TabBarBadge
  246. },
  247. storesJia(){
  248. const skuCurGoods = this.data.skuCurGoods
  249. if (skuCurGoods.basicInfo.storesBuy < skuCurGoods.basicInfo.stores) {
  250. skuCurGoods.basicInfo.storesBuy++
  251. this.setData({
  252. skuCurGoods
  253. })
  254. }
  255. },
  256. storesJian(){
  257. const skuCurGoods = this.data.skuCurGoods
  258. if (skuCurGoods.basicInfo.storesBuy > 1) {
  259. skuCurGoods.basicInfo.storesBuy--
  260. this.setData({
  261. skuCurGoods
  262. })
  263. }
  264. },
  265. closeSku(){
  266. this.setData({
  267. skuCurGoods: null,
  268. skuCurGoodsShow: false
  269. })
  270. wx.showTabBar()
  271. },
  272. skuSelect(e){
  273. const pid = e.currentTarget.dataset.pid
  274. const id = e.currentTarget.dataset.id
  275. // 处理选中
  276. const skuCurGoods = this.data.skuCurGoods
  277. const property = skuCurGoods.properties.find(ele => {return ele.id == pid})
  278. let child
  279. property.childsCurGoods.forEach(ele => {
  280. if (ele.id == id) {
  281. ele.active = true
  282. child = ele
  283. } else {
  284. ele.active = false
  285. }
  286. })
  287. // 显示图片
  288. let skuGoodsPic = this.data.skuGoodsPic
  289. if (skuCurGoods.subPics && skuCurGoods.subPics.length > 0) {
  290. const _subPic = skuCurGoods.subPics.find(ele => {
  291. return ele.optionValueId == child.id
  292. })
  293. if (_subPic) {
  294. skuGoodsPic = _subPic.pic
  295. }
  296. }
  297. this.setData({
  298. skuCurGoods,
  299. skuGoodsPic
  300. })
  301. // 计算价格
  302. this.calculateGoodsPrice()
  303. },
  304. async calculateGoodsPrice() {
  305. // 计算最终的商品价格
  306. let price = this.data.skuCurGoods.basicInfo.minPrice
  307. let originalPrice = this.data.skuCurGoods.basicInfo.originalPrice
  308. let totalScoreToPay = this.data.skuCurGoods.basicInfo.minScore
  309. let buyNumMax = this.data.skuCurGoods.basicInfo.stores
  310. let buyNumber = this.data.skuCurGoods.basicInfo.minBuyNumber
  311. // 计算 sku 价格
  312. const needSelectNum = this.data.skuCurGoods.properties.length
  313. let curSelectNum = 0;
  314. let propertyChildIds = "";
  315. let propertyChildNames = "";
  316. this.data.skuCurGoods.properties.forEach(p => {
  317. p.childsCurGoods.forEach(c => {
  318. if (c.active) {
  319. curSelectNum++;
  320. propertyChildIds = propertyChildIds + p.id + ":" + c.id + ",";
  321. propertyChildNames = propertyChildNames + p.name + ":" + c.name + " ";
  322. }
  323. })
  324. })
  325. let canSubmit = false;
  326. if (needSelectNum == curSelectNum) {
  327. canSubmit = true;
  328. }
  329. if (canSubmit) {
  330. const res = await WXAPI.goodsPrice(this.data.skuCurGoods.basicInfo.id, propertyChildIds)
  331. if (res.code == 0) {
  332. price = res.data.price
  333. originalPrice = res.data.originalPrice
  334. totalScoreToPay = res.data.score
  335. buyNumMax = res.data.stores
  336. }
  337. }
  338. this.setData({
  339. selectSizePrice: price,
  340. selectSizeOPrice: originalPrice,
  341. totalScoreToPay: totalScoreToPay,
  342. buyNumMax,
  343. buyNumber: (buyNumMax > buyNumber) ? buyNumber : 0
  344. });
  345. },
  346. addCarSku(){
  347. const skuCurGoods = this.data.skuCurGoods
  348. const propertySize = skuCurGoods.properties.length // 有几组SKU
  349. const sku = []
  350. skuCurGoods.properties.forEach(p => {
  351. const o = p.childsCurGoods.find(ele => {return ele.active})
  352. if (!o) {
  353. return
  354. }
  355. sku.push({
  356. optionId: o.propertyId,
  357. optionValueId: o.id
  358. })
  359. })
  360. if (sku.length != propertySize) {
  361. wx.showToast({
  362. title: '请选择规格',
  363. icon: 'none'
  364. })
  365. return
  366. }
  367. const options = {
  368. goodsId: skuCurGoods.basicInfo.id,
  369. buyNumber: skuCurGoods.basicInfo.storesBuy,
  370. sku
  371. }
  372. this.addShopCarDone(options)
  373. },
  374. processLogin(e) {
  375. if (!e.detail.userInfo) {
  376. wx.showToast({
  377. title: '已取消',
  378. icon: 'none',
  379. })
  380. return;
  381. }
  382. AUTH.register(this);
  383. },
  384. goodsGoBottom() {
  385. this.data.page++
  386. this.getGoodsList()
  387. },
  388. })