getList($this->request->param()); return $this->renderSuccess(compact('list')); } /** * 下拉选项(合作商绑定企业时用) */ public function options(): Json { return $this->renderSuccess(['list' => EnterpriseModel::options()]); } /** * 企业详情 */ public function detail(int $enterpriseId): Json { $detail = EnterpriseModel::detail($enterpriseId); if (!$detail) { return $this->renderError('企业不存在'); } return $this->renderSuccess(compact('detail')); } /** * 新增企业 */ public function add(): Json { $model = new EnterpriseModel; if ($model->add($this->postForm())) { return $this->renderSuccess('添加成功'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 编辑企业 */ public function edit(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } if ($model->edit($this->postForm())) { return $this->renderSuccess('更新成功'); } return $this->renderError($model->getError() ?: '更新失败'); } /** * 删除企业 */ public function delete(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } if ($model->remove()) { return $this->renderSuccess('删除成功'); } return $this->renderError($model->getError() ?: '删除失败'); } /** * 设为默认企业(合作商未绑定企业时的兜底) */ public function setDefault(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } if ($model->setDefault()) { return $this->renderSuccess('设置成功'); } return $this->renderError($model->getError() ?: '设置失败'); } /** * 7.2 企业申请创建:把本地企业提交到企油通开户 */ public function apply(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } if (empty($model['business_license_url'])) { return $this->renderError('请先上传营业执照照片地址(7.2 必填)'); } // 申请可同时开通多个通道,文档要求逗号分隔的编码,如 "1,2" $codes = $model['bound_passageway'] !== '' ? $model['bound_passageway'] : (string) CommonEnterprise::PASSAGEWAY_CODE[$model['passageway']]; $res = QytOil::forEnterprise($model)->openAnAccount([ 'business_license' => $model['business_license'], 'business_license_url' => $model['business_license_url'], 'passageway' => $codes, 'company_name' => $model['company_name'], 'contact_mobile' => $model['contact_mobile'], 'contact_name' => $model['contact_name'], ]); if ($res['code'] !== '00') { $this->saveSync($model, '申请失败:' . $res['message']); return $this->renderError('企油通返回:' . ($res['message'] ?: '申请失败')); } // status: 0 失败 / 1 待审核 / 2 审核成功 $status = (int) ($res['data']['status'] ?? 0); $map = [0 => 'rejected', 1 => 'submitted', 2 => 'approved']; $model->save([ 'enterprise_number' => (string) ($res['data']['enterprise_number'] ?? $model['enterprise_number']), 'status' => $map[$status] ?? 'submitted', 'bound_passageway' => $codes, 'sync_time' => time(), 'sync_text' => (string) ($res['data']['msg'] ?? '已提交'), 'update_time' => time(), ]); return $this->renderSuccess((string) ($res['data']['msg'] ?? '已提交,等待企油通审核')); } /** * 7.5 企业账户信息查询:回填开户行 / 账户 / 开户状态 */ public function syncAccount(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } $res = QytOil::forEnterprise($model)->accountQuery(); if ($res['code'] !== '00') { $this->saveSync($model, '账户查询失败:' . $res['message']); return $this->renderError('企油通返回:' . ($res['message'] ?: '查询失败')); } $d = $res['data']; $model->save([ 'account' => (string) ($d['account'] ?? ''), 'account_bank' => (string) ($d['account_bank'] ?? ''), 'account_name' => (string) ($d['account_name'] ?? ''), 'account_status' => (int) ($d['status'] ?? 0), 'company_name' => $d['enterprise_name'] ?? $model['company_name'], 'sync_time' => time(), 'sync_text' => (int) ($d['status'] ?? 0) === 2 ? '开户成功' : '审核中', 'update_time' => time(), ]); return $this->renderSuccess('同步成功'); } /** * 7.22 企业列表查询:拉取本 appId 名下企业,本地没有的自动建档 * 需先向企油通开通「企业列表查询权限」,未开通会返回 403 */ public function syncList(): Json { $res = (new QytOil())->enterpriseList(['page' => 1, 'page_size' => 100]); if ($res['code'] !== '00') { return $this->renderError('企油通返回:' . ($res['message'] ?: '查询失败') . '(该接口需先开通企业列表查询权限)'); } $rows = (array) ($res['data']['list'] ?? []); $added = $updated = 0; foreach ($rows as $row) { $license = trim((string) ($row['business_license'] ?? '')); if ($license === '') { continue; } // 已绑定通道数组如 [1,2],转成本地逗号串 $bound = implode(',', array_map('intval', (array) ($row['passageway'] ?? []))); $exist = Db::name('qyt_enterprise')->where('business_license', $license)->find(); if ($exist) { Db::name('qyt_enterprise')->where('enterprise_id', $exist['enterprise_id'])->update([ 'company_name' => (string) ($row['enterprise_name'] ?? $exist['company_name']), 'status' => (string) ($row['status'] ?? $exist['status']), 'bound_passageway' => $bound ?: $exist['bound_passageway'], 'sync_time' => time(), 'sync_text' => '已从企业列表同步', 'update_time' => time(), ]); $updated++; continue; } // 新企业建档:默认走支付宝企业码,机构号需人工补填后才能下发 Db::name('qyt_enterprise')->insert([ 'company_name' => (string) ($row['enterprise_name'] ?? ''), 'business_license' => $license, 'passageway' => in_array(2, (array) ($row['passageway'] ?? []), true) && !in_array(1, (array) ($row['passageway'] ?? []), true) ? 'pingan' : 'alipay', 'bound_passageway' => $bound, 'status' => (string) ($row['status'] ?? 'pending'), 'is_enable' => 1, 'is_default' => 0, 'sort' => 100, 'sync_time' => time(), 'sync_text' => '由企业列表自动建档,请补填机构号', 'store_id' => $this->storeId, 'create_time' => time(), 'update_time' => time(), ]); $added++; } return $this->renderSuccess("同步完成:新增 {$added} 家,更新 {$updated} 家"); } /** * 7.11 企业账户余额(仅平安银行通道;银行侧实时查询,勿高频调用) */ public function balance(int $enterpriseId): Json { $model = EnterpriseModel::detail($enterpriseId); if (!$model) { return $this->renderError('企业不存在'); } $res = QytOil::forEnterprise($model)->distributable(); if ($res['code'] !== '00') { return $this->renderError('企油通返回:' . ($res['message'] ?: '查询失败')); } return $this->renderSuccess([ 'balance' => QytOil::fenToYuan($res['data']['balance'] ?? 0), 'available_balance' => QytOil::fenToYuan($res['data']['available_balance'] ?? 0), ], '查询成功'); } /** 记录同步结果 */ private function saveSync($model, string $text): void { $model->save(['sync_time' => time(), 'sync_text' => $text, 'update_time' => time()]); } }