goods.js 17 KB

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