1
0

goods.js 14 KB

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