goods.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // goods.js
  2. var api = require('../../api.js');
  3. var utils = require('../../utils.js');
  4. var app = getApp();
  5. var WxParse = require('../../wxParse/wxParse.js');
  6. var p = 1;
  7. var is_loading_comment = false;
  8. var is_more_comment = true;
  9. var share_count = 0;
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. id: null,
  16. goods: {},
  17. show_attr_picker: false,
  18. form: {
  19. number: 1,
  20. },
  21. tab_detail: "active",
  22. tab_comment: "",
  23. comment_list: [],
  24. comment_count: {
  25. score_all: 0,
  26. score_3: 0,
  27. score_2: 0,
  28. score_1: 0,
  29. },
  30. autoplay: false,
  31. hide: "hide",
  32. show: false,
  33. x: wx.getSystemInfoSync().windowWidth,
  34. y: wx.getSystemInfoSync().windowHeight - 20,
  35. miaosha_end_time_over: {
  36. h: "--",
  37. m: "--",
  38. s: "--",
  39. },
  40. type: 0,
  41. page: 1
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad: function (options) {
  47. app.pageOnLoad(this);
  48. console.log(wx.getSystemInfoSync());
  49. share_count = 0;
  50. p = 1;
  51. is_loading_comment = false;
  52. is_more_comment = true;
  53. this.setData({
  54. store: wx.getStorageSync('store'),
  55. });
  56. var parent_id = 0;
  57. var user_id = options.user_id;
  58. console.log("options=>" + JSON.stringify(options));
  59. var scene = decodeURIComponent(options.scene);
  60. if (user_id != undefined) {
  61. parent_id = user_id;
  62. } else if (scene != undefined) {
  63. console.log("scene string=>" + scene);
  64. var scene_obj = utils.scene_decode(scene);
  65. console.log("scene obj=>" + JSON.stringify(scene_obj));
  66. if (scene_obj.uid && scene_obj.gid) {
  67. parent_id = scene_obj.uid;
  68. options.id = scene_obj.gid;
  69. } else {
  70. parent_id = scene;
  71. }
  72. }
  73. app.loginBindParent({parent_id: parent_id});
  74. var page = this;
  75. page.setData({
  76. id: options.id,
  77. });
  78. page.getGoods();
  79. page.getCommentList();
  80. },
  81. change: function(e){
  82. var type = e.currentTarget.dataset.type;
  83. this.setData({
  84. type: type,
  85. page: 1
  86. })
  87. this.getCommentList();
  88. },
  89. getGoods: function () {
  90. var page = this;
  91. app.request({
  92. url: api.default.goods,
  93. data: {
  94. id: page.data.id
  95. },
  96. success: function (res) {
  97. if (res.code == 0) {
  98. var detail = res.data.detail;
  99. WxParse.wxParse("detail", "html", detail, page);
  100. page.setData({
  101. goods: res.data,
  102. attr_group_list: res.data.attr_group_list,
  103. });
  104. if (page.data.goods.miaosha)
  105. page.setMiaoshaTimeOver();
  106. page.selectDefaultAttr();
  107. }
  108. if (res.code == 1) {
  109. wx.showModal({
  110. title: "提示",
  111. content: res.msg,
  112. showCancel: false,
  113. success: function (res) {
  114. if (res.confirm) {
  115. wx.switchTab({
  116. url: "/pages/index/index"
  117. });
  118. }
  119. }
  120. });
  121. }
  122. }
  123. });
  124. },
  125. selectDefaultAttr: function () {
  126. var page = this;
  127. if (!page.data.goods || page.data.goods.use_attr !== 0)
  128. return;
  129. for (var i in page.data.attr_group_list) {
  130. for (var j in page.data.attr_group_list[i].attr_list) {
  131. if (i == 0 && j == 0)
  132. page.data.attr_group_list[i].attr_list[j]['checked'] = true;
  133. }
  134. }
  135. page.setData({
  136. attr_group_list: page.data.attr_group_list,
  137. });
  138. },
  139. getCommentList: function (more) {
  140. var page = this;
  141. var p = page.data.page;
  142. if (more)
  143. return;
  144. // if (is_loading_comment)
  145. // return;
  146. // if (!is_more_comment)
  147. // return;
  148. is_loading_comment = true;
  149. app.request({
  150. url: api.default.comment_list,
  151. data: {
  152. goods_id: page.data.id,
  153. page: p,
  154. type: page.data.type
  155. },
  156. success: function (res) {
  157. if (res.code != 0)
  158. return;
  159. is_loading_comment = false;
  160. // p++;
  161. page.setData({
  162. comment_count: res.data.comment_count,
  163. comment_list: more ? page.data.comment_list.concat(res.data.list) : res.data.list,
  164. page: p++
  165. });
  166. if (res.data.list.length == 0)
  167. is_more_comment = false;
  168. }
  169. });
  170. },
  171. onGoodsImageClick: function (e) {
  172. var page = this;
  173. var urls = [];
  174. var index = e.currentTarget.dataset.index;
  175. //console.log(page.data.goods.pic_list);
  176. for (var i in page.data.goods.pic_list) {
  177. urls.push(page.data.goods.pic_list[i].pic_url);
  178. }
  179. wx.previewImage({
  180. urls: urls, // 需要预览的图片http链接列表
  181. current: urls[index],
  182. });
  183. },
  184. numberSub: function () {
  185. var page = this;
  186. var num = page.data.form.number;
  187. if (num <= 1)
  188. return true;
  189. num--;
  190. page.setData({
  191. form: {
  192. number: num,
  193. }
  194. });
  195. },
  196. numberAdd: function () {
  197. var page = this;
  198. var num = page.data.form.number;
  199. num++;
  200. page.setData({
  201. form: {
  202. number: num,
  203. }
  204. });
  205. },
  206. numberBlur: function (e) {
  207. var page = this;
  208. var num = e.detail.value;
  209. num = parseInt(num);
  210. if (isNaN(num))
  211. num = 1;
  212. if (num <= 0)
  213. num = 1;
  214. page.setData({
  215. form: {
  216. number: num,
  217. }
  218. });
  219. },
  220. addCart: function () {
  221. this.submit('ADD_CART');
  222. },
  223. buyNow: function () {
  224. this.submit('BUY_NOW');
  225. },
  226. submit: function (type) {
  227. var page = this;
  228. if (!page.data.show_attr_picker) {
  229. page.setData({
  230. show_attr_picker: true,
  231. });
  232. return true;
  233. }
  234. if (page.data.miaosha_data && page.data.miaosha_data.rest_num > 0 && page.data.form.number > page.data.miaosha_data.rest_num) {
  235. wx.showToast({
  236. title: "商品库存不足,请选择其它规格或数量",
  237. image: "/images/icon-warning.png",
  238. });
  239. return true;
  240. }
  241. if (page.data.form.number > page.data.goods.num) {
  242. wx.showToast({
  243. title: "商品库存不足,请选择其它规格或数量",
  244. image: "/images/icon-warning.png",
  245. });
  246. return true;
  247. }
  248. var attr_group_list = page.data.attr_group_list;
  249. var checked_attr_list = [];
  250. for (var i in attr_group_list) {
  251. var attr = false;
  252. for (var j in attr_group_list[i].attr_list) {
  253. if (attr_group_list[i].attr_list[j].checked) {
  254. attr = {
  255. attr_id: attr_group_list[i].attr_list[j].attr_id,
  256. attr_name: attr_group_list[i].attr_list[j].attr_name,
  257. };
  258. break;
  259. }
  260. }
  261. if (!attr) {
  262. wx.showToast({
  263. title: "请选择" + attr_group_list[i].attr_group_name,
  264. image: "/images/icon-warning.png",
  265. });
  266. return true;
  267. } else {
  268. checked_attr_list.push({
  269. attr_group_id: attr_group_list[i].attr_group_id,
  270. attr_group_name: attr_group_list[i].attr_group_name,
  271. attr_id: attr.attr_id,
  272. attr_name: attr.attr_name,
  273. });
  274. }
  275. }
  276. if (type == 'ADD_CART') {//加入购物车
  277. wx.showLoading({
  278. title: "正在提交",
  279. mask: true,
  280. });
  281. app.request({
  282. url: api.cart.add_cart,
  283. method: "POST",
  284. data: {
  285. goods_id: page.data.id,
  286. attr: JSON.stringify(checked_attr_list),
  287. num: page.data.form.number,
  288. },
  289. success: function (res) {
  290. wx.showToast({
  291. title: res.msg,
  292. duration: 1500
  293. });
  294. page.getGoods();
  295. wx.hideLoading();
  296. page.setData({
  297. show_attr_picker: false,
  298. });
  299. }
  300. });
  301. }
  302. if (type == 'BUY_NOW') {//立即购买
  303. page.setData({
  304. show_attr_picker: false,
  305. });
  306. wx.redirectTo({
  307. url: "/pages/order-submit/order-submit?goods_info=" + JSON.stringify({
  308. goods_id: page.data.id,
  309. attr: checked_attr_list,
  310. num: page.data.form.number,
  311. }),
  312. });
  313. }
  314. },
  315. hideAttrPicker: function () {
  316. var page = this;
  317. page.setData({
  318. show_attr_picker: false,
  319. });
  320. },
  321. showAttrPicker: function () {
  322. var page = this;
  323. page.setData({
  324. show_attr_picker: true,
  325. });
  326. },
  327. attrClick: function (e) {
  328. var page = this;
  329. var attr_group_id = e.target.dataset.groupId;
  330. var attr_id = e.target.dataset.id;
  331. var attr_group_list = page.data.attr_group_list;
  332. for (var i in attr_group_list) {
  333. if (attr_group_list[i].attr_group_id != attr_group_id)
  334. continue;
  335. for (var j in attr_group_list[i].attr_list) {
  336. if (attr_group_list[i].attr_list[j].attr_id == attr_id) {
  337. attr_group_list[i].attr_list[j].checked = true;
  338. } else {
  339. attr_group_list[i].attr_list[j].checked = false;
  340. }
  341. }
  342. }
  343. page.setData({
  344. attr_group_list: attr_group_list,
  345. });
  346. var check_attr_list = [];
  347. var check_all = true;
  348. for (var i in attr_group_list) {
  349. var group_checked = false;
  350. for (var j in attr_group_list[i].attr_list) {
  351. if (attr_group_list[i].attr_list[j].checked) {
  352. check_attr_list.push(attr_group_list[i].attr_list[j].attr_id);
  353. group_checked = true;
  354. break;
  355. }
  356. }
  357. if (!group_checked) {
  358. check_all = false;
  359. break;
  360. }
  361. }
  362. if (!check_all)
  363. return;
  364. wx.showLoading({
  365. title: "正在加载",
  366. mask: true,
  367. });
  368. app.request({
  369. url: api.default.goods_attr_info,
  370. data: {
  371. goods_id: page.data.goods.id,
  372. attr_list: JSON.stringify(check_attr_list),
  373. },
  374. success: function (res) {
  375. wx.hideLoading();
  376. if (res.code == 0) {
  377. var goods = page.data.goods;
  378. goods.price = res.data.price;
  379. goods.num = res.data.num;
  380. goods.attr_pic = res.data.pic;
  381. page.setData({
  382. goods: goods,
  383. miaosha_data: res.data.miaosha,
  384. });
  385. }
  386. }
  387. });
  388. },
  389. favoriteAdd: function () {
  390. var page = this;
  391. app.request({
  392. url: api.user.favorite_add,
  393. method: "post",
  394. data: {
  395. goods_id: page.data.goods.id,
  396. },
  397. success: function (res) {
  398. if (res.code == 0) {
  399. var goods = page.data.goods;
  400. goods.is_favorite = 1;
  401. page.setData({
  402. goods: goods,
  403. });
  404. }
  405. }
  406. });
  407. },
  408. favoriteRemove: function () {
  409. var page = this;
  410. app.request({
  411. url: api.user.favorite_remove,
  412. method: "post",
  413. data: {
  414. goods_id: page.data.goods.id,
  415. },
  416. success: function (res) {
  417. if (res.code == 0) {
  418. var goods = page.data.goods;
  419. goods.is_favorite = 0;
  420. page.setData({
  421. goods: goods,
  422. });
  423. }
  424. }
  425. });
  426. },
  427. tabSwitch: function (e) {
  428. var page = this;
  429. var tab = e.currentTarget.dataset.tab;
  430. if (tab == "detail") {
  431. page.setData({
  432. tab_detail: "active",
  433. tab_comment: "",
  434. });
  435. } else {
  436. page.setData({
  437. tab_detail: "",
  438. tab_comment: "active",
  439. });
  440. }
  441. },
  442. commentPicView: function (e) {
  443. console.log(e);
  444. var page = this;
  445. var index = e.currentTarget.dataset.index;
  446. var pic_index = e.currentTarget.dataset.picIndex;
  447. wx.previewImage({
  448. current: page.data.comment_list[index].pic_list[pic_index],
  449. urls: page.data.comment_list[index].pic_list,
  450. });
  451. },
  452. /**
  453. * 生命周期函数--监听页面初次渲染完成
  454. */
  455. onReady: function () {
  456. },
  457. /**
  458. * 生命周期函数--监听页面显示
  459. */
  460. onShow: function () {
  461. },
  462. /**
  463. * 生命周期函数--监听页面隐藏
  464. */
  465. onHide: function () {
  466. },
  467. /**
  468. * 生命周期函数--监听页面卸载
  469. */
  470. onUnload: function () {
  471. },
  472. /**
  473. * 页面相关事件处理函数--监听用户下拉动作
  474. */
  475. onPullDownRefresh: function () {
  476. },
  477. /**
  478. * 页面上拉触底事件的处理函数
  479. */
  480. onReachBottom: function () {
  481. var page = this;
  482. page.getCommentList(true);
  483. },
  484. /**
  485. * 用户点击右上角分享
  486. */
  487. onShareAppMessage: function () {
  488. var page = this;
  489. var user_info = wx.getStorageSync("user_info");
  490. var res = {
  491. path: "/pages/goods/goods?id=" + this.data.id + "&user_id=" + user_info.id,
  492. success: function (e) {
  493. console.log(e);
  494. share_count++;
  495. if (share_count == 1)
  496. app.shareSendCoupon(page);
  497. },
  498. title: page.data.goods.name,
  499. imageUrl: page.data.goods.pic_list[0].pic_url,
  500. };
  501. return res;
  502. },
  503. play: function (e) {
  504. var url = e.target.dataset.url;//获取视频链接
  505. this.setData({
  506. url: url,
  507. hide: '',
  508. show: true,
  509. });
  510. var videoContext = wx.createVideoContext('video');
  511. videoContext.play();
  512. },
  513. close: function (e) {
  514. if (e.target.id == 'video') {
  515. return true;
  516. }
  517. this.setData({
  518. hide: "hide",
  519. show: false
  520. });
  521. var videoContext = wx.createVideoContext('video');
  522. videoContext.pause();
  523. },
  524. hide: function (e) {
  525. if (e.detail.current == 0) {
  526. this.setData({
  527. img_hide: ""
  528. });
  529. } else {
  530. this.setData({
  531. img_hide: "hide"
  532. });
  533. }
  534. },
  535. showShareModal: function () {
  536. var page = this;
  537. page.setData({
  538. share_modal_active: "active",
  539. no_scroll: true,
  540. });
  541. },
  542. shareModalClose: function () {
  543. var page = this;
  544. page.setData({
  545. share_modal_active: "",
  546. no_scroll: false,
  547. });
  548. },
  549. getGoodsQrcode: function () {
  550. var page = this;
  551. page.setData({
  552. goods_qrcode_active: "active",
  553. share_modal_active: "",
  554. });
  555. if (page.data.goods_qrcode)
  556. return true;
  557. app.request({
  558. url: api.default.goods_qrcode,
  559. data: {
  560. goods_id: page.data.id,
  561. },
  562. success: function (res) {
  563. if (res.code == 0) {
  564. page.setData({
  565. goods_qrcode: res.data.pic_url,
  566. });
  567. }
  568. if (res.code == 1) {
  569. page.goodsQrcodeClose();
  570. wx.showModal({
  571. title: "提示",
  572. content: res.msg,
  573. showCancel: false,
  574. success: function (res) {
  575. if (res.confirm) {
  576. }
  577. }
  578. });
  579. }
  580. },
  581. });
  582. },
  583. goodsQrcodeClose: function () {
  584. var page = this;
  585. page.setData({
  586. goods_qrcode_active: "",
  587. no_scroll: false,
  588. });
  589. },
  590. saveGoodsQrcode: function () {
  591. var page = this;
  592. if (!wx.saveImageToPhotosAlbum) {
  593. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  594. wx.showModal({
  595. title: '提示',
  596. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
  597. showCancel: false,
  598. });
  599. return;
  600. }
  601. wx.showLoading({
  602. title: "正在保存图片",
  603. mask: false,
  604. });
  605. wx.downloadFile({
  606. url: page.data.goods_qrcode,
  607. success: function (e) {
  608. wx.showLoading({
  609. title: "正在保存图片",
  610. mask: false,
  611. });
  612. wx.saveImageToPhotosAlbum({
  613. filePath: e.tempFilePath,
  614. success: function () {
  615. wx.showModal({
  616. title: '提示',
  617. content: '商品海报保存成功',
  618. showCancel: false,
  619. });
  620. },
  621. fail: function (e) {
  622. wx.showModal({
  623. title: '图片保存失败',
  624. content: e.errMsg,
  625. showCancel: false,
  626. });
  627. },
  628. complete: function (e) {
  629. console.log(e);
  630. wx.hideLoading();
  631. }
  632. });
  633. },
  634. fail: function (e) {
  635. wx.showModal({
  636. title: '图片下载失败',
  637. content: e.errMsg + ";" + page.data.goods_qrcode,
  638. showCancel: false,
  639. });
  640. },
  641. complete: function (e) {
  642. console.log(e);
  643. wx.hideLoading();
  644. }
  645. });
  646. },
  647. goodsQrcodeClick: function (e) {
  648. var src = e.currentTarget.dataset.src;
  649. wx.previewImage({
  650. urls: [src],
  651. });
  652. },
  653. closeCouponBox: function (e) {
  654. this.setData({
  655. get_coupon_list: ""
  656. });
  657. },
  658. setMiaoshaTimeOver: function () {
  659. var page = this;
  660. function _init() {
  661. var time_over = page.data.goods.miaosha.end_time - page.data.goods.miaosha.now_time;
  662. time_over = time_over < 0 ? 0 : time_over;
  663. page.data.goods.miaosha.now_time++;
  664. page.setData({
  665. goods: page.data.goods,
  666. miaosha_end_time_over: secondToTime(time_over),
  667. });
  668. }
  669. _init();
  670. setInterval(function () {
  671. _init();
  672. }, 1000);
  673. function secondToTime(second) {
  674. var _h = parseInt(second / 3600);
  675. var _m = parseInt((second % 3600) / 60);
  676. var _s = second % 60;
  677. return {
  678. h: _h < 10 ? ("0" + _h) : ("" + _h),
  679. m: _m < 10 ? ("0" + _m) : ("" + _m),
  680. s: _s < 10 ? ("0" + _s) : ("" + _s),
  681. };
  682. }
  683. },
  684. to_dial: function (e) {
  685. var contact_tel = this.data.store.contact_tel;
  686. wx.makePhoneCall({
  687. phoneNumber: contact_tel
  688. })
  689. },
  690. });