goods.js 17 KB

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