goods.js 15 KB

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