第二次更新

This commit is contained in:
2026-09-10 20:56:46 +08:00
parent 4d6a7e7aac
commit caeb4d6760
310 changed files with 2427 additions and 382 deletions
@@ -15,6 +15,7 @@ namespace app\store\controller\partners;
use app\common\service\PartnersBalance;
use app\common\service\QueueService;
use app\common\model\qyt\Enterprise as EnterpriseModel;
use app\common\service\QytOil;
use app\store\controller\Controller;
use app\store\model\order\QrCode as QrCodeModel;
@@ -71,7 +72,15 @@ class Order extends Controller
if ($order['oil_status'] != 1) {
return $this->renderError('该单尚未发放成功,无法获取加油地址');
}
$h5Url = (new QytOil())->h5Url((string) $order['mobile']);
// 免登链接要带本单企业的 creditcode(营业执照号),把登录固定到该企业
$enterprise = EnterpriseModel::detail((int) $order['qyt_enterprise_id']);
if (!$enterprise) {
return $this->renderError('本单绑定的企油通企业不存在,无法生成加油地址');
}
if ($enterprise['status'] !== 'approved') {
return $this->renderError('企业「' . $enterprise['company_name'] . '」尚未通过企油通审核,无法生成加油地址');
}
$h5Url = QytOil::forEnterprise($enterprise)->h5Url((string) $order['mobile']);
if (!$h5Url) {
return $this->renderError('生成加油地址失败,请检查企油通配置');
}
@@ -115,11 +124,23 @@ class Order extends Controller
if ($logId === false) {
return $this->renderError('合作商余额不足,请先充值后再重发');
}
// 仅允许从「发放失败(3)」重置为「未提交(0)」,避免与正在处理的单冲突
// 仅允许从「发放失败(3)」重置为「未提交(0)」,避免与「发放中(1)」的单冲突。
// 同时清空编号并把重发次数 +1:企油通按 batch_no 幂等,沿用老批次的话
// 7.8 会一直返回那条旧的失败记录,重发永远不可能成功。
$reset = Db::name('partners_order')->where('order_id', $orderId)
->where('oil_status', 0)
->where('oil_status_zsy', 3)
->update(['oil_status_zsy' => 0, 'oil_text' => '后台重新发放', 'update_time' => time()]);
->update([
'oil_status_zsy' => 0,
'batch_no' => '',
'waybill_no' => '',
'waybill_no_code' => '',
'qyt_retry_num' => Db::raw('qyt_retry_num + 1'),
'qyt_query_time' => 0,
'qyt_query_num' => 0,
'oil_text' => '后台重新发放',
'update_time' => time(),
]);
if (!$reset) {
// 状态已被其它进程改变 -> 回补刚扣的余额
PartnersBalance::refund(
@@ -0,0 +1,242 @@
<?php
// +----------------------------------------------------------------------
// | 后台 · 企油通企业管理
// +----------------------------------------------------------------------
// | list/detail/add/edit/delete/setDefault 本地维护
// | apply() 7.2 企业申请创建(提交到企油通)
// | syncAccount() 7.5 企业账户信息查询(回填开户状态)
// | syncList() 7.22 企业列表查询(拉取本 appId 名下企业,需先开通权限)
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\store\controller\qyt;
use app\common\model\qyt\Enterprise as CommonEnterprise;
use app\common\service\QytOil;
use app\store\controller\Controller;
use app\store\model\qyt\Enterprise as EnterpriseModel;
use think\facade\Db;
use think\response\Json;
class Enterprise extends Controller
{
/**
* 企业列表
*/
public function list(): Json
{
$model = new EnterpriseModel;
$list = $model->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} 家");
}
/** 记录同步结果 */
private function saveSync($model, string $text): void
{
$model->save(['sync_time' => time(), 'sync_text' => $text, 'update_time' => time()]);
}
}