goods.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 goodsUtil = require('../../utils/goods.js');
  6. Page({
  7. data: {
  8. winHeight: "",
  9. id: 0,
  10. referrer: 0,
  11. goods: {},
  12. gallery: [],
  13. attribute: [],
  14. issueList: [],
  15. comment: [],
  16. brand: {},
  17. specificationList: [],
  18. productList: [],
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. crashList: {},
  23. number: 1,
  24. checkedSpecText: '请选择规格数量',
  25. openAttr: false,
  26. noCollectImage: "/static/images/icon_collect.png",
  27. hasCollectImage: "/static/images/icon_collect_checked.png",
  28. collectBackImage: "/static/images/icon_collect.png",
  29. showNavList: false,
  30. stockNum: '',
  31. cartNumber: 0,
  32. autoplay: false,
  33. defaultFreight: 0,
  34. current: 0,//banner当前的index
  35. isPlay: true,
  36. indexNum: 0,
  37. windowWidth: 0,
  38. leftWidth: 0,
  39. startX:0,
  40. curr_id: '',
  41. videoHiddenName: true,
  42. imgHiddenName: false
  43. },
  44. toggleNav() {
  45. this.setData({
  46. showNavList: !this.data.showNavList
  47. })
  48. },
  49. switchNav(event) {
  50. let name = event.currentTarget.dataset.name;
  51. wx.switchTab({
  52. url: `/pages/${name}/${name}`,
  53. });
  54. },
  55. getGoodsInfo: function () {
  56. wx.showLoading({
  57. title: '加载中...',
  58. });
  59. let that = this;
  60. util.request(api.GoodsDetail, { id: that.data.id, referrer: this.data.referrer,
  61. merchSn: wx.getStorageSync('merchSn') }).then(function (res) {
  62. if (res.errno === 0) {
  63. that.setData({
  64. goods: res.data.info,
  65. gallery: res.data.gallery,
  66. attribute: res.data.attribute,
  67. issueList: res.data.issue,
  68. comment: res.data.comment,
  69. brand: res.data.brand,
  70. specificationList: res.data.specificationList,
  71. productList: res.data.productList,
  72. userHasCollect: res.data.userHasCollect,
  73. stockNum: res.data.stockNum,
  74. cartNumber: res.data.cartNumber,
  75. defaultFreight: res.data.defaultFreight,
  76. checkedSpecText: res.data.specificationList[0].valueList[0].value
  77. });
  78. wx.hideLoading();
  79. if (res.data.userHasCollect == 1) {
  80. that.setData({
  81. 'collectBackImage': that.data.hasCollectImage
  82. });
  83. } else {
  84. that.setData({
  85. 'collectBackImage': that.data.noCollectImage
  86. });
  87. }
  88. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  89. that.getGoodsRelated();
  90. that.getGoodsCrashList();
  91. //
  92. let _specificationList = that.data.specificationList;
  93. for (let i = 0; i < _specificationList.length; i++) {
  94. if (_specificationList[i].valueList.length == 1) {
  95. //如果已经选中,则反选,前端默认选中
  96. _specificationList[i].valueList[0].checked = true;
  97. }
  98. }
  99. that.setData({
  100. 'specificationList': _specificationList
  101. });
  102. }
  103. });
  104. if (null != that.data.referrer && that.data.referrer > 0) {
  105. util.request(api.GoodsTransferCoupon, {
  106. goods_id: that.data.id,
  107. referrer: that.data.referrer, send_type: 2
  108. }).then(function (res) {
  109. });
  110. }
  111. util.request(api.GoodsGallery, {
  112. goods_id: that.data.id
  113. }).then(function (res) {
  114. if (res.errno === 0) {
  115. that.setData({
  116. gallery: res.data
  117. })
  118. }
  119. ;
  120. });
  121. },
  122. getGoodsCrashList: function () {
  123. let that = this;
  124. util.request(api.GoodsCrashList, { goodsId: that.data.id }).then(function (res) {
  125. if (res.errno === 0) {
  126. that.setData({
  127. crashList: res.data,
  128. });
  129. that.data.crashList.length;
  130. }
  131. });
  132. },
  133. getGoodsRelated: function () {
  134. let that = this;
  135. util.request(api.GoodsRelated, { id: that.data.id }).then(function (res) {
  136. if (res.errno === 0) {
  137. that.setData({
  138. relatedGoods: res.data.goodsList,
  139. });
  140. }
  141. });
  142. },
  143. clickSkuValue: function (event) {
  144. let that = this;
  145. let specNameId = event.currentTarget.dataset.nameId;
  146. let specValueId = event.currentTarget.dataset.valueId;
  147. //
  148. let _specificationList = this.data.specificationList;
  149. for (let i = 0; i < _specificationList.length; i++) {
  150. if (_specificationList[i].specification_id == specNameId) {
  151. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  152. if (_specificationList[i].valueList[j].id == specValueId) {
  153. //如果已经选中,则反选
  154. if (_specificationList[i].valueList[j].checked) {
  155. _specificationList[i].valueList[j].checked = false;
  156. } else {
  157. _specificationList[i].valueList[j].checked = true;
  158. }
  159. } else {
  160. _specificationList[i].valueList[j].checked = false;
  161. }
  162. }
  163. }
  164. }
  165. this.setData({
  166. 'specificationList': _specificationList
  167. });
  168. //重新计算spec改变后的信息
  169. goodsUtil.changeSpecInfo(that);
  170. },
  171. onLoad: function (options) {
  172. // 页面初始化 options为页面跳转所带来的参数
  173. this.setData({
  174. id: parseInt(options.id)
  175. // id: 1181000
  176. });
  177. var that = this;
  178. this.getGoodsInfo();
  179. util.request(api.CartGoodsCount).then(function (res) {
  180. if (res.errno === 0) {
  181. that.setData({
  182. cartGoodsCount: res.data.cartTotal.goodsCount
  183. });
  184. }
  185. });
  186. var that = this;
  187. // 高度自适应
  188. wx.getSystemInfo({
  189. success: function (res) {
  190. var clientHeight = res.windowHeight,
  191. clientWidth = res.windowWidth,
  192. rpxR = 750 / clientWidth;
  193. var calc = clientHeight * rpxR - 100;
  194. // console.log(calc);
  195. that.setData({
  196. winHeight: calc,
  197. windowWidth: res.windowWidth
  198. });
  199. }
  200. });
  201. },
  202. onReady: function () {
  203. let that = this;
  204. this.videoContext = wx.createVideoContext('myVideo');
  205. // 页面渲染完成
  206. // wx.setClipboardData({
  207. // data: '/pages/goods/goods?id=' + that.data.id,
  208. // success: function (res) {
  209. // wx.getClipboardData({
  210. // success: function (res) {
  211. // // console.log(res.data) // data
  212. // }
  213. // })
  214. // }
  215. // })
  216. },
  217. //购物车增加
  218. addNumber2: function (e) {
  219. let that = this;
  220. var goodsId = e.currentTarget.dataset.goodsId;
  221. var productId = e.currentTarget.dataset.productId;
  222. var hotGoods = that.data.hotGoods;
  223. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  224. if (res.errno === 0 && null != res.data) {
  225. util.request(api.CartGoodsCount).then(function (res) {
  226. if (res.errno === 0) {
  227. that.setData({
  228. cartGoodsCount: res.data.cartTotal.goodsCount
  229. });
  230. }
  231. });
  232. wx.showToast({
  233. title: '添加成功',
  234. icon: 'success',
  235. mask: true
  236. });
  237. } else {
  238. util.showErrorToast(res.errmsg)
  239. }
  240. });
  241. },
  242. onShow: function () {
  243. // 页面显示
  244. },
  245. onHide: function () {
  246. // 页面隐藏
  247. },
  248. onUnload: function () {
  249. // 页面关闭
  250. },
  251. switchAttrPop: function () {
  252. this.setData({
  253. openAttr: !this.data.openAttr
  254. })
  255. },
  256. hideSwitchAttrPop: function() {
  257. this.setData({
  258. openAttr: false
  259. })
  260. },
  261. changeProperty: function (e) {
  262. var propertyName = e.currentTarget.dataset.propertyName;
  263. console.log(propertyName);
  264. },
  265. closeAttrOrCollect: function () {
  266. let that = this;
  267. //添加或是取消收藏
  268. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  269. .then(function (res) {
  270. let _res = res;
  271. if (_res.errno == 0) {
  272. if (_res.data.type == 'add') {
  273. that.setData({
  274. 'collectBackImage': that.data.hasCollectImage
  275. });
  276. wx.showToast({
  277. title: "收藏成功",
  278. mask: true
  279. });
  280. } else {
  281. that.setData({
  282. 'collectBackImage': that.data.noCollectImage
  283. });
  284. wx.showToast({
  285. title: "收藏取消",
  286. mask: true
  287. });
  288. }
  289. } else {
  290. wx.showToast({
  291. image: '/static/images/icon_error.png',
  292. title: _res.errmsg,
  293. mask: true
  294. });
  295. }
  296. });
  297. },
  298. openCartPage: function () {
  299. wx.switchTab({
  300. url: '/pages/cart/cart',
  301. });
  302. },
  303. addToCart: function () {
  304. var that = this;
  305. if (this.data.openAttr == false) {
  306. //打开规格选择窗口
  307. this.setData({
  308. openAttr: !this.data.openAttr
  309. });
  310. } else {
  311. //提示选择完整规格
  312. if (!goodsUtil.isCheckedAllSpec(that)) {
  313. return false;
  314. }
  315. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  316. // wx.showToast({
  317. // title: '库存不足',
  318. // mask: true
  319. // });
  320. util.showErrorToast('库存不足');
  321. //找不到对应的product信息,提示没有库存
  322. return false;
  323. }
  324. // //根据选中的规格,判断是否有对应的sku信息
  325. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  326. if (!checkedProduct || checkedProduct.length <= 0) {
  327. wx.showToast({
  328. title: '库存不足',
  329. icon: 'none'
  330. });
  331. //找不到对应的product信息,提示没有库存
  332. return false;
  333. }
  334. //验证库存
  335. // if (checkedProduct.goods_number < this.data.number) {
  336. // //找不到对应的product信息,提示没有库存
  337. // return false;
  338. // }
  339. //添加到购物车
  340. util.request(api.CartAdd, {
  341. goodsId: this.data.goods.id,
  342. number: this.data.number,
  343. productId: checkedProduct[0].id
  344. }, "POST")
  345. .then(function (res) {
  346. let _res = res;
  347. if (_res.errno == 0) {
  348. wx.showToast({
  349. title: '添加成功'
  350. });
  351. that.setData({
  352. openAttr: !that.data.openAttr,
  353. cartGoodsCount: _res.data.cartTotal.goodsCount
  354. });
  355. if (that.data.userHasCollect == 1) {
  356. that.setData({
  357. 'collectBackImage': that.data.hasCollectImage
  358. });
  359. } else {
  360. that.setData({
  361. 'collectBackImage': that.data.noCollectImage
  362. });
  363. }
  364. } else {
  365. wx.showToast({
  366. title: _res.errmsg,
  367. icon: 'none'
  368. })
  369. }
  370. });
  371. }
  372. },
  373. cutNumber: function () {
  374. this.setData({
  375. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  376. });
  377. },
  378. addNumber: function () {
  379. this.setData({
  380. number: this.data.number + 1
  381. });
  382. },
  383. onShareAppMessage: function () {
  384. var that = this;
  385. // console.log("url:" + that.data.goods.list_pic_url);
  386. // var userId = wx.getStorageSync('userId');
  387. // console.log("userId:" + userId);
  388. return {
  389. title: '商业版',
  390. desc: null != that.data.goods.name ? that.data.goods.name : "商业版",
  391. imageUrl: that.data.goods.list_pic_url,
  392. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  393. success: function (res) {
  394. console.log("转发成功");
  395. // 转发成功
  396. },
  397. fail: function (res) {
  398. // 转发失败
  399. console.log("转发失败");
  400. }
  401. }
  402. },
  403. //购物车增加
  404. addCrashNumber: function (e) {
  405. let that = this;
  406. var goodsId = e.currentTarget.dataset.goodsId;
  407. var productId = e.currentTarget.dataset.productId;
  408. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  409. if (res.errno === 0 && null != res.data) {
  410. var hotGoods = that.data.hotGoods;
  411. hotGoods.forEach(function (val, index, arr) {
  412. if (val.id == goodsId) {
  413. val.cart_num = res.data;
  414. hotGoods[index] = val;
  415. that.setData({ hotGoods: hotGoods });
  416. }
  417. }, that);
  418. }
  419. });
  420. },
  421. previewPic(e) {
  422. let url = e.currentTarget.dataset.url;
  423. let urls = [];
  424. urls[0] = url;
  425. wx.previewImage({
  426. urls
  427. })
  428. },
  429. switchNav(event) {
  430. wx.switchTab({
  431. url: '/pages/index/index'
  432. });
  433. },
  434. //触摸事件start
  435. touchStart(e) {
  436. let startX = e.changedTouches[0].pageX
  437. this.setData({
  438. startX: startX
  439. })
  440. console.log("startX:" + startX);
  441. },
  442. //触摸事件end
  443. touchEnd(e) {
  444. let _self = this;
  445. let windowWidth = this.data.windowWidth;
  446. let moveWidth = e.changedTouches[0].pageX - this.data.startX;
  447. let defineWidth = windowWidth / 20;
  448. let rankList = this.data.gallery;
  449. let index_now = e.currentTarget.dataset.index;
  450. if (moveWidth >= defineWidth) {
  451. //上一张
  452. let transWidth = (index_now - 1) * (-this.data.windowWidth);
  453. if (index_now >= 1) {
  454. this.setData({
  455. indexNum: index_now - 1,
  456. rankList: rankList,
  457. leftWidth: transWidth
  458. })
  459. }
  460. } else if (moveWidth <= (0 - defineWidth)) {
  461. //下一张
  462. this.videoContext.pause();
  463. let transWidth = (index_now + 1) * (-this.data.windowWidth);
  464. if (index_now < rankList.length - 1) {
  465. this.setData({
  466. indexNum: index_now + 1,
  467. rankList: rankList,
  468. leftWidth: transWidth
  469. })
  470. }
  471. } else {
  472. //console.log('不能修改样式')
  473. }
  474. },
  475. videoPlay(e) {
  476. // if (this.data.curr_id == e.currentTarget.dataset.id) {
  477. // this.setData({
  478. // curr_id: 0,
  479. // })
  480. // } else {
  481. // this.setData({
  482. // curr_id: e.currentTarget.dataset.id,
  483. // })
  484. // }
  485. this.setData({
  486. imgHiddenName: true,
  487. videoHiddenName: false
  488. })
  489. this.videoContext.play();
  490. },
  491. //触摸事件start
  492. touchStart2(e) {
  493. },
  494. touchEnd2(e) {
  495. this.setData({
  496. curr_id: 0,
  497. imgHiddenName: false,
  498. videoHiddenName: true
  499. })
  500. this.videoContext.pause();
  501. }
  502. })