1
0

goods.js 18 KB

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