goods.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. let that = this;
  247. // 页面显示
  248. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  249. if (wx.getStorageSync('storeId')) {
  250. util.request(api.ChooseStoreId, {
  251. storeId: wx.getStorageSync('storeId'),
  252. merchSn: wx.getStorageSync('merchSn')
  253. }, 'POST').then(function (res) {
  254. if (res.errno === 0) {
  255. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  256. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  257. that.reLoad();
  258. }
  259. });
  260. }
  261. } else {
  262. wx.navigateTo({
  263. url: '/pages/auth/btnAuth/btnAuth',
  264. })
  265. }
  266. },
  267. onHide: function () {
  268. // 页面隐藏
  269. },
  270. onUnload: function () {
  271. // 页面关闭
  272. },
  273. switchAttrPop: function () {
  274. this.setData({
  275. openAttr: !this.data.openAttr
  276. })
  277. },
  278. hideSwitchAttrPop: function() {
  279. this.setData({
  280. openAttr: false
  281. })
  282. },
  283. reLoad: function () {
  284. let that = this;
  285. if (wx.getStorageSync('storeId')) {
  286. if (wx.getStorageSync('userId')) {
  287. wx.request({
  288. url: api.updateLoginUser,
  289. data: {
  290. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  291. },
  292. method: 'POST',
  293. header: {
  294. 'Content-Type': 'application/json'
  295. },
  296. success: function (wxRes) {
  297. if (wxRes.data.errno === 0) {
  298. // console.log("用户信息更新成功");
  299. }
  300. },
  301. fail: function (err) {
  302. console.log("failed");
  303. }
  304. });
  305. }
  306. }
  307. },
  308. changeProperty: function (e) {
  309. var propertyName = e.currentTarget.dataset.propertyName;
  310. console.log(propertyName);
  311. },
  312. closeAttrOrCollect: function () {
  313. let that = this;
  314. //添加或是取消收藏
  315. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  316. .then(function (res) {
  317. let _res = res;
  318. if (_res.errno == 0) {
  319. if (_res.data.type == 'add') {
  320. that.setData({
  321. 'collectBackImage': that.data.hasCollectImage
  322. });
  323. wx.showToast({
  324. title: "收藏成功",
  325. mask: true
  326. });
  327. } else {
  328. that.setData({
  329. 'collectBackImage': that.data.noCollectImage
  330. });
  331. wx.showToast({
  332. title: "收藏取消",
  333. mask: true
  334. });
  335. }
  336. } else {
  337. wx.showToast({
  338. image: '/static/images/icon_error.png',
  339. title: _res.errmsg,
  340. mask: true
  341. });
  342. }
  343. });
  344. },
  345. openCartPage: function () {
  346. wx.switchTab({
  347. url: '/pages/cart/cart',
  348. });
  349. },
  350. addToCart: function () {
  351. var that = this;
  352. if (this.data.openAttr == false) {
  353. //打开规格选择窗口
  354. this.setData({
  355. openAttr: !this.data.openAttr
  356. });
  357. } else {
  358. //提示选择完整规格
  359. if (!goodsUtil.isCheckedAllSpec(that)) {
  360. return false;
  361. }
  362. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  363. // wx.showToast({
  364. // title: '库存不足',
  365. // mask: true
  366. // });
  367. util.showErrorToast('库存不足');
  368. //找不到对应的product信息,提示没有库存
  369. return false;
  370. }
  371. // //根据选中的规格,判断是否有对应的sku信息
  372. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  373. if (!checkedProduct || checkedProduct.length <= 0) {
  374. wx.showToast({
  375. title: '库存不足',
  376. icon: 'none'
  377. });
  378. //找不到对应的product信息,提示没有库存
  379. return false;
  380. }
  381. //验证库存
  382. // if (checkedProduct.goods_number < this.data.number) {
  383. // //找不到对应的product信息,提示没有库存
  384. // return false;
  385. // }
  386. //添加到购物车
  387. util.request(api.CartAdd, {
  388. goodsId: this.data.goods.id,
  389. number: this.data.number,
  390. productId: checkedProduct[0].id
  391. }, "POST")
  392. .then(function (res) {
  393. let _res = res;
  394. if (_res.errno == 0) {
  395. wx.showToast({
  396. title: '添加成功'
  397. });
  398. that.setData({
  399. openAttr: !that.data.openAttr,
  400. cartGoodsCount: _res.data.cartTotal.goodsCount
  401. });
  402. if (that.data.userHasCollect == 1) {
  403. that.setData({
  404. 'collectBackImage': that.data.hasCollectImage
  405. });
  406. } else {
  407. that.setData({
  408. 'collectBackImage': that.data.noCollectImage
  409. });
  410. }
  411. } else {
  412. wx.showToast({
  413. title: _res.errmsg,
  414. icon: 'none'
  415. })
  416. that.hideSwitchAttrPop();
  417. // that.setData({
  418. // stockNum: 0
  419. // });
  420. }
  421. });
  422. }
  423. },
  424. cutNumber: function () {
  425. this.setData({
  426. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  427. });
  428. },
  429. addNumber: function () {
  430. this.setData({
  431. number: this.data.number + 1
  432. });
  433. },
  434. onShareAppMessage: function () {
  435. var that = this;
  436. // console.log("url:" + that.data.goods.list_pic_url);
  437. // var userId = wx.getStorageSync('userId');
  438. // console.log("userId:" + userId);
  439. return {
  440. title: '商业版',
  441. desc: null != that.data.goods.name ? that.data.goods.name : "商业版",
  442. imageUrl: that.data.goods.list_pic_url,
  443. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId'),
  444. success: function (res) {
  445. console.log("转发成功");
  446. // 转发成功
  447. },
  448. fail: function (res) {
  449. // 转发失败
  450. console.log("转发失败");
  451. }
  452. }
  453. },
  454. //购物车增加
  455. addCrashNumber: function (e) {
  456. let that = this;
  457. var goodsId = e.currentTarget.dataset.goodsId;
  458. var productId = e.currentTarget.dataset.productId;
  459. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  460. if (res.errno === 0 && null != res.data) {
  461. var hotGoods = that.data.hotGoods;
  462. hotGoods.forEach(function (val, index, arr) {
  463. if (val.id == goodsId) {
  464. val.cart_num = res.data;
  465. hotGoods[index] = val;
  466. that.setData({ hotGoods: hotGoods });
  467. }
  468. }, that);
  469. }
  470. });
  471. },
  472. previewPic(e) {
  473. let url = e.currentTarget.dataset.url;
  474. let urls = [];
  475. urls[0] = url;
  476. wx.previewImage({
  477. urls
  478. })
  479. },
  480. switchNav(event) {
  481. wx.switchTab({
  482. url: '/pages/index/index'
  483. });
  484. },
  485. //触摸事件start
  486. touchStart(e) {
  487. let startX = e.changedTouches[0].pageX
  488. this.setData({
  489. startX: startX
  490. })
  491. console.log("startX:" + startX);
  492. },
  493. //触摸事件end
  494. touchEnd(e) {
  495. let _self = this;
  496. let windowWidth = this.data.windowWidth;
  497. let moveWidth = e.changedTouches[0].pageX - this.data.startX;
  498. let defineWidth = windowWidth / 20;
  499. let rankList = this.data.gallery;
  500. let index_now = e.currentTarget.dataset.index;
  501. if (moveWidth >= defineWidth) {
  502. //上一张
  503. let transWidth = (index_now - 1) * (-this.data.windowWidth);
  504. if (index_now >= 1) {
  505. this.setData({
  506. indexNum: index_now - 1,
  507. rankList: rankList,
  508. leftWidth: transWidth
  509. })
  510. }
  511. } else if (moveWidth <= (0 - defineWidth)) {
  512. //下一张
  513. this.videoContext.pause();
  514. let transWidth = (index_now + 1) * (-this.data.windowWidth);
  515. if (index_now < rankList.length - 1) {
  516. this.setData({
  517. indexNum: index_now + 1,
  518. rankList: rankList,
  519. leftWidth: transWidth
  520. })
  521. }
  522. } else {
  523. //console.log('不能修改样式')
  524. }
  525. },
  526. videoPlay(e) {
  527. // if (this.data.curr_id == e.currentTarget.dataset.id) {
  528. // this.setData({
  529. // curr_id: 0,
  530. // })
  531. // } else {
  532. // this.setData({
  533. // curr_id: e.currentTarget.dataset.id,
  534. // })
  535. // }
  536. this.setData({
  537. imgHiddenName: true,
  538. videoHiddenName: false
  539. })
  540. this.videoContext.play();
  541. },
  542. //触摸事件start
  543. touchStart2(e) {
  544. },
  545. touchEnd2(e) {
  546. this.setData({
  547. curr_id: 0,
  548. imgHiddenName: false,
  549. videoHiddenName: true
  550. })
  551. this.videoContext.pause();
  552. }
  553. })