goods.js 13 KB

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