index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. let {
  2. statusBarHeight,
  3. screenHeight,
  4. safeArea: {
  5. bottom
  6. }
  7. } = wx.getSystemInfoSync();
  8. statusBarHeight = Math.abs(statusBarHeight)
  9. let custom = wx.getMenuButtonBoundingClientRect();
  10. let customBarHeight = custom.bottom + custom.top - statusBarHeight;
  11. customBarHeight = Math.abs(customBarHeight)
  12. const Parse = getApp().Parse;
  13. const company = getApp().globalData.company
  14. // nova-tourism/pages/shop/index.js
  15. Component({
  16. /**
  17. * 组件的属性列表
  18. */
  19. properties: {
  20. },
  21. /**
  22. * 组件的初始数据
  23. */
  24. data: {
  25. imgUrls: [
  26. 'https://s1.ax1x.com/2023/04/10/ppqifvF.png',
  27. 'https://s1.ax1x.com/2023/04/10/ppqiWgU.png',
  28. 'https://s1.ax1x.com/2023/04/10/ppqiR3T.png'
  29. ],
  30. statusBarHeight,
  31. customBarHeight,
  32. screenHeight,
  33. bottom,
  34. user: null,
  35. value: '',
  36. store: null,
  37. category: null,
  38. activeItem: 'all',
  39. goods: [],
  40. loading: true,
  41. cartLoading: false,
  42. show: false,
  43. allCheck: true,
  44. shopCart: [],
  45. },
  46. lifetimes: {
  47. created() {},
  48. attached() {
  49. // this.getBindStore()
  50. this.refresh()
  51. },
  52. },
  53. //监听页面数据加载
  54. pageLifetimes: {
  55. show: function () {
  56. this.getBindStore()
  57. }
  58. },
  59. /**
  60. * 组件的方法列表
  61. */
  62. methods: {
  63. async refresh() {
  64. this.getBanner()
  65. await this.getBindStore()
  66. this.getCategory()
  67. this.getGoods()
  68. },
  69. async getmy() {
  70. // let userLogin = wx.getStorageSync("userLogin");
  71. // if (userLogin == "") {
  72. // login.loginNow();
  73. // return;
  74. // }
  75. let uid = Parse.User.current().id
  76. let ShopOrder = new Parse.Query('_User')
  77. let shopOrder = await ShopOrder.get(uid)
  78. this.setData({
  79. user: shopOrder.toJSON()
  80. })
  81. },
  82. async getBanner() {
  83. let Banner = new Parse.Query('Banner')
  84. Banner.notEqualTo('isDeleted', "true")
  85. Banner.equalTo('company', company)
  86. Banner.equalTo('isEnabled', "true")
  87. Banner.equalTo('type', 'shop')
  88. let banner = await Banner.find()
  89. if (banner && banner.length > 0) {
  90. let listJSON = []
  91. banner.forEach(c => {
  92. listJSON.push(c.toJSON())
  93. })
  94. this.setData({
  95. banner: listJSON
  96. })
  97. }
  98. },
  99. // 获取当前绑定的店铺
  100. getBindStore: async function () {
  101. let storeID = wx.getStorageSync('storeID');
  102. let Store = new Parse.Query('ShopStore')
  103. if (storeID) {
  104. let store = await Store.get(storeID)
  105. let storeJSON = store.toJSON()
  106. this.setData({
  107. store: storeJSON
  108. })
  109. } else {
  110. return wx.showToast({
  111. title: '快去选择一个店铺吧~',
  112. icon: 'none'
  113. })
  114. }
  115. },
  116. // 跳转至选择店铺页面
  117. selectStore(e) {
  118. let store = e.target.dataset.store
  119. wx.navigateTo({
  120. url: '/nova-tourism/pages/shop/select-store/index?store=' + store,
  121. });
  122. },
  123. // 获取所有商品分类
  124. getCategory: async function () {
  125. let Category = new Parse.Query('Category')
  126. Category.notEqualTo('isDeleted', "true")
  127. Category.equalTo('company', company)
  128. Category.equalTo('type', 'shop')
  129. Category.descending('createdAt')
  130. let category = await Category.find()
  131. let list = []
  132. category.forEach(val => {
  133. val = val.toJSON()
  134. list.push(val)
  135. })
  136. this.setData({
  137. category: list
  138. })
  139. },
  140. // 点击分类添加激活类名,以及显示分类下的商品
  141. changeCate: async function (e) {
  142. this.setData({
  143. loading: true,
  144. goods: []
  145. })
  146. let id = e.currentTarget.dataset.id
  147. this.setData({
  148. activeItem: id,
  149. })
  150. this.getGoods()
  151. },
  152. // 获取商品
  153. getGoods: async function (value) {
  154. let storeID = wx.getStorageSync('storeID');
  155. let Goods = new Parse.Query('ShopGoods')
  156. Goods.notEqualTo('isDeleted', "true")
  157. Goods.equalTo('company', company)
  158. if (this.data.activeItem !== 'all' && !value) {
  159. Goods.equalTo('category', this.data.activeItem)
  160. }
  161. Goods.equalTo('shopStore', storeID)
  162. Goods.equalTo('status', true)
  163. Goods.equalTo('type', 'shop')
  164. Goods.include('category')
  165. Goods.descending('createdAt')
  166. if (value) {
  167. Goods.contains('name', value)
  168. }
  169. Goods.limit(10)
  170. Goods.skip(this.data.goods.length)
  171. let goods = await Goods.find()
  172. let list = this.data.goods
  173. goods.forEach(val => {
  174. val = val.toJSON()
  175. list.push(val)
  176. })
  177. this.setData({
  178. loading: false,
  179. goods: list
  180. })
  181. },
  182. // 根据商品objectId获取库存
  183. getTotal: async function (ID) {
  184. let Goods = new Parse.Query('ShopGoods')
  185. let goods = await Goods.get(ID)
  186. return goods.toJSON().total
  187. },
  188. // 将商品添加至购物车,同时该商品显示的库存减少
  189. addCount(e) {
  190. let store = wx.getStorageSync('storeID');
  191. if (!store) {
  192. return wx.showToast({
  193. title: '请先绑定店铺后购买商品!',
  194. icon: 'none',
  195. duration: 1500,
  196. mask: false,
  197. });
  198. }
  199. let index = e.currentTarget.dataset.index
  200. let goods = this.data.goods[index]
  201. let shopCart = this.data.shopCart
  202. goods.total--
  203. shopCart.push(goods)
  204. let Goods = this.data.goods
  205. Goods[index] = goods
  206. this.setData({
  207. goods: Goods,
  208. shopCart: shopCart
  209. })
  210. },
  211. // 查看购物车
  212. checkCart() {
  213. let store = wx.getStorageSync('storeID');
  214. if (!store) {
  215. return wx.showToast({
  216. title: '请先绑定店铺后购买商品!',
  217. icon: 'none',
  218. duration: 1500,
  219. mask: false,
  220. });
  221. }
  222. this.setData({
  223. show: true,
  224. })
  225. let shopGoods = this.data.shopCart
  226. let list = []
  227. let result = shopGoods.reduce((init, current, index) => {
  228. list[current.objectId] ? '' : list[current.objectId] = true && init.push(current)
  229. return init
  230. }, [])
  231. let obj = {}
  232. for (let i = 0; i < shopGoods.length; i++) {
  233. if (shopGoods[i].objectId in obj) {
  234. obj[shopGoods[i].objectId] = obj[shopGoods[i].objectId] + 1
  235. } else {
  236. obj[shopGoods[i].objectId] = 1
  237. }
  238. }
  239. for (let k in obj) {
  240. let index = result.findIndex(val => {
  241. return val.objectId == k
  242. })
  243. result[index].check = false
  244. result[index].open = true
  245. result[index].count = obj[k]
  246. result[index].totalPrice = (obj[k] * result[index].price).toFixed(2)
  247. }
  248. this.setData({
  249. orderGoods: result
  250. })
  251. this.checkAll()
  252. },
  253. // 点击勾选商品
  254. checkItem(e) {
  255. let index = e.currentTarget.dataset.index
  256. let orderGoods = this.data.orderGoods
  257. let totalPrice = this.data.totalPrice
  258. orderGoods[index].check = !orderGoods[index].check
  259. if (orderGoods[index].check) {
  260. totalPrice = parseFloat(totalPrice) + parseFloat(orderGoods[index].totalPrice)
  261. totalPrice = parseFloat(totalPrice).toFixed(2)
  262. } else {
  263. totalPrice = parseFloat(totalPrice) - parseFloat(orderGoods[index].totalPrice)
  264. totalPrice = parseFloat(totalPrice).toFixed(2)
  265. }
  266. let result = orderGoods.filter(val => {
  267. return val.check !== true
  268. })
  269. if (result.length > 0) {
  270. this.setData({
  271. allCheck: false
  272. })
  273. } else {
  274. this.setData({
  275. allCheck: true
  276. })
  277. }
  278. this.setData({
  279. totalPrice: totalPrice,
  280. orderGoods: orderGoods
  281. })
  282. },
  283. // 步进器,单件商品的购买数量增减处理
  284. onChange: async function (e) {
  285. let num = e.detail
  286. let index = e.currentTarget.dataset.index
  287. let orderGoods = this.data.orderGoods
  288. let total = await this.getTotal(orderGoods[index].objectId)
  289. if ((total - num) < 0) {
  290. wx.showToast({
  291. title: '库存不足!',
  292. icon: 'none',
  293. duration: 1500,
  294. });
  295. orderGoods[index].open = false
  296. num--
  297. } else {
  298. orderGoods[index].open = true
  299. }
  300. orderGoods[index].count = num
  301. orderGoods[index].totalPrice = (orderGoods[index].count * orderGoods[index].price).toFixed(2)
  302. this.setData({
  303. orderGoods: orderGoods
  304. })
  305. this.checkAll()
  306. },
  307. // 点击加号按钮触发
  308. plus(e) {
  309. let index = e.currentTarget.dataset.index
  310. let orderGoods = this.data.orderGoods
  311. orderGoods[index].total--
  312. // 复制一条与该商品相同的订单数据
  313. let shopCart = this.data.shopCart
  314. let cartIndex = shopCart.findIndex(val => {
  315. return val.objectId == orderGoods[index].objectId
  316. })
  317. let goods = shopCart[cartIndex]
  318. shopCart.push(goods)
  319. this.setData({
  320. shopCart: shopCart,
  321. orderGoods: orderGoods
  322. })
  323. },
  324. // 搜索
  325. search() {
  326. console.log(this.data.value)
  327. if (!this.data.value) {
  328. return
  329. }
  330. this.setData({
  331. loading: true,
  332. goods: []
  333. })
  334. this.getGoods(this.data.value)
  335. },
  336. // 点击减号按钮触发
  337. minus(e) {
  338. // 将减少的商品数量加回显示的库存数量
  339. let index = e.currentTarget.dataset.index
  340. let orderGoods = this.data.orderGoods
  341. orderGoods[index].total++
  342. // 移除对应shopCart数组中一条数据
  343. let shopCart = this.data.shopCart
  344. let cartIndex = shopCart.findIndex(val => {
  345. return val.objectId == orderGoods[index].objectId
  346. })
  347. shopCart.splice(cartIndex, 1)
  348. this.setData({
  349. shopCart: shopCart,
  350. orderGoods: orderGoods
  351. })
  352. },
  353. // 删除当前滑动的订单商品
  354. delete(e) {
  355. wx.showModal({
  356. title: '删除商品',
  357. content: '确认删除该件商品吗',
  358. showCancel: true,
  359. cancelText: '取消',
  360. cancelColor: '#000000',
  361. confirmText: '确定',
  362. confirmColor: '#e42929',
  363. success: async (result) => {
  364. if (result.confirm) {
  365. let index = e.currentTarget.dataset.index
  366. let id = e.currentTarget.dataset.id
  367. let totalPrice = this.data.totalPrice
  368. let orderGoods = this.data.orderGoods
  369. let shopCart = this.data.shopCart
  370. let goods = this.data.goods
  371. shopCart = shopCart.filter(val => {
  372. return val.objectId !== id
  373. })
  374. let goodsIndex = goods.findIndex(val => {
  375. return val.objectId == id
  376. })
  377. goods[goodsIndex].total += orderGoods[index].count
  378. totalPrice = parseFloat(totalPrice) - parseFloat(orderGoods[index].totalPrice)
  379. totalPrice = parseFloat(totalPrice).toFixed(2)
  380. orderGoods.splice(index, 1)
  381. this.setData({
  382. totalPrice: totalPrice,
  383. goods: goods,
  384. shopCart: shopCart,
  385. orderGoods: orderGoods
  386. })
  387. this.checkAll()
  388. }
  389. },
  390. });
  391. },
  392. // 点击全选按钮
  393. checkAll(e) {
  394. // 1. 选中全部商品
  395. // 2. 计算总价
  396. let orderGoods = this.data.orderGoods
  397. if (e) {
  398. this.setData({
  399. allCheck: !this.data.allCheck
  400. })
  401. }
  402. if (this.data.allCheck) {
  403. let totalPrice = 0
  404. orderGoods.forEach(val => {
  405. val.check = true
  406. totalPrice += parseFloat(val.totalPrice)
  407. })
  408. this.setData({
  409. totalPrice: totalPrice,
  410. orderGoods: orderGoods
  411. })
  412. } else if (e && !this.data.allCheck) {
  413. orderGoods.forEach(val => {
  414. val.check = false
  415. })
  416. this.setData({
  417. totalPrice: 0,
  418. orderGoods: orderGoods
  419. })
  420. }
  421. },
  422. // 点击删除按钮
  423. onDelete() {
  424. wx.showModal({
  425. title: '删除订单',
  426. content: '确认删除这些商品吗',
  427. showCancel: true,
  428. cancelText: '取消',
  429. cancelColor: '#000000',
  430. confirmText: '确定',
  431. confirmColor: '#e42929',
  432. success: (result) => {
  433. if (result.confirm) {
  434. if (this.data.allCheck) {
  435. // 全选之后的删除
  436. this.setData({
  437. totalPrice: 0,
  438. goods: [],
  439. shopCart: [],
  440. orderGoods: []
  441. })
  442. this.getGoods()
  443. } else {
  444. // 选中部分删除
  445. let totalPrice = this.data.totalPrice
  446. let shopCart = this.data.shopCart
  447. let orderGoods = this.data.orderGoods
  448. let goods = this.data.goods
  449. let list = orderGoods.filter(val => {
  450. return val.check == true
  451. })
  452. list.forEach(val => {
  453. shopCart = shopCart.filter(item => {
  454. return item.objectId !== val.objectId
  455. })
  456. let index = goods.findIndex(value => {
  457. return value.objectId == val.objectId
  458. })
  459. goods[index].total += val.count
  460. totalPrice = parseFloat(totalPrice) - parseFloat(val.totalPrice)
  461. totalPrice = parseFloat(totalPrice).toFixed(2)
  462. })
  463. orderGoods = orderGoods.filter(val => {
  464. return val.check !== true
  465. })
  466. this.setData({
  467. totalPrice: totalPrice,
  468. goods: goods,
  469. shopCart: shopCart,
  470. orderGoods: orderGoods
  471. })
  472. }
  473. }
  474. },
  475. });
  476. },
  477. // 点击结算按钮
  478. submit() {
  479. // 生成一条随机订单编号
  480. let orderNum = "C" +
  481. String(new Date().getFullYear()) +
  482. (new Date().getMonth() + 1) +
  483. new Date().getDate() +
  484. new Date().getHours() +
  485. new Date().getMinutes() +
  486. new Date().getSeconds() +
  487. Math.random().toString().slice(-6); //生成六位随机数
  488. // 创建specMap对象以及tagetObject数组
  489. let specMap = {}
  490. let targetObject = []
  491. let orderGoods = this.data.orderGoods
  492. orderGoods = orderGoods.filter(val => {
  493. return val.check == true
  494. })
  495. if (orderGoods.length == 0) {
  496. return
  497. }
  498. orderGoods.forEach(val => {
  499. specMap[val.objectId] = val.count
  500. let option = {
  501. '__type': 'Pointer',
  502. 'className': 'ShopGoods',
  503. 'objectId': val.objectId
  504. }
  505. targetObject.push(option)
  506. })
  507. // 生成一条Order记录
  508. let Order = Parse.Object.extend('Order')
  509. let order = new Order()
  510. order.set('orderNum', orderNum)
  511. order.set('company', {
  512. '__type': 'Pointer',
  513. 'className': 'Company',
  514. 'objectId': company
  515. })
  516. order.set('store', {
  517. '__type': 'Pointer',
  518. 'className': 'ShopStore',
  519. 'objectId': wx.getStorageSync('storeID')
  520. })
  521. order.set('specMap', specMap)
  522. order.set('totalPrice', this.data.totalPrice)
  523. order.set('user', {
  524. '__type': 'Pointer',
  525. 'className': '_User',
  526. 'objectId': Parse.User.current().id
  527. })
  528. order.set('targetObject', targetObject)
  529. order.set('status', '100')
  530. order.set('type', 'shop')
  531. order.set('isPay', false)
  532. order.save().then((res) => {
  533. let order = res.toJSON()
  534. wx.navigateTo({
  535. url: '/nova-tourism/pages/shop/order/index?id=' + order.objectId,
  536. success: () => {
  537. this.setData({
  538. totalPrice: 0,
  539. goods: [],
  540. shopCart: [],
  541. orderGoods: []
  542. })
  543. this.onClose()
  544. }
  545. });
  546. })
  547. // 跳转至订单支付页面
  548. },
  549. // 关闭购物车
  550. onClose() {
  551. this.setData({
  552. show: false,
  553. })
  554. this.getGoods()
  555. },
  556. // 库存不足
  557. onTips() {
  558. wx.showToast({
  559. title: '商品库存不足',
  560. icon: 'none',
  561. image: '',
  562. duration: 1500,
  563. mask: false,
  564. });
  565. },
  566. // 当容器滑动至底部时,加载商品数据
  567. scrolltolower: function () {
  568. this.getGoods()
  569. }
  570. }
  571. })