| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- /**
- * 提现账户配置控制器(客户银行卡配置)
- */
- namespace app\admin\controller\pay;
- use app\common\controller\Backend;
- use think\facade\Db;
- class AccountConfig extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 提现账户列表
- * @return void
- */
- public function index(): void
- {
- $where = [];
- // 用户ID搜索
- if (!empty($this->request->get('user_id'))) {
- $where['wac.user_id'] = $this->request->get('user_id');
- }
- // 账户类型筛选
- if (!empty($this->request->get('account_type'))) {
- $where['wac.account_type'] = $this->request->get('account_type');
- }
- // 银行名称搜索
- if (!empty($this->request->get('bank_name'))) {
- $where[] = ['wac.bank_name', 'like', '%' . $this->request->get('bank_name') . '%'];
- }
- // 账户姓名搜索
- if (!empty($this->request->get('account_name'))) {
- $where[] = ['wac.account_name', 'like', '%' . $this->request->get('account_name') . '%'];
- }
- // 状态筛选
- if ($this->request->get('status') !== '' && $this->request->get('status') !== null) {
- $where['wac.status'] = $this->request->get('status');
- }
- // 排序
- $order = [];
- if (!empty($this->request->get('order'))) {
- $order_a = $this->request->get('order');
- $order_a = explode(',', $order_a);
- $order[$order_a[0]] = $order_a[1];
- } else {
- $order['wac.create_time'] = 'desc';
- }
- // 分页
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 20);
- // 查询数据
- $accountList = Db::name('withdrawal_account_config')
- ->alias('wac')
- ->leftJoin('user_on u', 'wac.user_id = u.id')
- ->field('
- wac.id,
- wac.user_id,
- u.name as user_name,
- wac.account_type,
- wac.account_name,
- wac.bank_name,
- wac.bank_branch,
- wac.bank_province,
- wac.bank_city,
- wac.card_number,
- wac.alipay_account,
- wac.wechat_account,
- wac.is_default,
- wac.status,
- wac.remark,
- wac.create_time,
- wac.update_time
- ')
- ->where($where)
- ->order($order)
- ->paginate([
- 'list_rows' => $limit,
- 'page' => $page
- ]);
- $list = $accountList->items();
- foreach ($list as &$item) {
- // 添加账户类型文本
- $item['account_type_text'] = $this->getAccountTypeText($item['account_type']);
- // 添加状态文本
- $item['status_text'] = $item['status'] ? '启用' : '禁用';
- // 添加默认账户文本
- $item['is_default_text'] = $item['is_default'] ? '是' : '否';
- // 部分掩码显示(保护隐私)
- if ($item['card_number']) {
- $item['card_number_masked'] = substr($item['card_number'], 0, 4) . '****' . substr($item['card_number'], -4);
- }
- if ($item['alipay_account']) {
- // 支付宝邮箱掩码
- if (strpos($item['alipay_account'], '@') !== false) {
- list($prefix, $domain) = explode('@', $item['alipay_account']);
- if (strlen($prefix) > 3) {
- $item['alipay_account_masked'] = substr($prefix, 0, 3) . '***@' . $domain;
- } else {
- $item['alipay_account_masked'] = $prefix . '***@' . $domain;
- }
- } else {
- $item['alipay_account_masked'] = substr($item['alipay_account'], 0, 3) . '***';
- }
- }
- }
- $this->success('查询成功', [
- 'list' => $list,
- 'total' => $accountList->total()
- ]);
- }
- /**
- * 获取账户详情
- * @return void
- */
- public function detail(): void
- {
- $id = $this->request->get('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $account = Db::name('withdrawal_account_config')
- ->alias('wac')
- ->leftJoin('user_on u', 'wac.user_id = u.id')
- ->where('wac.id', $id)
- ->field('wac.*, u.name as user_name')
- ->find();
- if (!$account) {
- $this->error(__('账户不存在'));
- }
- // 添加账户类型文本
- $account['account_type_text'] = $this->getAccountTypeText($account['account_type']);
- $this->success('查询成功', $account);
- }
- /**
- * 新增/编辑提现账户
- * @return void
- */
- public function edit(): void
- {
- $id = $this->request->post('id');
- $userId = $this->request->post('user_id');
- $accountType = $this->request->post('account_type');
- $accountName = $this->request->post('account_name');
- $isDefault = $this->request->post('is_default', 0);
- $status = $this->request->post('status', 1);
- $remark = $this->request->post('remark', '');
- // 根据账户类型接收不同的字段
- $bankName = $this->request->post('bank_name', '');
- $bankBranch = $this->request->post('bank_branch', '');
- $bankProvince = $this->request->post('bank_province', '');
- $bankCity = $this->request->post('bank_city', '');
- $cardNumber = $this->request->post('card_number', '');
- $alipayAccount = $this->request->post('alipay_account', '');
- $wechatAccount = $this->request->post('wechat_account', '');
- if (empty($userId)) {
- $this->error(__('用户ID不能为空'));
- }
- if (empty($accountType)) {
- $this->error(__('请选择账户类型'));
- }
- if (empty($accountName)) {
- $this->error(__('账户姓名不能为空'));
- }
- // 验证用户是否存在
- $userInfo = Db::name('user')
- ->where('id', $userId)
- ->find();
- if (!$userInfo) {
- $this->error(__('用户不存在'));
- }
- // 验证账户类型对应的必填字段
- $validationResult = $this->validateAccountFields($accountType, [
- 'bank_name' => $bankName,
- 'card_number' => $cardNumber,
- 'alipay_account' => $alipayAccount,
- 'wechat_account' => $wechatAccount
- ]);
- if ($validationResult !== true) {
- $this->error($validationResult);
- }
- // 验证银行卡号格式(如果是银行卡)
- if ($accountType == 'bank' && !empty($cardNumber)) {
- if (!preg_match('/^[0-9]{16,19}$/', $cardNumber)) {
- $this->error(__('银行卡号格式不正确,应为16-19位数字'));
- }
- }
- // 验证支付宝账号格式
- if ($accountType == 'alipay' && !empty($alipayAccount)) {
- if (!filter_var($alipayAccount, FILTER_VALIDATE_EMAIL) && !preg_match('/^1[3-9]\d{9}$/', $alipayAccount)) {
- $this->error(__('支付宝账号应为邮箱或手机号格式'));
- }
- }
- $data = [
- 'user_id' => $userId,
- 'account_type' => $accountType,
- 'account_name' => $accountName,
- 'bank_name' => $accountType == 'bank' ? $bankName : '',
- 'bank_branch' => $accountType == 'bank' ? $bankBranch : '',
- 'bank_province' => $accountType == 'bank' ? $bankProvince : '',
- 'bank_city' => $accountType == 'bank' ? $bankCity : '',
- 'card_number' => $accountType == 'bank' ? $cardNumber : '',
- 'alipay_account' => $accountType == 'alipay' ? $alipayAccount : '',
- 'wechat_account' => $accountType == 'wechat' ? $wechatAccount : '',
- 'is_default' => $isDefault,
- 'status' => $status,
- 'remark' => $remark,
- 'update_time' => date('Y-m-d H:i:s')
- ];
- try {
- if (!empty($id)) {
- // 编辑
- $accountInfo = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->find();
- if (!$accountInfo) {
- $this->error(__('账户不存在'));
- }
- // 如果设置为默认账户,需要取消该用户的其他默认账户
- if ($isDefault == 1) {
- Db::name('withdrawal_account_config')
- ->where('user_id', $userId)
- ->where('id', '<>', $id)
- ->where('account_type', $accountType)
- ->update(['is_default' => 0]);
- }
- $result = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->update($data);
- $message = '编辑成功';
- } else {
- // 新增
- $data['create_time'] = date('Y-m-d H:i:s');
- // 检查是否已存在相同账户
- $existCheck = Db::name('withdrawal_account_config')
- ->where('user_id', $userId)
- ->where('account_type', $accountType);
- if ($accountType == 'bank') {
- $existCheck->where('card_number', $cardNumber);
- } elseif ($accountType == 'alipay') {
- $existCheck->where('alipay_account', $alipayAccount);
- } elseif ($accountType == 'wechat') {
- $existCheck->where('wechat_account', $wechatAccount);
- }
- if ($existCheck->find()) {
- throw new \Exception('该账户已存在');
- }
- // 如果设置为默认账户,需要取消该用户的其他默认账户
- if ($isDefault == 1) {
- Db::name('withdrawal_account_config')
- ->where('user_id', $userId)
- ->where('account_type', $accountType)
- ->update(['is_default' => 0]);
- }
- $result = Db::name('withdrawal_account_config')
- ->insert($data);
- $id = Db::name('withdrawal_account_config')->getLastInsID();
- $message = '新增成功';
- }
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($message, ['id' => $id]);
- } else {
- $this->error(__('操作失败'));
- }
- }
- /**
- * 删除提现账户
- * @return void
- */
- public function delete(): void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $accountInfo = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->find();
- if (!$accountInfo) {
- $this->error(__('账户不存在'));
- }
- // 检查是否正在使用(如果有提现记录)
- $hasWithdrawal = Db::name('withdrawal_record')
- ->where('account_info', 'like', '%' . $accountInfo['card_number'] . '%')
- // ->whereOr('account_info', 'like', '%' . $accountInfo['alipay_account'] . '%')
- // ->whereOr('account_info', 'like', '%' . $accountInfo['wechat_account'] . '%')
- ->find();
- if ($hasWithdrawal) {
- $this->error(__('该账户已有提现记录,不能删除'));
- }
- $result = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->delete();
- if ($result) {
- $this->success('删除成功');
- } else {
- $this->error(__('删除失败'));
- }
- }
- /**
- * 设置默认账户
- * @return void
- */
- public function setDefault(): void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- $accountInfo = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->find();
- if (!$accountInfo) {
- $this->error(__('账户不存在'));
- }
- try {
- // 取消该用户同类型账户的其他默认设置
- Db::name('withdrawal_account_config')
- ->where('user_id', $accountInfo['user_id'])
- ->where('account_type', $accountInfo['account_type'])
- ->where('id', '<>', $id)
- ->update(['is_default' => 0]);
- // 设置当前账户为默认
- $result = Db::name('withdrawal_account_config')
- ->where('id', $id)
- ->update([
- 'is_default' => 1,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if ($result) {
- $this->success('设置默认成功');
- } else {
- $this->error(__('设置失败'));
- }
- } catch (\Exception $e) {
- $this->error('操作失败: ' . $e->getMessage());
- }
- }
- /**
- * 获取用户的提现账户列表
- * @return void
- */
- public function getUserAccounts(): void
- {
- $userId = $this->request->get('user_id');
- if (empty($userId)) {
- $this->error(__('用户ID不能为空'));
- }
- $accounts = Db::name('withdrawal_account_config')
- ->where('user_id', $userId)
- ->where('status', 1)
- ->order('is_default', 'desc')
- ->order('create_time', 'desc')
- ->select()
- ->toArray();
- foreach ($accounts as &$account) {
- // 添加账户类型文本
- $account['account_type_text'] = $this->getAccountTypeText($account['account_type']);
- // 部分掩码显示
- if ($account['card_number']) {
- $account['card_number_masked'] = substr($account['card_number'], 0, 4) . '****' . substr($account['card_number'], -4);
- }
- }
- $this->success('查询成功', $accounts);
- }
- /**
- * 验证账户字段
- * @param string $accountType 账户类型
- * @param array $fields 字段值
- * @return bool|string
- */
- private function validateAccountFields(string $accountType, array $fields): bool|string
- {
- if ($accountType == 'bank') {
- if (empty($fields['bank_name'])) {
- return '银行名称不能为空';
- }
- if (empty($fields['card_number'])) {
- return '银行卡号不能为空';
- }
- } elseif ($accountType == 'alipay') {
- if (empty($fields['alipay_account'])) {
- return '支付宝账号不能为空';
- }
- } elseif ($accountType == 'wechat') {
- if (empty($fields['wechat_account'])) {
- return '微信账号不能为空';
- }
- }
- return true;
- }
- /**
- * 获取账户类型文本
- * @param string $type
- * @return string
- */
- private function getAccountTypeText(string $type): string
- {
- $map = [
- 'bank' => '银行卡',
- 'alipay' => '支付宝',
- 'wechat' => '微信'
- ];
- return $map[$type] ?? '未知类型';
- }
- /**
- * 获取账户类型选项
- * @return void
- */
- public function accountTypeOptions(): void
- {
- $options = [
- ['value' => 'bank', 'label' => '银行卡'],
- ['value' => 'alipay', 'label' => '支付宝'],
- ['value' => 'wechat', 'label' => '微信']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 获取状态选项
- * @return void
- */
- public function statusOptions(): void
- {
- $options = [
- ['value' => 0, 'label' => '禁用'],
- ['value' => 1, 'label' => '启用']
- ];
- $this->success('获取成功', $options);
- }
- /**
- * 获取银行列表(常见银行)
- * @return void
- */
- public function bankList(): void
- {
- $banks = [
- '中国工商银行',
- '中国农业银行',
- '中国银行',
- '中国建设银行',
- '交通银行',
- '招商银行',
- '中信银行',
- '中国民生银行',
- '中国光大银行',
- '华夏银行',
- '兴业银行',
- '浦发银行',
- '平安银行',
- '广发银行',
- '邮政储蓄银行'
- ];
- $options = [];
- foreach ($banks as $bank) {
- $options[] = ['value' => $bank, 'label' => $bank];
- }
- $this->success('获取成功', $options);
- }
- }
|