comment.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. // pages/goods/comment.js
  2. var util = require('../../utils/util.js');
  3. var app = getApp();
  4. var buyClearTime = null;
  5. var status = require('../../utils/index.js');
  6. var WxParse = require('../../wxParse/wxParse.js');
  7. function count_down(that, total_micro_second) {
  8. var second = Math.floor(total_micro_second / 1000);
  9. var days = second / 3600 / 24;
  10. var daysRound = Math.floor(days);
  11. var hours = second / 3600 - (24 * daysRound);
  12. var hoursRound = Math.floor(hours);
  13. var minutes = second / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
  14. var minutesRound = Math.floor(minutes);
  15. var seconds = second - (24 * 3600 * daysRound) - (3600 * hoursRound) - (60 * minutesRound);
  16. that.setData({
  17. endtime: {
  18. days: fill_zero_prefix(daysRound),
  19. hours: fill_zero_prefix(hoursRound),
  20. minutes: fill_zero_prefix(minutesRound),
  21. seconds: fill_zero_prefix(seconds),
  22. show_detail: 1
  23. }
  24. });
  25. if (total_micro_second <= 0) {
  26. clearTimeout(buyClearTime);
  27. that.setData({
  28. endtime: {
  29. days: "00",
  30. hours: "00",
  31. minutes: "00",
  32. seconds: "00",
  33. }
  34. });
  35. return;
  36. }
  37. buyClearTime = setTimeout(function() {
  38. total_micro_second -= 1000;
  39. count_down(that, total_micro_second);
  40. }, 1000)
  41. }
  42. // 位数不足补零
  43. function fill_zero_prefix(num) {
  44. return num < 10 ? "0" + num : num
  45. }
  46. Page({
  47. /**
  48. * 页面的初始数据
  49. */
  50. data: {
  51. showData: 1,
  52. cartNum: 0,
  53. is_login: true,
  54. iconArr: {
  55. home: '',
  56. car: ''
  57. },
  58. list: [],
  59. loadMore: true,
  60. tip: '加载中'
  61. },
  62. page: 1,
  63. hasRefeshin: false,
  64. id: 0,
  65. community_id: 0,
  66. /**
  67. * 生命周期函数--监听页面加载
  68. */
  69. onLoad: function(options) {
  70. var that = this;
  71. var token = wx.getStorageSync('token');
  72. status.setNavBgColor();
  73. status.setIcon().then(function(iconArr) {
  74. that.setData({iconArr});
  75. });
  76. this.id = options.id;
  77. this.community_id = options.community_id;
  78. if (!util.check_login()) {
  79. this.setData({
  80. is_login: false
  81. })
  82. wx.hideLoading();
  83. }
  84. let currentCommunity = wx.getStorageSync('community');
  85. let currentCommunity_id = (currentCommunity && currentCommunity.communityId) || '';
  86. if (!currentCommunity_id) {
  87. let community = {};
  88. if (options.community_id !== void 0 && options.community_id > 0) {
  89. community.communityId = options.community_id;
  90. }
  91. util.getCommunityInfo(community).then(function (res) {
  92. console.log('step1')
  93. paramHandle();
  94. get_goods_details(res);
  95. }).catch((param) => {
  96. console.log('step4 新人')
  97. if (Object.keys(param) != '') {
  98. that.addhistory(param, true);
  99. }
  100. });
  101. } else {
  102. console.log('step3')
  103. paramHandle();
  104. get_goods_details();
  105. }
  106. if (options.share_id != 'undefined' && options.share_id > 0) wx.setStorage({ key: "share_id", data: options.share_id })
  107. function paramHandle() {
  108. console.log('step2')
  109. if (options.community_id != 'undefined' && options.community_id > 0) {
  110. app.util.request({
  111. 'url': 'entry/wxapp/index',
  112. 'data': {
  113. controller: 'index.get_community_info',
  114. 'community_id': options.community_id
  115. },
  116. dataType: 'json',
  117. success: function (res) {
  118. if (res.data.code == 0) {
  119. var community = res.data.data;
  120. let hisCommunity = currentCommunity;
  121. let community_id = currentCommunity_id;
  122. if (options.community_id != community_id && that.data.is_login) {
  123. wx.showModal({
  124. title: '温馨提示',
  125. content: '是否切换为分享人所在小区“' + community.communityName,
  126. confirmColor: '#F75451',
  127. success(res) {
  128. if (res.confirm) {
  129. app.globalData.community = community;
  130. app.globalData.changedCommunity = true;
  131. wx.setStorage({
  132. key: "community",
  133. data: community
  134. })
  135. that.addhistory(community);
  136. get_goods_details(community);
  137. console.log('用户点击确定')
  138. } else if (res.cancel) {
  139. that.showNoBindCommunity();
  140. console.log('用户点击取消')
  141. }
  142. }
  143. })
  144. }
  145. }
  146. }
  147. })
  148. }
  149. that.setData({
  150. goods_id: options.id
  151. }, ()=>{
  152. that.load_comment_list();
  153. })
  154. }
  155. function get_goods_details(communityInfo) {
  156. if (communityInfo) currentCommunity_id = communityInfo.communityId;
  157. app.util.request({
  158. 'url': 'entry/wxapp/index',
  159. 'data': {
  160. controller: 'goods.get_goods_detail',
  161. 'token': token,
  162. 'id': options.id,
  163. community_id: currentCommunity_id
  164. },
  165. dataType: 'json',
  166. success: function (res) {
  167. wx.hideLoading();
  168. let goods = res.data.data.goods;
  169. // 商品不存在
  170. if (!goods || goods.length == 0 || Object.keys(goods) == '') {
  171. wx.showModal({
  172. title: '提示',
  173. content: '该商品不存在,回首页',
  174. showCancel: false,
  175. confirmColor: '#F75451',
  176. success(res) {
  177. if (res.confirm) {
  178. wx.switchTab({
  179. url: '/lionfish_comshop/pages/index/index',
  180. })
  181. }
  182. }
  183. })
  184. }
  185. let comment_list = res.data.comment_list;
  186. comment_list.map(function (item) {
  187. 14 * item.content.length / app.globalData.systemInfo.windowWidth > 3 && (item.showOpen = true), item.isOpen = true;
  188. })
  189. that.setData({
  190. order_comment_count: res.data.order_comment_count,
  191. order_comment_images: res.data.order_comment_images,
  192. comment_list: comment_list,
  193. loadover: true,
  194. goods: goods,
  195. buy_record_arr: res.data.data.buy_record_arr,
  196. site_name: res.data.data.site_name,
  197. share_title: goods.share_title,
  198. options: res.data.data.options,
  199. goods_image: res.data.data.goods_image,
  200. goods_image_length: res.data.data.goods_image.length,
  201. service: goods.tag,
  202. favgoods: goods.favgoods,
  203. cur_time: res.data.data.cur_time,
  204. order: {
  205. goods_id: res.data.data.goods.goods_id,
  206. pin_id: res.data.data.pin_id,
  207. },
  208. showSkeleton: false,
  209. is_comunity_rest: res.data.is_comunity_rest,
  210. goodsdetails_addcart_bg_color: res.data.goodsdetails_addcart_bg_color || 'linear-gradient(270deg, #f9c706 0%, #feb600 100%)',
  211. goodsdetails_buy_bg_color: res.data.goodsdetails_buy_bg_color || 'linear-gradient(90deg, #ff5041 0%, #ff695c 100%)'
  212. })
  213. if (res.data.is_comunity_rest == 1) {
  214. wx.showModal({
  215. title: '温馨提示',
  216. content: '团长休息中,欢迎下次光临!',
  217. showCancel: false,
  218. confirmColor: '#F75451',
  219. confirmText: '好的',
  220. success(res) { }
  221. })
  222. }
  223. let over_type = goods.over_type;
  224. var seconds = 0;
  225. if (over_type == 0) {
  226. seconds = (goods.begin_time - res.data.data.cur_time) * 1000;
  227. } else {
  228. seconds = (goods.end_time - res.data.data.cur_time) * 1000;
  229. }
  230. if (seconds > 0) {
  231. count_down(that, seconds);
  232. }
  233. var article = res.data.data.goods.description;
  234. WxParse.wxParse('article', 'html', article, that, 0);
  235. }
  236. })
  237. }
  238. },
  239. //未绑定提示
  240. showNoBindCommunity: function () {
  241. wx.showModal({
  242. title: '提示',
  243. content: '您未绑定该小区,请切换后下单!',
  244. showCancel: false,
  245. confirmColor: '#F75451',
  246. success(res) {
  247. if (res.confirm) {
  248. wx.redirectTo({
  249. url: '/lionfish_comshop/pages/position/community',
  250. })
  251. }
  252. }
  253. })
  254. },
  255. /**
  256. * 历史社区
  257. */
  258. addhistory: function (community, isNew = false) {
  259. var community_id = community.communityId;
  260. console.log('addhistory');
  261. var token = wx.getStorageSync('token');
  262. app.util.request({
  263. 'url': 'entry/wxapp/index',
  264. 'data': {
  265. controller: 'index.addhistory_community',
  266. community_id: community_id,
  267. 'token': token
  268. },
  269. dataType: 'json',
  270. success: function (res) {
  271. if (isNew) {
  272. console.log('新人 社区')
  273. app.util.request({
  274. 'url': 'entry/wxapp/index',
  275. 'data': {
  276. controller: 'index.get_community_info',
  277. community_id: community_id
  278. },
  279. dataType: 'json',
  280. success: function (result) {
  281. if (result.data.code == 0) {
  282. let community = result.data.data;
  283. app.globalData.community = community;
  284. app.globalData.changedCommunity = true;
  285. wx.setStorage({
  286. key: "community",
  287. data: community
  288. })
  289. }
  290. }
  291. })
  292. }
  293. }
  294. })
  295. },
  296. load_comment_list: function(){
  297. let goods_id = this.data.goods_id;
  298. let token = wx.getStorageSync('token');
  299. let that = this;
  300. !this.hasRefeshin && (that.hasRefeshin = true, that.setData({ loadMore: true, tip: '加载中' }), app.util.request({
  301. 'url': 'entry/wxapp/index',
  302. 'data': {
  303. controller: 'goods.comment_info',
  304. token: token,
  305. goods_id: goods_id,
  306. page: that.page
  307. },
  308. dataType: 'json',
  309. success: function (res) {
  310. if(res.data.code==0){
  311. let commentList = res.data.list;
  312. commentList.map(function (item) {
  313. 14 * item.content.length / app.globalData.systemInfo.windowWidth > 3 && (item.showOpen = true), item.isOpen = true;
  314. })
  315. let list = that.data.list.concat(commentList);
  316. that.page++;
  317. that.hasRefeshin = false;
  318. that.setData({ list, loadMore: false, tip: '' })
  319. } else if (res.data.code == 1) {
  320. // 无数据
  321. if (that.page == 1) that.setData({ showData:0 })
  322. that.setData({
  323. loadMore: false,
  324. tip: '^_^已经到底了'
  325. })
  326. } else if(res.data.code == 2) {
  327. //no login
  328. } else {
  329. //其他
  330. }
  331. }
  332. }))
  333. },
  334. /**
  335. * 授权成功回调
  336. */
  337. authSuccess: function () {
  338. var id = this.id;
  339. var community_id = this.community_id;
  340. wx.redirectTo({
  341. url: '/lionfish_comshop/pages/goods/comment?id=' + id + '&community_id' + community_id,
  342. })
  343. },
  344. onShow: function() {
  345. this.setData({
  346. cartNum: app.globalData.cartNum
  347. });
  348. },
  349. //加入购物车
  350. addToCart: function(e) {
  351. var that = this;
  352. var from_id = e.detail.formId;
  353. var token = wx.getStorageSync('token');
  354. app.util.request({
  355. 'url': 'entry/wxapp/user',
  356. 'data': {
  357. controller: 'user.get_member_form_id',
  358. 'token': token,
  359. "from_id": from_id
  360. },
  361. dataType: 'json',
  362. success: function(res) {}
  363. })
  364. that.setData({
  365. is_just_addcar: 1
  366. })
  367. //加入购物车
  368. that.openSku();
  369. },
  370. // 打开选框
  371. openSku: function() {
  372. var that = this;
  373. var goods_id = this.data.goods_id;
  374. var options = this.data.options;
  375. that.setData({
  376. addCar_goodsid: goods_id
  377. })
  378. let list = options.list || [];
  379. let arr = [];
  380. if (list.length > 0) {
  381. for (let i = 0; i < list.length; i++) {
  382. let sku = list[i]['option_value'][0];
  383. let temp = {
  384. name: sku['name'],
  385. id: sku['option_value_id'],
  386. index: i,
  387. idx: 0
  388. };
  389. arr.push(temp);
  390. }
  391. //把单价剔除出来begin
  392. var id = '';
  393. for (let i = 0; i < arr.length; i++) {
  394. if (i == arr.length - 1) {
  395. id = id + arr[i]['id'];
  396. } else {
  397. id = id + arr[i]['id'] + "_";
  398. }
  399. }
  400. var cur_sku_arr = options.sku_mu_list[id];
  401. that.setData({
  402. sku: arr,
  403. sku_val: 1,
  404. cur_sku_arr: cur_sku_arr,
  405. skuList: options,
  406. visible: true,
  407. showSku: true
  408. });
  409. } else {
  410. let goods = this.data.goods;
  411. let cur_sku_arr = {
  412. canBuyNum: goods.total,
  413. spuName: goods.goodsname,
  414. actPrice: goods.actPrice,
  415. marketPrice: goods.marketPrice,
  416. stock: goods.total,
  417. skuImage: goods.image_thumb
  418. }
  419. that.setData({
  420. sku: [],
  421. sku_val: 1,
  422. cur_sku_arr: cur_sku_arr,
  423. skuList: []
  424. })
  425. let formIds = {
  426. detail: {
  427. formId: ""
  428. }
  429. };
  430. formIds.detail.formId = "the formId is a mock one";
  431. that.gocarfrom(formIds);
  432. //todo...addcart
  433. }
  434. },
  435. gocarfrom: function(e) {
  436. var that = this;
  437. var is_just_addcar = this.data.is_just_addcar;
  438. wx.showLoading();
  439. var token = wx.getStorageSync('token');
  440. app.util.request({
  441. 'url': 'entry/wxapp/user',
  442. 'data': {
  443. controller: 'user.get_member_form_id',
  444. 'token': token,
  445. "from_id": e.detail.formId
  446. },
  447. dataType: 'json',
  448. success: function(res) {}
  449. })
  450. that.goOrder();
  451. },
  452. closeSku: function() {
  453. this.setData({
  454. visible: 0,
  455. stopClick: false,
  456. });
  457. },
  458. goOrder: function() {
  459. var that = this;
  460. if (that.data.can_car) {
  461. that.data.can_car = false;
  462. }
  463. var token = wx.getStorageSync('token');
  464. var community = wx.getStorageSync('community');
  465. var goods_id = that.data.goods_id;
  466. var community_id = community.communityId;
  467. var quantity = that.data.sku_val;
  468. var cur_sku_arr = that.data.cur_sku_arr;
  469. var sku_str = '';
  470. var is_just_addcar = that.data.is_just_addcar;
  471. if (cur_sku_arr && cur_sku_arr.option_item_ids) {
  472. sku_str = cur_sku_arr.option_item_ids;
  473. }
  474. //addCar_goodsid: goods_id
  475. app.util.request({
  476. 'url': 'entry/wxapp/user',
  477. 'data': {
  478. controller: 'car.add',
  479. 'token': token,
  480. "goods_id": goods_id,
  481. "community_id": community_id,
  482. "quantity": quantity,
  483. "sku_str": sku_str,
  484. "buy_type": 'dan',
  485. "pin_id": 0,
  486. "is_just_addcar": is_just_addcar
  487. },
  488. dataType: 'json',
  489. method: 'POST',
  490. success: function(res) {
  491. //console.log(res);
  492. if (res.data.code == 3) {
  493. wx.showToast({
  494. title: res.data.msg,
  495. icon: 'none',
  496. duration: 2000
  497. })
  498. } else if (res.data.code == 4) {
  499. wx.showToast({
  500. title: '您未登录',
  501. duration: 2000,
  502. })
  503. } else if (res.data.code == 6) {
  504. var msg = res.data.msg;
  505. let max_quantity = res.data.max_quantity || '';
  506. (max_quantity > 0) && that.setData({ sku_val: max_quantity })
  507. wx.showToast({
  508. title: msg,
  509. icon: 'none',
  510. duration: 2000
  511. })
  512. } else {
  513. if (is_just_addcar == 1) {
  514. that.closeSku();
  515. wx.showToast({
  516. title: "已加入购物车",
  517. image: "../../images/addShopCart.png"
  518. })
  519. app.globalData.cartNum = res.data.total
  520. that.setData({
  521. cartNum: res.data.total
  522. });
  523. status.indexListCarCount(goods_id);
  524. } else {
  525. var pages_all = getCurrentPages();
  526. if (pages_all.length > 3) {
  527. wx.redirectTo({
  528. url: '/lionfish_comshop/pages/order/placeOrder?type=dan'
  529. })
  530. } else {
  531. wx.navigateTo({
  532. url: '/lionfish_comshop/pages/order/placeOrder?type=dan'
  533. })
  534. }
  535. }
  536. }
  537. }
  538. })
  539. },
  540. selectSku: function(event) {
  541. var that = this;
  542. let str = event.currentTarget.dataset.type;
  543. let obj = str.split("_");
  544. let arr = that.data.sku;
  545. let temp = {
  546. name: obj[3],
  547. id: obj[2],
  548. index: obj[0],
  549. idx: obj[1]
  550. };
  551. arr.splice(obj[0], 1, temp);
  552. that.setData({
  553. sku: arr
  554. })
  555. var id = '';
  556. for (let i = 0; i < arr.length; i++) {
  557. if (i == arr.length - 1) {
  558. id = id + arr[i]['id'];
  559. } else {
  560. id = id + arr[i]['id'] + "_";
  561. }
  562. }
  563. var options = this.data.skuList;
  564. var cur_sku_arr = options.sku_mu_list[id];
  565. that.setData({
  566. cur_sku_arr: cur_sku_arr
  567. });
  568. console.log(id);
  569. },
  570. submit: function(e) {
  571. var from_id = e.detail.formId;
  572. var token = wx.getStorageSync('token');
  573. app.util.request({
  574. 'url': 'entry/wxapp/user',
  575. 'data': {
  576. controller: 'user.get_member_form_id',
  577. 'token': token,
  578. "from_id": from_id
  579. },
  580. dataType: 'json',
  581. success: function(res) {}
  582. })
  583. },
  584. balance: function(e) {
  585. console.log(3);
  586. this.setData({
  587. is_just_addcar: 0
  588. })
  589. //加入购物车
  590. this.openSku();
  591. },
  592. //加减商品数量
  593. setNum: function(event) {
  594. let types = event.currentTarget.dataset.type;
  595. var that = this;
  596. var num = 1;
  597. let sku_val = this.data.sku_val * 1;
  598. if (types == 'add') {
  599. num = sku_val + 1;
  600. } else if (types == 'decrease') {
  601. if (sku_val > 1) {
  602. num = sku_val - 1;
  603. }
  604. }
  605. let arr = that.data.sku;
  606. var options = this.data.skuList;
  607. var id = '';
  608. for (let i = 0; i < arr.length; i++) {
  609. if (i == arr.length - 1) {
  610. id = id + arr[i]['id'];
  611. } else {
  612. id = id + arr[i]['id'] + "_";
  613. }
  614. }
  615. var cur_sku_arr = options.sku_mu_list[id];
  616. if (num > cur_sku_arr['canBuyNum']) {
  617. num = num - 1;
  618. }
  619. this.setData({
  620. sku_val: num
  621. })
  622. },
  623. /**
  624. * 图片预览
  625. */
  626. preview: function(t) {
  627. var a = t.currentTarget.dataset.index,
  628. e = t.currentTarget.dataset.idx;
  629. wx.previewImage({
  630. urls: this.data.list[a].images,
  631. current: this.data.list[a].images[e],
  632. fail: function(t) {
  633. wx.showToast({
  634. title: "预览图片失败,请重试",
  635. icon: "none"
  636. }), console.log(t);
  637. }
  638. });
  639. },
  640. /**
  641. * 复制内容
  642. */
  643. copy: function(t) {
  644. wx.setClipboardData({
  645. data: t.currentTarget.dataset.val,
  646. success: function() {
  647. wx.showToast({
  648. title: "内容复制成功!",
  649. icon: "none"
  650. });
  651. }
  652. });
  653. },
  654. /**
  655. * 展开收起
  656. */
  657. bindOpen: function(t) {
  658. var idx = t.currentTarget.dataset.idx;
  659. if (this.data.list[idx].isOpen) {
  660. this.data.list[idx].isOpen = false;
  661. var list = this.data.list;
  662. this.setData({
  663. list: list
  664. });
  665. } else {
  666. this.data.list[idx].isOpen = true;
  667. var list = this.data.list;
  668. this.setData({
  669. list: list
  670. });
  671. }
  672. },
  673. onReachBottom: function () {
  674. console.log('我是底线');
  675. this.load_comment_list();
  676. }
  677. })