123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- // nova-tourism/components/template2/shop/index.js
- let {
- statusBarHeight,
- screenHeight,
- safeArea: {
- bottom
- }
- } = wx.getSystemInfoSync();
- statusBarHeight = Math.abs(statusBarHeight)
- let custom = wx.getMenuButtonBoundingClientRect();
- let customBarHeight = custom.bottom + custom.top - statusBarHeight;
- customBarHeight = Math.abs(customBarHeight)
- const Parse = getApp().Parse;
- const company = getApp().globalData.company
- const compute = require("../../../../utils/compute.js");
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- },
- /**
- * 组件的初始数据
- */
- data: {
- latitude: 0,
- longitude: 0,
- statusBarHeight,
- customBarHeight,
- screenHeight,
- bottom,
- goods: [],
- shopCart: [],
- // activeItem: 'zS9XEd0Im1',
- activeItem: 'all',
- allCheck: true,
- value: '',
- imgUrls: [
- 'https://s1.ax1x.com/2023/04/10/ppqifvF.png',
- 'https://s1.ax1x.com/2023/04/10/ppqiWgU.png',
- 'https://s1.ax1x.com/2023/04/10/ppqiR3T.png'
- ],
- },
- lifetimes: {
- created() {},
- attached() {
- this.refresh()
- },
- },
- /**
- * 组件的方法列表
- */
- methods: {
- async refresh() {
- if (this.data.latitude == 0 && this.data.longitude == 0) {
- let {
- latitude,
- longitude
- } = await this.getLocation()
- this.setData({
- latitude,
- longitude
- })
- }
- this.getBanner()
- await this.getBindStore()
- this.getCategory()
- this.getGoods()
- },
- getLocation() {
- return new Promise((resolve, reject) => {
- wx.getLocation({
- type: 'gcj02',
- success: (res) => {
- resolve({
- latitude: res.latitude,
- longitude: res.longitude
- })
- },
- fail: () => {
- resolve({
- latitude: 0,
- longitude: 0
- })
- },
- complete: () => {}
- });
- })
- },
- async getBanner() {
- let Banner = new Parse.Query('Banner')
- Banner.notEqualTo('isDeleted', "true")
- Banner.equalTo('company', company)
- Banner.equalTo('isEnabled', "true")
- Banner.equalTo('type', 'shop')
- let banner = await Banner.find()
- if (banner && banner.length > 0) {
- let listJSON = []
- banner.forEach(c => {
- listJSON.push(c.toJSON())
- })
- this.setData({
- banner: listJSON
- })
- }
- },
- // 获取所有商品分类
- getCategory: async function () {
- let Category = new Parse.Query('Category')
- Category.notEqualTo('isDeleted', "true")
- Category.equalTo('company', company)
- Category.equalTo('type', 'shop')
- Category.descending('createdAt')
- let category = await Category.find()
- let list = []
- category.forEach(val => {
- val = val.toJSON()
- list.push(val)
- })
- this.setData({
- category: list
- })
- },
- // 获取商品
- getGoods: async function (value) {
- this.setData({
- goods:[]
- })
- let storeID = wx.getStorageSync('storeID');
- let Goods = new Parse.Query('ShopGoods')
- Goods.notEqualTo('isDeleted', "true")
- Goods.equalTo('company', company)
- if (this.data.activeItem !== 'all' && !value) {
- Goods.equalTo('category', this.data.activeItem)
- }
- Goods.equalTo('shopStore', storeID)
- Goods.equalTo('status', true)
- Goods.equalTo('type', 'shop')
- Goods.include('category')
- Goods.descending('createdAt')
- if (value) {
- Goods.contains('name', value)
- }
- Goods.limit(10)
- Goods.skip(this.data.goods.length)
- let goods = await Goods.find()
- let list = this.data.goods
- goods.forEach(val => {
- val = val.toJSON()
- list.push(val)
- })
- this.setData({
- loading: false,
- goods: list
- })
- },
- // 获取当前绑定的店铺
- getBindStore: async function () {
- let storeID = wx.getStorageSync('storeID');
- let Store = new Parse.Query('ShopStore')
- if (storeID) {
- console.log(storeID);
- let store = await Store.get(storeID)
- let storeJSON = store.toJSON()
- let distance = compute.computeDistance(
- this.data.latitude,
- this.data.longitude,
- storeJSON.location.latitude,
- storeJSON.location.longitude
- )
- storeJSON.distance = distance
- this.setData({
- store: storeJSON
- })
- } else {
- return wx.showToast({
- title: '快去选择一个店铺吧~',
- icon: 'none'
- })
- }
- },
- // 点击分类添加激活类名,以及显示分类下的商品
- changeCate: async function (e) {
- this.setData({
- loading: true,
- goods: []
- })
- let id = e.currentTarget.dataset.id
- this.setData({
- activeItem: id,
- })
- this.getGoods()
- },
- // 将商品添加至购物车,同时该商品显示的库存减少
- addCount(e) {
- let store = wx.getStorageSync('storeID');
- if (!store) {
- return wx.showToast({
- title: '请先绑定店铺后购买商品!',
- icon: 'none',
- duration: 1500,
- mask: false,
- });
- }
- let index = e.currentTarget.dataset.index
- let goods = this.data.goods[index]
- let shopCart = this.data.shopCart
- goods.total--
- shopCart.push(goods)
- let Goods = this.data.goods
- Goods[index] = goods
- this.setData({
- goods: Goods,
- shopCart: shopCart
- })
- this.checkCart('add')
- },
- // 库存不足
- onTips() {
- wx.showToast({
- title: '商品库存不足',
- icon: 'none',
- image: '',
- duration: 1500,
- mask: false,
- });
- },
- // 查看购物车
- checkCart(str) {
- let store = wx.getStorageSync('storeID');
- if (!store) {
- return wx.showToast({
- title: '请先绑定店铺后购买商品!',
- icon: 'none',
- duration: 1500,
- mask: false,
- });
- }
- if (str != 'add') {
- this.setData({
- show: true,
- })
- }
- let shopGoods = this.data.shopCart
- console.log(shopGoods);
- let list = []
- let result = shopGoods.reduce((init, current, index) => {
- list[current.objectId] ? '' : list[current.objectId] = true && init.push(current)
- return init
- }, [])
- console.log(result);
- let obj = {}
- for (let i = 0; i < shopGoods.length; i++) {
- if (shopGoods[i].objectId in obj) {
- obj[shopGoods[i].objectId] = obj[shopGoods[i].objectId] + 1
- } else {
- obj[shopGoods[i].objectId] = 1
- }
- }
- for (let k in obj) {
- let index = result.findIndex(val => {
- return val.objectId == k
- })
- result[index].check = false
- result[index].open = true
- result[index].count = obj[k]
- result[index].totalPrice = (obj[k] * result[index].price).toFixed(2)
- }
- console.log(result);
- this.setData({
- orderGoods: result
- })
- this.checkAll()
- },
- // 删除当前滑动的订单商品
- delete(e) {
- console.log("dalskd", e);
- wx.showModal({
- title: '删除商品',
- content: '确认删除该件商品吗',
- showCancel: true,
- cancelText: '取消',
- cancelColor: '#000000',
- confirmText: '确定',
- confirmColor: '#e42929',
- success: async (result) => {
- if (result.confirm) {
- let index = e.currentTarget.dataset.index
- let id = e.currentTarget.dataset.id
- let totalPrice = this.data.totalPrice
- let orderGoods = this.data.orderGoods
- let shopCart = this.data.shopCart
- let goods = this.data.goods
- shopCart = shopCart.filter(val => {
- return val.objectId !== id
- })
- let goodsIndex = goods.findIndex(val => {
- return val.objectId == id
- })
- goods[goodsIndex].total += orderGoods[index].count
- totalPrice = parseFloat(totalPrice) - parseFloat(orderGoods[index].totalPrice)
- totalPrice = parseFloat(totalPrice).toFixed(2)
- orderGoods.splice(index, 1)
- this.setData({
- totalPrice: totalPrice,
- goods: goods,
- shopCart: shopCart,
- orderGoods: orderGoods
- })
- this.checkAll()
- }
- },
- });
- },
- // 点击全选按钮
- checkAll(e) {
- // 1. 选中全部商品
- // 2. 计算总价
- let orderGoods = this.data.orderGoods
- if (e) {
- this.setData({
- allCheck: !this.data.allCheck
- })
- }
- if (this.data.allCheck) {
- let totalPrice = 0
- orderGoods.forEach(val => {
- val.check = true
- totalPrice += parseFloat(val.totalPrice)
- })
- this.setData({
- totalPrice: totalPrice,
- orderGoods: orderGoods
- })
- } else if (e && !this.data.allCheck) {
- orderGoods.forEach(val => {
- val.check = false
- })
- this.setData({
- totalPrice: 0,
- orderGoods: orderGoods
- })
- }
- },
- // 点击加号按钮触发
- plus(e) {
- let index = e.currentTarget.dataset.index
- let orderGoods = this.data.orderGoods
- orderGoods[index].total--
- // 复制一条与该商品相同的订单数据
- let shopCart = this.data.shopCart
- let cartIndex = shopCart.findIndex(val => {
- return val.objectId == orderGoods[index].objectId
- })
- let goods = shopCart[cartIndex]
- shopCart.push(goods)
- this.setData({
- shopCart: shopCart,
- orderGoods: orderGoods
- })
- },
- // 点击减号按钮触发
- minus(e) {
- // 将减少的商品数量加回显示的库存数量
- let index = e.currentTarget.dataset.index
- let orderGoods = this.data.orderGoods
- orderGoods[index].total++
- // 移除对应shopCart数组中一条数据
- let shopCart = this.data.shopCart
- let cartIndex = shopCart.findIndex(val => {
- return val.objectId == orderGoods[index].objectId
- })
- shopCart.splice(cartIndex, 1)
- this.setData({
- shopCart: shopCart,
- orderGoods: orderGoods
- })
- },
- // 点击删除按钮
- onDelete() {
- wx.showModal({
- title: '删除订单',
- content: '确认删除这些商品吗',
- showCancel: true,
- cancelText: '取消',
- cancelColor: '#000000',
- confirmText: '确定',
- confirmColor: '#e42929',
- success: (result) => {
- if (result.confirm) {
- if (this.data.allCheck) {
- // 全选之后的删除
- this.setData({
- totalPrice: 0,
- goods: [],
- shopCart: [],
- orderGoods: []
- })
- this.getGoods()
- } else {
- // 选中部分删除
- let totalPrice = this.data.totalPrice
- let shopCart = this.data.shopCart
- let orderGoods = this.data.orderGoods
- let goods = this.data.goods
- let list = orderGoods.filter(val => {
- return val.check == true
- })
- list.forEach(val => {
- shopCart = shopCart.filter(item => {
- return item.objectId !== val.objectId
- })
- let index = goods.findIndex(value => {
- return value.objectId == val.objectId
- })
- goods[index].total += val.count
- totalPrice = parseFloat(totalPrice) - parseFloat(val.totalPrice)
- totalPrice = parseFloat(totalPrice).toFixed(2)
- })
- orderGoods = orderGoods.filter(val => {
- return val.check !== true
- })
- this.setData({
- totalPrice: totalPrice,
- goods: goods,
- shopCart: shopCart,
- orderGoods: orderGoods
- })
- }
- }
- },
- });
- },
- // 根据商品objectId获取库存
- getTotal: async function (ID) {
- let Goods = new Parse.Query('ShopGoods')
- let goods = await Goods.get(ID)
- return goods.toJSON().total
- },
- // 点击结算按钮
- submit() {
- // 生成一条随机订单编号
- let orderNum = "C" +
- String(new Date().getFullYear()) +
- (new Date().getMonth() + 1) +
- new Date().getDate() +
- new Date().getHours() +
- new Date().getMinutes() +
- new Date().getSeconds() +
- Math.random().toString().slice(-6); //生成六位随机数
- // 创建specMap对象以及tagetObject数组
- let specMap = {}
- let targetObject = []
- let orderGoods = this.data.orderGoods
- orderGoods = orderGoods.filter(val => {
- return val.check == true
- })
- if (orderGoods.length == 0) {
- return
- }
- orderGoods.forEach(val => {
- specMap[val.objectId] = val.count
- let option = {
- '__type': 'Pointer',
- 'className': 'ShopGoods',
- 'objectId': val.objectId
- }
- targetObject.push(option)
- })
- // 生成一条Order记录
- let Order = Parse.Object.extend('Order')
- let order = new Order()
- order.set('orderNum', orderNum)
- order.set('company', {
- '__type': 'Pointer',
- 'className': 'Company',
- 'objectId': company
- })
- order.set('store', {
- '__type': 'Pointer',
- 'className': 'ShopStore',
- 'objectId': wx.getStorageSync('storeID')
- })
- order.set('specMap', specMap)
- order.set('totalPrice', this.data.totalPrice)
- order.set('user', {
- '__type': 'Pointer',
- 'className': '_User',
- 'objectId': Parse.User.current().id
- })
- order.set('targetObject', targetObject)
- order.set('status', '100')
- order.set('type', 'shop')
- order.set('isPay', false)
- order.save().then((res) => {
- let order = res.toJSON()
- wx.navigateTo({
- url: '/nova-tourism/pages/shop/order/index?id=' + order.objectId,
- success: () => {
- this.setData({
- totalPrice: 0,
- goods: [],
- shopCart: [],
- orderGoods: []
- })
- this.onClose()
- }
- });
- })
- // 跳转至订单支付页面
- },
- // 关闭购物车
- onClose() {
- this.setData({
- show: false,
- })
- this.getGoods()
- },
- // 步进器,单件商品的购买数量增减处理
- onChange: async function (e) {
- let num = e.detail
- let index = e.currentTarget.dataset.index
- let orderGoods = this.data.orderGoods
- let total = await this.getTotal(orderGoods[index].objectId)
- if ((total - num) < 0) {
- wx.showToast({
- title: '库存不足!',
- icon: 'none',
- duration: 1500,
- });
- orderGoods[index].open = false
- num--
- } else {
- orderGoods[index].open = true
- }
- orderGoods[index].count = num
- orderGoods[index].totalPrice = (orderGoods[index].count * orderGoods[index].price).toFixed(2)
- this.setData({
- orderGoods: orderGoods
- })
- this.checkAll()
- },
- // 点击勾选商品
- checkItem(e) {
- let index = e.currentTarget.dataset.index
- let orderGoods = this.data.orderGoods
- let totalPrice = this.data.totalPrice
- orderGoods[index].check = !orderGoods[index].check
- if (orderGoods[index].check) {
- totalPrice = parseFloat(totalPrice) + parseFloat(orderGoods[index].totalPrice)
- totalPrice = parseFloat(totalPrice).toFixed(2)
- } else {
- totalPrice = parseFloat(totalPrice) - parseFloat(orderGoods[index].totalPrice)
- totalPrice = parseFloat(totalPrice).toFixed(2)
- }
- let result = orderGoods.filter(val => {
- return val.check !== true
- })
- if (result.length > 0) {
- this.setData({
- allCheck: false
- })
- } else {
- this.setData({
- allCheck: true
- })
- }
- this.setData({
- totalPrice: totalPrice,
- orderGoods: orderGoods
- })
- },
- // 搜索
- search() {
- console.log(this.data.value)
- if (!this.data.value && this.data.value != '') {
- return
- }
- this.setData({
- loading: true,
- goods: []
- })
- this.getGoods(this.data.value)
- },
- // 当容器滑动至底部时,加载商品数据
- // scrolltolower: function () {
- // this.getGoods()
- // },
- // 跳转至选择店铺页面
- selectStore(e) {
- let store = e.target.dataset.store
- wx.navigateTo({
- url: '/nova-tourism/pages/shop/select-store/index?store=' + store,
- });
- },
- }
- })
|