cashier_wx/stores/user.js

230 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
defineStore
} from 'pinia';
import {
ref
} from 'vue';
import {
APIuserlogin,
APIuser
} from '@/common/api/api.js'
import {
APIproductqueryShop,
APIshopUserInfo
} from '@/common/api/member.js'
export const Storelogin = defineStore('login', {
state: () => ({
token: '',
miniAppOpenId: '',
userInfo: ''
}),
actions: {
actionslogin() {
return new Promise(async (resolve, reject) => {
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: (data) => {
// 微信小程序环境
uni.getUserInfo({
provider: 'weixin',
success: async (infoRes) => {
let res = await APIuserlogin({
code: data.code, //临时登录凭证
rawData: infoRes.rawData,
source: 'wechat'
})
if (res) {
this.token = res.token
this.miniAppOpenId = res.userInfo
.miniAppOpenId
this.userInfo = res.userInfo
uni.cache.set('token', res.token);
uni.cache.set('userInfo', res.userInfo);
}
resolve(true);
},
fail: (err) => {
reject(false);
}
});
}
});
// #endif
// #ifdef MP-ALIPAY
my.getAuthCode({
scopes: 'auth_base',
success: async (data) => {
// 支付宝小程序环境
// my.getAuthUserInfo({
// success: async (infoRes) => {
let res = await APIuserlogin({
code: data.authCode, //临时登录凭证
// rawData: JSON.stringify(infoRes),
source: 'alipay'
})
if (res) {
this.token = res.token
this.miniAppOpenId = res.userInfo.miniAppOpenId
this.userInfo = res.userInfo
uni.cache.set('token', res.token);
uni.cache.set('openId', res.userInfo
.alipayOpenId)
uni.cache.set('userInfo', res.userInfo);
resolve(true);
}
},
fail: () => {
reject(false);
}
})
// #endif
})
}
}
});
export const productStore = defineStore('product', {
actions: {
getQueryString(url, name) { //解码
var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
var r = url.substr(1).match(reg)
if (r != null) {
return r[2]
}
return null;
},
// 扫码请求
scanCodeactions() {
return new Promise(async (resolve, reject) => {
// #ifdef APP || MP-WEIXIN || MP-ALIPAY
uni.scanCode({
success: async (res) => {
let tableCode = this.getQueryString(
decodeURIComponent(res
.result),
'code')
// 储存卓玛
uni.cache.set('tableCode', tableCode)
if (tableCode) {
let data = await this.actionsproductqueryShop()
// -4请求登录
const store = Storelogin()
if (data.code == '-4') {
if (await store.actionslogin()) {
// 成功 接着在调用
await this.actionsproductqueryShop()
}
}
// 是否免除桌位费 0否1是
if (uni.cache.get('shopInfo').isTableFee == 0) {
uni.pro.navigateTo('product/choosetable')
} else {
uni.pro.navigateTo(
'product/index', {
tableCode: tableCode,
})
}
}
},
fail: (res) => {
console.log(res)
}
});
// #endif
// #ifdef H5
if (uni.cache.get('tableCode')) {
let data = await this.actionsproductqueryShop()
// -4请求登录
const store = Storelogin()
if (data.code == '-4') {
if (await store.actionslogin()) {
// 成功 接着在调用
await this.actionsproductqueryShop()
}
}
// idle-空闲 using-使用中 subscribe预定closed--关台, opening 开台中cleaning 台桌清理中
if (uni.cache.get('shopTable').status == 'cleaning' || uni.cache.get(
'shopTable').status == 'cleaning') {
uni.showToast({
title: '此台桌暂不可用...',
icon: 'none'
})
return false;
}
if (uni.cache.get('shopInfo').isTableFee == 0) {
uni.pro.navigateTo('product/choosetable')
} else {
uni.pro.navigateTo('product/index', {
tableCode: uni.cache.get('tableCode'),
})
}
}
// #endif
})
},
// /通过桌码获取当前店铺信息
actionsproductqueryShop() {
return new Promise(async (resolve, reject) => {
// try {
let res = await APIproductqueryShop({
tableCode: uni.cache.get('tableCode'),
})
res.shopInfo.isVip = res.vip ? '1' : '0'
res.shopTable.shopExtendMap = res.shopExtendMap
// 店铺信息
uni.cache.set('shopTable', res.shopTable)
// 台桌信息
uni.cache.set('shopInfo', res.shopInfo)
uni.cache.set('shopId', res.shopTable.shopId, 30)
// 当前用户距离店铺的米数
uni.cache.set('distance', res.distance)
resolve(res)
// } catch (e) {
// reject(false)
// }
})
},
// 通过shopId 获取店铺会员信息
actionsproductqueryProduct() {
return new Promise(async (resolve, reject) => {
try {
let res = await APIshopUserInfo()
uni.cache.set('shopUserInfo', res);
resolve(true)
} catch (e) {
reject(false)
}
})
},
// 用户信息获取
actionsAPIuser() {
return new Promise(async (resolve, reject) => {
try {
// 获取店铺用户会员信息
if (uni.cache.get('shopId')) {
this.actionsproductqueryProduct()
}
let res = await APIuser()
uni.cache.set('userInfo', res);
// }
resolve(true)
} catch (e) {
reject(false)
}
})
}
}
});