comment.js 19 KB

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