index.js 20 KB

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