goods.js 14 KB

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