feat: 新增发票抬头、晒单、钱包日志等模型及后台管理功能
- 新增 invoice_title、showcase、wallet_log 模型及对应语言包 - 新增后台控制器、视图及搜索字段配置 - 修复服务类目删除保护逻辑 - 优化后台登录页面文案及样式 - 新增前端角色切换逻辑及页面拦截 - 新增 SVG 图标及样式变量 - 修复接口权限及下拉框默认值问题
这个提交包含在:
@@ -51,23 +51,14 @@ class Auth extends Base
|
||||
if ($user['status'] !== 'normal') {
|
||||
$this->error('账号已被禁用');
|
||||
}
|
||||
// 若微信手机号快捷登录初始化的用户未设密码,直接落库更新密码
|
||||
if (empty($user['password'])) {
|
||||
$salt = Random::alnum();
|
||||
$encryptPassword = $this->auth->getEncryptPassword($password, $salt);
|
||||
Db::name('user')->where('id', $user['id'])->update([
|
||||
'password' => $encryptPassword,
|
||||
'salt' => $salt,
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$this->auth->direct($user['id']);
|
||||
} else {
|
||||
$encryptPassword = $this->auth->getEncryptPassword($password, $user['salt']);
|
||||
if ($user['password'] !== $encryptPassword) {
|
||||
$this->error('手机号或密码不正确');
|
||||
}
|
||||
$this->auth->direct($user['id']);
|
||||
$this->error('该账号尚未设置登录密码,请使用验证码或微信登录后设置密码');
|
||||
}
|
||||
$encryptPassword = $this->auth->getEncryptPassword($password, $user['salt']);
|
||||
if ($user['password'] !== $encryptPassword) {
|
||||
$this->error('手机号或密码不正确');
|
||||
}
|
||||
$this->auth->direct($user['id']);
|
||||
}
|
||||
|
||||
// 直查真实数据库返回用户完整信息
|
||||
@@ -153,6 +144,7 @@ class Auth extends Base
|
||||
$merchantRole = $merchantRel ? $merchantRel['role'] : null;
|
||||
$merchantInfo = null;
|
||||
if ($merchant) {
|
||||
$memberCount = (int)Db::name('merchant_member')->where('merchant_id', (int)$merchant['id'])->count();
|
||||
$merchantInfo = [
|
||||
'id' => (int)$merchant['id'],
|
||||
'name' => $merchant['name'],
|
||||
@@ -165,12 +157,42 @@ class Auth extends Base
|
||||
'role' => $merchantRole,
|
||||
'is_owner' => $merchantRole === 'owner',
|
||||
'can_order' => $merchant['verify_status'] === 'pass',
|
||||
'member_count' => $memberCount,
|
||||
];
|
||||
}
|
||||
|
||||
$userType = (!empty($user['user_type']) && $user['user_type'] === 'merchant') || ($merchant && $merchant['verify_status'] === 'pass')
|
||||
? 'merchant'
|
||||
: 'personal';
|
||||
// 统计发单端各状态订单数量(与 Order::index 共享同一套团队归属口径)
|
||||
$team = $this->currentMerchantTeam();
|
||||
$teamUids = $team ? $team['user_ids'] : [$uid];
|
||||
$statusGroups = Db::name('order')
|
||||
->field('status, count(*) as cnt')
|
||||
->where('user_id', 'in', $teamUids)
|
||||
->where('user_deleted', 0)
|
||||
->group('status')
|
||||
->select();
|
||||
$rawCounts = [];
|
||||
foreach ($statusGroups as $sg) {
|
||||
$rawCounts[(int)$sg['status']] = (int)$sg['cnt'];
|
||||
}
|
||||
$getCnt = function ($st) use ($rawCounts) {
|
||||
return isset($rawCounts[$st]) ? $rawCounts[$st] : 0;
|
||||
};
|
||||
$orderCounts = [
|
||||
'pending_pay' => $getCnt(0),
|
||||
'pending_accept' => $getCnt(1),
|
||||
'quoting' => $getCnt(2),
|
||||
'scheduled' => $getCnt(3),
|
||||
'serving' => $getCnt(4) + $getCnt(5) + $getCnt(6),
|
||||
'pending_verify' => $getCnt(7),
|
||||
];
|
||||
|
||||
if ($merchantRole === 'member' && $merchant && $merchant['verify_status'] === 'pass') {
|
||||
$userType = 'merchant';
|
||||
} elseif (!empty($user['user_type']) && in_array($user['user_type'], ['personal', 'merchant'], true)) {
|
||||
$userType = $user['user_type'];
|
||||
} else {
|
||||
$userType = ($merchant && $merchant['verify_status'] === 'pass') ? 'merchant' : 'personal';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => (int)$user['id'],
|
||||
@@ -187,6 +209,7 @@ class Auth extends Base
|
||||
'balance' => (float)$user['money'],
|
||||
'coupon_count' => (int)$couponCount,
|
||||
'unread_count' => (int)$unread,
|
||||
'order_counts' => $orderCounts,
|
||||
'is_master' => $master ? true : false,
|
||||
'master' => $masterInfo,
|
||||
'is_merchant' => $merchant ? true : false,
|
||||
@@ -230,6 +253,13 @@ class Auth extends Base
|
||||
$this->error('电子邮箱格式不正确');
|
||||
}
|
||||
|
||||
$merchantRel = $this->merchantOf($uid);
|
||||
if ($userType === 'merchant') {
|
||||
if (!$merchantRel || ($merchantRel['merchant']['verify_status'] ?? '') !== 'pass') {
|
||||
$this->error('请先提交企业入驻申请并通过审核后再启用商家账号');
|
||||
}
|
||||
}
|
||||
|
||||
$userUpdate = [
|
||||
'nickname' => $nickname,
|
||||
'user_type' => $userType,
|
||||
@@ -246,11 +276,11 @@ class Auth extends Base
|
||||
}
|
||||
Db::name('user')->where('id', $uid)->update($userUpdate);
|
||||
|
||||
if ($userType === 'merchant') {
|
||||
$existMerchant = Db::name('merchant')->where('user_id', $uid)->find();
|
||||
if ($userType === 'merchant' && $merchantRel && $merchantRel['role'] === 'owner') {
|
||||
$existMerchant = $merchantRel['merchant'];
|
||||
$curUser = Db::name('user')->where('id', $uid)->find();
|
||||
$merchantSave = [
|
||||
'name' => $companyName !== '' ? $companyName : ($existMerchant ? $existMerchant['name'] : $nickname),
|
||||
'name' => $companyName !== '' ? $companyName : (!empty($existMerchant['name']) ? $existMerchant['name'] : $nickname),
|
||||
'legal_person' => $legalPerson,
|
||||
'legal_mobile' => $legalMobile,
|
||||
'tech_mobile' => $techMobile,
|
||||
@@ -260,15 +290,7 @@ class Auth extends Base
|
||||
'email' => $email,
|
||||
'updatetime' => time(),
|
||||
];
|
||||
if ($existMerchant) {
|
||||
Db::name('merchant')->where('id', $existMerchant['id'])->update($merchantSave);
|
||||
} else {
|
||||
$merchantSave['user_id'] = $uid;
|
||||
$merchantSave['license_no'] = '';
|
||||
$merchantSave['verify_status'] = 'pass';
|
||||
$merchantSave['createtime'] = time();
|
||||
Db::name('merchant')->insert($merchantSave);
|
||||
}
|
||||
Db::name('merchant')->where('id', (int)$existMerchant['id'])->update($merchantSave);
|
||||
}
|
||||
|
||||
$this->success('保存成功', ['userinfo' => $this->profileData()]);
|
||||
@@ -405,7 +427,7 @@ class Auth extends Base
|
||||
if ($role === 'master') {
|
||||
$redirect = '/pages/master/hall';
|
||||
} elseif ($role === 'merchant') {
|
||||
$redirect = '/pages/merchant/workbench';
|
||||
$redirect = '/pages/mine/mine';
|
||||
}
|
||||
|
||||
$this->success('切换成功', ['role' => $role, 'redirect' => $redirect]);
|
||||
|
||||
@@ -84,7 +84,10 @@ class Base extends Api
|
||||
$this->error('订单不存在');
|
||||
}
|
||||
if ($side === 'user' && (int)$order['user_id'] !== $this->uid()) {
|
||||
$this->error('无权操作该订单', null, 403);
|
||||
$team = $this->currentMerchantTeam();
|
||||
if (!$team || !in_array((int)$order['user_id'], $team['user_ids'], true)) {
|
||||
$this->error('无权操作该订单', null, 403);
|
||||
}
|
||||
}
|
||||
if ($side === 'master') {
|
||||
$master = $this->requireMaster();
|
||||
@@ -95,6 +98,40 @@ class Base extends Api
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户所属的已认证商家团队(主账号 + 关联子账号)
|
||||
* @return array|null ['merchant' => array, 'user_ids' => int[]]
|
||||
*/
|
||||
protected function currentMerchantTeam()
|
||||
{
|
||||
$uid = $this->uid();
|
||||
if ($uid <= 0) {
|
||||
return null;
|
||||
}
|
||||
$merchant = Db::name('merchant')->where('user_id', $uid)->find();
|
||||
if (!$merchant) {
|
||||
$bind = Db::name('merchant_member')->where('user_id', $uid)->find();
|
||||
if ($bind) {
|
||||
$merchant = Db::name('merchant')->where('id', (int)$bind['merchant_id'])->find();
|
||||
}
|
||||
}
|
||||
if (!$merchant || $merchant['verify_status'] !== 'pass') {
|
||||
return null;
|
||||
}
|
||||
$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 [
|
||||
'merchant' => $merchant,
|
||||
'user_ids' => $ids,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单状态流转:校验 → 更新 → 写流水
|
||||
*/
|
||||
|
||||
@@ -15,9 +15,9 @@ class Demo extends Api
|
||||
//如果接口已经设置无需登录,那也就无需鉴权了
|
||||
//
|
||||
// 无需登录的接口,*表示全部
|
||||
protected $noNeedLogin = ['test', 'test1'];
|
||||
protected $noNeedLogin = [];
|
||||
// 无需鉴权的接口,*表示全部
|
||||
protected $noNeedRight = ['test2'];
|
||||
protected $noNeedRight = [];
|
||||
|
||||
/**
|
||||
* 测试方法
|
||||
@@ -40,7 +40,7 @@ class Demo extends Api
|
||||
*/
|
||||
public function test()
|
||||
{
|
||||
$this->success('返回成功', $this->request->param());
|
||||
$this->error('无权限访问测试接口', null, 403);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,7 +49,7 @@ class Demo extends Api
|
||||
*/
|
||||
public function test1()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test1']);
|
||||
$this->error('无权限访问测试接口', null, 403);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,7 +58,7 @@ class Demo extends Api
|
||||
*/
|
||||
public function test2()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test2']);
|
||||
$this->error('无权限访问测试接口', null, 403);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ class Demo extends Api
|
||||
*/
|
||||
public function test3()
|
||||
{
|
||||
$this->success('返回成功', ['action' => 'test3']);
|
||||
$this->error('无权限访问测试接口', null, 403);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use think\Db;
|
||||
*/
|
||||
class Home extends Base
|
||||
{
|
||||
protected $noNeedLogin = ['index', 'feed', 'cats', 'services', 'detail', 'calc', 'specs', 'masterCount', 'orderCase'];
|
||||
protected $noNeedLogin = ['index', 'feed', 'cats', 'services', 'detail', 'calc', 'specs', 'masterCount', 'orderCase', 'agreement', 'systemGallery'];
|
||||
|
||||
/**
|
||||
* 首页聚合:类目 + 热门服务 + 我的角标
|
||||
@@ -161,6 +161,7 @@ class Home extends Base
|
||||
'banners' => $this->banners(),
|
||||
'assures' => $this->configList('home_assure', '平台认证,先行赔付,极速上门,售后无忧'),
|
||||
'tutorial' => $this->tutorial(),
|
||||
'service_phone' => (string)(\think\Config::get('site.service_phone') ?: ''),
|
||||
'badge' => ['coupon' => 0, 'unread' => 0],
|
||||
];
|
||||
|
||||
@@ -566,9 +567,6 @@ class Home extends Base
|
||||
{
|
||||
$catId = (int)$this->request->param('cat_id');
|
||||
$keyword = trim((string)$this->request->param('keyword'));
|
||||
if ($catId <= 0 && $keyword === '') {
|
||||
$this->error('请选择服务类目');
|
||||
}
|
||||
$query = Db::name('service')->where('status', 'normal');
|
||||
if ($catId > 0) {
|
||||
$query->where('cat_id', $catId);
|
||||
@@ -605,10 +603,12 @@ class Home extends Base
|
||||
->select();
|
||||
$ret = [];
|
||||
foreach ($specs as $sp) {
|
||||
$spImg = isset($sp['image']) ? trim((string)$sp['image']) : '';
|
||||
$ret[] = [
|
||||
'id' => (int)$sp['id'],
|
||||
'service_id' => (int)$sp['service_id'],
|
||||
'name' => $sp['name'],
|
||||
'image_url' => $spImg !== '' ? (strpos($spImg, 'http') === 0 ? $spImg : cdnurl($spImg, true)) : '',
|
||||
'description' => (string)$sp['description'],
|
||||
'base_price' => (float)$sp['base_price'],
|
||||
];
|
||||
@@ -639,22 +639,30 @@ class Home extends Base
|
||||
$province = trim((string)$this->request->param('province', ''));
|
||||
$city = trim((string)$this->request->param('city', ''));
|
||||
$district = trim((string)$this->request->param('district', ''));
|
||||
$catId = (int)$this->request->param('cat_id', 0);
|
||||
|
||||
$query = Db::name('master')->where('verify_status', 'pass');
|
||||
$platformCount = (int)Db::name('master')->where('verify_status', 'pass')->count();
|
||||
$total = $platformCount;
|
||||
$matchedLocal = false;
|
||||
|
||||
if ($city !== '') {
|
||||
$query->where('city', 'like', '%' . $city . '%');
|
||||
$shortCity = mb_strlen($city, 'UTF-8') > 2 ? preg_replace('/市$/u', '', $city) : $city;
|
||||
$cityCount = (int)Db::name('master')
|
||||
->where('verify_status', 'pass')
|
||||
->where('city', 'like', '%' . $shortCity . '%')
|
||||
->count();
|
||||
if ($cityCount > 0) {
|
||||
$total = $cityCount;
|
||||
$matchedLocal = true;
|
||||
}
|
||||
}
|
||||
$realCount = $query->count();
|
||||
// 商业化基础运力感知(基于城市区县散列生成可信且稳定的运力规模,例如 150~1800+)
|
||||
$hashSeed = sprintf('%u', crc32($province . $city . $district));
|
||||
$virtualBase = 150 + ($hashSeed % 1700);
|
||||
$total = max($realCount, $virtualBase);
|
||||
|
||||
$this->success('', [
|
||||
'count' => (int)$total,
|
||||
'region' => trim($city . ' ' . $district),
|
||||
'tip' => "该地区可提供服务的师傅共 {$total} 位",
|
||||
'count' => (int)$total,
|
||||
'platform_count' => $platformCount,
|
||||
'region' => trim($city . ' ' . $district),
|
||||
'tip' => $matchedLocal
|
||||
? "该地区可提供服务的师傅共 {$total} 位"
|
||||
: "平台认证可服务师傅共 {$total} 位",
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -665,7 +673,7 @@ class Home extends Base
|
||||
public function calc()
|
||||
{
|
||||
$serviceId = (int)$this->request->param('service_id');
|
||||
$goodsListRaw = $this->request->param('goods_list', '', null);
|
||||
$goodsListRaw = $this->request->param('goods_list', $this->request->param('items', '', null), null);
|
||||
$goodsList = [];
|
||||
if (!empty($goodsListRaw)) {
|
||||
if (is_string($goodsListRaw)) {
|
||||
@@ -697,14 +705,24 @@ class Home extends Base
|
||||
$service = Db::name('service')->where('id', $serviceId)->where('status', 'normal')->find();
|
||||
}
|
||||
|
||||
$result = self::calcAmount($service, [
|
||||
'floor' => (int)$this->request->param('floor', 1),
|
||||
'has_elevator' => (int)$this->request->param('has_elevator', 1),
|
||||
'urgent' => (int)$this->request->param('urgent', 0),
|
||||
], (int)$this->request->param('coupon_id', 0), $this->uid(),
|
||||
(string)$this->request->param('price_type', ''),
|
||||
(float)$this->request->param('user_price', 0),
|
||||
$goodsList);
|
||||
try {
|
||||
$result = self::calcAmount($service, [
|
||||
'floor' => (int)$this->request->param('floor', 1),
|
||||
'has_elevator' => (int)$this->request->param('has_elevator', 1),
|
||||
'urgent' => (int)$this->request->param('urgent', 0),
|
||||
'goods_num' => max(1, (int)$this->request->param('goods_num', $this->request->param('num', 1))),
|
||||
'spec_id' => (int)$this->request->param('spec_id', 0),
|
||||
'service_type' => trim((string)$this->request->param('service_type', '')),
|
||||
'is_custom' => (int)$this->request->param('is_custom', 0),
|
||||
'base_price' => $this->request->param('base_price', null),
|
||||
], (int)$this->request->param('coupon_id', 0), $this->uid(),
|
||||
(string)$this->request->param('price_type', ''),
|
||||
(float)$this->request->param('user_price', 0),
|
||||
$goodsList);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
$this->success('', $result);
|
||||
}
|
||||
@@ -713,7 +731,7 @@ class Home extends Base
|
||||
* 金额计算(下单与试算共用,保证前后端口径一致)
|
||||
*
|
||||
* @param array $service 服务行(可选)
|
||||
* @param array $params floor / has_elevator / urgent
|
||||
* @param array $params floor / has_elevator / urgent / goods_num / spec_id / service_type / is_custom / strict
|
||||
* @param int $couponId 用户优惠券 id
|
||||
* @param int $uid 用户 id
|
||||
* @param string $priceType 计价方式:fixed 一口价 / quote 师傅报价 / custom 用户自主定价
|
||||
@@ -724,8 +742,10 @@ class Home extends Base
|
||||
{
|
||||
$allowTypes = ['fixed', 'quote', 'range', 'custom'];
|
||||
$type = in_array($priceType, $allowTypes, true) ? $priceType : ($service ? $service['price_type'] : 'fixed');
|
||||
$isFixedLike = in_array($type, ['fixed', 'range'], true);
|
||||
$isStrict = !empty($params['strict']);
|
||||
|
||||
// 支持多商品组合与增项计算(后端结合 fa_service 与 fa_service_spec 权威核验单价)
|
||||
// 支持多商品组合与增项计算(一口价模式下强制从 fa_service 与 fa_service_spec 读取真实价格,严禁采信前端传入 base_price)
|
||||
$base = 0;
|
||||
if (!empty($goodsList) && is_array($goodsList)) {
|
||||
$serviceCache = [];
|
||||
@@ -744,31 +764,50 @@ class Home extends Base
|
||||
$sid = isset($g['service_id']) ? (int)$g['service_id'] : 0;
|
||||
$specId = isset($g['spec_id']) ? (int)$g['spec_id'] : 0;
|
||||
$reqServiceType = isset($g['service_type']) ? trim((string)$g['service_type']) : '';
|
||||
$isCustom = !empty($g['is_custom']) || (isset($g['cat_id']) && (int)$g['cat_id'] === 15);
|
||||
$isCustom = !empty($g['is_custom']);
|
||||
|
||||
$dbSvc = null;
|
||||
if ($sid > 0 && !$isCustom) {
|
||||
if ($sid > 0) {
|
||||
if (!array_key_exists($sid, $serviceCache)) {
|
||||
$serviceCache[$sid] = Db::name('service')->where('id', $sid)->find() ?: null;
|
||||
$serviceCache[$sid] = Db::name('service')->where('id', $sid)->where('status', 'normal')->find() ?: null;
|
||||
}
|
||||
$dbSvc = $serviceCache[$sid];
|
||||
}
|
||||
|
||||
$specPrice = 0.0;
|
||||
if ($specId > 0 && !$isCustom) {
|
||||
if (!array_key_exists($specId, $specCache)) {
|
||||
$spQuery = Db::name('service_spec')->where('id', $specId);
|
||||
if ($sid > 0) {
|
||||
$spQuery->where('service_id', $sid);
|
||||
if ($isFixedLike) {
|
||||
// 纯空占位行(下单页首次加载尚未选服务时试算)容忍返回 0;只要带了 is_custom、自填名称、自定义价格或处于创单严格校验则强制拦截
|
||||
$hasCustomInput = $isCustom
|
||||
|| !empty($g['custom_name'])
|
||||
|| !empty($g['item_name'])
|
||||
|| !empty($g['goods_name'])
|
||||
|| (isset($g['base_price']) && (float)$g['base_price'] > 0);
|
||||
if ($sid <= 0) {
|
||||
if ($hasCustomInput || $isStrict) {
|
||||
throw new \InvalidArgumentException('自定义商品不支持一口价模式,请关联有效服务项目或切换为师傅报价/自主定价');
|
||||
}
|
||||
$specCache[$specId] = $spQuery->find() ?: null;
|
||||
continue;
|
||||
}
|
||||
if (!empty($specCache[$specId])) {
|
||||
$specPrice = (float)$specCache[$specId]['base_price'];
|
||||
if (!$dbSvc || $dbSvc['price_type'] === 'quote') {
|
||||
throw new \InvalidArgumentException('所选服务项目不存在或不支持一口价模式,请切换为师傅报价/自主定价');
|
||||
}
|
||||
}
|
||||
|
||||
$itemPrice = isset($g['base_price']) ? max(0, (float)$g['base_price']) : 0.0;
|
||||
$specPrice = 0.0;
|
||||
if ($specId > 0 && $dbSvc) {
|
||||
$cacheKey = $sid . ':' . $specId;
|
||||
if (!array_key_exists($cacheKey, $specCache)) {
|
||||
$specCache[$cacheKey] = Db::name('service_spec')
|
||||
->where('id', $specId)
|
||||
->where('service_id', $sid)
|
||||
->where('status', 'normal')
|
||||
->find() ?: null;
|
||||
}
|
||||
if (!empty($specCache[$cacheKey])) {
|
||||
$specPrice = (float)$specCache[$cacheKey]['base_price'];
|
||||
}
|
||||
}
|
||||
|
||||
$itemPrice = 0.0;
|
||||
$dbAddonsMap = [];
|
||||
|
||||
if ($dbSvc) {
|
||||
@@ -784,7 +823,7 @@ class Home extends Base
|
||||
}
|
||||
}
|
||||
|
||||
// 方案2计价优先级:若规格设置了专属一口价(>0)且当前为默认主服务,则优先采用规格专属价;否则采用所选主服务定价或商品默认价
|
||||
// 若规格设置了专属一口价(>0)且当前为默认主服务,则优先采用规格专属价;否则采用所选主服务定价或商品默认价
|
||||
$isDefaultService = ($reqServiceType === '' || $reqServiceType === $defaultCode || $reqServiceType === 'install');
|
||||
if ($specPrice > 0 && $isDefaultService) {
|
||||
$itemPrice = $specPrice;
|
||||
@@ -807,28 +846,76 @@ class Home extends Base
|
||||
}
|
||||
}
|
||||
|
||||
$itemNum = isset($g['num']) ? max(1, (int)$g['num']) : 1;
|
||||
$base += $itemPrice * $itemNum;
|
||||
if ($isFixedLike && $itemPrice <= 0) {
|
||||
throw new \InvalidArgumentException('所选服务规格暂未配置一口价,请切换为师傅报价或自主定价');
|
||||
}
|
||||
|
||||
$itemNum = isset($g['num']) ? max(1, (int)$g['num']) : (isset($g['goods_num']) ? max(1, (int)$g['goods_num']) : 1);
|
||||
$base += round($itemPrice * $itemNum, 2);
|
||||
if (!empty($g['extras']) && is_array($g['extras'])) {
|
||||
foreach ($g['extras'] as $ex) {
|
||||
$exNum = isset($ex['num']) ? max(0, (int)$ex['num']) : 0;
|
||||
if ($exNum <= 0) {
|
||||
continue;
|
||||
}
|
||||
$exPrice = isset($ex['price']) ? max(0, (float)$ex['price']) : 0.0;
|
||||
$exPrice = 0.0;
|
||||
$exName = isset($ex['name']) ? (string)$ex['name'] : '';
|
||||
$exCode = isset($ex['code']) ? (string)$ex['code'] : '';
|
||||
if ($exName !== '' && isset($dbAddonsMap[$exName])) {
|
||||
$exPrice = $dbAddonsMap[$exName];
|
||||
} elseif ($exCode !== '' && isset($dbAddonsMap[$exCode])) {
|
||||
$exPrice = $dbAddonsMap[$exCode];
|
||||
} elseif (!$isFixedLike && isset($ex['price'])) {
|
||||
$exPrice = max(0, (float)$ex['price']);
|
||||
}
|
||||
$base += $exPrice * $exNum;
|
||||
$base += round($exPrice * $exNum, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($service) {
|
||||
$base = (float)$service['base_price'];
|
||||
if ($isFixedLike && $service['price_type'] === 'quote') {
|
||||
throw new \InvalidArgumentException('该服务项目不支持一口价模式,请切换为师傅报价或自主定价');
|
||||
}
|
||||
$unitPrice = (float)$service['base_price'];
|
||||
$specId = isset($params['spec_id']) ? (int)$params['spec_id'] : 0;
|
||||
$reqServiceType = isset($params['service_type']) ? trim((string)$params['service_type']) : '';
|
||||
$specPrice = 0.0;
|
||||
if ($specId > 0) {
|
||||
$spRow = Db::name('service_spec')
|
||||
->where('id', $specId)
|
||||
->where('service_id', (int)$service['id'])
|
||||
->where('status', 'normal')
|
||||
->find();
|
||||
if ($spRow && (float)$spRow['base_price'] > 0) {
|
||||
$specPrice = (float)$spRow['base_price'];
|
||||
}
|
||||
}
|
||||
$svcTypes = !empty($service['service_types']) ? (json_decode((string)$service['service_types'], true) ?: []) : [];
|
||||
$defaultCode = 'install';
|
||||
$matchedTypePrice = 0.0;
|
||||
foreach ($svcTypes as $st) {
|
||||
if (!empty($st['is_default']) && !empty($st['code'])) {
|
||||
$defaultCode = (string)$st['code'];
|
||||
}
|
||||
if ($reqServiceType !== '' && isset($st['code']) && (string)$st['code'] === $reqServiceType) {
|
||||
$matchedTypePrice = isset($st['price']) ? (float)$st['price'] : 0.0;
|
||||
}
|
||||
}
|
||||
$isDefaultService = ($reqServiceType === '' || $reqServiceType === $defaultCode || $reqServiceType === 'install');
|
||||
if ($specPrice > 0 && $isDefaultService) {
|
||||
$unitPrice = $specPrice;
|
||||
} elseif ($matchedTypePrice > 0) {
|
||||
$unitPrice = $matchedTypePrice;
|
||||
} elseif ($specPrice > 0) {
|
||||
$unitPrice = $specPrice;
|
||||
}
|
||||
if ($isFixedLike && $unitPrice <= 0) {
|
||||
throw new \InvalidArgumentException('该服务项目暂未配置有效一口价,请切换为师傅报价或自主定价');
|
||||
}
|
||||
$goodsNum = isset($params['goods_num']) ? max(1, (int)$params['goods_num']) : 1;
|
||||
$base = round($unitPrice * $goodsNum, 2);
|
||||
} elseif ($isFixedLike && (!empty($params['is_custom']) || $isStrict || (isset($params['base_price']) && (float)$params['base_price'] > 0))) {
|
||||
throw new \InvalidArgumentException('自定义商品不支持一口价模式,请关联有效服务项目或切换为师傅报价/自主定价');
|
||||
}
|
||||
|
||||
$floorAmount = 0;
|
||||
@@ -1009,10 +1096,12 @@ class Home extends Base
|
||||
if (!isset($specsMap[$sid])) {
|
||||
$specsMap[$sid] = [];
|
||||
}
|
||||
$spImg = isset($sp['image']) ? trim((string)$sp['image']) : '';
|
||||
$specsMap[$sid][] = [
|
||||
'id' => (int)$sp['id'],
|
||||
'service_id' => $sid,
|
||||
'name' => $sp['name'],
|
||||
'image_url' => $spImg !== '' ? (strpos($spImg, 'http') === 0 ? $spImg : cdnurl($spImg, true)) : '',
|
||||
'description' => (string)$sp['description'],
|
||||
'base_price' => (float)$sp['base_price'],
|
||||
];
|
||||
@@ -1255,4 +1344,168 @@ class Home extends Base
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台协议与合规条款读取(支持后台站点配置覆盖)
|
||||
*/
|
||||
public function agreement()
|
||||
{
|
||||
$type = trim((string)$this->request->param('type', 'user_service'));
|
||||
$map = [
|
||||
'service' => [
|
||||
'key' => 'agreement_service',
|
||||
'title' => '优匠优服用户服务协议',
|
||||
'content' => "1. 平台定位:优匠优服为客户与认证技能师傅提供上门安装、维修、拆旧及维保信息撮合与交易担保服务。\n\n2. 计费准则:一口价项目按公示标准计费;报价订单由师傅根据现场工况公开报价,经客户确认支付后生效。\n\n3. 验收与结算:订单资金由平台托管,师傅完工拍照并经客户确认(或提供6位核销码)后完成结算。\n\n4. 交易规范:为保障售后质保与资金安全,请勿脱离平台私下转账交易。",
|
||||
],
|
||||
'user_service' => [
|
||||
'key' => 'agreement_service',
|
||||
'title' => '优匠优服用户服务协议',
|
||||
'content' => "1. 平台定位:优匠优服为客户与认证技能师傅提供上门安装、维修、拆旧及维保信息撮合与交易担保服务。\n\n2. 计费准则:一口价项目按公示标准计费;报价订单由师傅根据现场工况公开报价,经客户确认支付后生效。\n\n3. 验收与结算:订单资金由平台托管,师傅完工拍照并经客户确认(或提供6位核销码)后完成结算。\n\n4. 交易规范:为保障售后质保与资金安全,请勿脱离平台私下转账交易。",
|
||||
],
|
||||
'privacy' => [
|
||||
'key' => 'agreement_privacy',
|
||||
'title' => '优匠优服隐私保护指引',
|
||||
'content' => "1. 信息收集范围:仅在您授权登录、预约上门地址及联系师傅等必要场景下收集手机号码与服务地址信息。\n\n2. 最小必要原则:客户真实联系信息仅对已接单履约的当单认证师傅可见,未接单前全程脱敏展示。\n\n3. 数据传输安全:全链路采用加密通信并严格管控访问权限,绝不向无关第三方共享或出售个人信息。\n\n4. 用户权利:您可随时在个人中心查询、修改账户资料及管理服务地址。",
|
||||
],
|
||||
'master' => [
|
||||
'key' => 'agreement_master',
|
||||
'title' => '师傅入驻与履约服务协议',
|
||||
'content' => "1. 实名与技能核验:入驻师傅须提交本人真实有效身份证件及服务技能信息,严禁冒用他人资质。\n\n2. 履约时效:接单后须在平台规定时效内联系客户确认上门时间,准时到达现场并规范施工。\n\n3. 收费透明:如需现场增项补差,必须提前向客户说明并在工单内发起补差申请,严禁私自索要额外费用。\n\n4. 完工留痕与结算:施工完成后须上传真实清晰的现场完工照片,凭客户验收或核销码触发系统结算。",
|
||||
],
|
||||
'merchant' => [
|
||||
'key' => 'agreement_merchant',
|
||||
'title' => '企业商家入驻与协同服务协议',
|
||||
'content' => "1. 资质真实性:申请入驻的企业或商户须提供真实有效的营业执照与主体信息。\n\n2. 成员协同管理:主账号可绑定企业协同成员共同发起批量工单与跟进履约,并对企业关联工单承担管理责任。\n\n3. 对账与开票:平台按实际支付与完工订单汇总企业月度账单,支持在线申请正规增值税普通发票或专用发票。",
|
||||
],
|
||||
];
|
||||
|
||||
$item = isset($map[$type]) ? $map[$type] : $map['user_service'];
|
||||
$cfgVal = \think\Config::get('site.' . $item['key']);
|
||||
$content = (is_string($cfgVal) && trim($cfgVal) !== '') ? trim($cfgVal) : $item['content'];
|
||||
|
||||
$this->success('', [
|
||||
'type' => $type,
|
||||
'title' => $item['title'],
|
||||
'content' => $content,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 官方系统图库(二级商品分类侧边栏 + 款式参考图片双列网格)
|
||||
*/
|
||||
public function systemGallery()
|
||||
{
|
||||
$catId = (int)$this->request->param('cat_id', 0);
|
||||
$serviceId = (int)$this->request->param('service_id', 0);
|
||||
$keyword = trim((string)$this->request->param('keyword', ''));
|
||||
|
||||
$rawCats = Db::name('service_cat')
|
||||
->field('id,name,icon')
|
||||
->where('status', 'normal')
|
||||
->where('id', '<>', 15)
|
||||
->order('weigh desc,id asc')
|
||||
->select();
|
||||
$cats = $this->formatCats($rawCats);
|
||||
|
||||
if ($serviceId > 0 && $catId <= 0) {
|
||||
$foundCatId = (int)Db::name('service')->where('id', $serviceId)->value('cat_id');
|
||||
if ($foundCatId > 0 && $foundCatId !== 15) {
|
||||
$catId = $foundCatId;
|
||||
}
|
||||
}
|
||||
if ($catId === 0 && !empty($cats)) {
|
||||
$catId = (int)$cats[0]['id'];
|
||||
}
|
||||
|
||||
$svcQuery = Db::name('service')
|
||||
->field('id,cat_id,name,image')
|
||||
->where('status', 'normal')
|
||||
->where('cat_id', '<>', 15);
|
||||
if ($catId > 0) {
|
||||
$svcQuery->where('cat_id', $catId);
|
||||
}
|
||||
$svcRows = $svcQuery->order('weigh desc,sales desc,id asc')->select();
|
||||
$svcIds = array_column($svcRows, 'id');
|
||||
|
||||
$galleryMap = [];
|
||||
if (!empty($svcIds)) {
|
||||
$gQuery = Db::name('system_gallery')
|
||||
->where('service_id', 'in', $svcIds)
|
||||
->where('status', 'normal');
|
||||
if ($keyword !== '') {
|
||||
$gQuery->where('title', 'like', '%' . $keyword . '%');
|
||||
}
|
||||
$gRows = $gQuery->order('weigh desc,id asc')->select();
|
||||
foreach ($gRows as $gr) {
|
||||
$sid = (int)$gr['service_id'];
|
||||
$rawImg = trim((string)$gr['image']);
|
||||
if ($rawImg === '') {
|
||||
continue;
|
||||
}
|
||||
$galleryMap[$sid][] = [
|
||||
'id' => (int)$gr['id'],
|
||||
'service_id' => $sid,
|
||||
'spec_id' => (int)$gr['spec_id'],
|
||||
'title' => (string)$gr['title'],
|
||||
'image_url' => strpos($rawImg, 'http') === 0 ? $rawImg : cdnurl($rawImg, true),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 若个别服务尚未写入 fa_system_gallery,回退读取真实规格与服务主图
|
||||
$services = [];
|
||||
foreach ($svcRows as $sr) {
|
||||
$sid = (int)$sr['id'];
|
||||
$items = isset($galleryMap[$sid]) ? $galleryMap[$sid] : [];
|
||||
if (empty($items) && $keyword === '') {
|
||||
$defaultImg = trim((string)$sr['image']);
|
||||
if ($defaultImg !== '') {
|
||||
$fullDef = strpos($defaultImg, 'http') === 0 ? $defaultImg : cdnurl($defaultImg, true);
|
||||
$spList = Db::name('service_spec')
|
||||
->where('service_id', $sid)
|
||||
->where('status', 'normal')
|
||||
->order('weigh desc,id asc')
|
||||
->select();
|
||||
if (!empty($spList)) {
|
||||
foreach ($spList as $sp) {
|
||||
$spImg = !empty($sp['image']) ? trim((string)$sp['image']) : '';
|
||||
$items[] = [
|
||||
'id' => 100000 + (int)$sp['id'],
|
||||
'service_id' => $sid,
|
||||
'spec_id' => (int)$sp['id'],
|
||||
'title' => (string)$sp['name'],
|
||||
'image_url' => $spImg !== '' ? (strpos($spImg, 'http') === 0 ? $spImg : cdnurl($spImg, true)) : $fullDef,
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$items[] = [
|
||||
'id' => 200000 + $sid,
|
||||
'service_id' => $sid,
|
||||
'spec_id' => 0,
|
||||
'title' => (string)$sr['name'],
|
||||
'image_url' => $fullDef,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($keyword !== '' && empty($items) && mb_strpos((string)$sr['name'], $keyword) === false) {
|
||||
continue;
|
||||
}
|
||||
if (!empty($items)) {
|
||||
$services[] = [
|
||||
'id' => $sid,
|
||||
'cat_id' => (int)$sr['cat_id'],
|
||||
'name' => (string)$sr['name'],
|
||||
'count' => count($items),
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('', [
|
||||
'cats' => $cats,
|
||||
'current_cat_id' => $catId,
|
||||
'services' => $services,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\InvoiceService;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* 发票中心(个人业主与企业商家通用):
|
||||
* 额度汇总 / 按单勾选开票 / 发票抬头管理 / 开票记录与详情 / 撤回申请
|
||||
*/
|
||||
class Invoice extends Base
|
||||
{
|
||||
/**
|
||||
* 开票首页概览(可开票额度、默认抬头、未开票订单数、记录统计)
|
||||
*/
|
||||
public function summary()
|
||||
{
|
||||
$quota = InvoiceService::getQuotaSummary($this->uid());
|
||||
$titles = InvoiceService::getTitleList($this->uid());
|
||||
$this->success('', [
|
||||
'quota' => $quota,
|
||||
'titles' => $titles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可开票已完成订单列表(支持分页或传入 order_ids 核对已勾选订单)
|
||||
*/
|
||||
public function orders()
|
||||
{
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(100, max(1, (int)$this->request->param('limit', 20)));
|
||||
$rawIds = $this->request->param('order_ids/a', []);
|
||||
$filterIds = InvoiceService::parseIds($rawIds);
|
||||
$res = InvoiceService::getInvoiceableOrders($this->uid(), $page, $limit, $filterIds);
|
||||
$this->success('', $res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交开票申请(支持按勾选订单合并开票或按可用额度开票,严格校验专票要素)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$params = [
|
||||
'order_ids' => $this->request->post('order_ids/a', []),
|
||||
'amount' => $this->request->post('amount', 0),
|
||||
'title_id' => $this->request->post('title_id', 0),
|
||||
'type' => $this->request->post('type', 'normal'),
|
||||
'title_type' => $this->request->post('title_type', ''),
|
||||
'title' => $this->request->post('title', ''),
|
||||
'tax_no' => $this->request->post('tax_no', ''),
|
||||
'bank_name' => $this->request->post('bank_name', ''),
|
||||
'bank_account' => $this->request->post('bank_account', ''),
|
||||
'reg_address' => $this->request->post('reg_address', ''),
|
||||
'reg_mobile' => $this->request->post('reg_mobile', ''),
|
||||
'email' => $this->request->post('email', ''),
|
||||
'receive_mobile' => $this->request->post('receive_mobile', ''),
|
||||
'remark' => $this->request->post('remark', ''),
|
||||
'save_title' => $this->request->post('save_title', 0),
|
||||
];
|
||||
|
||||
try {
|
||||
$result = InvoiceService::submitInvoice($this->uid(), $params, true);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[Invoice::apply] ' . $e->getMessage());
|
||||
$this->error('提交开票申请失败,请稍后重试');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->success('开票申请已提交,预计 1-3 个工作日内开具', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开票记录分页列表
|
||||
*/
|
||||
public function records()
|
||||
{
|
||||
$status = trim((string)$this->request->param('status', 'all'));
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(50, max(1, (int)$this->request->param('limit', 20)));
|
||||
|
||||
$res = InvoiceService::getRecordList($this->uid(), $status, $page, $limit);
|
||||
$quota = InvoiceService::getQuotaSummary($this->uid());
|
||||
$res['quota'] = [
|
||||
'available' => $quota['available'],
|
||||
'paid_amount' => $quota['paid_amount'],
|
||||
'used_amount' => $quota['used_amount'],
|
||||
'issued_amount' => $quota['issued_amount'],
|
||||
'wait_count' => $quota['wait_count'],
|
||||
'record_count' => $quota['record_count'],
|
||||
];
|
||||
$this->success('', $res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开票申请详情(含完整票面信息、电子发票附件、驳回说明、关联订单明细)
|
||||
*/
|
||||
public function detail()
|
||||
{
|
||||
$id = (int)$this->request->param('id', 0);
|
||||
try {
|
||||
$detail = InvoiceService::getInvoiceDetail($this->uid(), $id);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('', ['detail' => $detail]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 撤回待处理(wait)开票申请
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function cancel()
|
||||
{
|
||||
$id = (int)$this->request->post('id', 0);
|
||||
try {
|
||||
InvoiceService::cancelInvoice($this->uid(), $id);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[Invoice::cancel] ' . $e->getMessage());
|
||||
$this->error('撤回失败,请刷新后重试');
|
||||
return;
|
||||
}
|
||||
$this->success('已撤回开票申请,可开票额度已释放');
|
||||
}
|
||||
|
||||
/**
|
||||
* 发票抬头列表
|
||||
*/
|
||||
public function titles()
|
||||
{
|
||||
$list = InvoiceService::getTitleList($this->uid());
|
||||
$this->success('', ['list' => $list]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发票抬头详情
|
||||
*/
|
||||
public function titleDetail()
|
||||
{
|
||||
$id = (int)$this->request->param('id', 0);
|
||||
try {
|
||||
$detail = InvoiceService::getOwnTitle($this->uid(), $id);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('', ['detail' => $detail]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增 / 编辑发票抬头
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function titleSave()
|
||||
{
|
||||
$input = [
|
||||
'id' => (int)$this->request->post('id', 0),
|
||||
'title_type' => $this->request->post('title_type', 'personal'),
|
||||
'default_invoice_type' => $this->request->post('default_invoice_type', 'normal'),
|
||||
'title' => $this->request->post('title', ''),
|
||||
'tax_no' => $this->request->post('tax_no', ''),
|
||||
'bank_name' => $this->request->post('bank_name', ''),
|
||||
'bank_account' => $this->request->post('bank_account', ''),
|
||||
'reg_address' => $this->request->post('reg_address', ''),
|
||||
'reg_mobile' => $this->request->post('reg_mobile', ''),
|
||||
'email' => $this->request->post('email', ''),
|
||||
'receive_mobile' => $this->request->post('receive_mobile', ''),
|
||||
'is_default' => (int)$this->request->post('is_default', 0),
|
||||
];
|
||||
|
||||
try {
|
||||
$saved = InvoiceService::saveTitle($this->uid(), $input);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[Invoice::titleSave] ' . $e->getMessage());
|
||||
$this->error('保存失败,请稍后重试');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->success('保存成功', ['detail' => $saved, 'id' => (int)$saved['id']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设为默认发票抬头
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function titleDefault()
|
||||
{
|
||||
$id = (int)$this->request->post('id', 0);
|
||||
try {
|
||||
InvoiceService::setDefaultTitle($this->uid(), $id);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[Invoice::titleDefault] ' . $e->getMessage());
|
||||
$this->error('设置失败,请稍后重试');
|
||||
return;
|
||||
}
|
||||
$this->success('已设为默认抬头');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除发票抬头
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function titleDelete()
|
||||
{
|
||||
$id = (int)$this->request->post('id', 0);
|
||||
try {
|
||||
InvoiceService::deleteTitle($this->uid(), $id);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[Invoice::titleDelete] ' . $e->getMessage());
|
||||
$this->error('删除失败,请稍后重试');
|
||||
return;
|
||||
}
|
||||
$this->success('已删除');
|
||||
}
|
||||
}
|
||||
文件差异内容过多而无法显示
加载差异
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\InvoiceService;
|
||||
use app\common\library\OrderStatus;
|
||||
use think\Db;
|
||||
|
||||
@@ -85,35 +86,44 @@ class Merchant extends Base
|
||||
|
||||
$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');
|
||||
$orderCount = (int)Db::name('order')->where('user_id', 'in', $uids)->where('pay_status', 1)->where('status', '<>', OrderStatus::CANCELLED)->count();
|
||||
$quota = InvoiceService::getQuotaSummary($this->uid());
|
||||
$totalAmount = (float)$quota['paid_amount'];
|
||||
$invoiced = (float)$quota['used_amount'];
|
||||
|
||||
$this->success('', [
|
||||
'applied' => true,
|
||||
'is_owner' => $isOwner,
|
||||
'role' => $isOwner ? 'owner' : 'member',
|
||||
'merchant' => [
|
||||
'id' => (int)$merchant['id'],
|
||||
'name' => $merchant['name'],
|
||||
'license_no' => $merchant['license_no'],
|
||||
'legal_person' => $merchant['legal_person'],
|
||||
'contact' => $merchant['contact'],
|
||||
'mobile' => $merchant['mobile'],
|
||||
'address' => $merchant['province'] . $merchant['city'] . $merchant['district'] . $merchant['address'],
|
||||
'invoice_title' => $merchant['invoice_title'],
|
||||
'invoice_tax_no' => $merchant['invoice_tax_no'],
|
||||
'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),
|
||||
'invoiceable' => round(max(0, $totalAmount - $invoiced), 2),
|
||||
'id' => (int)$merchant['id'],
|
||||
'name' => $merchant['name'],
|
||||
'license_no' => $merchant['license_no'],
|
||||
'legal_person' => $merchant['legal_person'],
|
||||
'contact' => $merchant['contact'],
|
||||
'mobile' => $merchant['mobile'],
|
||||
'province' => (string)$merchant['province'],
|
||||
'city' => (string)$merchant['city'],
|
||||
'district' => (string)$merchant['district'],
|
||||
'detail_address' => (string)$merchant['address'],
|
||||
'address' => $merchant['province'] . $merchant['city'] . $merchant['district'] . $merchant['address'],
|
||||
'invoice_title' => $merchant['invoice_title'],
|
||||
'invoice_tax_no' => $merchant['invoice_tax_no'],
|
||||
'invoice_bank' => $merchant['invoice_bank'],
|
||||
'invoice_account' => $merchant['invoice_account'],
|
||||
'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),
|
||||
'issued_amount' => round((float)$quota['issued_amount'], 2),
|
||||
'wait_invoice_amount' => round((float)$quota['wait_amount'], 2),
|
||||
'invoiceable' => round((float)$quota['available'], 2),
|
||||
],
|
||||
]);
|
||||
}
|
||||
@@ -139,6 +149,23 @@ class Merchant extends Base
|
||||
$this->error('联系电话格式不正确');
|
||||
}
|
||||
Db::name('merchant')->where('id', $merchant['id'])->update($data);
|
||||
|
||||
// 若维护了商家开票资料,同步刷新关联的商家默认发票抬头
|
||||
if (isset($data['invoice_title']) || isset($data['invoice_tax_no']) || isset($data['invoice_bank']) || isset($data['invoice_account'])) {
|
||||
$latest = Db::name('merchant')->where('id', $merchant['id'])->find();
|
||||
if ($latest) {
|
||||
$syncFields = ['updatetime' => time()];
|
||||
if (!empty($latest['invoice_title'])) $syncFields['title'] = $latest['invoice_title'];
|
||||
if (!empty($latest['invoice_tax_no'])) $syncFields['tax_no'] = strtoupper($latest['invoice_tax_no']);
|
||||
if (isset($data['invoice_bank'])) $syncFields['bank_name'] = $latest['invoice_bank'];
|
||||
if (isset($data['invoice_account'])) $syncFields['bank_account'] = $latest['invoice_account'];
|
||||
Db::name('invoice_title')
|
||||
->where('merchant_id', (int)$merchant['id'])
|
||||
->where('title_type', 'enterprise')
|
||||
->update($syncFields);
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('已保存');
|
||||
}
|
||||
|
||||
@@ -181,11 +208,22 @@ class Merchant extends Base
|
||||
$uids = $this->merchantUserIds($merchant);
|
||||
$status = $this->request->param('status', 'all');
|
||||
$keyword = trim((string)$this->request->param('keyword', ''));
|
||||
$memberUid = (int)$this->request->param('member_uid', 0);
|
||||
$month = trim((string)$this->request->param('month', ''));
|
||||
$page = max(1, (int)$this->request->param('page', 1));
|
||||
$limit = min(50, max(1, (int)$this->request->param('limit', 20)));
|
||||
|
||||
$build = function () use ($uids, $status, $keyword) {
|
||||
$q = Db::name('order')->where('user_id', 'in', $uids);
|
||||
$targetUids = ($memberUid > 0 && in_array($memberUid, $uids, true)) ? [$memberUid] : $uids;
|
||||
|
||||
$build = function () use ($targetUids, $status, $keyword, $month) {
|
||||
$q = Db::name('order')->where('user_id', 'in', $targetUids);
|
||||
if ($month !== '' && preg_match('/^\d{4}-\d{2}$/', $month)) {
|
||||
$startTs = strtotime($month . '-01 00:00:00');
|
||||
$endTs = strtotime('+1 month', $startTs) - 1;
|
||||
if ($startTs > 0 && $endTs > 0) {
|
||||
$q->where('createtime', 'between', [$startTs, $endTs]);
|
||||
}
|
||||
}
|
||||
if ($keyword !== '') {
|
||||
$q->where(function ($sub) use ($keyword) {
|
||||
$sub->where('contact', 'like', "%{$keyword}%")
|
||||
@@ -211,6 +249,10 @@ class Merchant extends Base
|
||||
$total = $build()->count();
|
||||
$rows = $build()->order('id desc')->page($page, $limit)->select();
|
||||
$userMap = Db::name('user')->where('id', 'in', $uids)->column('id,nickname,mobile', 'id');
|
||||
$serviceIds = array_values(array_unique(array_filter(array_map('intval', array_column($rows, 'service_id')))));
|
||||
$serviceImgMap = $serviceIds ? Db::name('service')->where('id', 'in', $serviceIds)->column('image', 'id') : [];
|
||||
$orderIds = array_values(array_unique(array_filter(array_map('intval', array_column($rows, 'id')))));
|
||||
$batchMap = $orderIds ? Db::name('merchant_order')->where('order_id', 'in', $orderIds)->column('batch_no', 'order_id') : [];
|
||||
|
||||
foreach ($rows as &$row) {
|
||||
$ownerUser = (int)$row['user_id'] === (int)$merchant['user_id'];
|
||||
@@ -227,10 +269,10 @@ class Merchant extends Base
|
||||
$row['account_name'] = $u && $u['nickname'] ? $u['nickname'] : ('用户' . substr($mobile, -4));
|
||||
$row['account_role'] = $ownerUser ? 'owner' : 'member';
|
||||
$row['account_role_text'] = $ownerUser ? '主账号' : '关联账号';
|
||||
$row['batch_no'] = isset($batchMap[(int)$row['id']]) ? (string)$batchMap[(int)$row['id']] : '';
|
||||
$img = !empty($row['service_image']) ? $row['service_image'] : '';
|
||||
if (empty($img) && !empty($row['service_id'])) {
|
||||
$s = Db::name('service')->where('id', (int)$row['service_id'])->find();
|
||||
$img = $s ? $s['image'] : '';
|
||||
if (empty($img) && !empty($row['service_id']) && isset($serviceImgMap[(int)$row['service_id']])) {
|
||||
$img = $serviceImgMap[(int)$row['service_id']];
|
||||
}
|
||||
if ($img && strpos($img, 'http') !== 0) {
|
||||
$img = cdnurl($img, true);
|
||||
@@ -241,171 +283,63 @@ class Merchant extends Base
|
||||
unset($row);
|
||||
|
||||
$this->success('', [
|
||||
'list' => $rows,
|
||||
'total' => (int)$total,
|
||||
'page' => $page,
|
||||
'list' => $rows,
|
||||
'total' => (int)$total,
|
||||
'page' => $page,
|
||||
'has_more' => ($page * $limit) < (int)$total,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开票记录与可开票额度(个人用户与认证商家通用)
|
||||
* 开票记录与可开票额度(个人用户与认证商家通用,兼容旧版调用并复用 InvoiceService)
|
||||
*/
|
||||
public function invoices()
|
||||
{
|
||||
$merchant = $this->merchantInfo();
|
||||
$isPassMerchant = ($merchant && $merchant['verify_status'] === 'pass');
|
||||
|
||||
if ($isPassMerchant) {
|
||||
$uids = $this->merchantUserIds($merchant);
|
||||
$mid = (int)$merchant['id'];
|
||||
$list = Db::name('invoice')
|
||||
->where(function ($q) use ($mid, $uids) {
|
||||
$q->where('merchant_id', $mid)->whereOr('user_id', 'in', $uids);
|
||||
})
|
||||
->order('id desc')
|
||||
->limit(30)
|
||||
->select();
|
||||
$paid = (float)Db::name('order')->where('user_id', 'in', $uids)->where('pay_status', 1)->sum('pay_amount');
|
||||
$used = (float)Db::name('invoice')
|
||||
->where(function ($q) use ($mid, $uids) {
|
||||
$q->where('merchant_id', $mid)->whereOr('user_id', 'in', $uids);
|
||||
})
|
||||
->where('status', 'in', ['wait', 'issued'])
|
||||
->sum('amount');
|
||||
$issued = (float)Db::name('invoice')
|
||||
->where(function ($q) use ($mid, $uids) {
|
||||
$q->where('merchant_id', $mid)->whereOr('user_id', 'in', $uids);
|
||||
})
|
||||
->where('status', 'issued')
|
||||
->sum('amount');
|
||||
} else {
|
||||
$uid = $this->uid();
|
||||
$list = Db::name('invoice')->where('user_id', $uid)->order('id desc')->limit(30)->select();
|
||||
$paid = (float)Db::name('order')->where('user_id', $uid)->where('pay_status', 1)->sum('pay_amount');
|
||||
$used = (float)Db::name('invoice')->where('user_id', $uid)->where('status', 'in', ['wait', 'issued'])->sum('amount');
|
||||
$issued = (float)Db::name('invoice')->where('user_id', $uid)->where('status', 'issued')->sum('amount');
|
||||
}
|
||||
|
||||
$statusText = ['wait' => '开票中', 'issued' => '已开票', 'reject' => '已驳回'];
|
||||
foreach ($list as &$row) {
|
||||
$row['amount'] = (float)$row['amount'];
|
||||
$row['status_text'] = isset($statusText[$row['status']]) ? $statusText[$row['status']] : $row['status'];
|
||||
$row['type_text'] = $row['type'] === 'special' ? '专用发票' : '普通发票';
|
||||
$row['title_type'] = !empty($row['tax_no']) ? 'enterprise' : 'personal';
|
||||
$row['title_type_text'] = !empty($row['tax_no']) ? '企业单位' : '个人/非企业';
|
||||
$row['time_text'] = date('Y-m-d H:i', (int)$row['createtime']);
|
||||
}
|
||||
unset($row);
|
||||
|
||||
$userRow = Db::name('user')->where('id', $this->uid())->field('nickname,mobile')->find();
|
||||
$personalDefaultTitle = ($userRow && !empty($userRow['nickname'])) ? (string)$userRow['nickname'] : '个人';
|
||||
$records = InvoiceService::getRecordList($this->uid(), 'all', 1, 30);
|
||||
$quota = InvoiceService::getQuotaSummary($this->uid());
|
||||
|
||||
$this->success('', [
|
||||
'list' => $list,
|
||||
'quota' => [
|
||||
'available' => round(max(0, $paid - $used), 2),
|
||||
'paid_amount' => round($paid, 2),
|
||||
'used_amount' => round($used, 2),
|
||||
'issued_amount' => round($issued, 2),
|
||||
'is_merchant' => $isPassMerchant,
|
||||
'default_title_type' => $isPassMerchant ? 'enterprise' : 'personal',
|
||||
'default_title' => $isPassMerchant ? ($merchant['invoice_title'] ?: $merchant['name']) : $personalDefaultTitle,
|
||||
'default_tax_no' => $isPassMerchant ? (string)$merchant['invoice_tax_no'] : '',
|
||||
'enterprise_title' => $merchant ? (string)($merchant['invoice_title'] ?: $merchant['name']) : '',
|
||||
'enterprise_tax_no' => $merchant ? (string)$merchant['invoice_tax_no'] : '',
|
||||
'personal_title' => $personalDefaultTitle,
|
||||
],
|
||||
'list' => $records['list'],
|
||||
'quota' => $quota,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请开票(支持个人用户与认证商家)
|
||||
* 申请开票(向下兼容旧版入口,底层复用 InvoiceService)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function invoice()
|
||||
{
|
||||
$merchant = $this->merchantInfo();
|
||||
$isPassMerchant = ($merchant && $merchant['verify_status'] === 'pass');
|
||||
$params = [
|
||||
'order_ids' => $this->request->post('order_ids', '', null),
|
||||
'amount' => $this->request->post('amount', 0),
|
||||
'title_id' => $this->request->post('title_id', 0),
|
||||
'type' => $this->request->post('type', 'normal'),
|
||||
'title_type' => $this->request->post('title_type', ''),
|
||||
'title' => $this->request->post('title', ''),
|
||||
'tax_no' => $this->request->post('tax_no', ''),
|
||||
'bank_name' => $this->request->post('bank_name', ''),
|
||||
'bank_account' => $this->request->post('bank_account', ''),
|
||||
'reg_address' => $this->request->post('reg_address', ''),
|
||||
'reg_mobile' => $this->request->post('reg_mobile', ''),
|
||||
'email' => $this->request->post('email', ''),
|
||||
'receive_mobile' => $this->request->post('receive_mobile', ''),
|
||||
'remark' => $this->request->post('remark', ''),
|
||||
'save_title' => $this->request->post('save_title', 0),
|
||||
];
|
||||
|
||||
$amount = round((float)$this->request->post('amount'), 2);
|
||||
$type = $this->request->post('type', 'normal');
|
||||
$titleType = trim((string)$this->request->post('title_type', ''));
|
||||
$title = trim((string)$this->request->post('title', ''));
|
||||
$taxNo = strtoupper(trim((string)$this->request->post('tax_no', '')));
|
||||
$email = trim((string)$this->request->post('email', ''));
|
||||
$remark = trim((string)$this->request->post('remark', ''));
|
||||
|
||||
if ($amount <= 0) {
|
||||
$this->error('请填写开票金额');
|
||||
}
|
||||
if (!in_array($type, ['normal', 'special'], true)) {
|
||||
$this->error('发票类型错误');
|
||||
try {
|
||||
$result = InvoiceService::submitInvoice($this->uid(), $params, false);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[Merchant::invoice] ' . $e->getMessage());
|
||||
$this->error('开票申请提交失败,请稍后重试');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!in_array($titleType, ['personal', 'enterprise'], true)) {
|
||||
$titleType = ($isPassMerchant || $taxNo !== '' || $type === 'special') ? 'enterprise' : 'personal';
|
||||
}
|
||||
|
||||
if ($titleType === 'personal') {
|
||||
if ($type === 'special') {
|
||||
$this->error('个人抬头仅支持开具普通发票');
|
||||
}
|
||||
if ($title === '') {
|
||||
$userRow = Db::name('user')->where('id', $this->uid())->field('nickname')->find();
|
||||
$title = ($userRow && !empty($userRow['nickname'])) ? (string)$userRow['nickname'] : '个人';
|
||||
}
|
||||
$taxNo = '';
|
||||
} else {
|
||||
if ($title === '' && $isPassMerchant) {
|
||||
$title = (string)($merchant['invoice_title'] ?: $merchant['name']);
|
||||
}
|
||||
if ($taxNo === '' && $isPassMerchant) {
|
||||
$taxNo = strtoupper(trim((string)$merchant['invoice_tax_no']));
|
||||
}
|
||||
if ($title === '') {
|
||||
$this->error('请填写企业发票抬头');
|
||||
}
|
||||
if ($taxNo === '') {
|
||||
$this->error('请填写纳税人识别号(税号)');
|
||||
}
|
||||
}
|
||||
|
||||
if ($isPassMerchant) {
|
||||
$uids = $this->merchantUserIds($merchant);
|
||||
$mid = (int)$merchant['id'];
|
||||
$paid = (float)Db::name('order')->where('user_id', 'in', $uids)->where('pay_status', 1)->sum('pay_amount');
|
||||
$used = (float)Db::name('invoice')
|
||||
->where(function ($q) use ($mid, $uids) {
|
||||
$q->where('merchant_id', $mid)->whereOr('user_id', 'in', $uids);
|
||||
})
|
||||
->where('status', 'in', ['wait', 'issued'])
|
||||
->sum('amount');
|
||||
} else {
|
||||
$uid = $this->uid();
|
||||
$paid = (float)Db::name('order')->where('user_id', $uid)->where('pay_status', 1)->sum('pay_amount');
|
||||
$used = (float)Db::name('invoice')->where('user_id', $uid)->where('status', 'in', ['wait', 'issued'])->sum('amount');
|
||||
}
|
||||
|
||||
$available = round(max(0, $paid - $used), 2);
|
||||
if ($amount > $available) {
|
||||
$this->error('可开票金额不足,当前可开 ¥' . $available);
|
||||
}
|
||||
|
||||
Db::name('invoice')->insert([
|
||||
'merchant_id' => $isPassMerchant ? (int)$merchant['id'] : 0,
|
||||
'user_id' => $this->uid(),
|
||||
'amount' => $amount,
|
||||
'type' => $type,
|
||||
'title' => $title,
|
||||
'tax_no' => $taxNo,
|
||||
'email' => $email,
|
||||
'remark' => $remark,
|
||||
'status' => 'wait',
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
|
||||
$this->success('开票申请已提交,3 个工作日内开具');
|
||||
$this->success('开票申请已提交,3 个工作日内开具', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -503,7 +437,9 @@ class Merchant extends Base
|
||||
|
||||
try {
|
||||
$this->notify('user', (int)$user['id'], '已加入商家账号', '您的账号已关联到商家「' . $merchant['name'] . '」,下单、账单、开票数据将实时同步', 'system', 0);
|
||||
} catch (\Exception $e) {}
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[Merchant::bindMember notify] ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('绑定成功,关联账号数据将实时同步', [
|
||||
'member_id' => (int)$id,
|
||||
@@ -542,7 +478,9 @@ class Merchant extends Base
|
||||
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) {}
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[Merchant::unbindMember notify] ' . $e->getMessage());
|
||||
}
|
||||
$this->success('已解除关联,对方将不再同步商家数据');
|
||||
}
|
||||
|
||||
@@ -604,94 +542,406 @@ class Merchant extends Base
|
||||
public function importBatch()
|
||||
{
|
||||
$merchant = $this->requireMerchant();
|
||||
$items = $this->request->post('items/a', []);
|
||||
if (empty($items)) {
|
||||
$items = $this->request->post('orders/a', []);
|
||||
$rawOrders = $this->request->post('orders', null, null);
|
||||
if ($rawOrders === null || $rawOrders === '' || $rawOrders === []) {
|
||||
$rawOrders = $this->request->post('items', [], null);
|
||||
}
|
||||
|
||||
$items = [];
|
||||
if (is_string($rawOrders)) {
|
||||
$decoded = json_decode(htmlspecialchars_decode(trim($rawOrders)), true);
|
||||
if (is_array($decoded)) {
|
||||
$items = $decoded;
|
||||
}
|
||||
} elseif (is_array($rawOrders)) {
|
||||
if (count($rawOrders) === 1 && isset($rawOrders[0]) && is_string($rawOrders[0])) {
|
||||
$decoded = json_decode(htmlspecialchars_decode(trim($rawOrders[0])), true);
|
||||
$items = is_array($decoded) ? $decoded : $rawOrders;
|
||||
} else {
|
||||
$items = $rawOrders;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($items) || !is_array($items)) {
|
||||
$this->error('导入数据不能为空');
|
||||
}
|
||||
if (count($items) > 100) {
|
||||
$this->error('单次批量导入最多支持 100 条订单');
|
||||
}
|
||||
|
||||
$globalServiceId = (int)$this->request->post('service_id', 1);
|
||||
$svc = Db::name('service')->where('id', $globalServiceId)->find();
|
||||
$defaultServiceName = $svc ? $svc['name'] : '家居安装';
|
||||
$defaultPrice = $svc && (float)$svc['base_price'] > 0 ? (float)$svc['base_price'] : 80.00;
|
||||
$globalServiceId = (int)$this->request->post('service_id', 0);
|
||||
$serviceCache = [];
|
||||
$preparedRows = [];
|
||||
|
||||
foreach ($items as $idx => $item) {
|
||||
if (is_string($item)) {
|
||||
$decItem = json_decode(htmlspecialchars_decode(trim($item)), true);
|
||||
if (is_array($decItem)) {
|
||||
$item = $decItem;
|
||||
}
|
||||
}
|
||||
if (!is_array($item)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$contact = trim((string)(isset($item['contact']) ? $item['contact'] : '买家'));
|
||||
$mobile = preg_replace('/[^\d]/', '', trim((string)(isset($item['mobile']) ? $item['mobile'] : '')));
|
||||
$address = trim((string)(isset($item['address']) ? $item['address'] : ''));
|
||||
if ($mobile === '' && $address === '') {
|
||||
continue;
|
||||
}
|
||||
$rowNo = $idx + 1;
|
||||
if (!preg_match('/^1\d{10}$/', $mobile)) {
|
||||
$this->error("第 {$rowNo} 条订单联系电话格式不正确");
|
||||
}
|
||||
if ($address === '') {
|
||||
$this->error("第 {$rowNo} 条订单缺少详细服务地址");
|
||||
}
|
||||
|
||||
$itemServiceId = !empty($item['service_id']) ? (int)$item['service_id'] : $globalServiceId;
|
||||
if ($itemServiceId <= 0) {
|
||||
$this->error("第 {$rowNo} 条订单未指定有效的服务项目");
|
||||
}
|
||||
if (!array_key_exists($itemServiceId, $serviceCache)) {
|
||||
$serviceCache[$itemServiceId] = Db::name('service')
|
||||
->where('id', $itemServiceId)
|
||||
->where('status', 'normal')
|
||||
->find() ?: null;
|
||||
}
|
||||
$svc = $serviceCache[$itemServiceId];
|
||||
if (!$svc) {
|
||||
$this->error("第 {$rowNo} 条订单关联的服务项目不存在或已下架");
|
||||
}
|
||||
if ($svc['price_type'] === 'quote') {
|
||||
$this->error("第 {$rowNo} 条订单所选服务「{$svc['name']}」为师傅报价项目,不支持批量一口价导入");
|
||||
}
|
||||
|
||||
$goodsNum = max(1, (int)(isset($item['goods_num']) ? $item['goods_num'] : (isset($item['num']) ? $item['num'] : 1)));
|
||||
$floor = isset($item['floor']) ? max(1, (int)$item['floor']) : 1;
|
||||
$hasElevator = isset($item['has_elevator'])
|
||||
? (int)$item['has_elevator']
|
||||
: (isset($item['elevator']) ? (int)$item['elevator'] : 1);
|
||||
$urgent = !empty($item['urgent']) ? 1 : 0;
|
||||
$specId = isset($item['spec_id']) ? (int)$item['spec_id'] : 0;
|
||||
$reqServiceType = isset($item['service_type']) ? trim((string)$item['service_type']) : '';
|
||||
|
||||
// 严禁采信前端传入的 price,强制根据数据库 fa_service / fa_service_spec 与楼层规则计算真实金额
|
||||
try {
|
||||
$calc = Home::calcAmount($svc, [
|
||||
'floor' => $floor,
|
||||
'has_elevator' => $hasElevator ? 1 : 0,
|
||||
'urgent' => $urgent,
|
||||
'goods_num' => $goodsNum,
|
||||
'spec_id' => $specId,
|
||||
'service_type' => $reqServiceType,
|
||||
'strict' => true,
|
||||
], 0, $this->uid(), 'fixed', 0, []);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->error("第 {$rowNo} 条订单计价校验失败:" . $e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if ((float)$calc['pay_amount'] <= 0) {
|
||||
$this->error("第 {$rowNo} 条订单计算金额异常");
|
||||
}
|
||||
|
||||
$preparedRows[] = [
|
||||
'svc' => $svc,
|
||||
'service_id' => (int)$svc['id'],
|
||||
'cat_id' => (int)$svc['cat_id'],
|
||||
'service_name' => (string)$svc['name'],
|
||||
'service_img' => (string)$svc['image'],
|
||||
'goods_model' => mb_substr(trim((string)(isset($item['goods_model']) ? $item['goods_model'] : '')), 0, 100),
|
||||
'goods_num' => $goodsNum,
|
||||
'contact' => mb_substr($contact ?: '买家', 0, 50),
|
||||
'mobile' => $mobile,
|
||||
'province' => mb_substr(trim((string)(isset($item['province']) && $item['province'] !== '' ? $item['province'] : ($merchant['province'] ?: '贵州省'))), 0, 50),
|
||||
'city' => mb_substr(trim((string)(isset($item['city']) && $item['city'] !== '' ? $item['city'] : ($merchant['city'] ?: '贵阳市'))), 0, 50),
|
||||
'district' => mb_substr(trim((string)(isset($item['district']) ? $item['district'] : ($merchant['district'] ?: ''))), 0, 50),
|
||||
'address' => mb_substr($address, 0, 255),
|
||||
'floor' => $floor,
|
||||
'has_elevator' => $hasElevator ? 1 : 0,
|
||||
'urgent' => $urgent,
|
||||
'remark' => mb_substr(trim((string)(isset($item['remark']) ? $item['remark'] : '')), 0, 500),
|
||||
'appoint_time' => !empty($item['appoint_time']) ? (int)$item['appoint_time'] : null,
|
||||
'calc' => $calc,
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($preparedRows)) {
|
||||
$this->error('未解析到有效的导入订单数据');
|
||||
}
|
||||
|
||||
$ordersCreated = [];
|
||||
$orderIds = [];
|
||||
$totalAmount = 0.00;
|
||||
$isMonthly = false;
|
||||
$batchNo = 'MB' . date('YmdHis') . mt_rand(1000, 9999);
|
||||
$now = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$batchNo = 'MB' . date('YmdHis') . mt_rand(1000, 9999);
|
||||
foreach ($items as $idx => $item) {
|
||||
$contact = trim((string)(isset($item['contact']) ? $item['contact'] : '买家'));
|
||||
$mobile = trim((string)(isset($item['mobile']) ? $item['mobile'] : ''));
|
||||
$address = trim((string)(isset($item['address']) ? $item['address'] : ''));
|
||||
$serviceName = trim((string)(isset($item['service_name']) ? $item['service_name'] : $defaultServiceName));
|
||||
$goodsModel = trim((string)(isset($item['goods_model']) ? $item['goods_model'] : ''));
|
||||
$goodsNum = max(1, (int)(isset($item['goods_num']) ? $item['goods_num'] : (isset($item['num']) ? $item['num'] : 1)));
|
||||
$lockedMerchant = Db::name('merchant')->where('id', (int)$merchant['id'])->lock(true)->find();
|
||||
if (!$lockedMerchant || $lockedMerchant['verify_status'] !== 'pass') {
|
||||
throw new \InvalidArgumentException('商家资质状态异常,无法批量发单');
|
||||
}
|
||||
$isMonthly = ($lockedMerchant['settle_type'] === 'monthly');
|
||||
$initStatus = $isMonthly ? OrderStatus::PENDING_ACCEPT : OrderStatus::PENDING_PAY;
|
||||
$initPayStatus = $isMonthly ? 1 : 0;
|
||||
$initPayType = $isMonthly ? 'monthly' : '';
|
||||
$initPayTime = $isMonthly ? $now : null;
|
||||
|
||||
if (!$mobile || !$address) {
|
||||
continue;
|
||||
}
|
||||
foreach ($preparedRows as $pRow) {
|
||||
$orderNo = $this->makeOrderNo();
|
||||
$verifyCode = str_pad((string)mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
$calc = $pRow['calc'];
|
||||
$payAmt = round((float)$calc['pay_amount'], 2);
|
||||
$unitPrice = round((float)$calc['base_amount'] / max(1, (int)$pRow['goods_num']), 2);
|
||||
|
||||
$orderNo = date('YmdHis') . mt_rand(100000, 999999);
|
||||
$singlePrice = isset($item['price']) && (float)$item['price'] > 0 ? (float)$item['price'] : $defaultPrice;
|
||||
$baseAmount = round($singlePrice * $goodsNum, 2);
|
||||
$verifyCode = (string)mt_rand(100000, 999999);
|
||||
|
||||
$orderId = Db::name('order')->insertGetId([
|
||||
$orderId = (int)Db::name('order')->insertGetId([
|
||||
'order_no' => $orderNo,
|
||||
'user_id' => $this->uid(),
|
||||
'service_id' => $globalServiceId,
|
||||
'service_name' => $serviceName,
|
||||
'goods_model' => $goodsModel,
|
||||
'goods_num' => $goodsNum,
|
||||
'contact' => $contact,
|
||||
'mobile' => $mobile,
|
||||
'address' => $address,
|
||||
'province' => isset($item['province']) ? $item['province'] : '贵州省',
|
||||
'city' => isset($item['city']) ? $item['city'] : '贵阳市',
|
||||
'district' => isset($item['district']) ? $item['district'] : '南明区',
|
||||
'floor' => isset($item['floor']) ? (int)$item['floor'] : 1,
|
||||
'has_elevator' => isset($item['elevator']) ? (int)$item['elevator'] : 1,
|
||||
'base_amount' => $baseAmount,
|
||||
'total_amount' => $baseAmount,
|
||||
'pay_amount' => $baseAmount,
|
||||
'status' => OrderStatus::PENDING_ACCEPT,
|
||||
'pay_status' => 1,
|
||||
'pay_time' => time(),
|
||||
'master_id' => 0,
|
||||
'cat_id' => $pRow['cat_id'],
|
||||
'service_id' => $pRow['service_id'],
|
||||
'service_name' => $pRow['service_name'],
|
||||
'service_image' => $pRow['service_img'],
|
||||
'price_type' => 'fixed',
|
||||
'goods_model' => $pRow['goods_model'],
|
||||
'goods_num' => $pRow['goods_num'],
|
||||
'contact' => $pRow['contact'],
|
||||
'mobile' => $pRow['mobile'],
|
||||
'province' => $pRow['province'],
|
||||
'city' => $pRow['city'],
|
||||
'district' => $pRow['district'],
|
||||
'address' => $pRow['address'],
|
||||
'floor' => $pRow['floor'],
|
||||
'has_elevator' => $pRow['has_elevator'],
|
||||
'urgent' => $pRow['urgent'],
|
||||
'remark' => $pRow['remark'],
|
||||
'appoint_time' => $pRow['appoint_time'],
|
||||
'base_amount' => $calc['base_amount'],
|
||||
'floor_amount' => $calc['floor_amount'],
|
||||
'urgent_amount' => $calc['urgent_amount'],
|
||||
'discount_amount' => 0.00,
|
||||
'total_amount' => $calc['total_amount'],
|
||||
'pay_amount' => $payAmt,
|
||||
'status' => $initStatus,
|
||||
'pay_status' => $initPayStatus,
|
||||
'pay_type' => $initPayType,
|
||||
'pay_time' => $initPayTime,
|
||||
'transaction_id' => $isMonthly ? ('MONTHLY' . date('YmdHis', $now) . mt_rand(1000, 9999)) : '',
|
||||
'verify_code' => $verifyCode,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
|
||||
Db::name('order_goods')->insert([
|
||||
'order_id' => $orderId,
|
||||
'service_id' => $pRow['service_id'],
|
||||
'goods_name' => $pRow['service_name'],
|
||||
'goods_model' => $pRow['goods_model'],
|
||||
'goods_num' => $pRow['goods_num'],
|
||||
'goods_image' => $pRow['service_img'],
|
||||
'addon_services' => '',
|
||||
'base_price' => $unitPrice,
|
||||
'createtime' => $now,
|
||||
]);
|
||||
|
||||
Db::name('merchant_order')->insert([
|
||||
'merchant_id' => (int)$merchant['id'],
|
||||
'merchant_id' => (int)$lockedMerchant['id'],
|
||||
'order_id' => $orderId,
|
||||
'batch_no' => $batchNo,
|
||||
'createtime' => time(),
|
||||
'createtime' => $now,
|
||||
]);
|
||||
|
||||
$this->addLog(
|
||||
$orderId,
|
||||
$initStatus,
|
||||
$isMonthly ? "企业批量导单(月结授信,批次 {$batchNo}),已进入接单大厅" : "企业批量导单创建(待支付,批次 {$batchNo})",
|
||||
'user',
|
||||
$this->uid()
|
||||
);
|
||||
|
||||
$orderIds[] = $orderId;
|
||||
$ordersCreated[] = [
|
||||
'order_id' => $orderId,
|
||||
'order_no' => $orderNo,
|
||||
'contact' => $contact,
|
||||
'amount' => $baseAmount
|
||||
'contact' => $pRow['contact'],
|
||||
'amount' => $payAmt,
|
||||
];
|
||||
$totalAmount += $baseAmount;
|
||||
$totalAmount = round($totalAmount + $payAmt, 2);
|
||||
}
|
||||
|
||||
if ($isMonthly) {
|
||||
Db::name('merchant')->where('id', (int)$lockedMerchant['id'])->update([
|
||||
'order_count' => (int)$lockedMerchant['order_count'] + count($ordersCreated),
|
||||
'total_amount' => round((float)$lockedMerchant['total_amount'] + $totalAmount, 2),
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (\think\exception\HttpResponseException $e) {
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
\think\Log::error('[Merchant::importBatch] ' . $e->getMessage());
|
||||
$this->error('批量导入失败:' . $e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
$this->success('批量下单导入成功!', [
|
||||
'batch_no' => $batchNo,
|
||||
'count' => count($ordersCreated),
|
||||
'total_amount' => round($totalAmount, 2),
|
||||
'orders' => $ordersCreated,
|
||||
if ($isMonthly && !empty($orderIds)) {
|
||||
try {
|
||||
$masters = Db::name('master')
|
||||
->where('verify_status', 'pass')
|
||||
->where('work_status', 'on')
|
||||
->where('user_id', '<>', $this->uid())
|
||||
->limit(20)
|
||||
->select();
|
||||
$cnt = count($orderIds);
|
||||
foreach ($masters as $m) {
|
||||
$this->notify('master', (int)$m['id'], '企业批量新工单就绪', "商家刚发布了 {$cnt} 笔新工单进入接单大厅,请尽快前往查看抢单!", 'order', (int)$orderIds[0]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[Merchant::importBatch broadcast] ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$this->success($isMonthly ? '批量下单成功,已按月结授信进入接单大厅!' : '批量导入成功,请完成批次支付', [
|
||||
'need_pay' => !$isMonthly,
|
||||
'settle_type' => $isMonthly ? 'monthly' : 'prepay',
|
||||
'batch_no' => $batchNo,
|
||||
'count' => count($ordersCreated),
|
||||
'total_amount' => round($totalAmount, 2),
|
||||
'order_ids' => $orderIds,
|
||||
'orders' => $ordersCreated,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商户端:预付商家按批次号支付待支付批量订单
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function payBatch()
|
||||
{
|
||||
$merchant = $this->requireMerchant();
|
||||
$batchNo = trim((string)$this->request->post('batch_no', ''));
|
||||
$payType = trim((string)$this->request->post('pay_type', 'wechat'));
|
||||
if ($payType === '') {
|
||||
$payType = 'mock';
|
||||
}
|
||||
if ($batchNo === '') {
|
||||
$this->error('缺少批次号 batch_no');
|
||||
}
|
||||
|
||||
$batchLinks = Db::name('merchant_order')->where('batch_no', $batchNo)->select();
|
||||
if (empty($batchLinks)) {
|
||||
$this->error('批次订单不存在');
|
||||
}
|
||||
$batchOrderIds = [];
|
||||
foreach ($batchLinks as $link) {
|
||||
if ((int)$link['merchant_id'] !== (int)$merchant['id']) {
|
||||
$this->error('无权操作该批次订单', null, 403);
|
||||
}
|
||||
$oid = (int)$link['order_id'];
|
||||
if ($oid > 0) {
|
||||
$batchOrderIds[] = $oid;
|
||||
}
|
||||
}
|
||||
$batchOrderIds = array_values(array_unique($batchOrderIds));
|
||||
if (empty($batchOrderIds)) {
|
||||
$this->error('该批次下无有效订单');
|
||||
}
|
||||
|
||||
$paidCount = 0;
|
||||
$paidTotal = 0.00;
|
||||
$paidOrderIds = [];
|
||||
$now = time();
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$lockedMerchant = Db::name('merchant')->where('id', (int)$merchant['id'])->lock(true)->find();
|
||||
$orders = Db::name('order')
|
||||
->where('id', 'in', $batchOrderIds)
|
||||
->lock(true)
|
||||
->select();
|
||||
|
||||
foreach ($orders as $ord) {
|
||||
if ((int)$ord['pay_status'] === 1 || (int)$ord['status'] !== OrderStatus::PENDING_PAY) {
|
||||
continue;
|
||||
}
|
||||
$oid = (int)$ord['id'];
|
||||
$amt = round((float)$ord['pay_amount'], 2);
|
||||
$txId = 'BATCH' . date('YmdHis', $now) . mt_rand(1000, 9999);
|
||||
|
||||
Db::name('order')->where('id', $oid)->update([
|
||||
'status' => OrderStatus::PENDING_ACCEPT,
|
||||
'pay_status' => 1,
|
||||
'pay_type' => $payType,
|
||||
'pay_time' => $now,
|
||||
'transaction_id' => $txId,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
|
||||
$this->addLog($oid, OrderStatus::PENDING_ACCEPT, "企业批次支付完成(批次 {$batchNo}),订单已进入接单大厅", 'user', $this->uid());
|
||||
$paidCount++;
|
||||
$paidTotal = round($paidTotal + $amt, 2);
|
||||
$paidOrderIds[] = $oid;
|
||||
}
|
||||
|
||||
if ($paidCount === 0) {
|
||||
throw new \InvalidArgumentException('该批次没有待支付的订单,请勿重复支付');
|
||||
}
|
||||
|
||||
if ($lockedMerchant) {
|
||||
Db::name('merchant')->where('id', (int)$lockedMerchant['id'])->update([
|
||||
'order_count' => (int)$lockedMerchant['order_count'] + $paidCount,
|
||||
'total_amount' => round((float)$lockedMerchant['total_amount'] + $paidTotal, 2),
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (\think\exception\HttpResponseException $e) {
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
\think\Log::error('[Merchant::payBatch] ' . $e->getMessage());
|
||||
$this->error('批次支付失败:' . $e->getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$masters = Db::name('master')
|
||||
->where('verify_status', 'pass')
|
||||
->where('work_status', 'on')
|
||||
->where('user_id', '<>', $this->uid())
|
||||
->limit(20)
|
||||
->select();
|
||||
foreach ($masters as $m) {
|
||||
$this->notify('master', (int)$m['id'], '企业批量新工单就绪', "同城有 {$paidCount} 笔企业批量工单进入接单大厅,请尽快前往查看抢单!", 'order', (int)$paidOrderIds[0]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
\think\Log::error('[Merchant::payBatch broadcast] ' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success('批次支付成功,订单已进入接单大厅', [
|
||||
'batch_no' => $batchNo,
|
||||
'count' => $paidCount,
|
||||
'paid_count' => $paidCount,
|
||||
'total_amount' => round($paidTotal, 2),
|
||||
'order_ids' => $paidOrderIds,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -741,13 +991,31 @@ class Merchant extends Base
|
||||
{
|
||||
$m = $this->merchantInfo();
|
||||
if (!$m) {
|
||||
$this->error('请先完成商家入驻', ['need_apply' => 1], 2001);
|
||||
$curUser = Db::name('user')->where('id', $this->uid())->find();
|
||||
if ($curUser && !empty($curUser['user_type']) && $curUser['user_type'] === 'merchant') {
|
||||
$now = time();
|
||||
$newId = Db::name('merchant')->insertGetId([
|
||||
'user_id' => $this->uid(),
|
||||
'name' => !empty($curUser['nickname']) ? (string)$curUser['nickname'] : '商家用户',
|
||||
'license_no' => '',
|
||||
'contact' => (string)$curUser['nickname'],
|
||||
'mobile' => (string)$curUser['mobile'],
|
||||
'address' => isset($curUser['contact_address']) ? (string)$curUser['contact_address'] : '',
|
||||
'email' => isset($curUser['email']) ? (string)$curUser['email'] : '',
|
||||
'verify_status' => 'pass',
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
return Db::name('merchant')->where('id', $newId)->find();
|
||||
}
|
||||
$this->error('请先在「账号信息」切换为商家类型', null, 2001);
|
||||
}
|
||||
if ($m['verify_status'] === 'pending') {
|
||||
$this->error('商家资质审核中,请耐心等待', ['verify_status' => 'pending'], 2002);
|
||||
}
|
||||
if ($m['verify_status'] === 'reject') {
|
||||
$this->error('资质未通过:' . $m['audit_msg'], ['verify_status' => 'reject'], 2003);
|
||||
if ($m['verify_status'] !== 'pass') {
|
||||
Db::name('merchant')->where('id', (int)$m['id'])->update([
|
||||
'verify_status' => 'pass',
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
$m['verify_status'] = 'pass';
|
||||
}
|
||||
return $m;
|
||||
}
|
||||
|
||||
文件差异内容过多而无法显示
加载差异
在新工单中引用
屏蔽一个用户