goods.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. page: 1,
  44. size: 4,
  45. detailContent: '',
  46. storeId: '',
  47. detailStoreId: ''
  48. },
  49. toggleNav() {
  50. this.setData({
  51. showNavList: !this.data.showNavList
  52. })
  53. },
  54. switchNav(event) {
  55. let name = event.currentTarget.dataset.name;
  56. wx.switchTab({
  57. url: `/pages/${name}/${name}`,
  58. });
  59. },
  60. //小程序里的转义方法
  61. escape2Html: function (str) {
  62. var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
  63. return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
  64. },
  65. //调用在需要的地方直接调用即可。如:this.escape2Html(contents)
  66. getGoodsInfo: function () {
  67. wx.showLoading({
  68. title: '加载中...',
  69. });
  70. setTimeout(function () {
  71. }, 350)
  72. let that = this;
  73. const regex = new RegExp('<img', 'gi');
  74. console.log('分享的storeId'+ wx.getStorageSync('storeId'))
  75. util.request(api.GoodsDetail, { id: that.data.id, referrer: this.data.referrer,
  76. merchSn: wx.getStorageSync('merchSn'), storeId: wx.getStorageSync('storeId') }).then(function (res) {
  77. if (res.errno === 0) {
  78. that.setData({
  79. goods: res.data.info,
  80. gallery: res.data.gallery,
  81. attribute: res.data.attribute,
  82. issueList: res.data.issue,
  83. comment: res.data.comment,
  84. brand: res.data.brand,
  85. specificationList: res.data.specificationList,
  86. productList: res.data.productList,
  87. userHasCollect: res.data.userHasCollect,
  88. stockNum: res.data.stockNum,
  89. cartNumber: res.data.cartNumber,
  90. defaultFreight: res.data.defaultFreight,
  91. checkedSpecText: res.data.specificationList[0].valueList[0].value,
  92. detailContent: that.escape2Html(res.data.info.goods_desc).replace(regex, `<img style="width: 100%;"`),
  93. detailStoreId:res.data.info.storeId
  94. });
  95. if (res.data.userHasCollect == 1) {
  96. that.setData({
  97. 'collectBackImage': that.data.hasCollectImage
  98. });
  99. } else {
  100. that.setData({
  101. 'collectBackImage': that.data.noCollectImage
  102. });
  103. }
  104. // WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  105. that.getGoodsRelated();
  106. that.getGoodsCrashList();
  107. //
  108. let _specificationList = that.data.specificationList;
  109. for (let i = 0; i < _specificationList.length; i++) {
  110. if (_specificationList[i].valueList.length == 1) {
  111. //如果已经选中,则反选,前端默认选中
  112. _specificationList[i].valueList[0].checked = true;
  113. }
  114. }
  115. that.setData({
  116. 'specificationList': _specificationList
  117. });
  118. }
  119. wx.hideLoading();
  120. });
  121. if (null != that.data.referrer && that.data.referrer > 0) {
  122. util.request(api.GoodsTransferCoupon, {
  123. goods_id: that.data.id,
  124. referrer: that.data.referrer, send_type: 2
  125. }).then(function (res) {
  126. });
  127. }
  128. util.request(api.GoodsGallery, {
  129. goods_id: that.data.id
  130. }).then(function (res) {
  131. if (res.errno === 0) {
  132. that.setData({
  133. gallery: res.data
  134. })
  135. }
  136. ;
  137. });
  138. },
  139. getGoodsCrashList: function () {
  140. let that = this;
  141. util.request(api.GoodsCrashList, { goodsId: that.data.id }).then(function (res) {
  142. if (res.errno === 0) {
  143. that.setData({
  144. crashList: res.data,
  145. });
  146. that.data.crashList.length;
  147. }
  148. });
  149. },
  150. clickSkuValue: function (event) {
  151. let that = this;
  152. let specNameId = event.currentTarget.dataset.nameId;
  153. let specValueId = event.currentTarget.dataset.valueId;
  154. //
  155. let _specificationList = this.data.specificationList;
  156. for (let i = 0; i < _specificationList.length; i++) {
  157. if (_specificationList[i].specification_id == specNameId) {
  158. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  159. if (_specificationList[i].valueList[j].id == specValueId) {
  160. //如果已经选中,则反选
  161. if (_specificationList[i].valueList[j].checked) {
  162. _specificationList[i].valueList[j].checked = false;
  163. } else {
  164. _specificationList[i].valueList[j].checked = true;
  165. }
  166. } else {
  167. _specificationList[i].valueList[j].checked = false;
  168. }
  169. }
  170. }
  171. }
  172. this.setData({
  173. 'specificationList': _specificationList
  174. });
  175. //重新计算spec改变后的信息
  176. goodsUtil.changeSpecInfo(that);
  177. },
  178. onLoad: function (options) {
  179. // 页面初始化 options为页面跳转所带来的参数
  180. this.setData({
  181. id: parseInt(options.id),
  182. storeId: options.storeId
  183. // id: 1181000
  184. });
  185. if(options.storeId){
  186. wx.setStorageSync('storeId', options.storeId);
  187. wx.setStorageSync('isShare', 'true');
  188. }
  189. console.log('分享的storeId1:' + wx.getStorageSync('storeId'))
  190. var that = this;
  191. // 高度自适应
  192. wx.getSystemInfo({
  193. success: function (res) {
  194. var clientHeight = res.windowHeight,
  195. clientWidth = res.windowWidth,
  196. rpxR = 750 / clientWidth;
  197. var calc = clientHeight * rpxR - 100;
  198. // console.log(calc);
  199. that.setData({
  200. winHeight: calc,
  201. windowWidth: res.windowWidth
  202. });
  203. }
  204. });
  205. },
  206. onReady: function () {
  207. let that = this;
  208. this.videoContext = wx.createVideoContext('myVideo');
  209. // 页面渲染完成
  210. // wx.setClipboardData({
  211. // data: '/pages/goods/goods?id=' + that.data.id,
  212. // success: function (res) {
  213. // wx.getClipboardData({
  214. // success: function (res) {
  215. // // console.log(res.data) // data
  216. // }
  217. // })
  218. // }
  219. // })
  220. },
  221. //购物车增加
  222. addNumber2: function (e) {
  223. let that = this;
  224. var goodsId = e.currentTarget.dataset.goodsId;
  225. var productId = e.currentTarget.dataset.productId;
  226. var hotGoods = that.data.hotGoods;
  227. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  228. if (res.errno === 0 && null != res.data) {
  229. util.request(api.CartGoodsCount).then(function (res) {
  230. if (res.errno === 0) {
  231. that.setData({
  232. cartGoodsCount: res.data.cartTotal.goodsCount
  233. });
  234. }
  235. });
  236. wx.showToast({
  237. title: '添加成功',
  238. icon: 'success',
  239. mask: true
  240. });
  241. } else {
  242. wx.showToast({
  243. title: res.errmsg,
  244. icon: 'none'
  245. })
  246. }
  247. });
  248. },
  249. onShow: function () {
  250. let that = this;
  251. // 页面显示
  252. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  253. if (wx.getStorageSync('storeId')) {
  254. util.request(api.ChooseStoreId, {
  255. storeId: wx.getStorageSync('storeId'),
  256. merchSn: wx.getStorageSync('merchSn')
  257. }, 'POST').then(function (res) {
  258. if (res.errno === 0) {
  259. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  260. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  261. that.reLoad();
  262. that.getGoodsInfo();
  263. util.request(api.CartGoodsCount, { storeId: wx.getStorageSync('storeId') }).then(function (res) {
  264. if (res.errno === 0) {
  265. that.setData({
  266. cartGoodsCount: res.data.cartTotal.goodsCount
  267. });
  268. }
  269. });
  270. }
  271. });
  272. }
  273. } else {
  274. wx.navigateTo({
  275. url: '/pages/auth/btnAuth/btnAuth',
  276. })
  277. }
  278. },
  279. onHide: function () {
  280. // 页面隐藏
  281. },
  282. onUnload: function () {
  283. // 页面关闭
  284. },
  285. switchAttrPop: function () {
  286. this.setData({
  287. openAttr: !this.data.openAttr
  288. })
  289. },
  290. hideSwitchAttrPop: function() {
  291. this.setData({
  292. openAttr: false
  293. })
  294. },
  295. reLoad: function () {
  296. let that = this;
  297. if (wx.getStorageSync('storeId')) {
  298. if (wx.getStorageSync('userId')) {
  299. wx.request({
  300. url: api.updateLoginUser,
  301. data: {
  302. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  303. },
  304. method: 'POST',
  305. header: {
  306. 'Content-Type': 'application/json'
  307. },
  308. success: function (wxRes) {
  309. if (wxRes.data.errno === 0) {
  310. // console.log("用户信息更新成功");
  311. }
  312. },
  313. fail: function (err) {
  314. console.log("failed");
  315. }
  316. });
  317. }
  318. }
  319. },
  320. changeProperty: function (e) {
  321. var propertyName = e.currentTarget.dataset.propertyName;
  322. console.log(propertyName);
  323. },
  324. closeAttrOrCollect: function () {
  325. let that = this;
  326. //添加或是取消收藏
  327. util.request(api.CollectAddOrDelete, { typeId: 0, valueId: this.data.id }, "POST")
  328. .then(function (res) {
  329. let _res = res;
  330. if (_res.errno == 0) {
  331. if (_res.data.type == 'add') {
  332. that.setData({
  333. 'collectBackImage': that.data.hasCollectImage
  334. });
  335. wx.showToast({
  336. title: "收藏成功",
  337. mask: true
  338. });
  339. } else {
  340. that.setData({
  341. 'collectBackImage': that.data.noCollectImage
  342. });
  343. wx.showToast({
  344. title: "收藏取消",
  345. mask: true
  346. });
  347. }
  348. } else {
  349. wx.showToast({
  350. image: '/static/images/icon_error.png',
  351. title: _res.errmsg,
  352. mask: true
  353. });
  354. }
  355. });
  356. },
  357. openCartPage: function () {
  358. wx.switchTab({
  359. url: '/pages/cart/cart',
  360. });
  361. },
  362. addToCart: function () {
  363. var that = this;
  364. if (this.data.openAttr == false) {
  365. //打开规格选择窗口
  366. this.setData({
  367. openAttr: !this.data.openAttr
  368. });
  369. } else {
  370. //提示选择完整规格
  371. if (!goodsUtil.isCheckedAllSpec(that)) {
  372. return false;
  373. }
  374. if (that.data.number + that.data.cartNumber > that.data.stockNum){
  375. // wx.showToast({
  376. // title: '库存不足',
  377. // mask: true
  378. // });
  379. util.showErrorToast('库存不足');
  380. //找不到对应的product信息,提示没有库存
  381. return false;
  382. }
  383. // console.log(that.data.productList)
  384. // //根据选中的规格,判断是否有对应的sku信息
  385. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  386. // console.log(goodsUtil.getCheckedSpecKey(that))
  387. if (!checkedProduct || checkedProduct.length <= 0) {
  388. wx.showToast({
  389. title: '库存不足',
  390. icon: 'none'
  391. });
  392. //找不到对应的product信息,提示没有库存
  393. return false;
  394. }
  395. //验证库存
  396. // if (checkedProduct.goods_number < this.data.number) {
  397. // //找不到对应的product信息,提示没有库存
  398. // return false;
  399. // }
  400. //添加到购物车
  401. util.request(api.CartAdd, {
  402. goodsId: this.data.goods.id,
  403. number: this.data.number,
  404. productId: checkedProduct[0].id
  405. }, "POST")
  406. .then(function (res) {
  407. let _res = res;
  408. if (_res.errno == 0) {
  409. wx.showToast({
  410. title: '添加成功'
  411. });
  412. that.setData({
  413. openAttr: !that.data.openAttr,
  414. cartGoodsCount: _res.data.cartTotal.goodsCount
  415. });
  416. if (that.data.userHasCollect == 1) {
  417. that.setData({
  418. 'collectBackImage': that.data.hasCollectImage
  419. });
  420. } else {
  421. that.setData({
  422. 'collectBackImage': that.data.noCollectImage
  423. });
  424. }
  425. } else {
  426. wx.showToast({
  427. title: _res.errmsg,
  428. icon: 'none'
  429. })
  430. that.hideSwitchAttrPop();
  431. // that.setData({
  432. // stockNum: 0
  433. // });
  434. }
  435. });
  436. }
  437. },
  438. cutNumber: function () {
  439. this.setData({
  440. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  441. });
  442. },
  443. addNumber: function () {
  444. this.setData({
  445. number: this.data.number + 1
  446. });
  447. },
  448. onShareAppMessage: function () {
  449. var that = this;
  450. // console.log("url:" + that.data.goods.list_pic_url);
  451. var userId = wx.getStorageSync('userId');
  452. console.log("userId:" + userId);
  453. console.log("detailStoreId:" + that.data.detailStoreId);
  454. return {
  455. title: '商品详情',
  456. desc: null != that.data.goods.name ? that.data.goods.name : "商品详情",
  457. imageUrl: that.data.goods.list_pic_url,
  458. path: '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId') + '&&storeId=' + that.data.detailStoreId,
  459. success: function (res) {
  460. console.log("转发成功");
  461. // 转发成功
  462. },
  463. fail: function (res) {
  464. // 转发失败
  465. console.log("转发失败");
  466. }
  467. }
  468. },
  469. //购物车增加
  470. addCrashNumber: function (e) {
  471. let that = this;
  472. var goodsId = e.currentTarget.dataset.goodsId;
  473. var productId = e.currentTarget.dataset.productId;
  474. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  475. if (res.errno === 0 && null != res.data) {
  476. var hotGoods = that.data.hotGoods;
  477. hotGoods.forEach(function (val, index, arr) {
  478. if (val.id == goodsId) {
  479. val.cart_num = res.data;
  480. hotGoods[index] = val;
  481. that.setData({ hotGoods: hotGoods });
  482. }
  483. }, that);
  484. }
  485. });
  486. },
  487. previewPic(e) {
  488. let url = e.currentTarget.dataset.url;
  489. let urls = [];
  490. urls[0] = url;
  491. wx.previewImage({
  492. urls
  493. })
  494. },
  495. switchNav(event) {
  496. wx.switchTab({
  497. url: '/pages/index/index'
  498. });
  499. },
  500. //触摸事件start
  501. touchStart(e) {
  502. let startX = e.changedTouches[0].pageX
  503. this.setData({
  504. startX: startX
  505. })
  506. console.log("startX:" + startX);
  507. },
  508. //触摸事件end
  509. touchEnd(e) {
  510. let _self = this;
  511. let windowWidth = this.data.windowWidth;
  512. let moveWidth = e.changedTouches[0].pageX - this.data.startX;
  513. let defineWidth = windowWidth / 20;
  514. let rankList = this.data.gallery;
  515. let index_now = e.currentTarget.dataset.index;
  516. if (moveWidth >= defineWidth) {
  517. //上一张
  518. let transWidth = (index_now - 1) * (-this.data.windowWidth);
  519. if (index_now >= 1) {
  520. this.setData({
  521. indexNum: index_now - 1,
  522. rankList: rankList,
  523. leftWidth: transWidth
  524. })
  525. }
  526. } else if (moveWidth <= (0 - defineWidth)) {
  527. //下一张
  528. this.videoContext.pause();
  529. let transWidth = (index_now + 1) * (-this.data.windowWidth);
  530. if (index_now < rankList.length - 1) {
  531. this.setData({
  532. indexNum: index_now + 1,
  533. rankList: rankList,
  534. leftWidth: transWidth
  535. })
  536. }
  537. } else {
  538. //console.log('不能修改样式')
  539. }
  540. },
  541. videoPlay(e) {
  542. // if (this.data.curr_id == e.currentTarget.dataset.id) {
  543. // this.setData({
  544. // curr_id: 0,
  545. // })
  546. // } else {
  547. // this.setData({
  548. // curr_id: e.currentTarget.dataset.id,
  549. // })
  550. // }
  551. this.setData({
  552. imgHiddenName: true,
  553. videoHiddenName: false
  554. })
  555. this.videoContext.play();
  556. },
  557. //触摸事件start
  558. touchStart2(e) {
  559. },
  560. touchEnd2(e) {
  561. this.setData({
  562. curr_id: 0,
  563. imgHiddenName: false,
  564. videoHiddenName: true
  565. })
  566. this.videoContext.pause();
  567. },
  568. getGoodsRelated: function () {
  569. let that = this;
  570. util.request(api.GoodsRelated, { id: that.data.id, page: that.data.page, size: that.data.size }).then(function (res) {
  571. if (res.errno === 0) {
  572. let goodsList = that.data.relatedGoods.concat(res.data.goodsList);
  573. that.setData({
  574. relatedGoods: goodsList,
  575. });
  576. }
  577. });
  578. },
  579. onReachBottom() {
  580. var that = this;
  581. wx.showLoading({
  582. title: '加载中...',
  583. })
  584. that.setData({
  585. page: that.data.page + 1
  586. });
  587. that.getGoodsRelated();
  588. }
  589. })