goods.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. import Notify from '../../components/notify/index';
  2. var app = getApp();
  3. var WxParse = require('../../lib/wxParse/wxParse.js');
  4. var util = require('../../utils/util.js');
  5. var api = require('../../config/api.js');
  6. var user = require('../../utils/user.js');
  7. Page({
  8. data: {
  9. id: 0,
  10. goods: {},
  11. groupon: [], //该商品支持的团购规格
  12. grouponLink: {}, //参与的团购
  13. attribute: [],
  14. issueList: [],
  15. comment: [],
  16. brand: {},
  17. specificationList: [],
  18. productList: [],
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. number: 1,
  23. checkedSpecText: '规格数量选择',
  24. tmpSpecText: '请选择规格数量',
  25. checkedSpecPrice: 0,
  26. openAttr: false,
  27. noCollectImage: '/static/images/icon_collect.png',
  28. hasCollectImage: '/static/images/icon_collect_checked.png',
  29. collectImage: '/static/images/icon_collect.png',
  30. shareImage: '',
  31. isGroupon: false, //标识是否是一个参团购买
  32. soldout: false
  33. },
  34. // 页面分享
  35. onShareAppMessage: function() {
  36. let that = this;
  37. return {
  38. title: that.data.goods.name,
  39. desc: '唯爱与美食不可辜负',
  40. path: '/pages/index/index?goodId=' + this.data.id
  41. }
  42. },
  43. showShare: function() {
  44. this.sharePop.togglePopup();
  45. },
  46. //从分享的团购进入
  47. getGrouponInfo: function(grouponId) {
  48. let that = this;
  49. util.request(api.GroupOnJoin, {
  50. grouponId: grouponId
  51. }).then(function(res) {
  52. if (res.errno === 0) {
  53. that.setData({
  54. grouponLink: res.data.groupon,
  55. id: res.data.goods.id
  56. });
  57. //获取商品详情
  58. that.getGoodsInfo();
  59. }
  60. });
  61. },
  62. // 获取商品信息
  63. getGoodsInfo: function() {
  64. wx.showLoading({
  65. title: '加载中',
  66. });
  67. setTimeout(function() {
  68. wx.hideLoading()
  69. }, 2000);
  70. let that = this;
  71. util.request(api.GoodsDetail, {
  72. id: that.data.id
  73. }).then(function(res) {
  74. if (res.errno === 0) {
  75. let _specificationList = res.data.specificationList
  76. // 如果仅仅存在一种货品,那么商品页面初始化时默认checked
  77. if (_specificationList.length == 1) {
  78. if (_specificationList[0].valueList.length == 1) {
  79. _specificationList[0].valueList[0].checked = true
  80. // 如果仅仅存在一种货品,那么商品价格应该和货品价格一致
  81. // 这里检测一下
  82. let _productPrice = res.data.productList[0].price;
  83. let _goodsPrice = res.data.info.retailPrice;
  84. if (_productPrice != _goodsPrice) {
  85. console.error('商品数量价格和货品不一致');
  86. }
  87. that.setData({
  88. checkedSpecText: _specificationList[0].valueList[0].value,
  89. tmpSpecText: '已选择:' + _specificationList[0].valueList[0].value,
  90. });
  91. }
  92. }
  93. that.setData({
  94. goods: res.data.info,
  95. attribute: res.data.attribute,
  96. issueList: res.data.issue,
  97. comment: res.data.comment,
  98. brand: res.data.brand,
  99. specificationList: res.data.specificationList,
  100. productList: res.data.productList,
  101. userHasCollect: res.data.userHasCollect,
  102. shareImage: res.data.shareImage,
  103. checkedSpecPrice: res.data.info.retailPrice,
  104. groupon: res.data.groupon
  105. });
  106. //如果是通过分享的团购参加团购,则团购项目应该与分享的一致并且不可更改
  107. if (that.data.isGroupon) {
  108. let groupons = that.data.groupon;
  109. for (var i = 0; i < groupons.length; i++) {
  110. if (groupons[i].id != that.data.grouponLink.rulesId) {
  111. groupons.splice(i, 1);
  112. }
  113. }
  114. groupons[0].checked = true;
  115. //重设团购规格
  116. that.setData({
  117. groupon: groupons
  118. });
  119. }
  120. if (res.data.userHasCollect == 1) {
  121. that.setData({
  122. collectImage: that.data.hasCollectImage
  123. });
  124. } else {
  125. that.setData({
  126. collectImage: that.data.noCollectImage
  127. });
  128. }
  129. WxParse.wxParse('goodsDetail', 'html', res.data.info.detail, that);
  130. //获取推荐商品
  131. that.getGoodsRelated();
  132. wx.hideLoading();
  133. }
  134. });
  135. },
  136. // 获取推荐商品
  137. getGoodsRelated: function() {
  138. let that = this;
  139. util.request(api.GoodsRelated, {
  140. id: that.data.id
  141. }).then(function(res) {
  142. if (res.errno === 0) {
  143. that.setData({
  144. relatedGoods: res.data.goodsList,
  145. });
  146. }
  147. });
  148. },
  149. // 团购选择
  150. clickGroupon: function(event) {
  151. let that = this;
  152. //参与团购,不可更改选择
  153. if (that.data.isGroupon) {
  154. return;
  155. }
  156. let specName = event.currentTarget.dataset.name;
  157. let specValueId = event.currentTarget.dataset.valueId;
  158. let _grouponList = this.data.groupon;
  159. for (let i = 0; i < _grouponList.length; i++) {
  160. if (_grouponList[i].id == specValueId) {
  161. if (_grouponList[i].checked) {
  162. _grouponList[i].checked = false;
  163. } else {
  164. _grouponList[i].checked = true;
  165. }
  166. } else {
  167. _grouponList[i].checked = false;
  168. }
  169. }
  170. this.setData({
  171. groupon: _grouponList,
  172. });
  173. },
  174. // 规格选择
  175. clickSkuValue: function(event) {
  176. let that = this;
  177. let specName = event.currentTarget.dataset.name;
  178. let specValueId = event.currentTarget.dataset.valueId;
  179. //判断是否可以点击
  180. //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  181. let _specificationList = this.data.specificationList;
  182. for (let i = 0; i < _specificationList.length; i++) {
  183. if (_specificationList[i].name === specName) {
  184. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  185. if (_specificationList[i].valueList[j].id == specValueId) {
  186. //如果已经选中,则反选
  187. if (_specificationList[i].valueList[j].checked) {
  188. _specificationList[i].valueList[j].checked = false;
  189. } else {
  190. _specificationList[i].valueList[j].checked = true;
  191. }
  192. } else {
  193. _specificationList[i].valueList[j].checked = false;
  194. }
  195. }
  196. }
  197. }
  198. this.setData({
  199. specificationList: _specificationList,
  200. });
  201. //重新计算spec改变后的信息
  202. this.changeSpecInfo();
  203. //重新计算哪些值不可以点击
  204. },
  205. //获取选中的团购信息
  206. getCheckedGrouponValue: function() {
  207. let checkedValues = {};
  208. let _grouponList = this.data.groupon;
  209. for (let i = 0; i < _grouponList.length; i++) {
  210. if (_grouponList[i].checked) {
  211. checkedValues = _grouponList[i];
  212. }
  213. }
  214. return checkedValues;
  215. },
  216. //获取选中的规格信息
  217. getCheckedSpecValue: function() {
  218. let checkedValues = [];
  219. let _specificationList = this.data.specificationList;
  220. for (let i = 0; i < _specificationList.length; i++) {
  221. let _checkedObj = {
  222. name: _specificationList[i].name,
  223. valueId: 0,
  224. valueText: ''
  225. };
  226. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  227. if (_specificationList[i].valueList[j].checked) {
  228. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  229. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  230. }
  231. }
  232. checkedValues.push(_checkedObj);
  233. }
  234. return checkedValues;
  235. },
  236. //判断规格是否选择完整
  237. isCheckedAllSpec: function() {
  238. return !this.getCheckedSpecValue().some(function(v) {
  239. if (v.valueId == 0) {
  240. return true;
  241. }
  242. });
  243. },
  244. soldoutNotify: function() {
  245. Notify("商品已售完");
  246. },
  247. getCheckedSpecKey: function() {
  248. let checkedValue = this.getCheckedSpecValue().map(function(v) {
  249. return v.valueText;
  250. });
  251. return checkedValue;
  252. },
  253. // 规格改变时,重新计算价格及显示信息
  254. changeSpecInfo: function() {
  255. let checkedNameValue = this.getCheckedSpecValue();
  256. //设置选择的信息
  257. let checkedValue = checkedNameValue.filter(function(v) {
  258. if (v.valueId != 0) {
  259. return true;
  260. } else {
  261. return false;
  262. }
  263. }).map(function(v) {
  264. return v.valueText;
  265. });
  266. if (checkedValue.length > 0) {
  267. this.setData({
  268. tmpSpecText: checkedValue.join(' ')
  269. });
  270. } else {
  271. this.setData({
  272. tmpSpecText: '请选择规格数量'
  273. });
  274. }
  275. if (this.isCheckedAllSpec()) {
  276. this.setData({
  277. checkedSpecText: this.data.tmpSpecText
  278. });
  279. // 规格所对应的货品选择以后
  280. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  281. if (!checkedProductArray || checkedProductArray.length <= 0) {
  282. this.setData({
  283. soldout: true
  284. });
  285. this.soldoutNotify();
  286. console.error('规格所对应货品不存在');
  287. return;
  288. }
  289. let checkedProduct = checkedProductArray[0];
  290. if (checkedProduct.number > 0) {
  291. this.setData({
  292. checkedSpecPrice: checkedProduct.price,
  293. soldout: false
  294. });
  295. this.notify.hide();
  296. } else {
  297. this.setData({
  298. checkedSpecPrice: this.data.goods.retailPrice,
  299. soldout: true
  300. });
  301. this.soldoutNotify();
  302. }
  303. } else {
  304. this.setData({
  305. checkedSpecText: '规格数量选择',
  306. checkedSpecPrice: this.data.goods.retailPrice,
  307. soldout: false
  308. });
  309. this.notify.hide();
  310. }
  311. },
  312. // 获取选中的产品(根据规格)
  313. getCheckedProductItem: function(key) {
  314. return this.data.productList.filter(function(v) {
  315. if (v.specifications.toString() == key.toString()) {
  316. return true;
  317. } else {
  318. return false;
  319. }
  320. });
  321. },
  322. onLoad: function(options) {
  323. // 页面初始化 options为页面跳转所带来的参数
  324. if (options.id) {
  325. this.setData({
  326. id: parseInt(options.id)
  327. });
  328. this.getGoodsInfo();
  329. }
  330. if (options.grouponId) {
  331. this.setData({
  332. isGroupon: true,
  333. });
  334. this.getGrouponInfo(options.grouponId);
  335. }
  336. },
  337. onShow: function() {
  338. // 页面显示
  339. var that = this;
  340. util.request(api.CartGoodsCount).then(function(res) {
  341. if (res.errno === 0) {
  342. that.setData({
  343. cartGoodsCount: res.data
  344. });
  345. }
  346. });
  347. },
  348. //添加或是取消收藏
  349. addCollectOrNot: function() {
  350. let that = this;
  351. util.request(api.CollectAddOrDelete, {
  352. type: 0,
  353. valueId: this.data.id
  354. }, "POST")
  355. .then(function(res) {
  356. let _res = res;
  357. if (_res.errno == 0) {
  358. if (_res.data.type == 'add') {
  359. that.setData({
  360. collectImage: that.data.hasCollectImage
  361. });
  362. } else {
  363. that.setData({
  364. collectImage: that.data.noCollectImage
  365. });
  366. }
  367. } else {
  368. wx.showToast({
  369. image: '/static/images/icon_error.png',
  370. title: _res.errmsg,
  371. mask: true
  372. });
  373. }
  374. });
  375. },
  376. //立即购买(先自动加入购物车)
  377. addFast: function() {
  378. var that = this;
  379. if (this.data.openAttr == false) {
  380. //打开规格选择窗口
  381. this.setData({
  382. openAttr: !this.data.openAttr
  383. });
  384. } else {
  385. //提示选择完整规格
  386. if (!this.isCheckedAllSpec()) {
  387. wx.showToast({
  388. image: '/static/images/icon_error.png',
  389. title: '请选择完整规格'
  390. });
  391. return false;
  392. }
  393. //根据选中的规格,判断是否有对应的sku信息
  394. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  395. if (!checkedProductArray || checkedProductArray.length <= 0) {
  396. //找不到对应的product信息,提示没有库存
  397. wx.showToast({
  398. image: '/static/images/icon_error.png',
  399. title: '没有库存'
  400. });
  401. return false;
  402. }
  403. let checkedProduct = checkedProductArray[0];
  404. //验证库存
  405. if (checkedProduct.number <= 0) {
  406. wx.showToast({
  407. image: '/static/images/icon_error.png',
  408. title: '没有库存'
  409. });
  410. return false;
  411. }
  412. //验证团购是否有效
  413. let checkedGroupon = this.getCheckedGrouponValue();
  414. //立即购买
  415. util.request(api.CartFastAdd, {
  416. goodsId: this.data.goods.id,
  417. number: this.data.number,
  418. productId: checkedProduct.id
  419. }, "POST")
  420. .then(function(res) {
  421. if (res.errno == 0) {
  422. // 如果storage中设置了cartId,则是立即购买,否则是购物车购买
  423. try {
  424. wx.setStorageSync('cartId', res.data);
  425. wx.setStorageSync('grouponRulesId', checkedGroupon.id);
  426. wx.setStorageSync('grouponLinkId', that.data.grouponLink.id);
  427. wx.navigateTo({
  428. url: '/pages/shopping/checkout/checkout'
  429. })
  430. } catch (e) {}
  431. } else {
  432. wx.showToast({
  433. image: '/static/images/icon_error.png',
  434. title: res.errmsg,
  435. mask: true
  436. });
  437. }
  438. });
  439. }
  440. },
  441. //添加到购物车
  442. addToCart: function() {
  443. var that = this;
  444. if (this.data.openAttr == false) {
  445. //打开规格选择窗口
  446. this.setData({
  447. openAttr: !this.data.openAttr
  448. });
  449. } else {
  450. //提示选择完整规格
  451. if (!this.isCheckedAllSpec()) {
  452. wx.showToast({
  453. image: '/static/images/icon_error.png',
  454. title: '请选择完整规格'
  455. });
  456. return false;
  457. }
  458. //根据选中的规格,判断是否有对应的sku信息
  459. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  460. if (!checkedProductArray || checkedProductArray.length <= 0) {
  461. //找不到对应的product信息,提示没有库存
  462. wx.showToast({
  463. image: '/static/images/icon_error.png',
  464. title: '没有库存'
  465. });
  466. return false;
  467. }
  468. let checkedProduct = checkedProductArray[0];
  469. //验证库存
  470. if (checkedProduct.number <= 0) {
  471. wx.showToast({
  472. image: '/static/images/icon_error.png',
  473. title: '没有库存'
  474. });
  475. return false;
  476. }
  477. //添加到购物车
  478. util.request(api.CartAdd, {
  479. goodsId: this.data.goods.id,
  480. number: this.data.number,
  481. productId: checkedProduct.id
  482. }, "POST")
  483. .then(function(res) {
  484. let _res = res;
  485. if (_res.errno == 0) {
  486. wx.showToast({
  487. title: '添加成功'
  488. });
  489. that.setData({
  490. openAttr: !that.data.openAttr,
  491. cartGoodsCount: _res.data
  492. });
  493. if (that.data.userHasCollect == 1) {
  494. that.setData({
  495. collectImage: that.data.hasCollectImage
  496. });
  497. } else {
  498. that.setData({
  499. collectImage: that.data.noCollectImage
  500. });
  501. }
  502. } else {
  503. wx.showToast({
  504. image: '/static/images/icon_error.png',
  505. title: _res.errmsg,
  506. mask: true
  507. });
  508. }
  509. });
  510. }
  511. },
  512. cutNumber: function() {
  513. this.setData({
  514. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  515. });
  516. },
  517. addNumber: function() {
  518. this.setData({
  519. number: this.data.number + 1
  520. });
  521. },
  522. onHide: function() {
  523. // 页面隐藏
  524. },
  525. onUnload: function() {
  526. // 页面关闭
  527. },
  528. switchAttrPop: function() {
  529. if (this.data.openAttr == false) {
  530. this.setData({
  531. openAttr: !this.data.openAttr
  532. });
  533. }
  534. },
  535. closeAttr: function() {
  536. this.setData({
  537. openAttr: false,
  538. });
  539. },
  540. openCartPage: function() {
  541. wx.switchTab({
  542. url: '/pages/cart/cart'
  543. });
  544. },
  545. onReady: function() {
  546. // 页面渲染完成
  547. this.sharePop = this.selectComponent("#sharePop");
  548. this.notify = this.selectComponent("#van-notify");
  549. },
  550. // 下拉刷新
  551. onPullDownRefresh() {
  552. wx.showNavigationBarLoading() //在标题栏中显示加载
  553. this.getGoodsInfo();
  554. wx.hideNavigationBarLoading() //完成停止加载
  555. wx.stopPullDownRefresh() //停止下拉刷新
  556. },
  557. //根据已选的值,计算其它值的状态
  558. setSpecValueStatus: function() {},
  559. })