230 lines
5.4 KiB
JavaScript
230 lines
5.4 KiB
JavaScript
import {
|
|
defineStore
|
|
} from 'pinia';
|
|
import {
|
|
ref
|
|
} from 'vue';
|
|
import {
|
|
APIuserlogin,
|
|
APIuser
|
|
} from '@/common/api/api.js'
|
|
import {
|
|
APIproductqueryShop,
|
|
APIshopUserInfo
|
|
} from '@/common/api/product/product.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('openId', res
|
|
.userInfo
|
|
.wechatOpenId)
|
|
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()
|
|
}
|
|
}
|
|
if (uni.cache.get('productInfo')
|
|
.shopTableInfo && !uni
|
|
.cache.get('productInfo')
|
|
.shopTableInfo
|
|
.choseCount) {
|
|
uni.pro.navigateTo(
|
|
'/pagesOrder/orderAMeal/index', {
|
|
tableCode: tableCode,
|
|
shopId: uni.cache.get(
|
|
'shopId'),
|
|
})
|
|
} else {
|
|
uni.pro.navigateTo(
|
|
'product/index', {
|
|
tableCode: tableCode,
|
|
})
|
|
}
|
|
|
|
}
|
|
},
|
|
fail: (res) => {
|
|
console.log(111)
|
|
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()
|
|
}
|
|
}
|
|
if (uni.cache.get('productInfo').shopTableInfo && !uni
|
|
.cache.get('productInfo').shopTableInfo.choseCount) {
|
|
uni.pro.navigateTo('/pagesOrder/orderAMeal/index', {
|
|
tableCode: uni.cache.get('tableCode'),
|
|
shopId: uni.cache.get('shopId'),
|
|
})
|
|
} 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'),
|
|
})
|
|
// 店铺信息
|
|
uni.cache.set('shopTable', res.shopTable)
|
|
// 台桌信息
|
|
uni.cache.set('shopInfo', res.shopInfo)
|
|
uni.cache.set('shopId', res.shopTable.shopId)
|
|
// 当前用户距离店铺的米数
|
|
uni.cache.set('distance', res.distance)
|
|
resolve(res)
|
|
} catch (e) {
|
|
reject(false)
|
|
}
|
|
|
|
})
|
|
},
|
|
|
|
// 获取店铺会员信息
|
|
actionsproductqueryProduct() {
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
let res = await APIshopUserInfo({
|
|
"shopId": uni.cache.get('shopId'),
|
|
"userId": uni.cache.get('userInfo').id
|
|
})
|
|
uni.cache.set('ShopUser', res)
|
|
resolve(true)
|
|
} catch (e) {
|
|
reject(false)
|
|
}
|
|
|
|
})
|
|
},
|
|
|
|
// 用户信息获取
|
|
actionsAPIuser() {
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
let res = await APIuser()
|
|
uni.cache.set('userInfo', res);
|
|
resolve(true)
|
|
} catch (e) {
|
|
reject(false)
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|
|
}); |