feat(merchant): 商家关联账号绑定与数据同步
- 新增 fa_merchant_member 关联账号表,主账号可绑定/解绑已注册用户 - 关联账号可切换商家端,订单、账单、开票、批量归属按主账号+关联账号聚合 - 商家工作台新增关联账号列表、绑定弹层、解除入口与个人订单统计 - 关联账号在商家入驻页显示只读企业资料与同步说明 - 我的页面区分主账号/关联商家身份;修复 merchant 角色本地存储 - 绑定/解绑仅主账号可操作,关联账号操作返回 403 - 新增 schema_phase7.sql 迁移脚本
这个提交包含在:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
在新工单中引用
屏蔽一个用户