diff --git a/db/schema_phase7.sql b/db/schema_phase7.sql new file mode 100644 index 0000000..5e032dc --- /dev/null +++ b/db/schema_phase7.sql @@ -0,0 +1,20 @@ +-- ============================================================================ +-- 优匠优服 · Phase 7 商家关联账号 +-- 主账号可绑定其他已注册用户,关联账号与主账号共享商家数据: +-- 订单 / 账单 / 开票 / 批量下单归属统一按商家聚合 +-- ============================================================================ +SET NAMES utf8mb4; + +CREATE TABLE IF NOT EXISTS `fa_merchant_member` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `merchant_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '商家ID', + `user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '关联用户ID', + `remark` varchar(50) NOT NULL DEFAULT '' COMMENT '备注/岗位', + `createtime` bigint(16) DEFAULT NULL, + `updatetime` bigint(16) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user_id` (`user_id`), + KEY `merchant_id` (`merchant_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商家关联账号'; + +-- 回滚:DROP TABLE IF EXISTS `fa_merchant_member`; diff --git a/pages/merchant/apply.vue b/pages/merchant/apply.vue index 8d4a5ec..39fa83a 100644 --- a/pages/merchant/apply.vue +++ b/pages/merchant/apply.vue @@ -6,7 +6,7 @@ - 当前状态:{{ statusText }} + {{ info.merchant.is_owner === false ? '关联账号' : '主账号' }} · 当前状态:{{ statusText }} 审核意见:{{ info.merchant.audit_msg }} @@ -42,6 +42,7 @@ 经营地址{{ info.merchant.address }} 累计订单{{ info.merchant.order_count }} 单 累计金额¥{{ info.merchant.total_amount }} + 当前账号为关联账号,企业资料由主账号维护;订单、账单、开票数据与主账号实时同步。 进入商家工作台 › diff --git a/pages/merchant/workbench.vue b/pages/merchant/workbench.vue index 0de5aca..10e7225 100644 --- a/pages/merchant/workbench.vue +++ b/pages/merchant/workbench.vue @@ -23,6 +23,26 @@ + + + 关联账号 + + 绑定账号 + + + {{ (m.nickname || '用').slice(0, 1) }} + + + {{ m.nickname }} + {{ m.role_text }} + + {{ m.mobile }} · {{ m.order_count }} 单 · ¥{{ m.total_amount }} + + 解除 + + 暂无关联账号 + 关联账号的下单、账单、开票数据与主账号实时同步 + + 按月账单刷新 @@ -49,6 +69,18 @@ 切换到业主身份 + + + + + 绑定关联账号 + 关闭 + + 输入对方已注册手机号。绑定后,对方可进入商家工作台,下单、账单、开票数据与主账号实时同步。 + + {{ binding ? '绑定中...' : '确认绑定' }} + + @@ -62,6 +94,10 @@ const info = ref(null); const months = ref([]); const orders = ref([]); + const members = ref([]); + const showBind = ref(false); + const bindMobile = ref(''); + const binding = ref(false); const verifyText = computed(() => { if (!info.value) return ''; @@ -94,6 +130,53 @@ months.value = b.months || []; orders.value = b.orders || []; } catch (e) {} + await loadMembers(); + } + + async function loadMembers() { + try { + const res = await api.merchantMembers(); + members.value = res.list || []; + } catch (e) {} + } + + function openBind() { + showBind.value = true; + bindMobile.value = ''; + } + + async function doBind() { + if (binding.value) return; + const mobile = (bindMobile.value || '').trim(); + if (!/^1\d{10}$/.test(mobile)) { + uni.showToast({ title: '请输入正确的手机号', icon: 'none' }); + return; + } + binding.value = true; + try { + await api.merchantBindMember({ mobile: mobile }); + showBind.value = false; + uni.showToast({ title: '绑定成功' }); + await load(); + } catch (e) { + } finally { + binding.value = false; + } + } + + function unbindMember(m) { + uni.showModal({ + title: '解除关联', + content: `确认解除 ${m.nickname}(${m.mobile})?解除后对方将无法查看商家数据`, + success: async (r) => { + if (!r.confirm) return; + try { + await api.merchantUnbindMember({ member_id: m.id }); + uni.showToast({ title: '已解除关联' }); + await load(); + } catch (e) {} + } + }); } function backToUser() { @@ -140,6 +223,27 @@ .o-amt { font-size: 28rpx; font-weight: 700; color: #F0710D; } .o-status { margin-top: 4rpx; font-size: 20rpx; color: #9A9A9A; } .empty { padding: 40rpx 0; text-align: center; font-size: 24rpx; color: #9A9A9A; } + .member { display: flex; align-items: center; padding: 16rpx 0; border-bottom: 1rpx solid #F4F4F4; } + .member:last-of-type { border-bottom: none; } + .member-avatar { width: 64rpx; height: 64rpx; border-radius: 50%; flex-shrink: 0; background: #FFF1E3; display: flex; align-items: center; justify-content: center; } + .member-avatar-t { font-size: 26rpx; font-weight: 700; color: #E88A2C; } + .member-main { flex: 1; min-width: 0; margin-left: 14rpx; display: flex; flex-direction: column; } + .member-head { display: flex; align-items: center; } + .member-name { font-size: 27rpx; font-weight: 600; color: #222; } + .member-role { margin-left: 10rpx; padding: 0 10rpx; line-height: 30rpx; border-radius: 8rpx; background: #F0F0F0; color: #888; font-size: 18rpx; } + .member-role.owner { background: #FFF1E0; color: #E07A2E; } + .member-meta { margin-top: 4rpx; font-size: 20rpx; color: #9A9A9A; } + .member-unbind { flex-shrink: 0; margin-left: 12rpx; padding: 0 16rpx; line-height: 44rpx; border-radius: 22rpx; background: #FFF0F0; color: #E8331B; font-size: 20rpx; } + .member-tip { display: block; margin-top: 12rpx; font-size: 20rpx; color: #B5B5B5; line-height: 30rpx; } + .mask { position: fixed; left: 0; right: 0; top: 0; bottom: 0; background: rgba(0,0,0,.45); display: flex; align-items: flex-end; z-index: 50; } + .sheet { width: 100%; background: #fff; border-radius: 28rpx 28rpx 0 0; padding: 26rpx 28rpx; padding-bottom: calc(26rpx + env(safe-area-inset-bottom)); } + .sheet-head { display: flex; align-items: center; justify-content: space-between; } + .sheet-title { font-size: 32rpx; font-weight: 700; color: #1F1F1F; } + .sheet-close { font-size: 24rpx; color: #9A9A9A; } + .sheet-tip { display: block; margin: 14rpx 0 18rpx; font-size: 22rpx; color: #9A9A9A; line-height: 34rpx; } + .sheet-input { height: 84rpx; padding: 0 22rpx; background: #F7F7F7; border-radius: 14rpx; font-size: 28rpx; color: #222; } + .sheet-btn { height: 88rpx; margin-top: 20rpx; display: flex; align-items: center; justify-content: center; border-radius: 44rpx; color: #fff; font-size: 30rpx; font-weight: 600; background: linear-gradient(135deg,#FFA93F 0%,#F0710D 100%); } + .sheet-btn.disabled { opacity: .7; } .back { margin: 26rpx 28rpx 0; height: 92rpx; display: flex; align-items: center; justify-content: center; background: #fff; border-radius: 46rpx; font-size: 28rpx; color: #F0710D; font-weight: 600; } diff --git a/pages/mine/mine.vue b/pages/mine/mine.vue index c2f1dc6..26e1146 100644 --- a/pages/mine/mine.vue +++ b/pages/mine/mine.vue @@ -91,7 +91,9 @@ const avatarText = computed(() => (user.value && user.value.nickname ? user.value.nickname.slice(0, 1) : '我')); const roleText = computed(() => { if (master.value && master.value.verify_status === 'pass') return '认证师傅'; - if (merchant.value && merchant.value.verify_status === 'pass') return '认证商家'; + if (merchant.value && merchant.value.verify_status === 'pass') { + return merchant.value.is_owner === false ? '关联商家' : '认证商家'; + } return '个人用户'; }); @@ -120,6 +122,9 @@ if (m.verify_status === 'reject') { return { title: '商家资质未通过', desc: m.audit_msg || '请补充资料后重新提交', btn: '重新提交', action: () => go('/pages/merchant/apply') }; } + if (m.is_owner === false) { + return { title: m.name || '关联商家账号', desc: '已绑定企业账号,订单 / 账单 / 开票数据与主账号同步', btn: '切换商家端', action: switchToMerchant }; + } return { title: m.name || '我的企业账号', desc: '进入商家工作台:批量下单 / 账单 / 开票', btn: '切换商家端', action: switchToMerchant }; }); diff --git a/server/application/api/controller/Auth.php b/server/application/api/controller/Auth.php index 0c93fff..4f8f333 100644 --- a/server/application/api/controller/Auth.php +++ b/server/application/api/controller/Auth.php @@ -92,7 +92,9 @@ class Auth extends Base ]; } - $merchant = Db::name('merchant')->where('user_id', $uid)->find(); + $merchantRel = $this->merchantOf($uid); + $merchant = $merchantRel ? $merchantRel['merchant'] : null; + $merchantRole = $merchantRel ? $merchantRel['role'] : null; $merchantInfo = null; if ($merchant) { $merchantInfo = [ @@ -101,6 +103,8 @@ class Auth extends Base 'verify_status' => $merchant['verify_status'], 'audit_msg' => $merchant['audit_msg'], 'settle_type' => $merchant['settle_type'], + 'role' => $merchantRole, + 'is_owner' => $merchantRole === 'owner', 'can_order' => $merchant['verify_status'] === 'pass', ]; } @@ -179,7 +183,8 @@ class Auth extends Base } if ($role === 'merchant') { - $merchant = Db::name('merchant')->where('user_id', $this->uid())->find(); + $merchantRel = $this->merchantOf($this->uid()); + $merchant = $merchantRel ? $merchantRel['merchant'] : null; if (!$merchant) { $this->error('请先完成商家入驻', ['need_apply' => 1], 2001); } @@ -200,4 +205,24 @@ class Auth extends Base $this->success('切换成功', ['role' => $role, 'redirect' => $redirect]); } + + /** + * 当前用户的商家归属:主账号 / 关联账号 + * 返回 ['merchant' => 商家行, 'role' => owner|member] + */ + protected function merchantOf($uid) + { + $merchant = Db::name('merchant')->where('user_id', $uid)->find(); + if ($merchant) { + return ['merchant' => $merchant, 'role' => 'owner']; + } + $bind = Db::name('merchant_member')->where('user_id', $uid)->find(); + if ($bind) { + $merchant = Db::name('merchant')->where('id', (int)$bind['merchant_id'])->find(); + if ($merchant) { + return ['merchant' => $merchant, 'role' => 'member']; + } + } + return null; + } } diff --git a/server/application/api/controller/Merchant.php b/server/application/api/controller/Merchant.php index 485b9de..52384ba 100644 --- a/server/application/api/controller/Merchant.php +++ b/server/application/api/controller/Merchant.php @@ -43,6 +43,11 @@ class Merchant extends Base $data['settle_type'] = 'prepay'; } + // 已被商家绑定的关联账号,企业资料由主账号维护 + if (Db::name('merchant_member')->where('user_id', $this->uid())->find()) { + $this->error('当前账号已绑定商家,企业资料请由主账号维护'); + } + $exist = Db::name('merchant')->where('user_id', $this->uid())->find(); if ($exist && $exist['verify_status'] === 'pass') { $this->error('您已是认证商家,无需重复入驻'); @@ -77,13 +82,16 @@ class Merchant extends Base return; } - $uid = $this->uid(); - $orderCount = (int)Db::name('order')->where('user_id', $uid)->where('pay_status', 1)->count(); - $totalAmount = (float)Db::name('order')->where('user_id', $uid)->where('pay_status', 1)->sum('pay_amount'); + $uids = $this->merchantUserIds($merchant); + $isOwner = (int)$merchant['user_id'] === $this->uid(); + $orderCount = (int)Db::name('order')->where('user_id', 'in', $uids)->where('pay_status', 1)->count(); + $totalAmount = (float)Db::name('order')->where('user_id', 'in', $uids)->where('pay_status', 1)->sum('pay_amount'); $invoiced = (float)Db::name('invoice')->where('merchant_id', $merchant['id'])->where('status', 'issued')->sum('amount'); $this->success('', [ 'applied' => true, + 'is_owner' => $isOwner, + 'role' => $isOwner ? 'owner' : 'member', 'merchant' => [ 'id' => (int)$merchant['id'], 'name' => $merchant['name'], @@ -97,6 +105,10 @@ class Merchant extends Base 'settle_type' => $merchant['settle_type'], 'verify_status' => $merchant['verify_status'], 'audit_msg' => $merchant['audit_msg'], + 'role' => $isOwner ? 'owner' : 'member', + 'is_owner' => $isOwner, + 'owner_user_id' => (int)$merchant['user_id'], + 'member_count' => max(0, count($uids) - 1), 'order_count' => $orderCount, 'total_amount' => round($totalAmount, 2), 'invoiced_amount' => round($invoiced, 2), @@ -112,6 +124,7 @@ class Merchant extends Base public function update() { $merchant = $this->requireMerchant(); + $this->requireMerchantOwner($merchant); $fields = ['contact', 'mobile', 'province', 'city', 'district', 'address', 'invoice_title', 'invoice_tax_no', 'invoice_bank', 'invoice_account']; $data = ['updatetime' => time()]; @@ -133,12 +146,12 @@ class Merchant extends Base */ public function bills() { - $this->requireMerchant(); - $uid = $this->uid(); + $merchant = $this->requireMerchant(); + $uids = $this->merchantUserIds($merchant); $months = Db::name('order') ->field("FROM_UNIXTIME(createtime,'%Y-%m') as month, count(*) as cnt, sum(pay_amount) as amount") - ->where('user_id', $uid)->where('pay_status', 1) + ->where('user_id', 'in', $uids)->where('pay_status', 1) ->group('month')->order('month desc')->limit(12)->select(); foreach ($months as &$m) { $m['amount'] = round((float)$m['amount'], 2); @@ -147,7 +160,7 @@ class Merchant extends Base unset($m); $orders = Db::name('order')->field('id,order_no,service_name,pay_amount,status,createtime') - ->where('user_id', $uid)->order('id desc')->limit(20)->select(); + ->where('user_id', 'in', $uids)->order('id desc')->limit(20)->select(); foreach ($orders as &$o) { $o['pay_amount'] = (float)$o['pay_amount']; $o['status_text'] = \app\common\library\OrderStatus::label($o['status']); @@ -194,7 +207,7 @@ class Merchant extends Base $this->error('发票类型错误'); } - $paid = (float)Db::name('order')->where('user_id', $this->uid())->where('pay_status', 1)->sum('pay_amount'); + $paid = (float)Db::name('order')->where('user_id', 'in', $this->merchantUserIds($merchant))->where('pay_status', 1)->sum('pay_amount'); $used = (float)Db::name('invoice')->where('merchant_id', $merchant['id'])->where('status', 'in', ['wait', 'issued'])->sum('amount'); $available = round(max(0, $paid - $used), 2); if ($amount > $available) { @@ -217,6 +230,144 @@ class Merchant extends Base $this->success('开票申请已提交,3 个工作日内开具'); } + /** + * 商家关联账号列表(主账号 + 已绑定账号) + */ + public function members() + { + $merchant = $this->requireMerchant(); + $uids = $this->merchantUserIds($merchant); + $isOwner = (int)$merchant['user_id'] === $this->uid(); + + $users = Db::name('user')->where('id', 'in', $uids)->column('id,nickname,mobile,avatar', 'id'); + $bindRows = Db::name('merchant_member')->where('merchant_id', (int)$merchant['id'])->select(); + $bindMap = []; + foreach ($bindRows as $b) { + $bindMap[(int)$b['user_id']] = $b; + } + + $stats = Db::name('order')->field('user_id,count(*) as cnt,sum(pay_amount) as amount') + ->where('user_id', 'in', $uids)->where('pay_status', 1) + ->group('user_id')->select(); + $statMap = []; + foreach ($stats as $s) { + $statMap[(int)$s['user_id']] = $s; + } + + $list = []; + foreach ($uids as $uid) { + $u = isset($users[$uid]) ? $users[$uid] : null; + $owner = (int)$uid === (int)$merchant['user_id']; + $bind = isset($bindMap[(int)$uid]) ? $bindMap[(int)$uid] : null; + $mobile = $u ? (string)$u['mobile'] : ''; + $list[] = [ + 'id' => $owner ? 0 : (int)$bind['id'], + 'user_id' => (int)$uid, + 'nickname' => $u ? ($u['nickname'] ?: ('用户' . substr($mobile, -4))) : '用户', + 'mobile' => $mobile !== '' ? substr_replace($mobile, '****', 3, 4) : '', + 'avatar' => $u ? $u['avatar'] : '', + 'role' => $owner ? 'owner' : 'member', + 'role_text' => $owner ? '主账号' : '关联账号', + 'is_owner' => $owner, + 'order_count' => isset($statMap[(int)$uid]) ? (int)$statMap[(int)$uid]['cnt'] : 0, + 'total_amount' => isset($statMap[(int)$uid]) ? round((float)$statMap[(int)$uid]['amount'], 2) : 0, + 'bind_time_text' => (!$owner && $bind) ? date('Y-m-d H:i', (int)$bind['createtime']) : '', + ]; + } + + // 主账号排在最前 + usort($list, function ($a, $b) { + return (int)$b['is_owner'] - (int)$a['is_owner']; + }); + + $this->success('', [ + 'list' => $list, + 'is_owner' => $isOwner, + 'total' => count($list), + ]); + } + + /** + * 绑定关联账号(仅主账号) + * @ApiMethod (POST) + */ + public function bindMember() + { + $merchant = $this->requireMerchant(); + $this->requireMerchantOwner($merchant); + + $mobile = trim((string)$this->request->post('mobile')); + if (!preg_match('/^1\d{10}$/', $mobile)) { + $this->error('请输入正确的关联账号手机号'); + } + + $user = Db::name('user')->where('mobile', $mobile)->find(); + if (!$user) { + $this->error('该手机号尚未注册,请对方先登录小程序'); + } + if ((int)$user['id'] === $this->uid()) { + $this->error('不能绑定当前主账号'); + } + if (Db::name('merchant')->where('user_id', $user['id'])->find()) { + $this->error('该账号已是商家主账号,不能重复绑定'); + } + if (Db::name('merchant_member')->where('user_id', $user['id'])->find()) { + $this->error('该账号已绑定其他商家'); + } + + $id = Db::name('merchant_member')->insertGetId([ + 'merchant_id' => (int)$merchant['id'], + 'user_id' => (int)$user['id'], + 'remark' => '', + 'createtime' => time(), + 'updatetime' => time(), + ]); + + try { + $this->notify('user', (int)$user['id'], '已加入商家账号', '您的账号已关联到商家「' . $merchant['name'] . '」,下单、账单、开票数据将实时同步', 'system', 0); + } catch (\Exception $e) {} + + $this->success('绑定成功,关联账号数据将实时同步', [ + 'member_id' => (int)$id, + 'user_id' => (int)$user['id'], + 'nickname' => $user['nickname'] ?: ('用户' . substr($mobile, -4)), + ]); + } + + /** + * 解除关联账号(仅主账号) + * @ApiMethod (POST) + */ + public function unbindMember() + { + $merchant = $this->requireMerchant(); + $this->requireMerchantOwner($merchant); + + $memberId = (int)$this->request->post('member_id'); + $userId = (int)$this->request->post('user_id'); + $query = Db::name('merchant_member')->where('merchant_id', (int)$merchant['id']); + if ($memberId > 0) { + $query->where('id', $memberId); + } elseif ($userId > 0) { + $query->where('user_id', $userId); + } else { + $this->error('缺少关联账号参数'); + } + $row = $query->find(); + if (!$row) { + $this->error('关联账号不存在或已解除'); + } + if ((int)$row['user_id'] === (int)$merchant['user_id']) { + $this->error('不能解除主账号'); + } + + Db::name('merchant_member')->where('id', (int)$row['id'])->delete(); + try { + $this->notify('user', (int)$row['user_id'], '商家关联已解除', '您已与商家「' . $merchant['name'] . '」解除关联,对方商家数据不再同步', 'system', 0); + } catch (\Exception $e) {} + $this->success('已解除关联,对方将不再同步商家数据'); + } + /** * 批量下单后绑定批次(订单本身走已验收的 /order/create) * @ApiMethod (POST) @@ -224,6 +375,7 @@ class Merchant extends Base public function bindBatch() { $merchant = $this->requireMerchant(); + $uids = $this->merchantUserIds($merchant); // 兼容三种入参:数组 / 逗号串 / 混合 $raw = $this->request->post('order_ids'); $orderIds = []; @@ -247,7 +399,7 @@ class Merchant extends Base $ok = 0; foreach ($orderIds as $oid) { $oid = (int)$oid; - if (!Db::name('order')->where('id', $oid)->where('user_id', $this->uid())->find()) { + if (!Db::name('order')->where('id', $oid)->where('user_id', 'in', $uids)->find()) { continue; } if (Db::name('merchant_order')->where('order_id', $oid)->find()) { @@ -265,11 +417,45 @@ class Merchant extends Base } /** - * 当前用户商家档案 + * 当前用户商家档案:主账号直接匹配,关联账号通过绑定关系匹配 */ protected function merchantInfo() { - return Db::name('merchant')->where('user_id', $this->uid())->find(); + $merchant = Db::name('merchant')->where('user_id', $this->uid())->find(); + if ($merchant) { + return $merchant; + } + $bind = Db::name('merchant_member')->where('user_id', $this->uid())->find(); + if ($bind) { + return Db::name('merchant')->where('id', (int)$bind['merchant_id'])->find(); + } + return null; + } + + /** + * 商家数据归属用户集合:主账号 + 所有关联账号 + */ + protected function merchantUserIds($merchant) + { + $ids = [(int)$merchant['user_id']]; + $rows = Db::name('merchant_member')->where('merchant_id', (int)$merchant['id'])->column('user_id'); + foreach ($rows as $id) { + $id = (int)$id; + if ($id > 0 && !in_array($id, $ids, true)) { + $ids[] = $id; + } + } + return $ids; + } + + /** + * 仅商家主账号可执行敏感操作 + */ + protected function requireMerchantOwner($merchant) + { + if ((int)$merchant['user_id'] !== $this->uid()) { + $this->error('仅主账号可执行此操作', null, 403); + } } /** diff --git a/utils/api.js b/utils/api.js index 7355a67..50628f2 100644 --- a/utils/api.js +++ b/utils/api.js @@ -76,5 +76,8 @@ export default { merchantBills: () => get('/merchant/bills'), merchantInvoices: () => get('/merchant/invoices'), merchantInvoice: (data) => post('/merchant/invoice', data), + merchantMembers: () => get('/merchant/members'), + merchantBindMember: (data) => post('/merchant/bindMember', data), + merchantUnbindMember: (data) => post('/merchant/unbindMember', data), merchantBindBatch: (data) => post('/merchant/bindBatch', data) }; diff --git a/utils/role.js b/utils/role.js index 00662f0..41a5e3b 100644 --- a/utils/role.js +++ b/utils/role.js @@ -12,7 +12,8 @@ export function getRole() { } export function setRole(role) { - uni.setStorageSync(ROLE_KEY, role === 'master' ? 'master' : 'user'); + const valid = ['user', 'master', 'merchant']; + uni.setStorageSync(ROLE_KEY, valid.indexOf(role) > -1 ? role : 'user'); } export function clearRole() {