index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. // index.js
  2. let myPro = require("../../utils/wxRequest.js");
  3. let util = require("../../utils/util.js");
  4. // 点击计时器
  5. let clickScrollTimer;
  6. Page({
  7. data: {
  8. customTop: getApp().globalData.customTop,
  9. setBgHeight: getApp().globalData.setBgHeight, // 导航条高度
  10. imgUrl: getApp().globalData.imgUrl,
  11. keywords: '',
  12. showGoodsSpec: false, // 规格弹层
  13. showShopCar: false, // 购物车弹层
  14. store: null, // 所在门店
  15. loading: false, // 全局loading
  16. fare_type: 1, // 0外卖 1自提 默认自提
  17. showOpenLocation: false, // 开启定位提醒
  18. good_list: null,
  19. cartList: null,
  20. // showSelectFare: false, // 配送方式弹层
  21. showTopCate: false,
  22. toViewErji: null,
  23. totalNums: 0,
  24. totalPrice: 0,
  25. totalMemberPrice: 0,
  26. activeErjiId: 0,
  27. checkedSpecNums: 0,
  28. isClickToScroll: false, // 通过点击滚动产品列表 true 滚动事件中不赋值,false 时赋值
  29. activeCateId: '',
  30. toViewErji: '',
  31. activeErjiId: '',
  32. toView: '',
  33. currentCate: '',
  34. showErjiCate: false,
  35. tableId: null
  36. },
  37. onLoad: function (options) {
  38. // Do some initialize when page load.
  39. let that = this;
  40. if (options.tableId) {
  41. that.setData({
  42. tableId: options.tableId
  43. })
  44. }
  45. },
  46. onShow: function () {
  47. // Do something when page show.
  48. let that = this;
  49. that.setData({
  50. showGoodsSpec: false, // 规格弹层
  51. showShopCar: false, // 购物车弹层
  52. });
  53. // 获取配送方式
  54. if(wx.getStorageSync('fare_type')){
  55. that.setData({
  56. fare_type: wx.getStorageSync('fare_type')
  57. })
  58. }else{
  59. // 未获取到,需主动存一下,默认自提
  60. wx.setStorage({
  61. key: "fare_type",
  62. data: '1'
  63. })
  64. };
  65. // 单店开是否开定位,都需要把商品调出来
  66. getApp().getLocal(function(){
  67. that.afterAuthLocation();
  68. // 拒绝定位时,提醒去开启
  69. if(getApp().globalData.location_auth == -1){
  70. console.log('拒绝定位');
  71. that.setData({
  72. showOpenLocation: true
  73. });
  74. }
  75. });
  76. },
  77. onReady: function () {
  78. // Do something when page ready.
  79. let that = this;
  80. // 滚动区域高度计算
  81. wx.getSystemInfo({
  82. success: function(res){
  83. let height = res.windowHeight;
  84. // console.log('总height',height)
  85. const query = wx.createSelectorQuery();
  86. query.select('.store_wrap').boundingClientRect();
  87. query.select('.heji_wrap').boundingClientRect();
  88. // query.select('.erji_cate').boundingClientRect();
  89. query.exec((res) =>{
  90. // 分别取出节点的高度
  91. let storeWrapH = res[0].height; // 门店选择的高度 px
  92. let hejiWrapH = res[1].height; // 底部合计的高度 px
  93. // let erjiCateH = res[2].height; // 顶部分类固定宽
  94. // let scrollH = height - storeWrapH - hejiWrapH - erjiCateH;
  95. let scrollH = height - storeWrapH - hejiWrapH;
  96. that.setData({
  97. storeWrapH: storeWrapH,
  98. scrollH: scrollH,
  99. guigeH: height*0.5
  100. });
  101. // console.log('scrollH',scrollH)
  102. // console.log('门店高度',that.data.storeWrapH);
  103. });
  104. }
  105. });
  106. },
  107. onHide: function () {
  108. // Do something when page hide.
  109. // console.log('hide')
  110. let that = this;
  111. // return;
  112. // 主动模拟selectClassesClick
  113. clearTimeout(clickScrollTimer);
  114. let item = that.data.good_cate[0];
  115. that.setData({
  116. isClickToScroll: true,
  117. activeCateId: item.id,
  118. toView: 'erji' + (item.children && item.children.length != 0 ? item.children[0].id : item.id), //不能数字开头,所以开头加了productItem
  119. activeErjiId: item.children && item.children.length != 0 ? item.children[0].id : item.id,
  120. toViewYiji: 'yijicate'+item.id
  121. });
  122. // console.log('toView',that.data.toView);
  123. // console.log('activeErjiId',that.data.activeErjiId);
  124. let good_cate = that.data.good_cate;
  125. // 循环当前分类
  126. for(let i in good_cate){
  127. if(good_cate[i].id == item.id){
  128. that.setData({
  129. currentCate: good_cate[i]
  130. })
  131. }
  132. }
  133. },
  134. onUnload: function () {
  135. // Do something when page close.
  136. },
  137. onPullDownRefresh: function () {
  138. // Do something when pull down.
  139. },
  140. onReachBottom: function () {
  141. // Do something when page reach bottom.
  142. },
  143. onPageScroll: function () {
  144. // Do something when page scroll
  145. },
  146. onResize: function () {
  147. // Do something when page resize
  148. },
  149. // 获取购物车商品
  150. getCartsList(){
  151. let that = this;
  152. myPro.wxRequest("user/v3/carts/list","GET",{}).then(res=>{
  153. // 在首页只体现普通商品
  154. let list = res.result.list;
  155. that.setData({
  156. cartGoods: list,
  157. totalNums: res.result.total_num,
  158. totalPrice: res.result.money
  159. });
  160. // 同步商品列表
  161. that.updateTotalGoods();
  162. }).catch(err=>{
  163. console.log('报错信息',err);
  164. wx.showToast({
  165. title: err,
  166. icon: "none"
  167. });
  168. })
  169. },
  170. // 店铺列表(只拿最近的)
  171. getStoreList(lat,lng){
  172. let that = this;
  173. let data = {
  174. lat: lat ? lat : getApp().globalData.lat,
  175. lng: lng ? lng : getApp().globalData.lng,
  176. page: 1,
  177. size: 10
  178. };
  179. myPro.wxRequest("user/store/list","GET",data,true).then(res=>{
  180. let storeList = res.result;
  181. // 有门店
  182. if(storeList && storeList.length != 0){
  183. let currentStore = storeList[0];
  184. that.setData({
  185. store: currentStore
  186. });
  187. // 营业时间的判断
  188. // that.checkBusinessTime();
  189. // 存下门店
  190. wx.setStorage({
  191. key: "store",
  192. data: JSON.stringify(that.data.store),
  193. });
  194. // 获取店内商品
  195. that.getGoodsList(function(){
  196. // 登录状态
  197. if(getApp().globalData.token){
  198. getApp().globalData.loadingCount = 0; // 主动关
  199. that.getCartsList();
  200. }
  201. });
  202. }else{
  203. wx.showToast({
  204. title: "门店暂未营业",
  205. icon: "none"
  206. });
  207. }
  208. }).catch(err=>{
  209. console.log('报错信息',err);
  210. wx.showToast({
  211. title: err,
  212. icon: "none",
  213. });
  214. });
  215. },
  216. // 拨打店铺电话
  217. onMakePhone(){
  218. let that = this;
  219. wx.makePhoneCall({
  220. phoneNumber: that.data.store.phone
  221. });
  222. },
  223. // 获取商品分类及商品
  224. getGoodsList(callback){
  225. let that = this;
  226. let params = {
  227. store_id: that.data.store.id
  228. };
  229. myPro.wxRequest("user/v3/index","GET",params).then(res=>{
  230. let list = res.result.good;
  231. // 回调
  232. if(callback){
  233. callback();
  234. };
  235. that.setData({
  236. good_cate: res.result.good_cate,
  237. good_list: list,
  238. activeCateId: res.result.good_cate[0].id, //默认左侧第一个选中
  239. currentCate: res.result.good_cate[0], // 当前分类(结构为一级分类+二级分类)
  240. toView: 'erji' + res.result.good_cate[0].id
  241. });
  242. // console.log('左侧第一个分类',that.data.activeCateId);
  243. // console.log('右侧第一个内容',that.data.toView);
  244. // 一级节点
  245. that.data.good_list.forEach(item=>{
  246. // 添加节点的布局位置的查询请求
  247. wx.createSelectorQuery().select('#yiji' + item.good_cate_id).boundingClientRect(rect => {
  248. item.offsetTop = rect.top;
  249. item.height = rect.height;
  250. // console.log('rect',rect)
  251. // console.log('item',item);
  252. }).exec()
  253. // 二级节点
  254. if(item.children){
  255. item.children.forEach(erjiItem=>{
  256. wx.createSelectorQuery().select('#erji' + erjiItem.good_cate_id).boundingClientRect(rect => {
  257. erjiItem.offsetTop = rect.top;
  258. erjiItem.height = rect.height;
  259. // console.log('rect',rect)
  260. // console.log('item',item);
  261. }).exec()
  262. })
  263. }
  264. });
  265. // console.log('商品列表带节点',that.data.good_list);
  266. }).catch(err=>{
  267. console.log('报错信息',err)
  268. wx.showToast({
  269. title: err,
  270. icon: "none",
  271. });
  272. });
  273. },
  274. // 左侧菜单栏切换事件
  275. selectClassesClick: function(e) {
  276. let that = this
  277. // return;
  278. console.log('e===',e);
  279. clearTimeout(clickScrollTimer);
  280. let item = e.currentTarget.dataset.item;
  281. that.setData({
  282. isClickToScroll: true,
  283. activeCateId: item.id,
  284. toView: 'erji' + (item.children && item.children.length != 0 ? item.children[0].id : item.id), //不能数字开头,所以开头加了productItem
  285. activeErjiId: item.children && item.children.length != 0 ? item.children[0].id : item.id,
  286. toViewYiji: 'yijicate'+ item.id
  287. });
  288. // console.log('toView',that.data.toView);
  289. // console.log('activeErjiId',that.data.activeErjiId);
  290. let good_cate = that.data.good_cate;
  291. // 循环当前分类
  292. for(let i in good_cate){
  293. if(good_cate[i].id == item.id){
  294. that.setData({
  295. currentCate: good_cate[i]
  296. })
  297. }
  298. }
  299. // console.log('当前fenlei',that.data.currentCate);
  300. },
  301. // 监听右边商品列表滑动
  302. scrollProductList(e){
  303. let that = this;
  304. let good_cate = that.data.good_cate;
  305. let scrollTop = e.detail.scrollTop;
  306. if(scrollTop!=0){
  307. that.setData({
  308. showErjiCate: true
  309. })
  310. }
  311. // return;
  312. // console.log('isClickToScroll',that.data.isClickToScroll);
  313. // console.log('e.detail.scrollTop',scrollTop);
  314. if(!that.data.isClickToScroll){
  315. that.data.good_list.forEach(item=>{
  316. // console.log('item',item);
  317. // console.log('item.offsetTop',item.offsetTop );
  318. // console.log('item.height',item.height);
  319. if (scrollTop >= (item.offsetTop - that.data.storeWrapH) && e.detail.scrollTop <= (item.offsetTop - that.data.storeWrapH + item.height)){
  320. that.setData({
  321. activeCateId: item.good_cate_id,
  322. toViewYiji: 'yijicate'+item.good_cate_id
  323. });
  324. // console.log('左侧分类1111',item.good_cate_id)
  325. good_cate.forEach(cateItem=>{
  326. if(cateItem.id == item.good_cate_id){
  327. that.setData({
  328. currentCate: cateItem
  329. })
  330. }
  331. });
  332. // console.log('当前分类',that.data.currentCate);
  333. if(item.children){
  334. item.children.forEach(erjiItem=>{
  335. if(scrollTop >= erjiItem.offsetTop-180 && scrollTop<= erjiItem.offsetTop+erjiItem.height-180){
  336. that.setData({
  337. activeErjiId: erjiItem.good_cate_id,
  338. toViewErji: 'erjiCate'+erjiItem.good_cate_id
  339. })
  340. }
  341. })
  342. };
  343. return;
  344. }
  345. })
  346. }
  347. else{
  348. clickScrollTimer = setTimeout(function() {
  349. that.setData({
  350. isClickToScroll: false
  351. });
  352. },300)
  353. }
  354. // console.log('当前分类',that.data.currentCate)
  355. // console.log('左侧分类',that.data.activeCateId);
  356. // console.log('二级分类',that.data.activeErjiId);
  357. // console.log('右侧分类',that.data.toView);
  358. },
  359. // 点击某个二级菜单
  360. onErjiCate(event){
  361. // console.log('二级event',event)
  362. let that = this;
  363. let good_cate = that.data.good_cate;
  364. let item = event.currentTarget.dataset.item;
  365. // console.log('item',item);
  366. // return;
  367. that.setData({
  368. toView: 'erji'+item.id,
  369. activeErjiId: item.id,
  370. activeCateId: item.parentid,
  371. toViewErji: 'erjiCate'+item.id
  372. });
  373. // 循环当前分类
  374. // for(let i in good_cate){
  375. // if(good_cate[i].id == item.parentid){
  376. // that.setData({
  377. // currentCate: good_cate[i]
  378. // })
  379. // }
  380. // }
  381. // console.log('toView',that.data.toView);
  382. // console.log('当前的一级分类',that.data.currentCate)
  383. },
  384. // 查看规格
  385. onGoodsSpec(event){
  386. let that = this;
  387. let item = event.currentTarget.dataset.item;
  388. if(item.status != 1){
  389. // 如果商品售罄
  390. wx.showToast({
  391. title: "此商品已售罄",
  392. icon: "none"
  393. })
  394. return;
  395. };
  396. let checkedSpec = null;
  397. // 有规格
  398. if(item.good_specs && item.good_specs.length != 0){
  399. let specsArr = item.good_specs;
  400. for(let i in specsArr){
  401. if(i == 0){
  402. specsArr[i].is_checked = true; // 规格默认选中第一项
  403. checkedSpec = specsArr[i];
  404. }else{
  405. specsArr[i].is_checked = false;
  406. }
  407. }
  408. }else{
  409. // 无规格
  410. item['member_price'] = item.base_member_price;
  411. checkedSpec = item;
  412. }
  413. // console.log('当前商品',item);
  414. // console.log('当前选中规格',checkedSpec);
  415. that.setData({
  416. showGoodsSpec: true,
  417. currentGoods: item, // 当前商品
  418. checkedSpec: checkedSpec, // 当前选中规格
  419. checkedSpecNums: checkedSpec.nums ? checkedSpec.nums : 0
  420. });
  421. },
  422. // 更改商品规格弹层的商品数量
  423. onChangeSpecNum(event){
  424. let that = this;
  425. let nums = event.detail;
  426. that.setData({
  427. checkedSpecNums: nums
  428. });
  429. // 更新购物车
  430. let oldCartGoods = that.data.cartGoods;
  431. let item = event.currentTarget.dataset.item;
  432. // console.log('item',item);
  433. // console.log('购物车',oldCartGoods);
  434. if(that.isAddCart(item)){
  435. console.log('在')
  436. // 在购物车
  437. // return;
  438. let params = {};
  439. // 有规格id
  440. if(item.good_id){
  441. for(let i in oldCartGoods){
  442. if(oldCartGoods[i].good_id == item.good_id && oldCartGoods[i].good_specs_id == item.id){
  443. params.id = oldCartGoods[i].id; // 购物车ID
  444. break;
  445. }
  446. };
  447. }else{
  448. // 无规格id
  449. for(let i in oldCartGoods){
  450. if( oldCartGoods[i].good_id == item.id && !oldCartGoods[i].good_specs_id){
  451. params.id = oldCartGoods[i].id; // 购物车ID
  452. break;
  453. }
  454. }
  455. };
  456. if(nums != 0){
  457. params.nums = nums;
  458. // 修改数量
  459. myPro.wxRequest("user/v3/carts/editnum","POST",params).then(res=>{
  460. that.getCartsList();
  461. }).catch(err=>{
  462. console.log('报错信息',err);
  463. wx.showToast({
  464. title: err,
  465. icon: "none"
  466. })
  467. })
  468. }else{
  469. // 删除
  470. myPro.wxRequest("user/v3/carts/remove","POST",params).then(res=>{
  471. that.getCartsList();
  472. }).catch(err=>{
  473. console.log('报错信息',err);
  474. wx.showToast({
  475. title: err,
  476. icon: "none"
  477. })
  478. })
  479. }
  480. }else{
  481. console.log('不在');
  482. // return;
  483. let params = {
  484. good_id: item.good_id ? item.good_id : item.id,
  485. nums: nums,
  486. is_add: 0 // 是否为活动加购商品(1是 0否)
  487. };
  488. // 有规格id(加一个无规格的看看)
  489. if(item.good_id){
  490. params.good_specs_id = item.id;
  491. };
  492. // 添加购物车
  493. myPro.wxRequest("user/v3/carts/create","POST",params).then(res=>{
  494. that.getCartsList();
  495. }).catch(err=>{
  496. console.log('报错信息',err);
  497. wx.showToast({
  498. title: err,
  499. icon: "none"
  500. })
  501. })
  502. }
  503. },
  504. // 切换规格
  505. onChangeSpecGoods(event){
  506. let that = this;
  507. // console.log('event====',event);
  508. let currentSpecs = event.currentTarget.dataset.specs;
  509. let oldCartGoods = that.data.cartGoods;
  510. let checkedSpec = null;
  511. let checkedSpecNums = 0;
  512. // 同步选中状态
  513. let currentGoods = that.data.currentGoods;
  514. let specsArr = currentGoods.good_specs;
  515. for(let j in specsArr){
  516. if(specsArr[j].id == currentSpecs.id){
  517. specsArr[j].is_checked = true;
  518. if(that.isAddCart(specsArr[j])){
  519. // 同步购物车
  520. for(let i in oldCartGoods){
  521. if(oldCartGoods[i].good_id == currentSpecs.good_id && oldCartGoods[i].good_specs_id == currentSpecs.id){
  522. specsArr[j].nums = oldCartGoods[i].nums;
  523. checkedSpecNums = oldCartGoods[i].nums;
  524. // console.log('进来了==')
  525. break;
  526. }
  527. };
  528. }else{
  529. specsArr[j].nums = 0;
  530. checkedSpecNums = 0;
  531. };
  532. checkedSpec = specsArr[j];
  533. // break;
  534. }else{
  535. specsArr[j].is_checked = false;
  536. }
  537. };
  538. that.setData({
  539. currentGoods: currentGoods,
  540. checkedSpec: checkedSpec,
  541. checkedSpecNums: checkedSpecNums
  542. });
  543. // console.log('全规格',specsArr);
  544. // console.log('当前规格',checkedSpec);
  545. },
  546. // 关闭规格
  547. closeGoodsSpec(){
  548. let that = this;
  549. that.setData({
  550. showGoodsSpec: false
  551. });
  552. },
  553. // 计算价格和数量
  554. computedGoods(){
  555. let that = this;
  556. let list = that.data.cartGoods;
  557. let totalNums = 0;
  558. let totalPrice = 0;
  559. let totalMemberPrice = 0;
  560. for(let i in list){
  561. if(list[i].nums != 0){
  562. totalNums += parseInt(list[i].nums);
  563. totalPrice += parseFloat(list[i].total_prices);
  564. totalMemberPrice += parseFloat(list[i].total_member_prices);
  565. }
  566. };
  567. that.setData({
  568. totalNums: totalNums,
  569. totalPrice: totalPrice,
  570. totalMemberPrice: totalMemberPrice
  571. });
  572. // 同步商品列表
  573. // that.updateTotalGoods();
  574. },
  575. // 去结算
  576. toPay(){
  577. let that = this;
  578. // 未开启 不允许进入下一步
  579. if(getApp().globalData.location_auth == -1){
  580. wx.showToast({
  581. title: "地理位置未授权,请您授权定位后重试",
  582. icon: "none"
  583. })
  584. return;
  585. };
  586. // 验证登录
  587. getApp().verifyLogin(function(loginStatus){
  588. // console.log('登录状态',loginStatus)
  589. if(loginStatus){
  590. if(!that.data.totalNums){
  591. wx.showToast({
  592. title: "请先选择商品",
  593. icon: "none"
  594. });
  595. return;
  596. };
  597. // 验证是否能下单,如果不能给提示并进购物车(因为可能是购物车有商品,但未勾选)
  598. let params = {
  599. token: getApp().globalData.token
  600. };
  601. wx.request({
  602. url: getApp().globalData.api + "user/v3/order/createlist",
  603. data: params,
  604. success: function(res) {
  605. if(res.data.code != 200){
  606. wx.showToast({
  607. title: res.data.msg,
  608. icon: "none",
  609. duration: 3000
  610. });
  611. setTimeout(function(){
  612. wx.navigateTo({
  613. url: "/pages/shopcart/index"
  614. });
  615. },1000)
  616. }else{
  617. wx.navigateTo({
  618. url: "/pages/create-order/index"
  619. });
  620. }
  621. }
  622. });
  623. };
  624. })
  625. },
  626. // 取消授权提醒
  627. onCloseLocation(){
  628. let that = this;
  629. that.setData({
  630. showOpenLocation: false
  631. });
  632. },
  633. // 同意去打开地理位置
  634. onConfirmLocation(){
  635. // console.log('触发====');
  636. let that = this;
  637. wx.openSetting({
  638. success: function (res) {
  639. console.log(res)
  640. let authSetting = res.authSetting;
  641. if(authSetting["scope.userLocation"]){
  642. // 开启
  643. getApp().getLocal(function(){
  644. that.afterAuthLocation();
  645. });
  646. }
  647. }
  648. })
  649. },
  650. // 允许定位后的操作(或 拒绝定位,但选了默认店铺)
  651. afterAuthLocation(lat,lng){
  652. let that = this;
  653. // 外卖流程,需要验证是否登录
  654. if(that.data.fare_type == 0){
  655. getApp().verifyLogin(function(loginstatus){
  656. if(loginstatus){
  657. console.log('登录状态',loginstatus)
  658. try {
  659. wx.getStorage({
  660. key: 'checkedAddress',
  661. success: function(res){
  662. // console.log('res.data',res.data);
  663. let checkedAddress = JSON.parse(res.data);
  664. that.setData({
  665. checkedAddress: checkedAddress
  666. });
  667. // 按地址算
  668. that.getStoreList(that.data.checkedAddress.lat,that.data.checkedAddress.lnt);
  669. },
  670. fail: function(){
  671. wx.showToast({
  672. title: "请先选择收货地址",
  673. icon: "none",
  674. });
  675. setTimeout(function(){
  676. wx.navigateTo({
  677. url: '/pages/user/address/list/index'
  678. });
  679. },1000)
  680. }
  681. })
  682. }
  683. catch(err){
  684. console.log('报错信息',err)
  685. }
  686. }
  687. })
  688. }else{
  689. that.getStoreList();
  690. }
  691. console.log('定位成功');
  692. },
  693. // 切换配送方式
  694. changeFareType(event){
  695. // console.log(event);
  696. let that = this;
  697. let fare_type = event.currentTarget.dataset.type; // 0外卖 1自提
  698. let store = that.data.store;
  699. // 判断门店是否支持此配送方式
  700. if(fare_type == 0 && store.is_delivery != 1){
  701. wx.showToast({
  702. title: '门店暂不支持外卖',
  703. icon: 'none'
  704. });
  705. return;
  706. };
  707. if(fare_type == 1 && store.is_ziti != 1){
  708. wx.showToast({
  709. title: '门店暂不支持自提',
  710. icon: 'none'
  711. });
  712. return;
  713. };
  714. // 支持
  715. // if(that.data.showSelectFare){
  716. // that.setData({
  717. // showSelectFare: false
  718. // })
  719. // };
  720. // 切换时实现: 自取选门店,外卖选地址
  721. // 为每次选外卖时重选地址,清掉checkedAddress
  722. wx.removeStorageSync('checkedAddress');
  723. // 更新
  724. that.setData({
  725. fare_type: fare_type
  726. });
  727. wx.setStorage({
  728. key: "fare_type",
  729. data: that.data.fare_type
  730. });
  731. // 切到外卖
  732. if(fare_type == 0){
  733. // 选地址
  734. wx.navigateTo({
  735. url: '/pages/user/address/list/index'
  736. });
  737. };
  738. },
  739. // 关闭提醒
  740. closeShowTips(){
  741. let that = this;
  742. that.setData({
  743. showTips: false
  744. })
  745. },
  746. // 关闭选配送方式(用不到了)
  747. closeSelectFare(){
  748. let that = this;
  749. that.setData({
  750. showSelectFare: false
  751. })
  752. },
  753. // 计算每个商品的总数量
  754. updateTotalGoods(){
  755. let that = this;
  756. let oldCartGoods = that.data.cartGoods;
  757. // console.log('购物车',oldCartGoods);
  758. let list = that.data.good_list;
  759. list.forEach( item=> {
  760. if(item.children){
  761. // 有二级
  762. let erjiGoods = item.children;
  763. erjiGoods.forEach(erjiItem => {
  764. let goodsList = erjiItem.goodList;
  765. goodsList.forEach(goods=>{
  766. goods.cartNums = 0;
  767. oldCartGoods.forEach(oldGoods=>{
  768. if(!oldGoods.is_add){
  769. // 有规格
  770. if(goods.good_specs && goods.good_specs.length != 0){
  771. let specsArr = goods.good_specs;
  772. specsArr.forEach(goodsSpec=>{
  773. if(oldGoods.good_id == goodsSpec.good_id && oldGoods.good_specs_id == goodsSpec.id){
  774. goodsSpec.nums = oldGoods.nums;
  775. goods.cartNums += oldGoods.nums;
  776. }
  777. })
  778. }else{
  779. // 无规格
  780. if(oldGoods.good_id == goods.id){
  781. goods.nums = oldGoods.nums;
  782. }
  783. }
  784. }
  785. })
  786. })
  787. })
  788. }else{
  789. // 无二级
  790. let goodsList = item.goodList;
  791. goodsList.forEach(goods=>{
  792. goods.cartNums = 0
  793. oldCartGoods.forEach(oldGoods=>{
  794. if(!oldGoods.is_add){
  795. // 有规格
  796. if(goods.good_specs && goods.good_specs.length != 0){
  797. let specsArr = goods.good_specs;
  798. specsArr.forEach(goodsSpec=>{
  799. if(oldGoods.good_id == goodsSpec.good_id && oldGoods.good_specs_id == goodsSpec.id){
  800. goodsSpec.nums = oldGoods.nums;
  801. goods.cartNums += oldGoods.nums;
  802. }
  803. })
  804. }else{
  805. // 无规格
  806. if(oldGoods.good_id == goods.id){
  807. goods.nums = oldGoods.nums;
  808. }
  809. }
  810. }
  811. })
  812. })
  813. }
  814. })
  815. that.setData({
  816. good_list: list
  817. });
  818. // console.log('重组商品',list);
  819. },
  820. // 核实商品是否存在于购物车
  821. isAddCart(item){
  822. let that = this;
  823. let oldCartGoods = that.data.cartGoods;
  824. let is_have = false;
  825. // console.log('购物车',oldCartGoods);
  826. // console.log('操作商品',item);
  827. // 检测购物车是否有此商品
  828. for(let i in oldCartGoods){
  829. if(item.good_id){
  830. // 有规格
  831. console.log(item.id,oldCartGoods[i].good_specs_id);
  832. if(item.id == oldCartGoods[i].good_specs_id && !oldCartGoods[i].is_add){
  833. is_have = true;
  834. break;
  835. }
  836. }else{
  837. if(item.id == oldCartGoods[i].good_id && !oldCartGoods[i].is_add){
  838. is_have = true;
  839. break;
  840. }
  841. }
  842. };
  843. return is_have;
  844. },
  845. // 进入购物车页面
  846. goShopCart(){
  847. let that = this;
  848. wx.navigateTo({
  849. url: '/pages/shopcart/index'
  850. })
  851. },
  852. // 营业时间判断(弃用 改成多段时间了, 前端判断没意义了)
  853. checkBusinessTime(){
  854. let that = this;
  855. let store = that.data.store;
  856. // console.log('store',that.data.store,store.status);
  857. if(store.status != 1){
  858. wx.showToast({
  859. title: "门店暂未营业",
  860. icon: "none",
  861. duration: 3000
  862. });
  863. return;
  864. };
  865. let currentTime = new Date();
  866. let year = currentTime.getFullYear();
  867. let month = currentTime.getMonth()+1;
  868. let day = currentTime.getDate();
  869. let endDay = currentTime.getDate();
  870. if(store.end_at.split(":")[0] < store.begin_at.split(":")[0]){
  871. endDay = endDay+1;
  872. };
  873. let storeBeginAt = new Date(year+'/'+month+'/'+day+' '+store.begin_at);
  874. let storeEndAt = new Date(year+'/'+month+'/'+endDay+' '+store.end_at);
  875. if(storeBeginAt.getTime() > currentTime.getTime() || storeEndAt.getTime() < currentTime.getTime()){
  876. wx.showToast({
  877. title: "门店暂未营业",
  878. icon: "none",
  879. duration: 3000
  880. });
  881. };
  882. }
  883. });