storeGoods.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. import CustomPage from '../../../base/CustomPage'
  2. import storeGoods from '../../../api/storeGoods'
  3. import OrderApi from '../../../api/order'
  4. import {
  5. getCurrDistance
  6. } from '../../../utils/location'
  7. import storeList from '../../../api/storeList'
  8. const cache = require('../../../utils/cache.js');
  9. const user = require('../../../utils/user.js');
  10. import base from '../../../utils/base'
  11. CustomPage({
  12. onShareAppMessage() {
  13. return {
  14. storeInfo: null,
  15. store: null,
  16. distance: null,
  17. title: 'vtabs',
  18. path: 'page/weui/example/vtabs/vtabs',
  19. showViewDel: false
  20. }
  21. },
  22. data: {
  23. vtabs: [],
  24. activeTab: 0,
  25. totalPrice: 2,
  26. isShowCartPannel: false,
  27. cartSkuMapData: [{}],
  28. comBoFoodsList: [{}, {}],
  29. showView: true,
  30. showViewDel: false,
  31. showViewNotInStore: false,
  32. openScroll: true,
  33. openGoodsScroll: false,
  34. goodsScrollTop: 0,
  35. currSpecGoods: {},
  36. allNum: 0,
  37. allFee: 0.0,
  38. isShowSpecPannel: false,
  39. isShowComboPannel: false,
  40. collapseOrDetail: {},
  41. hasMobile: false,
  42. isShowComboSpecPannel: false,
  43. currSpecGoodsSpec: {},
  44. comBoShow: false,
  45. opacityStyle: "opacity8",
  46. orderMode: ''
  47. },
  48. // 进入门店列表
  49. goToStoreList() {
  50. wx.navigateTo({
  51. url: '/pages/groupmeallist/storeList/storeList',
  52. })
  53. },
  54. handleEmptyCart() {
  55. this.onChangeShowStateDelTrue()
  56. OrderApi.emptyCart({
  57. storeId: cache.getStoreId(),
  58. mobile: user.getMobileCache()
  59. // tableId: user.getTableId(),
  60. // orderMode: this.data.orderMode
  61. }).then(res => {
  62. if (res.code == 200) {
  63. this.loadCartData()
  64. this.setData({
  65. isShowCartPannel: false
  66. })
  67. }
  68. }).catch(_ => { })
  69. },
  70. //关闭删除弹出层
  71. onChangeShowStateDelTrue: function () {
  72. this.setData({
  73. showViewDel: false
  74. })
  75. },
  76. // 清空购物车
  77. onChangeShowStateDelFalse() {
  78. var that = this;
  79. that.setData({
  80. showViewDel: true
  81. })
  82. },
  83. // 商品详情
  84. gotoGoodDetails(e) {
  85. let idx = e.currentTarget.dataset.index;
  86. let fatherindex = e.currentTarget.dataset.fatherindex;
  87. let goods = this.data.vtabs[fatherindex].goodsList[idx]
  88. let goodsJson = JSON.stringify(goods)
  89. wx.navigateTo({
  90. url: '/pages/groupmeallist/goodDetails/goodDetails?goodsJson=' + goodsJson
  91. })
  92. },
  93. //创建订单
  94. confirmOrder() {
  95. wx.navigateTo({
  96. url: '../submitOrder/submitOrder',
  97. })
  98. },
  99. // 弹窗购物车面板
  100. popCartPannel: function () {
  101. if (this.data.isShowCartPannel) {
  102. this.closeCartPannel()
  103. return
  104. }
  105. if (this.data.allNum == 0) {
  106. return
  107. }
  108. this.showCartPannel();
  109. },
  110. // 详情和收起
  111. changeCollapseOrDetail: function (e) {
  112. let idx = e.currentTarget.dataset.idx;
  113. var that = this;
  114. that.data.cartSkuMapData[idx].show = !that.data.cartSkuMapData[idx].show
  115. that.setData({
  116. cartSkuMapData: that.data.cartSkuMapData
  117. });
  118. console.log("购物车详情");
  119. console.log(that.data.cartSkuMapData);
  120. },
  121. //关闭购物车面板
  122. closeCartPannel: function () {
  123. this.setData({
  124. isShowCartPannel: false
  125. })
  126. },
  127. //显示对话框
  128. showCartPannel: function () {
  129. // 显示遮罩层
  130. var animation = wx.createAnimation({
  131. duration: 100,
  132. timingFunction: "linear",
  133. delay: 0
  134. })
  135. this.animation = animation
  136. animation.translateY(300).step()
  137. this.setData({
  138. animationData: animation.export(),
  139. isShowCartPannel: true
  140. })
  141. setTimeout(function () {
  142. animation.translateY(0).step()
  143. this.setData({
  144. animationData: animation.export()
  145. })
  146. }.bind(this), 100)
  147. },
  148. onLoad(options) { },
  149. async onShow() {
  150. let _self = this;
  151. //获取缓存赋值
  152. let location = cache.getCurrStore()
  153. console.log("缓存值")
  154. console.log(location)
  155. if (location != null) {
  156. _self.setData({
  157. storeInfo: location
  158. })
  159. } else {
  160. _self.authMobileLocation()
  161. }
  162. await this.loadData()
  163. await this.loadStoreData()
  164. await this.loadCartData()
  165. await this.cartPanelNum();
  166. },
  167. loadStoreData() {
  168. let store = cache.getCurrStore()
  169. console.log("store")
  170. console.log(store)
  171. this.setData({
  172. store: store || {}
  173. })
  174. getCurrDistance(store.longitude, store.latitude, (m, m1, m2) => {
  175. this.setData({
  176. distance: "距离您" + m1
  177. })
  178. })
  179. },
  180. async loadData() {
  181. await storeGoods.getExhibit(cache.getStoreId()).then(res => {
  182. if (res.code == 200) {
  183. this.dataMapView(res.data)
  184. }
  185. }).catch(_ => {
  186. })
  187. },
  188. handleAddToCart(e) {
  189. let goodsId = e.currentTarget.dataset.goodsid;
  190. let skuId = e.currentTarget.dataset.skuid || null;
  191. let combo = e.currentTarget.dataset.combo;
  192. this.addSubCart(goodsId, skuId, 1, combo)
  193. },
  194. async addSubCart(goodsId, skuId, num, comboSkuList) {
  195. console.log("进入套餐购物api")
  196. await storeGoods.comBoaddToCart({
  197. storeId: cache.getStoreId(),
  198. mobile: cache.getMobile(),
  199. skuId: skuId,
  200. num: num,
  201. comboSkuList: comboSkuList,
  202. }).then(res => {
  203. if (res.code == 200) {
  204. this.refreshCartData()
  205. }
  206. }).catch(_ => { })
  207. await this.loadCartData()
  208. },
  209. handleSubToCart(e) {
  210. let goodsId = e.currentTarget.dataset.goodsid;
  211. let skuId = e.currentTarget.dataset.skuid || null;
  212. let combo = e.currentTarget.dataset.combo;
  213. this.addSubCart(goodsId, skuId, -1, combo)
  214. },
  215. dataMapView(data) {
  216. // let bannerList = null;
  217. // if (data.bannerList && data.bannerList.length > 0) {
  218. // bannerList = data.bannerList;
  219. // }
  220. let tabs = [];
  221. if (data.classifyGoodsList && data.classifyGoodsList.length > 0) {
  222. console.log("进入方法")
  223. data.classifyGoodsList.forEach(v => {
  224. let classify = v.classify || {}
  225. let node = {
  226. title: classify.classifyName,
  227. img: classify.fullClassifyFigure,
  228. classifyId: classify.classifyId,
  229. classifyCode: classify.classifyCode,
  230. classifyName: classify.classifyName,
  231. fullClassifyFigure: classify.fullClassifyFigure,
  232. goodsList: v.goodsList
  233. }
  234. v.goodsList.forEach((vv, i) => {
  235. if (base.isArray(vv.productSkuList)) {
  236. let minPriceSku = this.getMinPriceGoods(vv.productSkuList)
  237. vv.priceY = minPriceSku.priceY
  238. vv.oriPriceY = minPriceSku.oriPriceY
  239. vv.minPriceSku = minPriceSku
  240. }
  241. if (base.isEmpty(vv.openSpec) && base.isArray(vv.productSkuList) && vv.productSkuList.length > 0) {
  242. if (vv.minPriceSku) {
  243. vv.skuId = vv.minPriceSku.skuId
  244. } else {
  245. vv.skuId = vv.productSkuList[0].skuId
  246. }
  247. }
  248. })
  249. tabs.push(node)
  250. })
  251. this.setData({
  252. vtabs: tabs,
  253. })
  254. // console.log("this.data.vtabs")
  255. // console.log(this.data.vtabs)
  256. }
  257. },
  258. getMinPriceGoods(productSkuList) {
  259. let minPriceSku = null;
  260. productSkuList.forEach(sku => {
  261. if (minPriceSku == null) {
  262. minPriceSku = sku
  263. } else {
  264. if (minPriceSku.price > sku.price) {
  265. minPriceSku = sku
  266. }
  267. }
  268. })
  269. return minPriceSku
  270. },
  271. /**
  272. * 加载购物车数据
  273. */
  274. async loadCartData() {
  275. await storeGoods.getCartData(cache.getStoreId(), user.getMobileCache(), null, null).then(res => {
  276. if (res.code == 200) {
  277. this.cartDataMapToView(res.data)
  278. }
  279. }).catch(_ => {
  280. console.log(_)
  281. })
  282. },
  283. cartPanelNum() {
  284. this.data.vtabs.forEach((item, i) => {
  285. item.goodsList.forEach((children, x) => {
  286. if (this.data.cartSkuMapData == null) {
  287. console.log("被return")
  288. return
  289. }
  290. this.data.cartSkuMapData.forEach((sku, s) => {
  291. if (sku.skuId == children.minPriceSku.skuId) {
  292. children.chooseNum = sku.num
  293. }
  294. })
  295. })
  296. })
  297. this.setData({
  298. vtabs: this.data.vtabs
  299. })
  300. console.log(this.data.vtabs)
  301. },
  302. // 购物车数据映射
  303. cartDataMapToView(data) {
  304. // var isShowCartPannel = true;
  305. if (base.isEmpty(data.skuList) || data.skuList.length == 0) {
  306. // isShowCartPannel = false
  307. data.totalPriceY = "0.00"
  308. data.totalNum = 0
  309. }
  310. console.log("进入购物车塞值")
  311. this.setData({
  312. allFee: data.totalPriceY,
  313. allNum: data.totalNum,
  314. cartSkuMapData: data.skuList,
  315. // isShowCartPannel: isShowCartPannel,
  316. isShowSpecPannel: false,
  317. })
  318. // if (base.isEmpty(data.skuList) || data.skuList.length == 0) {
  319. // this.setData({
  320. // allFee: 0.0,
  321. // allNum: 0,
  322. // cartSkuMapData: {},
  323. // isShowCartPannel: false,
  324. // })
  325. // return
  326. // }
  327. // this.data.cartSkuMapData = {}
  328. // // 总金额
  329. // let allFee = 0;
  330. // // 总商品数
  331. // let allNum = 0;
  332. // data.skuList.forEach(sku => {
  333. // this.data.cartSkuMapData[sku.skuId] = sku
  334. // sku.allFee = base.fenToYuan(sku.num * sku.price)
  335. // allFee = allFee.add(sku.allFee)
  336. // allNum = allNum.add(sku.num)
  337. // sku.allOriFee = base.fenToYuan(sku.num * sku.oriPrice)
  338. // })
  339. // this.setData({
  340. // allFee: allFee,
  341. // allNum,
  342. // cartSkuMapData: this.data.cartSkuMapData
  343. // })
  344. },
  345. /**
  346. * 获取定位
  347. */
  348. getPosition() {
  349. let _self = this;
  350. wx.getLocation({
  351. type: 'wgs84',
  352. success(res) {
  353. console.log(res);
  354. const latitude = res.latitude
  355. const longitude = res.longitude
  356. //存储最近的经纬度
  357. cache.setNearLocation(longitude, latitude);
  358. _self.nearbyStoreList(longitude, latitude);
  359. },
  360. fail(res) {
  361. console.log(res)
  362. }
  363. })
  364. },
  365. /**
  366. * 获取最近的门店
  367. */
  368. async nearbyStoreList(longitude, latitude) {
  369. let _self = this;
  370. try {
  371. let storeRes = await storeList.nearbyStoreList(longitude, latitude);
  372. if (storeRes.code == 200 && storeRes.msg == "success") {
  373. let data = storeRes.data;
  374. _self.setData({
  375. storeInfo: data
  376. })
  377. cache.setCurrStore(data)
  378. }
  379. } catch (error) {
  380. console.log(error);
  381. }
  382. },
  383. authMobileLocation: function () {
  384. var _self = this;
  385. wx.getSystemInfoAsync({
  386. success(res) {
  387. console.log(res)
  388. if (res.locationEnabled) {
  389. _self.authLocation();
  390. } else {
  391. wx.showToast({
  392. title: '请打开手机位置信息',
  393. icon: 'none',
  394. duration: 1000
  395. })
  396. }
  397. }
  398. })
  399. },
  400. authLocation: function () {
  401. var _self = this;
  402. wx.getSetting({ //先查看授权情况
  403. success: function (res) {
  404. console.log(res);
  405. var statu = res.authSetting;
  406. //判断是否授权,没有授权就提示下面的信息
  407. //undefined 是第一次没有拒绝 也没有同意
  408. //true 是已经同意了
  409. //false 是拒绝了
  410. if (statu['scope.userLocation'] === undefined || statu['scope.userLocation'] === true) {
  411. _self.getPosition()
  412. return;
  413. }
  414. //授权被拒绝了 需要重新允许小程序授权
  415. if (statu['scope.userLocation'] === false) {
  416. wx.showModal({
  417. title: '需要获取您的地理位置,请确认授权,否则小程序功能将无法使用',
  418. cancelColor: '需要获取您的地理位置,请确认授权,否则小程序功能将无法使用',
  419. success: function (tip) {
  420. if (tip.confirm) { //查看是否点击确定
  421. wx.openSetting({ //打开设置
  422. success: function (data) {
  423. if (data.authSetting["scope.userLocation"] == true) { //到这一步表示打开了位置授权
  424. _self.getPosition()
  425. }
  426. },
  427. })
  428. } else {
  429. wx.showToast({
  430. title: '取消授权',
  431. icon: 'none',
  432. duration: 1000
  433. })
  434. }
  435. }
  436. })
  437. } else {
  438. wx.showToast({
  439. title: '请给小程序授权位置服务',
  440. icon: 'none',
  441. duration: 1000
  442. })
  443. }
  444. }
  445. })
  446. },
  447. onTabClick(e) {
  448. const index = e.detail.index
  449. console.log('tabClick', index)
  450. },
  451. onChange(e) {
  452. const index = e.detail.index
  453. console.log('change', index)
  454. },
  455. handleClick() {
  456. wx.navigateTo({
  457. url: '../tabs/webview',
  458. })
  459. }
  460. })