index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. var util = require('../../utils/util.js');
  2. var status = require('../../utils/index.js');
  3. var countDownInit = require("../../utils/countDown");
  4. var a = require("../../utils/public");
  5. var app = getApp();
  6. Page({
  7. mixins: [countDownInit.default, require('../../mixin/globalMixin.js')],
  8. data: {
  9. classification: {
  10. tabs: [],
  11. activeIndex: -1
  12. },
  13. rushList: [],
  14. pageNum: 1
  15. },
  16. $data: {
  17. stickyFlag: false,
  18. scrollTop: 0,
  19. overPageNum: 1,
  20. loadOver: false,
  21. hasOverGoods: false,
  22. countDownMap: {},
  23. actEndMap: {},
  24. timer: {},
  25. scrollHeight: 1300,
  26. stickyTop: 0,
  27. },
  28. /**
  29. * is_only_express
  30. */
  31. onLoad: function (options) {
  32. app.setShareConfig();
  33. let that = this;
  34. status.setNavBgColor();
  35. status.setGroupInfo().then((groupInfo) => {
  36. that.setData({
  37. groupInfo
  38. })
  39. });
  40. },
  41. loadPage: function () {
  42. wx.showLoading();
  43. this.hasRefeshin = false;
  44. this.$data = {
  45. ...this.$data,
  46. ...{
  47. overPageNum: 1,
  48. loadOver: false,
  49. hasOverGoods: false,
  50. countDownMap: {},
  51. actEndMap: {},
  52. timer: {},
  53. hasCommingGoods: true
  54. }
  55. }
  56. this.setData({
  57. rushList: [],
  58. pageNum: 1
  59. }, ()=>{
  60. this.getIndexInfo();
  61. this.load_goods_data();
  62. })
  63. },
  64. /**
  65. * 生命周期函数--监听页面显示
  66. */
  67. onShow: function () {
  68. let that = this;
  69. util.check_login_new().then((res) => {
  70. if (!res) {
  71. that.setData({
  72. needAuth: true
  73. })
  74. } else {
  75. (0, status.cartNum)('', true).then((res) => {
  76. that.setData({
  77. cartNum: res.data
  78. })
  79. });
  80. }
  81. })
  82. this.loadPage();
  83. },
  84. /**
  85. * 监控滚动事件
  86. */
  87. onPageScroll: function (t) {
  88. if (!this.$data.isLoadData) {
  89. if (t.scrollTop < this.$data.scrollHeight) {
  90. if (t.scrollTop > this.$data.scrollTop) {
  91. "down" !== this.data.scrollDirect && this.setData({
  92. scrollDirect: "down"
  93. })
  94. } else {
  95. "up" != this.data.scrollDirect && this.setData({
  96. scrollDirect: "up"
  97. })
  98. }
  99. } else {
  100. "down" !== this.data.scrollDirect && this.setData({
  101. scrollDirect: "down"
  102. })
  103. }
  104. if (t.scrollTop > this.$data.stickyTop) {
  105. this.data.isSticky || (this.setData({
  106. isSticky: true
  107. }), this.$data.stickyFlag = true)
  108. } else {
  109. t.scrollTop < this.$data.stickyBackTop && this.data.isSticky && (this.setData({
  110. isSticky: false
  111. }), this.$data.stickyFlag = false)
  112. }
  113. this.$data.scrollTop = t.scrollTop
  114. }
  115. },
  116. /**
  117. * 页面相关事件处理函数--监听用户下拉动作
  118. */
  119. onPullDownRefresh: function () {
  120. this.loadPage();
  121. wx.stopPullDownRefresh();
  122. },
  123. /**
  124. * 页面上拉触底事件的处理函数
  125. */
  126. onReachBottom: function () {
  127. this.load_goods_data();
  128. },
  129. authModal: function (e = {}) {
  130. let needAuth = (e && e.detail) || this.data.needAuth;
  131. if (this.data.needAuth || e.detail) {
  132. this.setData({
  133. showAuthModal: !this.data.showAuthModal,
  134. needAuth
  135. });
  136. return false;
  137. }
  138. return true;
  139. },
  140. /**
  141. * 授权成功回调
  142. */
  143. authSuccess: function () {
  144. this.setData({
  145. needAuth: false
  146. })
  147. this.loadPage();
  148. },
  149. /**
  150. * 导航小图标
  151. */
  152. getIndexInfo: function () {
  153. let that = this;
  154. app.util.request({
  155. url: 'entry/wxapp/index',
  156. data: {
  157. controller: 'generalmall.get_index_info'
  158. },
  159. dataType: 'json',
  160. success: function (res) {
  161. if (res.data.code == 0) {
  162. let {
  163. navigat_list,
  164. slider_list,
  165. category_list,
  166. shoname,
  167. index_type_first_name,
  168. theme,
  169. index_list_top_image,
  170. showTabbar,
  171. is_show_list_timer,
  172. is_show_list_count
  173. } = res.data.data;
  174. wx.setNavigationBarTitle({
  175. title: shoname
  176. });
  177. let navigatEmpty = [];
  178. if (navigat_list.length > 0) {
  179. let len = (5 - navigat_list.length % 5) || 0;
  180. if (len < 5 && len > 0) navigatEmpty = new Array(len);
  181. }
  182. let h = {
  183. classification: {
  184. tabs: ''
  185. }
  186. };
  187. h.index_type_first_name = index_type_first_name || '全部';
  188. if (category_list.length > 0) {
  189. category_list.unshift({
  190. name: index_type_first_name || '全部',
  191. id: 0
  192. })
  193. h.isShowClassification = true;
  194. h.classification.tabs = category_list;
  195. } else {
  196. h.isShowClassification = false;
  197. }
  198. that.setData({
  199. navigat: navigat_list,
  200. navigatEmpty,
  201. slider_list,
  202. theme,
  203. index_list_top_image,
  204. showTabbar,
  205. isShowListTimer: is_show_list_timer,
  206. isShowListCount: is_show_list_count,
  207. ...h
  208. })
  209. }
  210. }
  211. })
  212. },
  213. /**
  214. * 幻灯片跳转
  215. */
  216. goBannerUrl: function (t) {
  217. let idx = t.currentTarget.dataset.idx;
  218. let {
  219. slider_list,
  220. needAuth
  221. } = this.data;
  222. if (slider_list.length > 0) {
  223. let url = slider_list[idx].link;
  224. let type = slider_list[idx].linktype;
  225. if (util.checkRedirectTo(url, needAuth)) {
  226. this.authModal();
  227. return;
  228. }
  229. if (type == 0) {
  230. // 跳转webview
  231. url && wx.navigateTo({
  232. url: '/lionfish_comshop/pages/web-view?url=' + encodeURIComponent(url)
  233. })
  234. } else if (type == 1) {
  235. if (url.indexOf('lionfish_comshop/pages/index/index') != -1 || url.indexOf('lionfish_comshop/pages/order/shopCart') != -1 || url.indexOf('lionfish_comshop/pages/user/me') != -1 || url.indexOf('lionfish_comshop/pages/type/index') != -1) {
  236. url && wx.switchTab({
  237. url: url
  238. })
  239. } else {
  240. url && wx.navigateTo({
  241. url: url
  242. })
  243. }
  244. } else if (type == 2) {
  245. // 跳转小程序
  246. let appid = slider_list[idx].appid;
  247. appid && wx.navigateToMiniProgram({
  248. appId: slider_list[idx].appid,
  249. path: url,
  250. extraData: {},
  251. envVersion: 'release',
  252. success(res) {
  253. // 打开成功
  254. },
  255. fail(error) {
  256. console.log(error)
  257. }
  258. })
  259. }
  260. }
  261. },
  262. /**
  263. * 导航图标跳转
  264. */
  265. goNavUrl: function (t) {
  266. let idx = t.currentTarget.dataset.idx;
  267. let {
  268. navigat,
  269. needAuth
  270. } = this.data;
  271. if (navigat.length > 0) {
  272. let url = navigat[idx].link;
  273. let type = navigat[idx].type;
  274. if (util.checkRedirectTo(url, needAuth)) {
  275. this.authModal();
  276. return;
  277. }
  278. if (type == 0) {
  279. // 跳转webview
  280. wx.navigateTo({
  281. url: '/lionfish_comshop/pages/web-view?url=' + encodeURIComponent(url),
  282. })
  283. } else if (type == 1) {
  284. if (url.indexOf('lionfish_comshop/pages/index/index') != -1 || url.indexOf('lionfish_comshop/pages/order/shopCart') != -1 || url.indexOf('lionfish_comshop/pages/user/me') != -1 || url.indexOf('lionfish_comshop/pages/type/index') != -1) {
  285. wx.switchTab({
  286. url: url
  287. })
  288. } else {
  289. wx.navigateTo({
  290. url: url
  291. })
  292. }
  293. } else if (type == 2) {
  294. // 跳转小程序
  295. let appid = navigat[idx].appid;
  296. appid && wx.navigateToMiniProgram({
  297. appId: navigat[idx].appid,
  298. path: url,
  299. extraData: {},
  300. envVersion: 'release',
  301. success(res) {
  302. // 打开成功
  303. },
  304. fail(error) {
  305. console.log(error)
  306. }
  307. })
  308. } else if (type == 3) {
  309. //首页分类
  310. let classification = this.data.classification;
  311. let tabs = classification && classification.tabs;
  312. let cid = url;
  313. let activeIdx = tabs.findIndex((p) => {
  314. return p.id == cid
  315. });
  316. if (activeIdx != -1) {
  317. let cateInfo = {
  318. detail: {
  319. e: activeIdx,
  320. a: cid
  321. }
  322. };
  323. this.classificationChange(cateInfo);
  324. }
  325. } else if (type == 4) {
  326. //独立分类
  327. app.globalData.typeCateId = url;
  328. wx.switchTab({
  329. url: '/lionfish_comshop/pages/type/index'
  330. })
  331. }
  332. }
  333. },
  334. /**
  335. * 监控分类导航
  336. */
  337. classificationChange: function (t) {
  338. wx.showLoading();
  339. var that = this;
  340. this.$data = {
  341. ...this.$data,
  342. ...{
  343. overPageNum: 1,
  344. loadOver: false,
  345. hasOverGoods: false,
  346. countDownMap: {},
  347. actEndMap: {},
  348. timer: {}
  349. }
  350. }, this.hasRefeshin = false, this.setData({
  351. rushList: [],
  352. showEmpty: false,
  353. pageNum: 1,
  354. "classification.activeIndex": t.detail.e,
  355. classificationId: t.detail.a
  356. }, function () {
  357. if (!this.$data.stickyFlag && (that.$data.scrollTop != that.$data.stickyTop + 5)) {
  358. wx.pageScrollTo({
  359. scrollTop: that.$data.stickyTop - 30,
  360. duration: 0
  361. })
  362. }
  363. that.load_goods_data();
  364. });
  365. },
  366. /**
  367. * 获取商品列表
  368. */
  369. load_goods_data: function () {
  370. var token = wx.getStorageSync('token');
  371. var that = this;
  372. // var cur_community = wx.getStorageSync('community');
  373. var gid = that.data.classificationId;
  374. this.$data.isLoadData = true;
  375. console.log('load_goods_begin ');
  376. if (!that.hasRefeshin && !that.$data.loadOver) {
  377. console.log('load_goods_in ');
  378. this.hasRefeshin = true;
  379. that.setData({
  380. loadMore: true
  381. });
  382. app.util.request({
  383. 'url': 'entry/wxapp/index',
  384. 'data': {
  385. controller: 'index.load_gps_goodslist',
  386. token: token,
  387. pageNum: that.data.pageNum,
  388. head_id: '',
  389. gid,
  390. per_page: 12,
  391. is_only_express: 1
  392. },
  393. dataType: 'json',
  394. success: function (res) {
  395. if (that.data.pageNum == 1) {
  396. that.setData({
  397. cate_info: res.data.cate_info || {}
  398. })
  399. }
  400. if (res.data.code == 0) {
  401. let rushList = '';
  402. if (res.data.is_show_list_timer == 1) {
  403. rushList = that.transTime(res.data.list);
  404. for (let s in that.$data.countDownMap) that.initCountDown(that.$data.countDownMap[s]);
  405. } else {
  406. let oldRushList = that.data.rushList;
  407. rushList = oldRushList.concat(res.data.list);
  408. }
  409. let rdata = res.data;
  410. let {
  411. full_money,
  412. full_reducemoney,
  413. is_open_fullreduction,
  414. is_open_vipcard_buy,
  415. is_vip_card_member,
  416. is_member_level_buy
  417. } = rdata;
  418. let reduction = {
  419. full_money,
  420. full_reducemoney,
  421. is_open_fullreduction
  422. }
  423. // 是否可以会员折扣购买
  424. let canLevelBuy = false;
  425. if (is_open_vipcard_buy == 1) {
  426. if (is_vip_card_member != 1 && is_member_level_buy == 1) canLevelBuy = true;
  427. } else {
  428. (is_member_level_buy == 1) && (canLevelBuy = true);
  429. }
  430. if (that.data.pageNum == 1) that.setData({
  431. copy_text_arr: rdata.copy_text_arr || []
  432. })
  433. that.hasRefeshin = false;
  434. that.setData({
  435. rushList: rushList,
  436. pageNum: that.data.pageNum + 1,
  437. loadMore: false,
  438. reduction,
  439. tip: '',
  440. is_open_vipcard_buy: is_open_vipcard_buy || 0,
  441. is_vip_card_member,
  442. is_member_level_buy,
  443. canLevelBuy
  444. }, () => {
  445. if (that.isFirst == 1) {
  446. that.isFirst++;
  447. if (rushList.length && !that.$data.stickyTop) {
  448. wx.createSelectorQuery().select(".tab-nav-query").boundingClientRect(function (t) {
  449. if (t && t.top) {
  450. wcache.put('tabPos', t);
  451. that.$data.stickyTop = t.top + t.height, that.$data.stickyBackTop = t.top;
  452. } else {
  453. let tabpos = wcache.get('tabPos', false);
  454. if (tabpos) that.$data.stickyTop = tabpos.top + tabpos.height, that.$data.stickyBackTop = tabpos.top;
  455. }
  456. }).exec();
  457. that.$data.scrollTop > that.$data.stickyTop && wx.pageScrollTo({
  458. duration: 0,
  459. scrollTop: that.$data.stickyTop + 4
  460. });
  461. }
  462. }
  463. that.getScrollHeight();
  464. if (that.data.pageNum == 2 && res.data.list.length < 10) {
  465. console.log('load_over_goods_list_begin')
  466. that.$data.loadOver = true;
  467. that.hasRefeshin = true;
  468. that.setData({
  469. loadMore: true
  470. }, () => {
  471. that.load_over_gps_goodslist();
  472. });
  473. }
  474. });
  475. } else if (res.data.code == 1) {
  476. that.$data.loadOver = true;
  477. that.load_over_gps_goodslist();
  478. } else if (res.data.code == 2) {
  479. //no login
  480. that.setData({
  481. needAuth: true,
  482. couponRefresh: false
  483. })
  484. }
  485. },
  486. complete: function () {
  487. that.$data.isLoadData = false;
  488. // wx.hideLoading();
  489. setTimeout(function () {
  490. wx.hideLoading();
  491. }, 1000);
  492. }
  493. })
  494. } else {
  495. that.load_over_gps_goodslist();
  496. }
  497. },
  498. /**
  499. * 组合倒计时时间
  500. */
  501. transTime: function (list) {
  502. let that = this;
  503. let e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0;
  504. e === 0 && list.map(function (t) {
  505. t.end_time *= 1000;
  506. that.$data.countDownMap[t.end_time] = t.end_time, that.$data.actEndMap[t.end_time] = t.end_time <= new Date().getTime() || t.spuCanBuyNum == 0;
  507. })
  508. return that.data.rushList.concat(list);
  509. },
  510. /**
  511. * 获取售罄商品
  512. */
  513. load_over_gps_goodslist: function () {
  514. var token = wx.getStorageSync('token');
  515. var that = this;
  516. // var cur_community = wx.getStorageSync('community');
  517. var gid = that.data.classificationId;
  518. if (!that.$data.hasOverGoods && that.$data.loadOver) {
  519. that.$data.hasOverGoods = true;
  520. that.setData({
  521. loadMore: true
  522. });
  523. app.util.request({
  524. url: 'entry/wxapp/index',
  525. data: {
  526. controller: 'index.load_over_gps_goodslist',
  527. token: token,
  528. pageNum: that.$data.overPageNum,
  529. head_id: '',
  530. gid,
  531. is_index_show: 1,
  532. is_only_express: 1
  533. },
  534. dataType: 'json',
  535. success: function (res) {
  536. if (res.data.code == 0) {
  537. let rushList = that.transTime(res.data.list);
  538. for (let s in that.$data.countDownMap) that.initCountDown(that.$data.countDownMap[s]);
  539. that.$data.hasOverGoods = false;
  540. that.$data.overPageNum += 1;
  541. that.setData({
  542. rushList: rushList,
  543. loadMore: false,
  544. tip: ''
  545. }, () => {
  546. if (that.isFirst == 1) {
  547. that.isFirst++;
  548. if (rushList.length && !that.$data.stickyTop) {
  549. wx.createSelectorQuery().select(".tab-nav-query").boundingClientRect(function (t) {
  550. if (t && t.top) {
  551. wcache.put('tabPos', t);
  552. that.$data.stickyTop = t.top + t.height, that.$data.stickyBackTop = t.top;
  553. } else {
  554. let tabpos = wcache.get('tabPos', false);
  555. if (tabpos) that.$data.stickyTop = tabpos.top + tabpos.height, that.$data.stickyBackTop = tabpos.top;
  556. }
  557. }).exec();
  558. that.$data.scrollTop > that.$data.stickyTop && wx.pageScrollTo({
  559. duration: 0,
  560. scrollTop: that.$data.stickyTop + 4
  561. });
  562. }
  563. }
  564. that.getScrollHeight();
  565. });
  566. } else if (res.data.code == 1) {
  567. if (that.$data.overPageNum == 1 && that.data.rushList.length == 0) that.setData({
  568. showEmpty: true
  569. })
  570. that.setData({
  571. loadMore: false,
  572. tip: '^_^已经到底了'
  573. })
  574. } else if (res.data.code == 2) {
  575. that.setData({
  576. needAuth: true,
  577. couponRefresh: false
  578. })
  579. }
  580. that.$data.isLoadData = false;
  581. }
  582. })
  583. } else {
  584. that.$data.isLoadData = false;
  585. }
  586. },
  587. getScrollHeight: function () {
  588. wx.createSelectorQuery().select('.rush-list-box').boundingClientRect((rect) => {
  589. rect && rect.height && (this.$data.scrollHeight = rect.height || 1300);
  590. console.log(this.$data.scrollHeight)
  591. }).exec()
  592. },
  593. openSku: function (t) {
  594. if (!this.authModal()) return;
  595. var that = this,
  596. e = t.detail;
  597. var goods_id = e.actId;
  598. var options = e.skuList;
  599. that.setData({
  600. addCar_goodsid: goods_id
  601. })
  602. let list = options.list || [];
  603. let arr = [];
  604. if (list && list.length > 0) {
  605. for (let i = 0; i < list.length; i++) {
  606. let sku = list[i]['option_value'][0];
  607. console.log(sku)
  608. let temp = {
  609. name: sku['name'],
  610. id: sku['option_value_id'],
  611. index: i,
  612. idx: 0
  613. };
  614. arr.push(temp);
  615. }
  616. var id = '';
  617. for (let i = 0; i < arr.length; i++) {
  618. if (i == arr.length - 1) {
  619. id = id + arr[i]['id'];
  620. } else {
  621. id = id + arr[i]['id'] + "_";
  622. }
  623. }
  624. var cur_sku_arr = options.sku_mu_list[id] || {};
  625. that.setData({
  626. sku: arr,
  627. sku_val: 1,
  628. cur_sku_arr: cur_sku_arr,
  629. skuList: e.skuList,
  630. visible: true,
  631. showSku: true,
  632. is_take_vipcard: e.is_take_vipcard || '',
  633. is_mb_level_buy: e.is_mb_level_buy || ''
  634. });
  635. } else {
  636. let goodsInfo = e.allData;
  637. that.setData({
  638. sku: [],
  639. sku_val: 1,
  640. skuList: [],
  641. cur_sku_arr: goodsInfo
  642. })
  643. let formIds = {
  644. detail: {
  645. formId: ""
  646. }
  647. };
  648. formIds.detail.formId = "the formId is a mock one";
  649. that.gocarfrom(formIds);
  650. }
  651. },
  652. /**
  653. * 确认加入购物车
  654. */
  655. gocarfrom: function (e) {
  656. var that = this;
  657. wx.showLoading();
  658. this.setData({
  659. is_take_vipcard: '',
  660. is_mb_level_buy: ''
  661. })
  662. a.collectFormIds(e.detail.formId);
  663. that.goOrder();
  664. },
  665. /**
  666. * 关闭购物车选项卡
  667. */
  668. closeSku: function () {
  669. this.setData({
  670. visible: 0,
  671. stopClick: false,
  672. });
  673. },
  674. selectSku: function (event) {
  675. var that = this;
  676. let str = event.currentTarget.dataset.type;
  677. let obj = str.split("_");
  678. let {
  679. sku,
  680. skuList,
  681. sku_val
  682. } = this.data;
  683. let temp = {
  684. name: obj[3],
  685. id: obj[2],
  686. index: obj[0],
  687. idx: obj[1]
  688. };
  689. sku.splice(obj[0], 1, temp);
  690. var id = '';
  691. for (let i = 0; i < sku.length; i++) {
  692. if (i == sku.length - 1) {
  693. id = id + sku[i]['id'];
  694. } else {
  695. id = id + sku[i]['id'] + "_";
  696. }
  697. }
  698. var cur_sku_arr = skuList.sku_mu_list[id];
  699. let h = {};
  700. sku_val = sku_val || 1;
  701. if (sku_val > cur_sku_arr.canBuyNum) {
  702. h.sku_val = cur_sku_arr.canBuyNum;
  703. wx.showToast({
  704. title: `最多只能购买${cur_sku_arr.canBuyNum}件`,
  705. icon: 'none'
  706. })
  707. }
  708. that.setData({
  709. cur_sku_arr,
  710. sku,
  711. ...h
  712. });
  713. },
  714. /**
  715. * 数量加减
  716. */
  717. setNum: function (event) {
  718. let types = event.currentTarget.dataset.type;
  719. var that = this;
  720. var num = 1;
  721. let sku_val = this.data.sku_val * 1;
  722. if (types == 'add') {
  723. num = sku_val + 1;
  724. } else if (types == 'decrease') {
  725. if (sku_val > 1) {
  726. num = sku_val - 1;
  727. }
  728. }
  729. let arr = that.data.sku;
  730. var options = this.data.skuList;
  731. if (arr.length > 0) {
  732. var id = '';
  733. for (let i = 0; i < arr.length; i++) {
  734. if (i == arr.length - 1) {
  735. id = id + arr[i]['id'];
  736. } else {
  737. id = id + arr[i]['id'] + "_";
  738. }
  739. }
  740. }
  741. if (options.length > 0) {
  742. let cur_sku_arr = options.sku_mu_list[id];
  743. if (num > cur_sku_arr['canBuyNum']) {
  744. num = num - 1;
  745. }
  746. } else {
  747. let cur_sku_arr = this.data.cur_sku_arr;
  748. if (num > cur_sku_arr['canBuyNum']) {
  749. num = num - 1;
  750. }
  751. }
  752. this.setData({
  753. sku_val: num
  754. })
  755. },
  756. skuConfirm: function () {
  757. this.closeSku(), (0, status.cartNum)().then((res) => {
  758. res.code == 0 && that.setData({
  759. cartNum: res.data
  760. })
  761. });
  762. },
  763. goOrder: function() {
  764. var that = this;
  765. if (that.data.can_car) {
  766. that.data.can_car = false;
  767. }
  768. var token = wx.getStorageSync('token');
  769. var community = wx.getStorageSync('community');
  770. var goods_id = that.data.addCar_goodsid;
  771. var community_id = community.communityId;
  772. var quantity = that.data.sku_val;
  773. var cur_sku_arr = that.data.cur_sku_arr;
  774. var sku_str = '';
  775. var is_just_addcar = 1;
  776. if (cur_sku_arr && cur_sku_arr.option_item_ids) {
  777. sku_str = cur_sku_arr.option_item_ids;
  778. }
  779. let data = {
  780. goods_id,
  781. community_id,
  782. quantity,
  783. sku_str,
  784. buy_type: 'dan',
  785. pin_id: 0,
  786. is_just_addcar
  787. }
  788. util.addCart(data).then(res=>{
  789. if(res.showVipModal==1) {
  790. let { pop_vipmember_buyimage } = res.data;
  791. wx.hideLoading();
  792. that.setData({ pop_vipmember_buyimage, showVipModal: true, visible: false })
  793. } else {
  794. if (res.data.code == 3 || res.data.code == 7) {
  795. wx.showToast({
  796. title: res.data.msg,
  797. icon: 'none',
  798. duration: 2000
  799. })
  800. } else if (res.data.code == 4) {
  801. wx.hideLoading();
  802. that.setData({ needAuth: true, showAuthModal: true, visible: false })
  803. } else if (res.data.code == 6) {
  804. let max_quantity = res.data.max_quantity || '';
  805. (max_quantity > 0) && that.setData({ sku_val: max_quantity })
  806. var msg = res.data.msg;
  807. wx.showToast({
  808. title: msg,
  809. icon: 'none',
  810. duration: 2000
  811. })
  812. } else {
  813. if (is_just_addcar == 1) {
  814. that.closeSku();
  815. (0, status.cartNum)(res.data.total);
  816. that.setData({ cartNum: res.data.total })
  817. wx.showToast({
  818. title: "已加入购物车",
  819. image: "../../images/addShopCart.png"
  820. })
  821. } else {
  822. var pages_all = getCurrentPages();
  823. if (pages_all.length > 3) {
  824. wx.redirectTo({
  825. url: '/lionfish_comshop/pages/buy/index?type=' + that.data.order.buy_type
  826. })
  827. } else {
  828. wx.navigateTo({
  829. url: '/lionfish_comshop/pages/buy/index?type=' + that.data.order.buy_type
  830. })
  831. }
  832. }
  833. }
  834. }
  835. }).catch(res=>{
  836. app.util.message(res||'请求失败', '', 'error');
  837. })
  838. },
  839. vipModal: function (t) {
  840. this.setData(t.detail)
  841. },
  842. // 输入框获得焦点
  843. handleFocus: function () {
  844. this.focusFlag = true;
  845. },
  846. handleBlur: function (t) {
  847. let a = t.detail;
  848. let val = parseInt(a.value);
  849. if (val == '' || isNaN(val)) {
  850. this.setData({
  851. sku_val: 1
  852. })
  853. }
  854. },
  855. // 监控输入框变化
  856. changeNumber: function (t) {
  857. let {
  858. cur_sku_arr,
  859. sku_val
  860. } = this.data;
  861. let max = cur_sku_arr.stock * 1;
  862. let a = t.detail;
  863. this.focusFlag = false;
  864. if (a) {
  865. let val = parseInt(a.value);
  866. val = val < 1 ? 1 : val;
  867. if (val > max) {
  868. wx.showToast({
  869. title: `最多只能购买${max}件`,
  870. icon: 'none'
  871. })
  872. sku_val = max;
  873. } else {
  874. sku_val = val;
  875. }
  876. }
  877. this.setData({
  878. sku_val
  879. })
  880. },
  881. /**
  882. * 用户点击右上角分享
  883. */
  884. onShareAppMessage: function () {
  885. },
  886. onShareTimeline: function(res) {
  887. }
  888. })