goodsDetail.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. // lionfish_comshop/pages/goods/goodsDetail.js
  2. var util = require('../../utils/util.js');
  3. var status = require('../../utils/index.js');
  4. var WxParse = require('../../wxParse/wxParse.js');
  5. var app = getApp();
  6. var detailClearTime = null;
  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(detailClearTime);
  27. detailClearTime = null;
  28. if (that.data.goods.over_type==0){
  29. that.authSuccess();
  30. }
  31. that.setData({
  32. endtime: {
  33. days: "00",
  34. hours: "00",
  35. minutes: "00",
  36. seconds: "00",
  37. }
  38. });
  39. return;
  40. }
  41. detailClearTime = setTimeout(function() {
  42. total_micro_second -= 1000;
  43. count_down(that, total_micro_second);
  44. }, 1000)
  45. }
  46. // 位数不足补零
  47. function fill_zero_prefix(num) {
  48. return num < 10 ? "0" + num : num
  49. }
  50. Page({
  51. /**
  52. * 页面的初始数据
  53. */
  54. data: {
  55. is_login: true,
  56. goodsIndex: 1,
  57. goods_id: 0,
  58. endtime: {
  59. days: "00",
  60. hours: "00",
  61. minutes: "00",
  62. seconds: "00",
  63. },
  64. is_share_html: true,
  65. stickyFlag: false,
  66. showSkeleton: true,
  67. imageSize: {
  68. imageWidth: "100%",
  69. imageHeight: 600
  70. },
  71. cartNum: 0,
  72. noIns: false,
  73. index_bottom_image: '',
  74. hideModal: true,
  75. shareImgUrl: '',
  76. goods_details_middle_image: '',
  77. is_show_buy_record: 0,
  78. stopNotify: true,
  79. iconArr: {
  80. home: '',
  81. car: ''
  82. },
  83. canvasWidth: 375,
  84. canvasHeight: 300,
  85. fmShow: true
  86. },
  87. $data: {
  88. stickyFlag: false,
  89. id: '',
  90. scene: '',
  91. community_id: 0
  92. },
  93. imageUrl: '',
  94. goodsImg: '',
  95. /**
  96. * 生命周期函数--监听页面加载
  97. */
  98. onLoad: function(options) {
  99. var that = this;
  100. status.setNavBgColor();
  101. status.setIcon().then(function (iconArr){
  102. that.setData({ iconArr });
  103. });
  104. var token = wx.getStorageSync('token');
  105. var scene = decodeURIComponent(options.scene);
  106. this.$data.id = options.id;
  107. this.$data.community_id = options.community_id;
  108. this.$data.scene = options.scene;
  109. wx.showLoading();
  110. if (!util.check_login()) {
  111. this.setData({
  112. is_login: false
  113. })
  114. wx.hideLoading();
  115. }
  116. let currentCommunity = wx.getStorageSync('community');
  117. let currentCommunity_id = (currentCommunity && currentCommunity.communityId) || '';
  118. if (!currentCommunity_id) {
  119. let community = {};
  120. if (options.community_id !== 'undefined' && options.community_id > 0) {
  121. community.communityId = options.community_id;
  122. }
  123. if (scene !== 'undefined') {
  124. var opt_arr = scene.split("_");
  125. options.community_id = opt_arr[2];
  126. community.communityId = options.community_id;
  127. }
  128. util.getCommunityInfo(community).then(function(res){
  129. console.log('step1')
  130. paramHandle();
  131. get_goods_details(res);
  132. }).catch((param)=>{
  133. console.log('step4 新人')
  134. if(Object.keys(param) != '') {
  135. that.addhistory(param, true);
  136. }
  137. });
  138. } else {
  139. console.log('step3')
  140. paramHandle();
  141. get_goods_details(currentCommunity);
  142. }
  143. if (options.share_id != 'undefined' && options.share_id > 0) wx.setStorage({ key: "share_id", data: options.share_id })
  144. function paramHandle(){
  145. console.log('step2')
  146. if (options.community_id != 'undefined' && options.community_id > 0) {
  147. getCommunityInfo(options.community_id);
  148. }
  149. if (scene != 'undefined') {
  150. //$goods_id.'_'.$member_id_community_id
  151. var opt_arr = scene.split("_");
  152. options.id = opt_arr[0];
  153. wx.setStorage({
  154. key: "share_id",
  155. data: opt_arr[1]
  156. })
  157. getCommunityInfo(opt_arr[2]);
  158. }
  159. function getCommunityInfo(c_id) {
  160. app.util.request({
  161. 'url': 'entry/wxapp/index',
  162. 'data': {
  163. controller: 'index.get_community_info',
  164. community_id: c_id
  165. },
  166. dataType: 'json',
  167. success: function (res) {
  168. if (res.data.code == 0) {
  169. var community = res.data.data;
  170. let hisCommunity = currentCommunity;
  171. let community_id = currentCommunity_id;
  172. if (c_id != community_id && that.data.is_login) {
  173. wx.showModal({
  174. title: '温馨提示',
  175. content: '是否切换为分享人所在小区“' + community.communityName,
  176. confirmColor: '#F75451',
  177. success(res) {
  178. if (res.confirm) {
  179. app.globalData.community = community;
  180. app.globalData.changedCommunity = true;
  181. wx.setStorage({
  182. key: "community",
  183. data: community
  184. })
  185. that.addhistory(community);
  186. get_goods_details(community);
  187. console.log('用户点击确定')
  188. } else if (res.cancel) {
  189. that.showNoBindCommunity();
  190. console.log('用户点击取消')
  191. }
  192. }
  193. })
  194. } else {
  195. console.log('step5')
  196. let oldCommunity = wx.getStorageSync('community');
  197. if (!oldCommunity){
  198. app.globalData.community = community;
  199. app.globalData.changedCommunity = true;
  200. wx.setStorage({ key: "community", data: community })
  201. }
  202. }
  203. }
  204. }
  205. })
  206. }
  207. that.setData({
  208. goods_id: options.id
  209. })
  210. }
  211. function get_goods_details(communityInfo){
  212. if (!options.id) {
  213. wx.hideLoading();
  214. wx.showModal({
  215. title: '提示',
  216. content: '参数错误',
  217. showCancel: false,
  218. confirmColor: '#F75451',
  219. success(res) {
  220. if (res.confirm) {
  221. wx.redirectTo({
  222. url: '/lionfish_comshop/pages/index/index',
  223. })
  224. }
  225. }
  226. })
  227. return false;
  228. }
  229. if (communityInfo) currentCommunity_id = communityInfo.communityId;
  230. app.util.request({
  231. 'url': 'entry/wxapp/index',
  232. 'data': {
  233. controller: 'goods.get_goods_detail',
  234. 'token': token,
  235. 'id': options.id,
  236. community_id: currentCommunity_id
  237. },
  238. dataType: 'json',
  239. success: function (res) {
  240. wx.hideLoading();
  241. let goods = res.data.data.goods;
  242. // 商品不存在
  243. if (!goods || goods.length == 0 || Object.keys(goods) == '') {
  244. wx.showModal({
  245. title: '提示',
  246. content: '该商品不存在,回首页',
  247. showCancel: false,
  248. confirmColor: '#F75451',
  249. success(res) {
  250. if (res.confirm) {
  251. wx.switchTab({
  252. url: '/lionfish_comshop/pages/index/index',
  253. })
  254. }
  255. }
  256. })
  257. }
  258. let comment_list = res.data.comment_list;
  259. comment_list.map(function (item) {
  260. 14 * item.content.length / app.globalData.systemInfo.windowWidth > 3 && (item.showOpen = true), item.isOpen = true;
  261. })
  262. // 幻灯片预览数组
  263. let goods_images = res.data.data.goods_image;
  264. let prevImgArr = [];
  265. goods_images.forEach(function(item){ prevImgArr.push(item.image); })
  266. //群分享
  267. let isopen_community_group_share = res.data.isopen_community_group_share || 0;
  268. let group_share_info = res.data.group_share_info;
  269. that.setData({
  270. order_comment_count: res.data.order_comment_count,
  271. comment_list: comment_list,
  272. goods: goods,
  273. options: res.data.data.options,
  274. order: {
  275. goods_id: res.data.data.goods.goods_id,
  276. pin_id: res.data.data.pin_id,
  277. },
  278. share_title: goods.share_title,
  279. buy_record_arr: res.data.data.buy_record_arr,
  280. goods_image: res.data.data.goods_image,
  281. goods_image_length: res.data.data.goods_image.length,
  282. service: goods.tag,
  283. showSkeleton: false,
  284. is_comunity_rest: res.data.is_comunity_rest,
  285. prevImgArr,
  286. open_man_orderbuy: res.data.open_man_orderbuy,
  287. man_orderbuy_money: res.data.man_orderbuy_money,
  288. goodsdetails_addcart_bg_color: res.data.goodsdetails_addcart_bg_color || 'linear-gradient(270deg, #f9c706 0%, #feb600 100%)',
  289. goodsdetails_buy_bg_color: res.data.goodsdetails_buy_bg_color || 'linear-gradient(90deg, #ff5041 0%, #ff695c 100%)',
  290. isopen_community_group_share,
  291. group_share_info
  292. },()=>{
  293. let goods_share_image = goods.goods_share_image;
  294. if (goods_share_image){
  295. console.log('draw分享图');
  296. status.download(goods_share_image + "?imageView2/1/w/500/h/400").then(function (a) {
  297. that.goodsImg = a.tempFilePath, that.drawImgNoPrice();
  298. });
  299. } else {
  300. console.log('draw价格');
  301. let shareImg = goods.image_thumb;
  302. status.download(shareImg + "?imageView2/1/w/500/h/400").then(function (a) {
  303. that.goodsImg = a.tempFilePath, that.drawImg();
  304. });
  305. }
  306. })
  307. if (res.data.is_comunity_rest == 1) {
  308. wx.showModal({
  309. title: '温馨提示',
  310. content: '团长休息中,欢迎下次光临!',
  311. showCancel: false,
  312. confirmColor: '#F75451',
  313. confirmText: '好的',
  314. success(res) { }
  315. })
  316. }
  317. let over_type = goods.over_type;
  318. var seconds = 0;
  319. if (over_type == 0) {
  320. seconds = (goods.begin_time - res.data.data.cur_time) * 1000;
  321. } else {
  322. seconds = (goods.end_time - res.data.data.cur_time) * 1000;
  323. }
  324. if (seconds > 0) {
  325. count_down(that, seconds);
  326. }
  327. var article = res.data.data.goods.description;
  328. WxParse.wxParse('article', 'html', article, that, 0, app.globalData.systemInfo);
  329. }
  330. })
  331. }
  332. // this.addhistory();
  333. this.get_instructions();
  334. this.setData({
  335. canvasWidth: app.globalData.systemInfo.windowWidth,
  336. canvasHeight: 0.8 * app.globalData.systemInfo.windowWidth
  337. })
  338. },
  339. //未绑定提示
  340. showNoBindCommunity: function(){
  341. wx.showModal({
  342. title: '提示',
  343. content: '您未绑定该小区,请切换后下单!',
  344. showCancel: false,
  345. confirmColor: '#F75451',
  346. success(res) {
  347. if (res.confirm) {
  348. wx.redirectTo({
  349. url: '/lionfish_comshop/pages/position/community',
  350. })
  351. }
  352. }
  353. })
  354. },
  355. /**
  356. * 授权成功回调
  357. */
  358. authSuccess: function() {
  359. var id = this.$data.id;
  360. var scene = this.$data.scene;
  361. var community_id = this.$data.community_id;
  362. wx.redirectTo({
  363. url: '/lionfish_comshop/pages/goods/goodsDetail?id=' + id + '&community_id=' + community_id + '&scene=' + scene,
  364. })
  365. },
  366. /**
  367. * 历史社区
  368. */
  369. addhistory: function (community, isNew=false) {
  370. var community_id = community.communityId;
  371. console.log('addhistory');
  372. var token = wx.getStorageSync('token');
  373. app.util.request({
  374. 'url': 'entry/wxapp/index',
  375. 'data': {
  376. controller: 'index.addhistory_community',
  377. community_id: community_id,
  378. 'token': token
  379. },
  380. dataType: 'json',
  381. success: function(res) {
  382. if(isNew){
  383. console.log('新人 社区')
  384. app.util.request({
  385. 'url': 'entry/wxapp/index',
  386. 'data': {
  387. controller: 'index.get_community_info',
  388. community_id: community_id
  389. },
  390. dataType: 'json',
  391. success: function (result) {
  392. if (result.data.code == 0) {
  393. let community = result.data.data;
  394. app.globalData.community = community;
  395. app.globalData.changedCommunity = true;
  396. wx.setStorage({ key: "community", data: community })
  397. }
  398. }
  399. })
  400. }
  401. }
  402. })
  403. },
  404. /**
  405. * 图片信息
  406. */
  407. imageLoad: function(e) {
  408. var imageSize = util.imageUtil(e)
  409. this.setData({
  410. imageSize
  411. })
  412. },
  413. /**
  414. * 获取服务信息
  415. */
  416. get_instructions: function() {
  417. let that = this;
  418. app.util.request({
  419. 'url': 'entry/wxapp/index',
  420. 'data': {
  421. controller: 'goods.get_instructions'
  422. },
  423. dataType: 'json',
  424. success: function(res) {
  425. if (res.data.code == 0) {
  426. var instructions = res.data.data.value;
  427. WxParse.wxParse('instructions', 'html', instructions, that, 25);
  428. if (instructions == '') that.setData({ noIns: true })
  429. that.setData({
  430. index_bottom_image: res.data.data.index_bottom_image,
  431. goods_details_middle_image: res.data.data.goods_details_middle_image,
  432. is_show_buy_record: res.data.data.is_show_buy_record,
  433. order_notify_switch: res.data.data.order_notify_switch,
  434. is_show_comment_list: res.data.data.is_show_comment_list,
  435. goods_details_price_bg: res.data.data.goods_details_price_bg,
  436. isShowContactBtn: res.data.data.index_service_switch || 0,
  437. goods_industrial_switch: res.data.data.goods_industrial_switch || 0,
  438. goods_industrial: res.data.data.goods_industrial || '',
  439. is_show_ziti_time: res.data.data.is_show_ziti_time || 0
  440. })
  441. }
  442. }
  443. })
  444. },
  445. /**
  446. * 页面滚动事件
  447. */
  448. onPageScroll: function(t) {
  449. !this.data.stickyFlag && t.scrollTop > 200 && (this.$data.stickyFlag = true, this.setData({
  450. stickyFlag: true
  451. })), this.$data.stickyFlag && t.scrollTop <= 200 && (this.$data.stickyFlag = false,
  452. this.setData({
  453. stickyFlag: false
  454. }));
  455. },
  456. /**
  457. * 返回顶部
  458. */
  459. returnTop: function() {
  460. this.stickyFlag = false;
  461. this.setData({
  462. stickyFlag: false
  463. });
  464. wx.pageScrollTo({
  465. scrollTop: 0,
  466. duration: 500
  467. });
  468. },
  469. /**
  470. * 加入购物车
  471. */
  472. addToCart: function(e) {
  473. var that = this;
  474. var from_id = e.detail.formId;
  475. var token = wx.getStorageSync('token');
  476. app.util.request({
  477. 'url': 'entry/wxapp/user',
  478. 'data': {
  479. controller: 'user.get_member_form_id',
  480. 'token': token,
  481. "from_id": from_id
  482. },
  483. dataType: 'json',
  484. success: function(res) {}
  485. })
  486. that.setData({
  487. is_just_addcar: 1
  488. })
  489. //加入购物车
  490. that.openSku();
  491. },
  492. /**
  493. * 打开购物车
  494. */
  495. openSku: function() {
  496. var that = this;
  497. var goods_id = this.data.goods_id;
  498. var options = this.data.options;
  499. that.setData({
  500. addCar_goodsid: goods_id
  501. })
  502. console.log(options);
  503. let list = options.list || [];
  504. let arr = [];
  505. if (list.length > 0) {
  506. for (let i = 0; i < list.length; i++) {
  507. let sku = list[i]['option_value'][0];
  508. let temp = {
  509. name: sku['name'],
  510. id: sku['option_value_id'],
  511. index: i,
  512. idx: 0
  513. };
  514. arr.push(temp);
  515. }
  516. //把单价剔除出来begin
  517. var id = '';
  518. for (let i = 0; i < arr.length; i++) {
  519. if (i == arr.length - 1) {
  520. id = id + arr[i]['id'];
  521. } else {
  522. id = id + arr[i]['id'] + "_";
  523. }
  524. }
  525. var cur_sku_arr = options.sku_mu_list[id];
  526. that.setData({
  527. sku: arr,
  528. sku_val: 1,
  529. cur_sku_arr: cur_sku_arr,
  530. skuList: options,
  531. visible: true,
  532. showSku: true
  533. });
  534. } else {
  535. let goods = this.data.goods;
  536. let cur_sku_arr = {
  537. canBuyNum: goods.total,
  538. spuName: goods.goodsname,
  539. actPrice: goods.actPrice,
  540. marketPrice: goods.marketPrice,
  541. stock: goods.total,
  542. skuImage: goods.image_thumb
  543. }
  544. that.setData({
  545. sku: [],
  546. sku_val: 1,
  547. cur_sku_arr: cur_sku_arr,
  548. skuList: [],
  549. visible: true,
  550. showSku: true
  551. })
  552. // let formIds = {
  553. // detail: {
  554. // formId: ""
  555. // }
  556. // };
  557. // formIds.detail.formId = "the formId is a mock one";
  558. // that.gocarfrom(formIds);
  559. //todo...addcart
  560. }
  561. },
  562. /**
  563. * 确认购物车
  564. */
  565. gocarfrom: function(e) {
  566. var that = this;
  567. var is_just_addcar = this.data.is_just_addcar;
  568. wx.showLoading();
  569. var token = wx.getStorageSync('token');
  570. app.util.request({
  571. 'url': 'entry/wxapp/user',
  572. 'data': {
  573. controller: 'user.get_member_form_id',
  574. 'token': token,
  575. "from_id": e.detail.formId
  576. },
  577. dataType: 'json',
  578. success: function(res) {}
  579. })
  580. that.goOrder();
  581. },
  582. /**
  583. * 关闭购物车
  584. */
  585. closeSku: function() {
  586. this.setData({
  587. visible: 0,
  588. stopClick: false,
  589. });
  590. },
  591. goOrder: function() {
  592. var that = this;
  593. if (that.data.can_car) {
  594. that.data.can_car = false;
  595. }
  596. let open_man_orderbuy = this.data.open_man_orderbuy;
  597. if (open_man_orderbuy == 1 && this.data.is_just_addcar==0){
  598. let man_orderbuy_money = this.data.man_orderbuy_money*1;
  599. let sku_val = this.data.sku_val;
  600. let cur_sku_arr = this.data.cur_sku_arr;
  601. let actPrice = cur_sku_arr.actPrice[0] + '.' + cur_sku_arr.actPrice[1];
  602. console.log(actPrice * 1 * sku_val);
  603. if (actPrice * 1 * sku_val < man_orderbuy_money){
  604. wx.showToast({
  605. title: '满' + man_orderbuy_money + '元可下单!',
  606. icon: 'none'
  607. })
  608. return false;
  609. }
  610. }
  611. var token = wx.getStorageSync('token');
  612. var community = wx.getStorageSync('community');
  613. var goods_id = that.data.goods_id;
  614. var community_id = community.communityId;
  615. var quantity = that.data.sku_val;
  616. var cur_sku_arr = that.data.cur_sku_arr;
  617. var sku_str = '';
  618. var is_just_addcar = that.data.is_just_addcar;
  619. if (cur_sku_arr && cur_sku_arr.option_item_ids) {
  620. sku_str = cur_sku_arr.option_item_ids;
  621. }
  622. //addCar_goodsid: goods_id
  623. app.util.request({
  624. 'url': 'entry/wxapp/user',
  625. 'data': {
  626. controller: 'car.add',
  627. 'token': token,
  628. "goods_id": goods_id,
  629. "community_id": community_id,
  630. "quantity": quantity,
  631. "sku_str": sku_str,
  632. "buy_type": 'dan',
  633. "pin_id": 0,
  634. "is_just_addcar": is_just_addcar
  635. },
  636. dataType: 'json',
  637. method: 'POST',
  638. success: function(res) {
  639. //console.log(res);
  640. if (res.data.code == 3) {
  641. wx.showToast({
  642. title: res.data.msg,
  643. icon: 'none',
  644. duration: 2000
  645. })
  646. } else if (res.data.code == 4) {
  647. wx.showToast({
  648. title: '您未登录',
  649. duration: 2000,
  650. })
  651. that.setData({
  652. is_login: false
  653. })
  654. } else if (res.data.code == 6) {
  655. var msg = res.data.msg;
  656. let max_quantity = res.data.max_quantity || '';
  657. (max_quantity > 0) && that.setData({ sku_val: max_quantity })
  658. wx.showToast({
  659. title: msg,
  660. icon: 'none',
  661. duration: 2000
  662. })
  663. } else {
  664. if (is_just_addcar == 1) {
  665. that.closeSku();
  666. wx.showToast({
  667. title: "已加入购物车",
  668. image: "../../images/addShopCart.png"
  669. })
  670. app.globalData.cartNum = res.data.total
  671. that.setData({
  672. cartNum: res.data.total
  673. });
  674. status.indexListCarCount(goods_id);
  675. } else {
  676. var is_limit = res.data.is_limit_distance_buy;
  677. var pages_all = getCurrentPages();
  678. if (pages_all.length > 3) {
  679. wx.redirectTo({
  680. url: '/lionfish_comshop/pages/order/placeOrder?type=dan&is_limit=' + is_limit
  681. })
  682. } else {
  683. wx.navigateTo({
  684. url: '/lionfish_comshop/pages/order/placeOrder?type=dan&is_limit=' + is_limit
  685. })
  686. }
  687. }
  688. }
  689. }
  690. })
  691. },
  692. selectSku: function(event) {
  693. var that = this;
  694. let str = event.currentTarget.dataset.type;
  695. let obj = str.split("_");
  696. let arr = that.data.sku;
  697. let temp = {
  698. name: obj[3],
  699. id: obj[2],
  700. index: obj[0],
  701. idx: obj[1]
  702. };
  703. arr.splice(obj[0], 1, temp);
  704. that.setData({
  705. sku: arr
  706. })
  707. var id = '';
  708. for (let i = 0; i < arr.length; i++) {
  709. if (i == arr.length - 1) {
  710. id = id + arr[i]['id'];
  711. } else {
  712. id = id + arr[i]['id'] + "_";
  713. }
  714. }
  715. var options = this.data.skuList;
  716. var cur_sku_arr = options.sku_mu_list[id];
  717. that.setData({
  718. cur_sku_arr: cur_sku_arr
  719. });
  720. console.log(id);
  721. },
  722. submit: function(e) {
  723. var from_id = e.detail.formId;
  724. var token = wx.getStorageSync('token');
  725. app.util.request({
  726. 'url': 'entry/wxapp/user',
  727. 'data': {
  728. controller: 'user.get_member_form_id',
  729. 'token': token,
  730. "from_id": from_id
  731. },
  732. dataType: 'json',
  733. success: function(res) {}
  734. })
  735. },
  736. balance: function(e) {
  737. console.log(3);
  738. this.setData({
  739. is_just_addcar: 0
  740. })
  741. //加入购物车
  742. this.openSku();
  743. },
  744. /**
  745. * 数量加减
  746. */
  747. setNum: function(event) {
  748. let types = event.currentTarget.dataset.type;
  749. var that = this;
  750. var num = 1;
  751. let sku_val = this.data.sku_val * 1;
  752. if (types == 'add') {
  753. num = sku_val + 1;
  754. } else if (types == 'decrease') {
  755. if (sku_val > 1) {
  756. num = sku_val - 1;
  757. }
  758. }
  759. let arr = that.data.sku;
  760. var options = this.data.skuList;
  761. if (arr.length > 0) {
  762. var id = '';
  763. for (let i = 0; i < arr.length; i++) {
  764. if (i == arr.length - 1) {
  765. id = id + arr[i]['id'];
  766. } else {
  767. id = id + arr[i]['id'] + "_";
  768. }
  769. }
  770. }
  771. if (options.length > 0) {
  772. var cur_sku_arr = options.sku_mu_list[id];
  773. if (num > cur_sku_arr['canBuyNum']) {
  774. num = num - 1;
  775. }
  776. } else {
  777. let cur_sku_arr = this.data.cur_sku_arr;
  778. if (num > cur_sku_arr['canBuyNum']) {
  779. num = num - 1;
  780. }
  781. }
  782. this.setData({
  783. sku_val: num
  784. })
  785. },
  786. scrollImagesChange: function(t) {
  787. this.videoContext.pause();
  788. this.setData({
  789. fmShow: true,
  790. goodsIndex: t.detail.current + 1
  791. });
  792. },
  793. share_handler: function() {
  794. this.setData({
  795. is_share_html: false
  796. })
  797. },
  798. hide_share_handler: function() {
  799. this.setData({
  800. is_share_html: true
  801. })
  802. },
  803. share_quan: function() {
  804. wx.showLoading({
  805. title: '获取中',
  806. })
  807. var token = wx.getStorageSync('token');
  808. var community = wx.getStorageSync('community');
  809. var goods_id = this.data.order.goods_id;
  810. var community_id = community.communityId;
  811. var that = this;
  812. app.util.request({
  813. 'url': 'entry/wxapp/index',
  814. 'data': {
  815. controller: 'goods.get_user_goods_qrcode',
  816. "token": token,
  817. community_id: community_id,
  818. "goods_id": goods_id
  819. },
  820. dataType: 'json',
  821. success: function(res) {
  822. if (res.data.code == 0) {
  823. setTimeout(function() {
  824. wx.hideLoading()
  825. }, 2000)
  826. var image_path = res.data.image_path;
  827. wx.getImageInfo({
  828. src: image_path,
  829. success: function(res) {
  830. var real_path = res.path;
  831. wx.saveImageToPhotosAlbum({
  832. filePath: real_path,
  833. success(res) {
  834. wx.showToast({
  835. title: '图片保存成功,可以分享了',
  836. icon: 'none',
  837. duration: 2000
  838. })
  839. that.setData({
  840. is_share_html: true
  841. });
  842. }
  843. })
  844. }
  845. })
  846. }
  847. }
  848. })
  849. },
  850. /**
  851. * 生命周期函数--监听页面显示
  852. */
  853. onShow: function() {
  854. this.setData({
  855. cartNum: app.globalData.cartNum,
  856. stopNotify: false
  857. });
  858. },
  859. onReady: function (res) {
  860. this.videoContext = wx.createVideoContext('myVideo');
  861. },
  862. /**
  863. * 生命周期函数--监听页面隐藏
  864. */
  865. onHide: function() {
  866. this.setData({ stopNotify: true })
  867. console.log('详情页hide', this.data.stopNotify)
  868. },
  869. /**
  870. * 生命周期函数--监听页面卸载
  871. */
  872. onUnload: function() {
  873. console.log('onUnload')
  874. this.setData({ stopNotify: true })
  875. console.log('详情页unload', this.data.stopNotify);
  876. detailClearTime = null;
  877. clearTimeout(detailClearTime);
  878. },
  879. /**
  880. * 获取分享图并显示
  881. * 20181225 新形式
  882. */
  883. get_share_img: function () {
  884. wx.showLoading();
  885. let shareImgUrl = this.data.shareImgUrl;
  886. if (shareImgUrl != '') {
  887. wx.hideLoading();
  888. this.setData({
  889. hideModal: false,
  890. is_share_html: true
  891. })
  892. } else {
  893. var token = wx.getStorageSync('token');
  894. var community = wx.getStorageSync('community');
  895. var goods_id = this.data.goods_id;
  896. var community_id = community.communityId;
  897. var that = this;
  898. app.util.request({
  899. 'url': 'entry/wxapp/index',
  900. 'data': {
  901. controller: 'goods.get_user_goods_qrcode',
  902. "token": token,
  903. community_id: community_id,
  904. "goods_id": goods_id
  905. },
  906. dataType: 'json',
  907. success: function (res) {
  908. if (res.data.code == 0) {
  909. wx.hideLoading();
  910. var image_path = res.data.image_path;
  911. wx.previewImage({
  912. current: image_path, // 当前显示图片的http链接
  913. urls: [image_path] // 需要预览的图片http链接列表
  914. })
  915. // that.setData({
  916. // shareImgUrl: image_path,
  917. // is_share_html: true,
  918. // hideModal: false
  919. // })
  920. }
  921. }
  922. })
  923. }
  924. },
  925. closeShareModal: function () {
  926. this.setData({ hideModal: true })
  927. },
  928. /**
  929. * 展开收起
  930. */
  931. bindOpen: function (t) {
  932. var idx = t.currentTarget.dataset.idx;
  933. console.log(idx)
  934. if (this.data.comment_list[idx].isOpen) {
  935. this.data.comment_list[idx].isOpen = false;
  936. var comment_list = this.data.comment_list;
  937. this.setData({
  938. comment_list: comment_list
  939. });
  940. } else {
  941. this.data.comment_list[idx].isOpen = true;
  942. var comment_list = this.data.comment_list;
  943. this.setData({
  944. comment_list: comment_list
  945. });
  946. }
  947. },
  948. /**
  949. * 保存分享图并显示
  950. * 20181225 新形式
  951. */
  952. saveThumb: function (e) {
  953. wx.showLoading();
  954. let that = this;
  955. var image_path = this.data.shareImgUrl;
  956. wx.getImageInfo({
  957. src: image_path,
  958. success: function (res) {
  959. var real_path = res.path;
  960. real_path && wx.saveImageToPhotosAlbum({
  961. filePath: real_path,
  962. success(res) {
  963. console.log(res)
  964. wx.hideLoading();
  965. wx.showToast({
  966. title: '已保存相册',
  967. icon: 'none',
  968. duration: 2000
  969. })
  970. that.setData({
  971. hideModal: true
  972. });
  973. },
  974. fail: function (res) {
  975. wx.hideLoading();
  976. console.log(res)
  977. if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
  978. wx.openSetting({
  979. success(settingdata) {
  980. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  981. console.log("获取权限成功,再次点击图片保存到相册")
  982. } else {
  983. console.log("获取权限失败")
  984. }
  985. }
  986. })
  987. }
  988. }
  989. })
  990. }
  991. })
  992. },
  993. drawImgNoPrice: function () {
  994. var t = this;
  995. wx.createSelectorQuery().select(".canvas-img").boundingClientRect(function () {
  996. const context = wx.createCanvasContext("myCanvas");
  997. context.drawImage(t.goodsImg, 0, 0, status.getPx(375), status.getPx(300));
  998. if (t.data.goods.video) context.drawImage("../../images/play.png", status.getPx(150), status.getPx(105), status.getPx(76), status.getPx(76));
  999. context.save();
  1000. context.restore(), context.draw(false, t.checkCanvasNoPrice());
  1001. }).exec();
  1002. },
  1003. checkCanvasNoPrice: function () {
  1004. var that = this;
  1005. setTimeout(() => {
  1006. wx.canvasToTempFilePath({
  1007. canvasId: "myCanvas",
  1008. success: function (res) {
  1009. res.tempFilePath ? that.imageUrl = res.tempFilePath : that.drawImgNoPrice();
  1010. console.log('我画完了')
  1011. },
  1012. fail: function (a) {
  1013. that.drawImgNoPrice();
  1014. }
  1015. })
  1016. }, 500)
  1017. },
  1018. drawImg: function () {
  1019. let endtime = this.data.endtime;
  1020. let shareTime = (endtime.days > 0 ? endtime.days + '天' : '') + endtime.hours + ':' + endtime.minutes + ':' + endtime.seconds;
  1021. var t = this;
  1022. wx.createSelectorQuery().select(".canvas-img").boundingClientRect(function () {
  1023. const context = wx.createCanvasContext("myCanvas");
  1024. context.font = "28px Arial";
  1025. var e = context.measureText("¥").width + 2;
  1026. var o = context.measureText(t.data.goods.price_front + "." + t.data.goods.price_after).width;
  1027. context.font = "17px Arial";
  1028. var s = context.measureText("¥" + t.data.goods.productprice).width + 3,
  1029. n = context.measureText("累计销售 " + t.data.goods.seller_count).width,
  1030. u = context.measureText("· 剩余" + t.data.goods.total + " ").width + 10;
  1031. context.font = "18px Arial";
  1032. var r = context.measureText("距结束").width;
  1033. var d = context.measureText(shareTime).width + 10;
  1034. context.drawImage(t.goodsImg, 0, 0, status.getPx(375), status.getPx(300));
  1035. context.drawImage("../../images/shareBottomBg.png", status.getPx(0), status.getPx(225), status.getPx(375), status.getPx(75));
  1036. if (t.data.goods.video) context.drawImage("../../images/play.png", status.getPx(149.5), status.getPx(74.5), status.getPx(76), status.getPx(76));
  1037. context.save();
  1038. status.drawText(context, { color: "#ffffff", size: 28, textAlign: "left" }, "¥", status.getPx(6), status.getPx(267), status.getPx(e));
  1039. status.drawText(context, { color: "#ffffff", size: 28, textAlign: "left" }, t.data.goods.price_front + "." + t.data.goods.price_after,
  1040. status.getPx(e), status.getPx(267), status.getPx(o));
  1041. context.restore();
  1042. context.save();
  1043. context.restore(),
  1044. context.save(),
  1045. (0, status.drawText)(context,
  1046. { color: "#ffffff", size: 15, textAlign: "left" },
  1047. "¥" + t.data.goods.productprice,
  1048. (0, status.getPx)(e + o + 10),
  1049. (0, status.getPx)(267),
  1050. (0, status.getPx)(s)
  1051. ),
  1052. context.restore(),
  1053. context.save(),
  1054. (0, status.drawText)(
  1055. context,
  1056. { color: "#ffffff", size: 17, textAlign: "left" },
  1057. "累计销售" + t.data.goods.seller_count,
  1058. (0, status.getPx)(10),
  1059. (0, status.getPx)(290),
  1060. (0, status.getPx)(n)
  1061. ),
  1062. context.restore(),
  1063. context.save(),
  1064. (0, status.drawText)(context,
  1065. { color: "#ffffff", size: 17, textAlign: "left" },
  1066. "· 剩余" + t.data.goods.total,
  1067. (0, status.getPx)(n + 10),
  1068. (0, status.getPx)(290),
  1069. (0, status.getPx)(u)
  1070. ),
  1071. context.restore(),
  1072. context.save(),
  1073. context.beginPath(),
  1074. context.setStrokeStyle("white"),
  1075. context.moveTo((0, status.getPx)(e + o + 10),
  1076. (0, status.getPx)(261)),
  1077. context.lineTo((0, status.getPx)(e + o + s + 15),
  1078. (0, status.getPx)(261)),
  1079. context.stroke(),
  1080. context.restore(),
  1081. context.save(),
  1082. (0, status.drawText)(context,
  1083. { color: "#F8E71C", size: 18, textAlign: "center" },
  1084. "距结束",
  1085. (0, status.getPx)(318),
  1086. (0, status.getPx)(260),
  1087. (0, status.getPx)(r)
  1088. ),
  1089. context.restore(),
  1090. context.save(),
  1091. (0, status.drawText)(context, { color: "#F8E71C", size: 18, textAlign: "center" },
  1092. shareTime,
  1093. (0, status.getPx)(315),
  1094. (0, status.getPx)(288),
  1095. (0, status.getPx)(d)
  1096. ),
  1097. context.restore();
  1098. context.draw(false, t.checkCanvas());
  1099. }).exec();
  1100. },
  1101. checkCanvas: function () {
  1102. var that = this;
  1103. setTimeout(() => {
  1104. wx.canvasToTempFilePath({
  1105. canvasId: "myCanvas",
  1106. success: function (res) {
  1107. res.tempFilePath ? that.imageUrl = res.tempFilePath : that.drawImg();
  1108. console.log('我画完了')
  1109. },
  1110. fail: function (a) {
  1111. that.drawImg();
  1112. }
  1113. })
  1114. }, 500)
  1115. },
  1116. previewImg: function(e){
  1117. let idx = e.currentTarget.dataset.idx || 0;
  1118. let prevImgArr = this.data.prevImgArr;
  1119. wx.previewImage({
  1120. current: prevImgArr[idx],
  1121. urls: prevImgArr
  1122. })
  1123. },
  1124. /**
  1125. * 播放视频隐藏封面图
  1126. */
  1127. btnPlay: function () {
  1128. this.setData({
  1129. fmShow: false
  1130. })
  1131. this.videoContext.play();
  1132. },
  1133. videEnd: function(){
  1134. this.setData({
  1135. fmShow: true
  1136. })
  1137. },
  1138. endPlay: function(){
  1139. this.videoContext.pause();
  1140. this.setData({
  1141. fmShow: true
  1142. })
  1143. },
  1144. // 显示群主二维码
  1145. showGroupCode: function(){
  1146. let group_share_info = this.data.group_share_info;
  1147. let imgUrl = group_share_info.share_wxcode || '';
  1148. imgUrl && wx.previewImage({
  1149. current: imgUrl, // 当前显示图片的http链接
  1150. urls: [imgUrl] // 需要预览的图片http链接列表
  1151. })
  1152. // wx.showToast({
  1153. // title: '长按保存',
  1154. // icon: 'none',
  1155. // duration: 3000
  1156. // })
  1157. },
  1158. /**
  1159. * 用户点击右上角分享
  1160. */
  1161. onShareAppMessage: function() {
  1162. var community = wx.getStorageSync('community');
  1163. var goods_id = this.data.goods_id;
  1164. var community_id = community.communityId;
  1165. var share_title = this.data.share_title;
  1166. var share_id = wx.getStorageSync('member_id');
  1167. var share_path = 'lionfish_comshop/pages/goods/goodsDetail?id=' + this.data.goods_id + '&share_id=' + share_id + '&community_id=' + community_id;
  1168. let shareImg = this.data.goods.goods_share_image;
  1169. console.log('商品分享地址:');
  1170. console.log(share_path);
  1171. var that = this;
  1172. that.setData({is_share_html: true, hideModal: true})
  1173. return {
  1174. title: share_title,
  1175. path: share_path,
  1176. imageUrl: shareImg ? shareImg:that.imageUrl,
  1177. success: function(res) {
  1178. // 转发成功
  1179. },
  1180. fail: function(res) {
  1181. // 转发失败
  1182. }
  1183. }
  1184. }
  1185. })