goods.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. if (res.data.userHasCollect == 1) {
  79. that.setData({
  80. 'collectBackImage': that.data.hasCollectImage
  81. });
  82. } else {
  83. that.setData({
  84. 'collectBackImage': that.data.noCollectImage
  85. });
  86. }
  87. WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  88. that.getGoodsRelated();
  89. that.getGoodsCrashList();
  90. //
  91. let _specificationList = that.data.specificationList;
  92. for (let i = 0; i < _specificationList.length; i++) {
  93. if (_specificationList[i].valueList.length == 1) {
  94. //如果已经选中,则反选,前端默认选中
  95. _specificationList[i].valueList[0].checked = true;
  96. }
  97. }
  98. that.setData({
  99. 'specificationList': _specificationList
  100. });
  101. }
  102. wx.hideLoading();
  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. wx.showToast({
  239. title: res.errmsg,
  240. icon: 'none'
  241. })
  242. }
  243. });
  244. },
  245. onShow: function () {
  246. // 页面显示
  247. },
  248. onHide: function () {
  249. // 页面隐藏
  250. },
  251. onUnload: function () {
  252. // 页面关闭
  253. },
  254. switchAttrPop: function () {
  255. this.setData({
  256. openAttr: !this.data.openAttr
  257. })
  258. },
  259. hideSwitchAttrPop: function() {
  260. this.setData({
  261. openAttr: false
  262. })
  263. },
  264. changeProperty: function (e) {
  265. var propertyName = e.currentTarget.dataset.propertyName;
  266. console.log(propertyName);
  267. },
  268. closeAttrOrCollect: function () {
  269. let that = this;
  270. //添加或是取消收藏
  271. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  272. .then(function (res) {
  273. let _res = res;
  274. if (_res.errno == 0) {
  275. if (_res.data.type == 'add') {
  276. that.setData({
  277. 'collectBackImage': that.data.hasCollectImage
  278. });
  279. wx.showToast({
  280. title: "收藏成功",
  281. mask: true
  282. });
  283. } else {
  284. that.setData({
  285. 'collectBackImage': that.data.noCollectImage
  286. });
  287. wx.showToast({
  288. title: "收藏取消",
  289. mask: true
  290. });
  291. }
  292. } else {
  293. wx.showToast({
  294. image: '/static/images/icon_error.png',
  295. title: _res.errmsg,
  296. mask: true
  297. });
  298. }
  299. });
  300. },
  301. openCartPage: function () {
  302. wx.switchTab({
  303. url: '/pages/cart/cart',
  304. });
  305. },
  306. addToCart: function () {
  307. var that = this;
  308. if (this.data.openAttr == false) {
  309. //打开规格选择窗口
  310. this.setData({
  311. openAttr: !this.data.openAttr
  312. });
  313. } else {
  314. //提示选择完整规格
  315. if (!goodsUtil.isCheckedAllSpec(that)) {
  316. return false;
  317. }
  318. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  319. // wx.showToast({
  320. // title: '库存不足',
  321. // mask: true
  322. // });
  323. util.showErrorToast('库存不足');
  324. //找不到对应的product信息,提示没有库存
  325. return false;
  326. }
  327. // //根据选中的规格,判断是否有对应的sku信息
  328. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  329. if (!checkedProduct || checkedProduct.length <= 0) {
  330. wx.showToast({
  331. title: '库存不足',
  332. icon: 'none'
  333. });
  334. //找不到对应的product信息,提示没有库存
  335. return false;
  336. }
  337. //验证库存
  338. // if (checkedProduct.goods_number < this.data.number) {
  339. // //找不到对应的product信息,提示没有库存
  340. // return false;
  341. // }
  342. //添加到购物车
  343. util.request(api.CartAdd, {
  344. goodsId: this.data.goods.id,
  345. number: this.data.number,
  346. productId: checkedProduct[0].id
  347. }, "POST")
  348. .then(function (res) {
  349. let _res = res;
  350. if (_res.errno == 0) {
  351. wx.showToast({
  352. title: '添加成功'
  353. });
  354. that.setData({
  355. openAttr: !that.data.openAttr,
  356. cartGoodsCount: _res.data.cartTotal.goodsCount
  357. });
  358. if (that.data.userHasCollect == 1) {
  359. that.setData({
  360. 'collectBackImage': that.data.hasCollectImage
  361. });
  362. } else {
  363. that.setData({
  364. 'collectBackImage': that.data.noCollectImage
  365. });
  366. }
  367. } else {
  368. wx.showToast({
  369. title: _res.errmsg,
  370. icon: 'none'
  371. })
  372. that.hideSwitchAttrPop();
  373. that.setData({
  374. stockNum: 0
  375. });
  376. }
  377. });
  378. }
  379. },
  380. cutNumber: function () {
  381. this.setData({
  382. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  383. });
  384. },
  385. addNumber: function () {
  386. this.setData({
  387. number: this.data.number + 1
  388. });
  389. },
  390. onShareAppMessage: function () {
  391. var that = this;
  392. // console.log("url:" + that.data.goods.list_pic_url);
  393. // var userId = wx.getStorageSync('userId');
  394. // console.log("userId:" + userId);
  395. return {
  396. title: '商业版',
  397. desc: null != that.data.goods.name ? that.data.goods.name : "商业版",
  398. imageUrl: that.data.goods.list_pic_url,
  399. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  400. success: function (res) {
  401. console.log("转发成功");
  402. // 转发成功
  403. },
  404. fail: function (res) {
  405. // 转发失败
  406. console.log("转发失败");
  407. }
  408. }
  409. },
  410. //购物车增加
  411. addCrashNumber: function (e) {
  412. let that = this;
  413. var goodsId = e.currentTarget.dataset.goodsId;
  414. var productId = e.currentTarget.dataset.productId;
  415. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  416. if (res.errno === 0 && null != res.data) {
  417. var hotGoods = that.data.hotGoods;
  418. hotGoods.forEach(function (val, index, arr) {
  419. if (val.id == goodsId) {
  420. val.cart_num = res.data;
  421. hotGoods[index] = val;
  422. that.setData({ hotGoods: hotGoods });
  423. }
  424. }, that);
  425. }
  426. });
  427. },
  428. previewPic(e) {
  429. let url = e.currentTarget.dataset.url;
  430. let urls = [];
  431. urls[0] = url;
  432. wx.previewImage({
  433. urls
  434. })
  435. },
  436. switchNav(event) {
  437. wx.switchTab({
  438. url: '/pages/index/index'
  439. });
  440. },
  441. //触摸事件start
  442. touchStart(e) {
  443. let startX = e.changedTouches[0].pageX
  444. this.setData({
  445. startX: startX
  446. })
  447. console.log("startX:" + startX);
  448. },
  449. //触摸事件end
  450. touchEnd(e) {
  451. let _self = this;
  452. let windowWidth = this.data.windowWidth;
  453. let moveWidth = e.changedTouches[0].pageX - this.data.startX;
  454. let defineWidth = windowWidth / 20;
  455. let rankList = this.data.gallery;
  456. let index_now = e.currentTarget.dataset.index;
  457. if (moveWidth >= defineWidth) {
  458. //上一张
  459. let transWidth = (index_now - 1) * (-this.data.windowWidth);
  460. if (index_now >= 1) {
  461. this.setData({
  462. indexNum: index_now - 1,
  463. rankList: rankList,
  464. leftWidth: transWidth
  465. })
  466. }
  467. } else if (moveWidth <= (0 - defineWidth)) {
  468. //下一张
  469. this.videoContext.pause();
  470. let transWidth = (index_now + 1) * (-this.data.windowWidth);
  471. if (index_now < rankList.length - 1) {
  472. this.setData({
  473. indexNum: index_now + 1,
  474. rankList: rankList,
  475. leftWidth: transWidth
  476. })
  477. }
  478. } else {
  479. //console.log('不能修改样式')
  480. }
  481. },
  482. videoPlay(e) {
  483. // if (this.data.curr_id == e.currentTarget.dataset.id) {
  484. // this.setData({
  485. // curr_id: 0,
  486. // })
  487. // } else {
  488. // this.setData({
  489. // curr_id: e.currentTarget.dataset.id,
  490. // })
  491. // }
  492. this.setData({
  493. imgHiddenName: true,
  494. videoHiddenName: false
  495. })
  496. this.videoContext.play();
  497. },
  498. //触摸事件start
  499. touchStart2(e) {
  500. },
  501. touchEnd2(e) {
  502. this.setData({
  503. curr_id: 0,
  504. imgHiddenName: false,
  505. videoHiddenName: true
  506. })
  507. this.videoContext.pause();
  508. }
  509. })