goods.js 21 KB

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