修改公众号关注逻辑,增加电子围栏

This commit is contained in:
2025-11-25 13:24:35 +08:00
parent 26f5beb660
commit 4d78311aaf
10 changed files with 1315 additions and 1057 deletions

View File

@@ -1,273 +1,365 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { APIuserlogin, APIuser } from "@/common/api/api.js";
import {
defineStore
} from 'pinia';
import {
ref
} from 'vue';
import {
APIuserlogin,
APIuser
} from '@/common/api/api.js'
import {
APIproductqueryShop,
APIusershopInfodetail,
APIshopUserInfo
} from '@/common/api/member.js'
export const Storelogin = defineStore('login', {
state: () => ({
token: '',
miniAppOpenId: '',
userInfo: '',
shopInfo: {}
}),
actions: {
async getShopInfo(shopId) {
const shopRes = await APIusershopInfodetail({
shopId
})
console.log(shopRes);
},
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);
uni.cache.set('followIndex', res.followIndex||'');
}
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
})
}
}
APIproductqueryShop,
APIusershopInfodetail,
APIshopUserInfo,
} from "@/common/api/member.js";
import { getDistance } from "@/utils/address.js";
import { APIgeocodelocation } from "@/common/api/api.js";
export const Storelogin = defineStore("login", {
state: () => ({
token: "",
miniAppOpenId: "",
userInfo: "",
shopInfo: {},
}),
actions: {
async getShopInfo(shopId) {
const shopRes = await APIusershopInfodetail({
shopId,
});
console.log(shopRes);
},
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);
uni.cache.set("followIndex", res.followIndex || "");
}
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(q) {
console.log('扫码内容', q)
return new Promise(async (resolve, reject) => {
if (q) {
console.log(q)
let tableCode = ""
// #ifdef MP-WEIXIN
tableCode = this.getQueryString(decodeURIComponent(q), 'code')
// #endif
// #ifdef MP-ALIPAY
tableCode = q
// #endif
// #ifdef H5
tableCode = q.tableCode
// #endif
console.log(tableCode);
// 储存卓玛
uni.cache.set('tableCode', tableCode)
if (tableCode) {
console.log('台桌码', uni.cache.get('tableCode'));
let data = await this.actionsproductqueryShop(tableCode)
console.log('data', data)
// -4请求登录
if (data.code == '500') {
if (await this.actionslogin()) {
// 成功 接着在调用
await this.actionsproductqueryShop()
}
}
// 是否免除桌位费 0否1是
if (uni.cache.get('shopInfo').isTableFee == 0) {
uni.reLaunch({
url: '/pages/product/choosetable'
});
} else {
uni.reLaunch({
url: '/pages/product/index'
});
}
export const productStore = defineStore("product", {
state: () => ({
location: {
latitude: "",
longitude: "",
},
shopInfo: {
shopId: "",
isOrderFence: 0,
},
}),
actions: {
getLocation() {
return new Promise((resolve, reject) => {
console.log("获取经纬度");
uni.getLocation({
type: "wgs84",
altitude: true,
isHighAccuracy: true,
success: (res) => {
console.log("获取经纬度成功", res);
}
} else {
// #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()
if (!data) {
uni.showToast({
title: '店铺已过期或其他问题,请联系商家',
icon:'none'
})
return
}
// -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.navigateTo({
url: '/pages/product/choosetable'
});
} else {
uni.reLaunch({
url: '/pages/product/index'
});
}
this.location = res;
this.APIgeocodelocation();
resolve(res);
},
fail: (err) => {
console.error("获取经纬度失败", err);
reject(err);
},
});
});
},
async APIgeocodelocation() {
let successres = await APIgeocodelocation({
lng: this.location.longitude,
lat: this.location.latitude,
});
if (successres) {
let datastorage = {
country: successres.addressComponent.country, // "中国"
province: successres.addressComponent.province, //province: "陕西省"
address: successres.addressComponent.city, //district: "西安市"
district: successres.addressComponent.district, //district: "未央区"
lng: this.location.longitude,
lat: this.location.latitude,
};
uni.cache.set("getLocationstorage", datastorage);
}
},
getQueryString(url, name) {
//解码
var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
var r = url.substr(1).match(reg);
if (r != null) {
return r[2];
}
return null;
},
}
},
fail: (res) => {
console.log(res)
}
});
// #endif
}
/**
* 扫码请求
* @param {*} q
* @returns
*/
async scanCodeactions(q) {
console.log("扫码内容", q);
return new Promise(async (resolve, reject) => {
if (q) {
console.log(q);
let tableCode = "";
// #ifdef MP-WEIXIN
tableCode = this.getQueryString(decodeURIComponent(q), "code");
// #endif
// #ifdef MP-ALIPAY
tableCode = q;
// #endif
// #ifdef H5
tableCode = q.tableCode;
// #endif
console.log(tableCode);
// 储存卓玛
uni.cache.set("tableCode", tableCode);
if (tableCode) {
console.log("台桌码", uni.cache.get("tableCode"));
let data = await this.actionsproductqueryShop(tableCode);
})
},
console.log("data", data);
// -4请求登录
if (data.code == "500") {
if (await this.actionslogin()) {
// 成功 接着在调用
await this.actionsproductqueryShop();
}
}
const canOrder = await this.computedDistance();
}
} else {
// #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();
if (!data) {
uni.showToast({
title: "店铺已过期或其他问题,请联系商家",
icon: "none",
});
return;
}
// -4请求登录
const store = Storelogin();
if (data.code == "-4") {
if (await store.actionslogin()) {
// 成功 接着在调用
await this.actionsproductqueryShop();
}
}
const canOrder = await this.computedDistance();
}
},
fail: (res) => {
console.log(res);
},
});
// #endif
}
});
},
// /通过桌码获取当前店铺信息
actionsproductqueryShop(tableCode) {
console.log('台桌码',tableCode);
return new Promise(async (resolve, reject) => {
// try {
try {
let res = await APIproductqueryShop({
tableCode: tableCode ? tableCode : uni.cache.get('tableCode'),
})
if (res) {
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)
//计算距离判断是否可以点餐
async computedDistance() {
return new Promise((resolve, reject) => {
console.log("店铺经纬度", this.shopInfo.lat, this.shopInfo.lng);
console.log(
"用户经纬度",
this.location.latitude,
this.location.longitude
);
resolve(res)
} else {
console.error('通过桌码获取当前店铺信息失败' ,res)
reject()
}
if (this.shopInfo.isOrderFence == 0) {
this.jumpToOrderPage();
resolve(true);
return;
}
const juli = getDistance(
this.location.latitude,
this.location.longitude,
this.shopInfo.lat,
this.shopInfo.lng
);
const orderFenceDistance = (
(this.shopInfo.orderFenceDistance || 2000) / 2000
).toFixed(2);
} catch (error) {
console.log(error);
reject()
//TODO handle the exception
}
console.log("距离", juli);
// } catch (e) {
// reject(false)
// }
if (this.shopInfo.isOrderFence && juli > orderFenceDistance) {
uni.showModal({
title: "提示",
confirmText: "重新定位",
content:
"抱歉,您当前距离店铺过远,为保障您的用餐体验,请您到店后或在门店附近再下单。若您已在店铺附近,可尝试重新定位",
success: async(res) => {
if (res.confirm) {
console.log("用户点击了确认");
await this.getLocation();
this.computedDistance();
} else if (res.cancel) {
console.log("用户点击了取消");
}
},
});
reject();
return;
}
this.jumpToOrderPage();
resolve(juli);
});
},
// 跳转点餐页面
jumpToOrderPage() {
// 是否免除桌位费 0否1是
if (this.shopInfo.isTableFee == 0) {
uni.reLaunch({
url: "/pages/product/choosetable",
});
} else {
uni.reLaunch({
url: "/pages/product/index",
});
}
},
})
},
// /通过桌码获取当前店铺信息
actionsproductqueryShop(tableCode) {
console.log("台桌码", tableCode);
return new Promise(async (resolve, reject) => {
// try {
try {
let res = await APIproductqueryShop({
tableCode: tableCode ? tableCode : uni.cache.get("tableCode"),
});
if (res) {
res.shopInfo.isVip = res.vip ? "1" : "0";
res.shopTable.shopExtendMap = res.shopExtendMap;
this.shopInfo = res.shopInfo;
// 店铺信息
uni.cache.set("shopTable", res.shopTable);
// 台桌信息
uni.cache.set("shopInfo", res.shopInfo);
uni.cache.set("shopId", res.shopTable.shopId, 30);
// 通过shopId 获取店铺会员信息
actionsproductqueryProduct() {
return new Promise(async (resolve, reject) => {
try {
let res = await APIshopUserInfo()
uni.cache.set('shopUserInfo', res);
uni.cache.set('orderVIP', res)
uni.cache.set('ordershopUserInfo', res.shopInfo)
resolve(res)
} catch (e) {
reject(false)
}
resolve(res);
} else {
uni.showToast({
title: "通过桌码获取当前店铺信息失败",
icon: "none",
});
console.error("通过桌码获取当前店铺信息失败", res);
reject();
}
} catch (error) {
console.log(error);
reject();
//TODO handle the exception
}
})
},
// } catch (e) {
// reject(false)
// }
});
},
// 用户信息获取
actionsAPIuser() {
return new Promise(async (resolve, reject) => {
try {
let res = null
// 获取店铺用户会员信息
if (uni.cache.get('shopId')) {
res = await this.actionsproductqueryProduct()
} else {
res = await APIuser()
uni.cache.set('userInfo', res);
}
console.log('actionsAPIuser res', res);
resolve(res)
} catch (e) {
reject(false)
}
// 通过shopId 获取店铺会员信息
actionsproductqueryProduct() {
return new Promise(async (resolve, reject) => {
try {
let res = await APIshopUserInfo();
uni.cache.set("shopUserInfo", res);
uni.cache.set("orderVIP", res);
uni.cache.set("ordershopUserInfo", res.shopInfo);
resolve(res);
} catch (e) {
reject(false);
}
});
},
})
}
}
});
// 用户信息获取
actionsAPIuser() {
return new Promise(async (resolve, reject) => {
try {
let res = null;
// 获取店铺用户会员信息
if (uni.cache.get("shopId")) {
res = await this.actionsproductqueryProduct();
} else {
res = await APIuser();
uni.cache.set("userInfo", res);
}
console.log("actionsAPIuser res", res);
resolve(res);
} catch (e) {
reject(false);
}
});
},
},
});