goods.js 16 KB

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