model = new \addons\shopro\model\split\Receiver; } public function index() { $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(); $total = $this->model->where($where)->count(); $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select(); $list = $this->formatReceiverList($list); return $this->success('操作成功', null, ['total' => $total, 'rows' => $list]); } $this->assignconfig('splitHuifuOnly', (new SplitConfig())->isHuifuMode()); return $this->view->fetch(); } public function add() { if ($this->request->isPost()) { $params = $this->request->post('row/a'); if (empty($params['receiver_account'])) { $this->error('请填写接收方账号'); } $this->model->create($params); $this->success('添加成功'); } return $this->view->fetch(); } public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error('记录不存在'); } if ($this->request->isPost()) { $params = $this->request->post('row/a'); $row->save($params); $this->success('保存成功'); } $this->view->assign('row', $row); return $this->view->fetch(); } public function del($ids = null) { $row = $this->model->get($ids); if ($row) { $row->status = 'forbidden'; $row->save(); } $this->success('操作成功'); } protected function formatReceiverList($list) { $rows = []; $userIds = []; foreach ($list as $item) { $data = is_array($item) ? $item : $item->toArray(); $roleId = intval($data['role_id'] ?? 0); if ($roleId > 0) { $userIds[] = $roleId; } } $userIds = array_values(array_unique($userIds)); $users = []; if ($userIds) { $userRows = ShopUser::where('id', 'in', $userIds)->field('id,nickname,mobile')->select(); foreach ($userRows as $user) { $u = is_array($user) ? $user : $user->toArray(); $users[intval($u['id'])] = $u; } } $onboardMap = []; if ($userIds) { try { $onboardRows = Db::name('shopro_huifu_onboard') ->where('user_id', 'in', $userIds) ->order('id desc') ->select(); foreach ($onboardRows as $ob) { $uid = intval($ob['user_id']); if (!isset($onboardMap[$uid])) { $onboardMap[$uid] = $ob; } } } catch (\Throwable $e) { $onboardMap = []; } } foreach ($list as $item) { $data = is_array($item) ? $item : $item->toArray(); $roleId = intval($data['role_id'] ?? 0); $user = $roleId > 0 && isset($users[$roleId]) ? $users[$roleId] : null; $onboard = $roleId > 0 && isset($onboardMap[$roleId]) ? $onboardMap[$roleId] : null; $data['user_id'] = $roleId; $data['user_nickname'] = is_array($user) ? trim((string) ($user['nickname'] ?? '')) : ''; $data['user_mobile'] = is_array($user) ? trim((string) ($user['mobile'] ?? '')) : ''; $data['user_display'] = $this->formatUserDisplay($roleId, $user); $data['role_id_text'] = $roleId > 0 ? (string) $roleId : '0(通用)'; $onboardHuifuId = trim((string) ($onboard['huifu_id'] ?? '')); $receiverAccount = trim((string) ($data['receiver_account'] ?? '')); $data['onboard_huifu_id'] = $onboardHuifuId; $data['onboard_status'] = $onboard['onboard_status'] ?? ''; $data['onboard_status_text'] = $this->formatOnboardStatus($onboard['onboard_status'] ?? ''); $data['account_match'] = ($onboardHuifuId !== '' && $receiverAccount !== '' && $onboardHuifuId === $receiverAccount) ? 1 : 0; $data['account_match_text'] = $this->formatAccountMatch($roleId, $onboardHuifuId, $receiverAccount); $data['channel_text'] = ($data['channel'] ?? '') === 'huifu' ? '汇付' : '微信'; $data['role_type_text'] = $this->roleTypeLabel($data['role_type'] ?? ''); $data['status_text'] = ($data['status'] ?? '') === 'forbidden' ? '禁用' : '正常'; $data['createtime_text'] = !empty($data['createtime']) ? date('Y-m-d H:i:s', $data['createtime']) : '-'; $rows[] = $data; } return $rows; } protected function formatUserDisplay($roleId, $user) { if ($roleId <= 0) { return '—(规则模式通用接收方)'; } if (!is_array($user)) { return '用户#' . $roleId . '(未找到)'; } $nickname = trim((string) ($user['nickname'] ?? '')); $mobile = trim((string) ($user['mobile'] ?? '')); if ($nickname !== '' && $mobile !== '') { return $nickname . '(' . $mobile . ')'; } if ($nickname !== '') { return $nickname; } if ($mobile !== '') { return $mobile; } return '用户#' . $roleId; } protected function formatOnboardStatus($status) { $map = [ 'draft' => '草稿', 'submitting' => '提交中', 'basic_ok' => '基础进件完成', 'busi_pending' => '业务开通中', 'approved' => '已通过', 'rejected' => '已驳回', 'failed' => '失败', ]; $status = trim((string) $status); return $status !== '' ? ($map[$status] ?? $status) : '—'; } protected function formatAccountMatch($roleId, $onboardHuifuId, $receiverAccount) { if ($roleId <= 0) { return '—'; } if ($onboardHuifuId === '' && $receiverAccount === '') { return '未配置'; } if ($onboardHuifuId === '') { return '未进件'; } if ($receiverAccount === '') { return '未填账号'; } return $onboardHuifuId === $receiverAccount ? '与进件一致' : '与进件不一致'; } protected function roleTypeLabel($type) { $map = [ 'platform' => '平台', 'promoter' => '推广员', 'regional_agent' => '区域公司', ]; return $map[$type] ?? ($type ?: '-'); } public function syncWx($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error('记录不存在'); } if ((new SplitConfig())->isHuifuMode()) { $this->error('当前为汇付分账:无需同步微信接收方。请在汇付控台绑定分账关系,并在此填写 huifu_id'); } try { $service = new ProfitSharingService(); $service->addReceiver([ 'type' => $row['receiver_type'], 'account' => $row['receiver_account'], 'name' => $row['receiver_name'], 'relation_type' => $row['relation_type'] ?: 'USER', ]); $row->wx_status = 1; $row->wx_message = '同步成功'; $row->save(); $this->success('同步成功'); } catch (\Exception $e) { $row->wx_status = 2; $row->wx_message = $e->getMessage(); $row->save(); $this->error($e->getMessage()); } } }