goods.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. import { Router } from '../../utils/router.js'
  6. Router({
  7. data: {
  8. winHeight: "",
  9. id: 0,
  10. goods: {},
  11. gallery: [],
  12. attribute: [],
  13. issueList: [],
  14. comment: [],
  15. brand: {},
  16. specificationList: [],
  17. productList: [],
  18. goodsNums:0,
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. number: 1,
  23. checkedSpecText: '请选择规格数量',
  24. openAttr: false,
  25. selectId:'',
  26. noCollectImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_collect.png",
  27. hasCollectImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_collect_checked.png",
  28. collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_collect.png",
  29. collected:'',
  30. skuActiveItem:[],
  31. isfromBrand:false
  32. },
  33. getGoodsInfo: function () {
  34. let that = this;
  35. util.request(api.GoodsDetail, { id: that.data.id }).then(function (res) {
  36. if (res.errno === 0) {
  37. let goodsNums = 0
  38. res.data.productList.forEach(item=>{
  39. goodsNums+=item.goods_number
  40. })
  41. that.setData({
  42. goods: res.data.info,
  43. gallery: res.data.gallery,
  44. attribute: res.data.attribute,
  45. issueList: res.data.issue,
  46. comment: res.data.comment,
  47. brand: res.data.brand,
  48. specificationList: res.data.specificationList,
  49. productList: res.data.productList,
  50. userHasCollect: res.data.userHasCollect,
  51. goodsNums:goodsNums
  52. });
  53. //设置默认值
  54. that.setDefSpecInfo(that.data.specificationList);
  55. if (res.data.userHasCollect == 1) {
  56. that.setData({
  57. 'collectBackImage': that.data.hasCollectImage,
  58. collected: that.data.hasCollectImage
  59. });
  60. } else {
  61. that.setData({
  62. 'collectBackImage': that.data.noCollectImage,
  63. collected: that.data.hasCollectImage
  64. });
  65. }
  66. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  67. that.getGoodsRelated();
  68. }
  69. });
  70. },
  71. getGoodsRelated: function () {
  72. let that = this;
  73. util.request(api.GoodsRelated, { id: that.data.id }).then(function (res) {
  74. if (res.errno === 0) {
  75. that.setData({
  76. relatedGoods: res.data.goodsList,
  77. });
  78. }
  79. });
  80. },
  81. back:function(){
  82. wx.navigateBack({
  83. })
  84. },
  85. clickSkuValue: function (event) {
  86. let that = this;
  87. let specNameId = event.currentTarget.dataset.nameId;
  88. let specValueId = event.currentTarget.dataset.valueId;
  89. let picUrl = event.currentTarget.dataset.url;
  90. //判断是否可以点击
  91. //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  92. let _specificationList = this.data.specificationList;
  93. for (let i = 0; i < _specificationList.length; i++) {
  94. if (_specificationList[i].specification_id == specNameId) {
  95. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  96. if (_specificationList[i].valueList[j].id == specValueId) {
  97. //如果已经选中,则反选
  98. if (_specificationList[i].valueList[j].checked) {
  99. _specificationList[i].valueList[j].checked = false;
  100. } else {
  101. _specificationList[i].valueList[j].checked = true;
  102. }
  103. } else {
  104. _specificationList[i].valueList[j].checked = false;
  105. }
  106. }
  107. }
  108. }
  109. this.setData({
  110. 'specificationList': _specificationList,
  111. 'goods.list_pic_url': picUrl,
  112. selectId: specValueId
  113. });
  114. //重新计算spec改变后的信息
  115. this.changeSpecInfo();
  116. //重新计算哪些值不可以点击
  117. },
  118. //获取选中的规格信息
  119. getCheckedSpecValue: function () {
  120. let checkedValues = [];
  121. let _specificationList = this.data.specificationList;
  122. for (let i = 0; i < _specificationList.length; i++) {
  123. let _checkedObj = {
  124. nameId: _specificationList[i].specification_id,
  125. valueId: 0,
  126. valueText: ''
  127. };
  128. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  129. if (_specificationList[i].valueList[j].checked) {
  130. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  131. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  132. }
  133. else{
  134. this.setData({
  135. skuActiveItem : []
  136. })
  137. }
  138. }
  139. checkedValues.push(_checkedObj);
  140. }
  141. let goods_specification_ids = ''
  142. checkedValues.forEach(item=>{
  143. goods_specification_ids+=('_'+item.valueId)
  144. })
  145. let productItem = this.data.productList.find(item=>item.goods_specification_ids===goods_specification_ids) || []
  146. console.log(productItem)
  147. this.setData({
  148. skuActiveItem : productItem
  149. })
  150. return checkedValues;
  151. },
  152. //根据已选的值,计算其它值的状态
  153. setSpecValueStatus: function () {
  154. },
  155. //判断规格是否选择完整
  156. isCheckedAllSpec: function () {
  157. return !this.getCheckedSpecValue().some(function (v) {
  158. if (v.valueId == 0) {
  159. return true;
  160. }
  161. });
  162. },
  163. getCheckedSpecKey: function () {
  164. let checkedValue = this.getCheckedSpecValue().map(function (v) {
  165. return v.valueId;
  166. });
  167. return checkedValue.join('_');
  168. },
  169. changeSpecInfo: function () {
  170. let checkedNameValue = this.getCheckedSpecValue();
  171. //设置选择的信息
  172. let checkedValue = checkedNameValue.filter(function (v) {
  173. if (v.valueId != 0) {
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }).map(function (v) {
  179. return v.valueText;
  180. });
  181. if (checkedValue.length > 0) {
  182. this.setData({
  183. 'checkedSpecText': checkedValue.join(' ')
  184. });
  185. } else {
  186. this.setData({
  187. 'checkedSpecText': '请选择规格数量'
  188. });
  189. }
  190. },
  191. getCheckedProductItem: function (key) {
  192. console.log(this.data.productList, '1')
  193. return this.data.productList.filter(function (v) {
  194. console.log('----',v)
  195. console.log('--key--', key)
  196. if (String(v.goods_specification_ids).indexOf(key) > -1) {
  197. return true;
  198. } else {
  199. return false;
  200. }
  201. });
  202. },
  203. onLoad: function (options) {
  204. // 页面初始化 options为页面跳转所带来的参数
  205. this.setData({
  206. id: parseInt(options.id),
  207. isfromBrand: options.isBrand||false,
  208. imgServer: util.imgServer
  209. // id: 1181000
  210. });
  211. },
  212. onReady: function () {
  213. // 页面渲染完成
  214. },
  215. onShow: function () {
  216. var that = this;
  217. this.getGoodsInfo();
  218. util.request(api.CartGoodsCount).then(function (res) {
  219. if (res.errno === 0) {
  220. that.setData({
  221. cartGoodsCount: res.data.cartTotal.goodsCount
  222. });
  223. }
  224. });
  225. var that = this
  226. // 高度自适应
  227. wx.getSystemInfo({
  228. success: function (res) {
  229. var clientHeight = res.windowHeight,
  230. clientWidth = res.windowWidth,
  231. rpxR = 750 / clientWidth;
  232. var calc = clientHeight * rpxR - 100;
  233. that.setData({
  234. winHeight: calc
  235. });
  236. }
  237. });
  238. },
  239. onHide: function () {
  240. // 页面隐藏
  241. },
  242. onUnload: function () {
  243. // 页面关闭
  244. },
  245. switchAttrPop: function () {
  246. if (this.data.openAttr == false) {
  247. this.setData({
  248. openAttr: !this.data.openAttr,
  249. collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png"
  250. });
  251. }
  252. },
  253. closeAttrOrCollect: function () {
  254. let that = this;
  255. if (this.data.openAttr) {
  256. this.setData({
  257. openAttr: false,
  258. });
  259. if (that.data.userHasCollect == 1) {
  260. that.setData({
  261. 'collectBackImage': that.data.hasCollectImage,
  262. collected: that.data.hasCollectImage
  263. });
  264. } else {
  265. that.setData({
  266. 'collectBackImage': that.data.noCollectImage,
  267. collected: that.data.noCollectImage
  268. });
  269. }
  270. } else {
  271. //添加或是取消收藏
  272. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST", "application/json")
  273. .then(function (res) {
  274. let _res = res;
  275. if (_res.errno == 0) {
  276. if ( _res.data.type == 'add') {
  277. that.setData({
  278. 'collectBackImage': that.data.hasCollectImage,
  279. collected: that.data.hasCollectImage
  280. });
  281. wx.showToast({
  282. title: '收藏成功'
  283. })
  284. } else {
  285. that.setData({
  286. 'collectBackImage': that.data.noCollectImage,
  287. collected: that.data.noCollectImage
  288. });
  289. }
  290. } else {
  291. wx.showToast({
  292. image: 'https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_error.png',
  293. title: _res.errmsg,
  294. mask: true
  295. });
  296. }
  297. });
  298. }
  299. },
  300. openCartPage: function () {
  301. wx.switchTab({
  302. url: '/pages/cart/cart',
  303. });
  304. },
  305. /**
  306. * 直接购买
  307. */
  308. buyGoods: function () {
  309. var that = this;
  310. if (this.data.openAttr == false) {
  311. //打开规格选择窗口
  312. this.setData({
  313. openAttr: !this.data.openAttr,
  314. collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png"
  315. });
  316. } else {
  317. //提示选择完整规格
  318. if (!this.isCheckedAllSpec()) {
  319. wx.showToast({
  320. title: '请选择完整规格'
  321. });
  322. return false;
  323. }
  324. //根据选中的规格,判断是否有对应的sku信息
  325. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  326. if (!checkedProduct || checkedProduct.length <= 0) {
  327. //找不到对应的product信息,提示没有库存
  328. return false;
  329. }
  330. //验证库存
  331. if (checkedProduct.goods_number < this.data.number) {
  332. //找不到对应的product信息,提示没有库存
  333. return false;
  334. }
  335. // 直接购买商品
  336. util.request(api.BuyAdd, { goodsSpecificationIds:this.data.selectId, goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, "POST",'application/json')
  337. .then(function (res) {
  338. let _res = res;
  339. if (_res.errno == 0) {
  340. that.setData({
  341. openAttr: !that.data.openAttr,
  342. });
  343. wx.navigateTo({
  344. url: '/pages/shopping/checkout/checkout?isBuy=true',
  345. })
  346. } else {
  347. wx.showToast({
  348. image: 'https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/icon_error.png',
  349. title: _res.errmsg,
  350. mask: true
  351. });
  352. }
  353. });
  354. }
  355. },
  356. /**
  357. * 添加到购物车
  358. */
  359. addToCart: function () {
  360. var that = this;
  361. if (this.data.openAttr == false) {
  362. //打开规格选择窗口
  363. this.setData({
  364. openAttr: !this.data.openAttr,
  365. collectBackImage: "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/wxmall/images/detail_back.png"
  366. });
  367. } else {
  368. //提示选择完整规格
  369. if (!this.isCheckedAllSpec()) {
  370. util.showErrorToast('请选择完整规格');
  371. return false;
  372. }
  373. //根据选中的规格,判断是否有对应的sku信息
  374. let checkedProduct = this.getCheckedProductItem(this.getCheckedSpecKey());
  375. if (!checkedProduct || checkedProduct.length <= 0) {
  376. //找不到对应的product信息,提示没有库存
  377. return false;
  378. }
  379. //验证库存
  380. if (checkedProduct.goods_number < this.data.number) {
  381. //找不到对应的product信息,提示没有库存
  382. return false;
  383. }
  384. //添加到购物车
  385. util.request(api.CartAdd, { goodsSpecificationIds: this.data.selectId, goodsId: this.data.goods.id, number: this.data.number, productId: checkedProduct[0].id }, 'POST', 'application/json')
  386. .then(function (res) {
  387. let _res = res;
  388. if (_res.errno == 0) {
  389. wx.showToast({
  390. title: '添加成功'
  391. });
  392. that.setData({
  393. openAttr: !that.data.openAttr,
  394. collectBackImage: that.data.collected,
  395. cartGoodsCount: _res.data.cartTotal.goodsCount,
  396. number:1
  397. });
  398. // if (that.data.userHasCollect == 1) {
  399. // that.setData({
  400. // 'collectBackImage': that.data.hasCollectImage
  401. // });
  402. // } else {
  403. // that.setData({
  404. // 'collectBackImage': that.data.noCollectImage
  405. // });
  406. // }
  407. } else {
  408. return util.showErrorToast(_res.errmsg);
  409. }
  410. });
  411. }
  412. },
  413. cutNumber: function () {
  414. this.setData({
  415. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  416. });
  417. },
  418. addNumber: function () {
  419. if (this.data.number >= this.data.productList[0].goods_number)
  420. return util.showErrorToast('库存不足');
  421. this.setData({
  422. number: this.data.number + 1
  423. });
  424. },
  425. setDefSpecInfo: function (specificationList) {
  426. //未考虑规格联动情况
  427. let that = this;
  428. if (!specificationList)return;
  429. for (let i = 0; i < specificationList.length;i++){
  430. let specification = specificationList[i];
  431. let specNameId = specification.specification_id;
  432. //规格只有一个时自动选择规格
  433. if (specification.valueList && specification.valueList.length == 1){
  434. let specValueId = specification.valueList[0].id;
  435. that.clickSkuValue({ currentTarget: { dataset: { "nameId": specNameId, "valueId": specValueId } } });
  436. }
  437. }
  438. specificationList.map(function(item){
  439. });
  440. }
  441. })