104
uniapp/sheep/store/app.js
普通文件
104
uniapp/sheep/store/app.js
普通文件
@@ -0,0 +1,104 @@
|
||||
import appApi from '@/sheep/api/app';
|
||||
import { defineStore } from 'pinia';
|
||||
import $platform from '@/sheep/platform';
|
||||
import $router from '@/sheep/router';
|
||||
import user from './user';
|
||||
import sys from './sys';
|
||||
import { baseUrl } from '@/sheep/config';
|
||||
|
||||
const app = defineStore({
|
||||
id: 'app',
|
||||
state: () => ({
|
||||
info: {
|
||||
// 应用信息
|
||||
name: '', // 商城名称
|
||||
logo: '', // logo
|
||||
version: '', // 版本号
|
||||
cdnurl: '', // 云存储域名
|
||||
filesystem: '', // 云存储平台
|
||||
user_protocol: {}, // 用户协议
|
||||
privacy_protocol: {}, // 隐私协议
|
||||
about_us: {}, // 关于我们
|
||||
copyright: '', // 版权信息 I
|
||||
copytime: '', // 版权信息 II
|
||||
},
|
||||
platform: {
|
||||
payment: [], // 支持的支付方式
|
||||
share: {
|
||||
methods: [], // 支持的分享方式
|
||||
forwardInfo: {}, // 默认转发信息
|
||||
posterInfo: {}, // 海报信息
|
||||
linkAddress: '', // 复制链接地址
|
||||
},
|
||||
auto_login: 0, // 自动登陆
|
||||
bind_mobile: 0, // 登陆后绑定手机号提醒 (弱提醒,可手动关闭)
|
||||
},
|
||||
chat: {},
|
||||
template: {
|
||||
// 店铺装修模板
|
||||
basic: {}, // 基本信息
|
||||
home: {
|
||||
// 首页模板
|
||||
style: {},
|
||||
data: [],
|
||||
},
|
||||
user: {
|
||||
// 个人中心模板
|
||||
style: {},
|
||||
data: [],
|
||||
},
|
||||
},
|
||||
shareInfo: {}, // 全局分享信息
|
||||
has_wechat_trade_managed: 0 // 小程序发货信息管理 0 没有 || 1 有
|
||||
}),
|
||||
actions: {
|
||||
// 获取Shopro应用配置和模板
|
||||
async init(templateId = null) {
|
||||
//检查网络
|
||||
const networkStatus = await $platform.checkNetwork();
|
||||
if (!networkStatus) {
|
||||
$router.error('NetworkError');
|
||||
}
|
||||
|
||||
// 检查配置
|
||||
if (typeof baseUrl === 'undefined') {
|
||||
$router.error('EnvError');
|
||||
}
|
||||
|
||||
const res = await appApi.init(templateId);
|
||||
if (res.code === 1) {
|
||||
this.info = res.data.app;
|
||||
this.platform = res.data.platform;
|
||||
this.template = res.data.template;
|
||||
this.has_wechat_trade_managed = res.data.has_wechat_trade_managed;
|
||||
if (!res.data.template) {
|
||||
$router.error('TemplateError');
|
||||
}
|
||||
this.chat = res.data.chat;
|
||||
|
||||
// 加载主题
|
||||
const sysStore = sys();
|
||||
sysStore.setTheme();
|
||||
|
||||
// 模拟用户登录
|
||||
const userStore = user();
|
||||
if (userStore.isLogin) {
|
||||
userStore.loginAfter();
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
} else {
|
||||
$router.error('InitError', res.msg || '加载失败');
|
||||
}
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'app-store',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default app;
|
||||
149
uniapp/sheep/store/cart.js
普通文件
149
uniapp/sheep/store/cart.js
普通文件
@@ -0,0 +1,149 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import cartApi from '@/sheep/api/cart';
|
||||
|
||||
const cart = defineStore({
|
||||
id: 'cart',
|
||||
state: () => ({
|
||||
list: [], // 购物车列表
|
||||
selectedIds: [], // 已选列表
|
||||
isAllSelected: false, //是否全选
|
||||
cartSelectedTotalPrice: '0.00', // 选中项总金额
|
||||
newList: [], // 除去已下架的购物车列表
|
||||
editMode: false, // 是否是编辑模式
|
||||
}),
|
||||
getters: {
|
||||
totalPriceSelected: (state) => {
|
||||
let price = 0;
|
||||
if (!state.selectedIds.length) return price.toFixed(2);
|
||||
state.list.forEach((item) => {
|
||||
price += state.selectedIds.includes(item.id)
|
||||
? Number(item.sku_price.price) * item.goods_num
|
||||
: 0;
|
||||
});
|
||||
return price.toFixed(2);
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
// 获取购物车列表
|
||||
async getList() {
|
||||
const { data, code } = await cartApi.list();
|
||||
if (code === 1) {
|
||||
this.list = data;
|
||||
this.newList = [];
|
||||
data.forEach((item) => {
|
||||
if (item.status !== 'down') {
|
||||
this.newList.push(item);
|
||||
}
|
||||
});
|
||||
if (this.list.length === 0) {
|
||||
this.selectAll(false);
|
||||
} else {
|
||||
if (this.editMode) {
|
||||
this.isAllSelected = this.selectedIds.length === this.list.length;
|
||||
} else {
|
||||
this.isAllSelected = this.selectedIds.length === this.newList.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 提交订单更新购物车
|
||||
submitUpdateList() {
|
||||
this.selectedIds = [];
|
||||
this.getList();
|
||||
},
|
||||
|
||||
// 切换编辑模式
|
||||
onChangeEditMode(flag) {
|
||||
this.editMode = flag;
|
||||
this.selectedIds = [];
|
||||
this.getList();
|
||||
},
|
||||
|
||||
// 添加购物车
|
||||
async add(goodsInfo) {
|
||||
const { code } = await cartApi.append({
|
||||
goods_id: goodsInfo.goods_id,
|
||||
goods_num: goodsInfo.goods_num,
|
||||
goods_sku_price_id: goodsInfo.goods_sku_price_id,
|
||||
});
|
||||
if (code === 1) {
|
||||
this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 更新购物车
|
||||
async update(goodsInfo) {
|
||||
const { code } = await cartApi.update({
|
||||
goods_id: goodsInfo.goods_id,
|
||||
goods_num: goodsInfo.goods_num,
|
||||
goods_sku_price_id: goodsInfo.goods_sku_price_id,
|
||||
});
|
||||
if (code === 1) {
|
||||
this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 移除购物车
|
||||
async delete(ids) {
|
||||
if (typeof ids === 'array') {
|
||||
ids = ids.join(',');
|
||||
}
|
||||
const { code } = await cartApi.delete(ids);
|
||||
if (code === 1) {
|
||||
this.selectAll(false);
|
||||
this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 选择购物车商品
|
||||
selectSingle(goodsId) {
|
||||
if (!this.selectedIds.includes(goodsId)) {
|
||||
this.selectedIds.push(goodsId);
|
||||
} else {
|
||||
this.selectedIds.splice(this.selectedIds.indexOf(goodsId), 1);
|
||||
}
|
||||
if (this.editMode) {
|
||||
this.isAllSelected = this.selectedIds.length === this.list.length;
|
||||
} else {
|
||||
this.isAllSelected = this.selectedIds.length === this.newList.length;
|
||||
}
|
||||
},
|
||||
|
||||
// 全选
|
||||
selectAll(flag) {
|
||||
this.isAllSelected = flag;
|
||||
this.selectedIds = [];
|
||||
if (flag) {
|
||||
this.list.forEach((item) => {
|
||||
if (this.editMode) {
|
||||
if (item.status === 'down') {
|
||||
this.selectedIds.push(item.id);
|
||||
}
|
||||
}
|
||||
if (item.status !== 'down') {
|
||||
this.selectedIds.push(item.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 清空购物车
|
||||
emptyList() {
|
||||
this.list = [];
|
||||
this.selectedIds = [];
|
||||
this.isAllSelected = false;
|
||||
this.cartSelectedTotalPrice = '0.00';
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'cart-store',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default cart;
|
||||
20
uniapp/sheep/store/index.js
普通文件
20
uniapp/sheep/store/index.js
普通文件
@@ -0,0 +1,20 @@
|
||||
import { createPinia } from 'pinia';
|
||||
import piniaPersist from 'pinia-plugin-persist-uni';
|
||||
|
||||
// 自动注入所有pinia模块
|
||||
const files = import.meta.glob('./*.js', { eager: true });
|
||||
const modules = {};
|
||||
Object.keys(files).forEach((key) => {
|
||||
modules[key.replace(/(.*\/)*([^.]+).*/gi, '$2')] = files[key].default;
|
||||
});
|
||||
|
||||
export const setupPinia = (app) => {
|
||||
const pinia = createPinia();
|
||||
pinia.use(piniaPersist);
|
||||
|
||||
app.use(pinia);
|
||||
};
|
||||
|
||||
export default (name) => {
|
||||
return modules[name]();
|
||||
};
|
||||
29
uniapp/sheep/store/modal.js
普通文件
29
uniapp/sheep/store/modal.js
普通文件
@@ -0,0 +1,29 @@
|
||||
import { defineStore } from 'pinia';
|
||||
|
||||
const modal = defineStore({
|
||||
id: 'modal',
|
||||
state: () => ({
|
||||
auth: '', // 授权弹框 accountLogin|smsLogin|smsRegister|resetPassword|changeMobile|changePassword|changeUsername
|
||||
share: false, // 分享弹框
|
||||
menu: false, // 快捷菜单弹框
|
||||
advHistory: [], // 广告弹框记录
|
||||
lastTimer: {
|
||||
// 短信验证码计时器,为了防止刷新请求做了持久化
|
||||
smsLogin: 0,
|
||||
smsRegister: 0,
|
||||
changeMobile: 0,
|
||||
resetPassword: 0,
|
||||
}
|
||||
}),
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'modal-store',
|
||||
paths: ['lastTimer', 'advHistory'],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default modal;
|
||||
32
uniapp/sheep/store/sys.js
普通文件
32
uniapp/sheep/store/sys.js
普通文件
@@ -0,0 +1,32 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import app from './app';
|
||||
|
||||
const sys = defineStore({
|
||||
id: 'sys',
|
||||
state: () => ({
|
||||
theme: '', // 主题,
|
||||
mode: 'light', // 明亮模式、暗黑模式(暂未支持)
|
||||
modeAuto: false, // 跟随系统
|
||||
fontSize: 1, // 设置默认字号等级(0-4)
|
||||
}),
|
||||
getters: {},
|
||||
actions: {
|
||||
setTheme(theme = '') {
|
||||
if (theme === '') {
|
||||
this.theme = app().template?.basic.theme || 'orange';
|
||||
} else {
|
||||
this.theme = theme;
|
||||
}
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'sys-store',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default sys;
|
||||
193
uniapp/sheep/store/user.js
普通文件
193
uniapp/sheep/store/user.js
普通文件
@@ -0,0 +1,193 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import userApi from '@/sheep/api/user';
|
||||
import commissionApi from '@/sheep/api/commission';
|
||||
import $share from '@/sheep/platform/share';
|
||||
import { isEmpty, cloneDeep } from 'lodash';
|
||||
import cart from './cart';
|
||||
import app from './app';
|
||||
import { showAuthModal } from '@/sheep/hooks/useModal';
|
||||
|
||||
// 默认用户信息
|
||||
const defaultUserInfo = {
|
||||
avatar: '', // 头像
|
||||
nickname: '', // 昵称
|
||||
gender: 0, // 性别
|
||||
mobile: '', // 手机号
|
||||
money: '--', // 余额
|
||||
commission: '--', // 佣金
|
||||
score: '--', // 积分
|
||||
verification: {}, // 认证字段
|
||||
identity_text: '', // 身份文案
|
||||
identities: [], // 身份列表 taster|maker|venue|county
|
||||
is_partner: false, // 是否合伙股东
|
||||
maker_level_name: '', // 创客等级名
|
||||
madow: '', // 马豆数量
|
||||
madow_rate: '', // 马豆汇率
|
||||
madow_value: '', // 马豆约合
|
||||
voucher_available: '', // 可用兑换券
|
||||
active_30d: true, // 近 30 日是否有本人订单
|
||||
agent_form: false,
|
||||
partner_mode: true,
|
||||
};
|
||||
|
||||
// 默认订单、优惠券等其他资产信息
|
||||
const defaultNumData = {
|
||||
coupons_num: '--',
|
||||
order_num: {
|
||||
aftersale: 0,
|
||||
nocomment: 0,
|
||||
noget: 0,
|
||||
nosend: 0,
|
||||
unpaid: 0,
|
||||
},
|
||||
};
|
||||
|
||||
const user = defineStore({
|
||||
id: 'user',
|
||||
state: () => ({
|
||||
userInfo: cloneDeep(defaultUserInfo), // 用户信息
|
||||
isLogin: !!uni.getStorageSync('token'), // 登录状态
|
||||
numData: cloneDeep(defaultNumData), // 用户其他数据
|
||||
agentInfo: {}, // 分销商信息
|
||||
lastUpdateTime: 0, // 上次更新时间
|
||||
}),
|
||||
|
||||
getters: {
|
||||
// 精准识别userId,防止用户信息更新缓慢
|
||||
userId: (state) => {
|
||||
if (!state.isLogin) return 0;
|
||||
if (state.userInfo && state.userInfo.id) return state.userInfo.id;
|
||||
|
||||
// 尝试从本地存储获取 userId
|
||||
const userStore = uni.getStorageSync('user-store');
|
||||
if (!isEmpty(userStore)) {
|
||||
const userInfo = JSON.parse(userStore).userInfo;
|
||||
if (userInfo && userInfo.id) return userInfo.id;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
// 获取个人信息
|
||||
async getInfo() {
|
||||
const { code, data } = await userApi.profile();
|
||||
if (code !== 1) return;
|
||||
const merged = {
|
||||
...cloneDeep(defaultUserInfo),
|
||||
...(data || {}),
|
||||
};
|
||||
if (!Array.isArray(merged.identities)) {
|
||||
merged.identities = [];
|
||||
}
|
||||
this.userInfo = merged;
|
||||
|
||||
return Promise.resolve(merged);
|
||||
},
|
||||
|
||||
// 获取分销商信息
|
||||
async getAgentInfo() {
|
||||
const res = await commissionApi.agent();
|
||||
if (res?.code === 1) {
|
||||
this.agentInfo = res.data;
|
||||
}
|
||||
return Promise.resolve(res);
|
||||
},
|
||||
|
||||
// 获取订单、优惠券等其他资产信息
|
||||
async getNumData() {
|
||||
const { code, data } = await userApi.data();
|
||||
if (code === 1) {
|
||||
this.numData = data;
|
||||
}
|
||||
},
|
||||
|
||||
// 添加分享记录
|
||||
async addShareLog(params) {
|
||||
const { code } = await userApi.addShareLog(params);
|
||||
if (code === 1) uni.removeStorageSync('shareLog');
|
||||
},
|
||||
|
||||
// 设置token
|
||||
setToken(token = '') {
|
||||
if (token === '') {
|
||||
this.isLogin = false;
|
||||
uni.removeStorageSync('token');
|
||||
} else {
|
||||
this.isLogin = true;
|
||||
uni.setStorageSync('token', token);
|
||||
this.loginAfter();
|
||||
}
|
||||
return this.isLogin;
|
||||
},
|
||||
|
||||
// 更新用户相关信息 (手动限流 5秒之内不刷新)
|
||||
async updateUserData() {
|
||||
if (!this.isLogin) {
|
||||
this.resetUserData();
|
||||
return;
|
||||
}
|
||||
const nowTime = new Date().getTime();
|
||||
if (this.lastUpdateTime + 5000 > nowTime) return;
|
||||
await this.getInfo();
|
||||
this.getNumData();
|
||||
this.lastUpdateTime = nowTime;
|
||||
return this.userInfo;
|
||||
},
|
||||
|
||||
// 重置用户默认数据
|
||||
resetUserData() {
|
||||
this.setToken();
|
||||
this.userInfo = cloneDeep(defaultUserInfo);
|
||||
this.numData = cloneDeep(defaultNumData);
|
||||
this.agentInfo = {};
|
||||
cart().emptyList();
|
||||
},
|
||||
|
||||
// 登录后
|
||||
async loginAfter() {
|
||||
await this.updateUserData();
|
||||
cart().getList();
|
||||
// 登录后设置全局分享参数
|
||||
$share.getShareInfo();
|
||||
// 提醒绑定手机号
|
||||
if (app().platform.bind_mobile && !this.userInfo.verification?.mobile) {
|
||||
showAuthModal('changeMobile');
|
||||
}
|
||||
|
||||
// 添加分享记录
|
||||
const shareLog = uni.getStorageSync('shareLog');
|
||||
if (!isEmpty(shareLog)) {
|
||||
this.addShareLog({
|
||||
...shareLog,
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 登出
|
||||
async logout(force = false) {
|
||||
if (!force) {
|
||||
const { code } = await userApi.logout();
|
||||
if (code === 1) {
|
||||
this.resetUserData();
|
||||
}
|
||||
}
|
||||
if (force) {
|
||||
this.resetUserData();
|
||||
}
|
||||
|
||||
return !this.isLogin;
|
||||
},
|
||||
},
|
||||
|
||||
persist: {
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'user-store',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
export default user;
|
||||
在新工单中引用
屏蔽一个用户