goods.js 17 KB

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