feat: 重构后台服务字典与商品独立定价体系及前后端交互优化
这个提交包含在:
@@ -126,7 +126,7 @@ class Auth extends Base
|
||||
protected function profileData()
|
||||
{
|
||||
$uid = $this->uid();
|
||||
$user = Db::name('user')->field('id,nickname,mobile,avatar,money,score,createtime')->where('id', $uid)->find();
|
||||
$user = Db::name('user')->field('id,nickname,mobile,email,avatar,money,score,user_type,contact_address,createtime')->where('id', $uid)->find();
|
||||
$master = Db::name('master')->where('user_id', $uid)->find();
|
||||
|
||||
$couponCount = Db::name('user_coupon')->where('user_id', $uid)->where('status', 'unused')->count();
|
||||
@@ -156,6 +156,9 @@ class Auth extends Base
|
||||
$merchantInfo = [
|
||||
'id' => (int)$merchant['id'],
|
||||
'name' => $merchant['name'],
|
||||
'legal_person' => isset($merchant['legal_person']) ? (string)$merchant['legal_person'] : '',
|
||||
'legal_mobile' => isset($merchant['legal_mobile']) ? (string)$merchant['legal_mobile'] : '',
|
||||
'tech_mobile' => isset($merchant['tech_mobile']) ? (string)$merchant['tech_mobile'] : '',
|
||||
'verify_status' => $merchant['verify_status'],
|
||||
'audit_msg' => $merchant['audit_msg'],
|
||||
'settle_type' => $merchant['settle_type'],
|
||||
@@ -165,21 +168,112 @@ class Auth extends Base
|
||||
];
|
||||
}
|
||||
|
||||
$userType = (!empty($user['user_type']) && $user['user_type'] === 'merchant') || ($merchant && $merchant['verify_status'] === 'pass')
|
||||
? 'merchant'
|
||||
: 'personal';
|
||||
|
||||
return [
|
||||
'id' => (int)$user['id'],
|
||||
'nickname' => $user['nickname'] ?: ('用户' . substr((string)$user['mobile'], -4)),
|
||||
'mobile' => $user['mobile'],
|
||||
'avatar' => $user['avatar'] ?: '',
|
||||
'balance' => (float)$user['money'],
|
||||
'coupon_count' => (int)$couponCount,
|
||||
'unread_count' => (int)$unread,
|
||||
'is_master' => $master ? true : false,
|
||||
'master' => $masterInfo,
|
||||
'is_merchant' => $merchant ? true : false,
|
||||
'merchant' => $merchantInfo,
|
||||
'id' => (int)$user['id'],
|
||||
'nickname' => $user['nickname'] ?: ('用户' . substr((string)$user['mobile'], -4)),
|
||||
'mobile' => $user['mobile'],
|
||||
'email' => !empty($user['email']) ? (string)$user['email'] : ($merchant && !empty($merchant['email']) ? (string)$merchant['email'] : ''),
|
||||
'contact_address' => !empty($user['contact_address']) ? (string)$user['contact_address'] : ($merchant ? (string)($merchant['province'] . $merchant['city'] . $merchant['district'] . $merchant['address']) : ''),
|
||||
'user_type' => $userType,
|
||||
'company_name' => $merchant ? (string)$merchant['name'] : '',
|
||||
'legal_person' => $merchant && isset($merchant['legal_person']) ? (string)$merchant['legal_person'] : '',
|
||||
'legal_mobile' => $merchant && isset($merchant['legal_mobile']) ? (string)$merchant['legal_mobile'] : '',
|
||||
'tech_mobile' => $merchant && isset($merchant['tech_mobile']) ? (string)$merchant['tech_mobile'] : '',
|
||||
'avatar' => $user['avatar'] ?: '',
|
||||
'balance' => (float)$user['money'],
|
||||
'coupon_count' => (int)$couponCount,
|
||||
'unread_count' => (int)$unread,
|
||||
'is_master' => $master ? true : false,
|
||||
'master' => $masterInfo,
|
||||
'is_merchant' => $merchant ? true : false,
|
||||
'merchant' => $merchantInfo,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存账号信息(对标奇兵到家「账号信息」页:支持 家庭/个人 与 商家 两种用户类型)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function saveAccount()
|
||||
{
|
||||
$uid = $this->uid();
|
||||
$userType = trim((string)$this->request->post('user_type', 'personal'));
|
||||
if (!in_array($userType, ['personal', 'merchant'], true)) {
|
||||
$userType = 'personal';
|
||||
}
|
||||
$nickname = trim((string)$this->request->post('nickname', ''));
|
||||
$mobile = trim((string)$this->request->post('mobile', ''));
|
||||
$contactAddress = trim((string)$this->request->post('contact_address', ''));
|
||||
$email = trim((string)$this->request->post('email', ''));
|
||||
$companyName = trim((string)$this->request->post('company_name', ''));
|
||||
$legalPerson = trim((string)$this->request->post('legal_person', ''));
|
||||
$legalMobile = trim((string)$this->request->post('legal_mobile', ''));
|
||||
$techMobile = trim((string)$this->request->post('tech_mobile', ''));
|
||||
|
||||
if ($nickname === '') {
|
||||
$this->error('请输入用户昵称');
|
||||
}
|
||||
if ($legalPerson !== '' && preg_match('/\d/', $legalPerson)) {
|
||||
$this->error('法人姓名不支持数字输入');
|
||||
}
|
||||
if ($legalMobile !== '' && !preg_match('/^1\d{10}$/', $legalMobile)) {
|
||||
$this->error('法人手机号格式不正确');
|
||||
}
|
||||
if ($techMobile !== '' && !preg_match('/^1\d{10}$/', $techMobile)) {
|
||||
$this->error('技术手机号格式不正确');
|
||||
}
|
||||
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->error('电子邮箱格式不正确');
|
||||
}
|
||||
|
||||
$userUpdate = [
|
||||
'nickname' => $nickname,
|
||||
'user_type' => $userType,
|
||||
'contact_address' => $contactAddress,
|
||||
'email' => $email,
|
||||
'updatetime' => time(),
|
||||
];
|
||||
if ($mobile !== '' && preg_match('/^1\d{10}$/', $mobile)) {
|
||||
$conflict = Db::name('user')->where('mobile', $mobile)->where('id', '<>', $uid)->find();
|
||||
if ($conflict) {
|
||||
$this->error('该联系号码已被其他账号绑定');
|
||||
}
|
||||
$userUpdate['mobile'] = $mobile;
|
||||
}
|
||||
Db::name('user')->where('id', $uid)->update($userUpdate);
|
||||
|
||||
if ($userType === 'merchant') {
|
||||
$existMerchant = Db::name('merchant')->where('user_id', $uid)->find();
|
||||
$curUser = Db::name('user')->where('id', $uid)->find();
|
||||
$merchantSave = [
|
||||
'name' => $companyName !== '' ? $companyName : ($existMerchant ? $existMerchant['name'] : $nickname),
|
||||
'legal_person' => $legalPerson,
|
||||
'legal_mobile' => $legalMobile,
|
||||
'tech_mobile' => $techMobile,
|
||||
'contact' => $nickname,
|
||||
'mobile' => $curUser ? (string)$curUser['mobile'] : '',
|
||||
'address' => $contactAddress,
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('保存成功', ['userinfo' => $this->profileData()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信授权登录(小程序 uni.login 拿到的 code)
|
||||
* @ApiMethod (POST)
|
||||
|
||||
@@ -155,4 +155,138 @@ class Base extends Api
|
||||
{
|
||||
return date('YmdHis') . str_pad((string)mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步写入或隐藏独立晒单表 fa_showcase(仅存储脱敏后的必要公开信息)
|
||||
* @param array $order 订单行
|
||||
* @param int $isShowcase 1=公开晒单, 0=取消晒单
|
||||
* @return int 晒单表主键 ID(取消时返回 0)
|
||||
*/
|
||||
protected function syncOrderShowcase(array $order, $isShowcase = 1)
|
||||
{
|
||||
$orderId = (int)$order['id'];
|
||||
$now = time();
|
||||
if (!$isShowcase) {
|
||||
Db::name('showcase')->where('order_id', $orderId)->update([
|
||||
'status' => 'hidden',
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
$rev = Db::name('review')->where('order_id', $orderId)->where('status', 'normal')->find();
|
||||
$u = Db::name('user')->where('id', (int)$order['user_id'])->find();
|
||||
$cat = Db::name('service_cat')->where('id', (int)$order['cat_id'])->find();
|
||||
$service = Db::name('service')->where('id', (int)$order['service_id'])->find();
|
||||
|
||||
$isAnon = $rev ? (int)$rev['is_anonymous'] : 0;
|
||||
$userName = '匿名业主';
|
||||
$userAvatar = '';
|
||||
if (!$isAnon && $u) {
|
||||
$nick = trim((string)$u['nickname']);
|
||||
if ($nick !== '' && !preg_match('/^[0-9]/', $nick)) {
|
||||
$userName = mb_substr($nick, 0, 1) . '业主';
|
||||
} else {
|
||||
$userName = '优匠业主';
|
||||
}
|
||||
$userAvatar = !empty($u['avatar']) ? (string)$u['avatar'] : '';
|
||||
}
|
||||
|
||||
// 地区仅保留区县或城市,绝不保存具体街道/小区/门牌
|
||||
$district = trim((string)$order['district']);
|
||||
$city = trim((string)$order['city']);
|
||||
$region = $district ?: ($city ?: '同城服务');
|
||||
|
||||
// 收集晒单实拍图(评价晒图 > 完工实况图,过滤无效测试域名)
|
||||
$imgList = [];
|
||||
if ($rev && !empty($rev['images'])) {
|
||||
$dec = json_decode((string)$rev['images'], true);
|
||||
$arr = is_array($dec) ? $dec : array_filter(explode(',', (string)$rev['images']));
|
||||
foreach ($arr as $it) {
|
||||
$it = trim((string)$it);
|
||||
if ($it !== '' && substr($it, -4) !== '.svg' && strpos($it, 'example.com') === false) {
|
||||
$imgList[] = $it;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($imgList)) {
|
||||
$mediaUrls = Db::name('order_media')->where('order_id', $orderId)->order('id desc')->limit(6)->column('url');
|
||||
foreach ($mediaUrls as $mu) {
|
||||
$mu = trim((string)$mu);
|
||||
if ($mu !== '' && substr($mu, -4) !== '.svg' && strpos($mu, 'example.com') === false) {
|
||||
$imgList[] = $mu;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 封面图提取
|
||||
$cover = !empty($imgList) ? $imgList[0] : '';
|
||||
if ($cover === '' && !empty($order['service_image']) && substr($order['service_image'], -4) !== '.svg') {
|
||||
$cover = (string)$order['service_image'];
|
||||
}
|
||||
if ($cover === '' && $service && !empty($service['image']) && substr($service['image'], -4) !== '.svg') {
|
||||
$cover = (string)$service['image'];
|
||||
}
|
||||
if ($cover === '') {
|
||||
$fallback = Db::name('service')
|
||||
->where('cat_id', (int)$order['cat_id'])
|
||||
->where('image', '<>', '')
|
||||
->where('image', 'not like', '%.svg%')
|
||||
->order('weigh desc, id asc')
|
||||
->value('image');
|
||||
if ($fallback) {
|
||||
$cover = (string)$fallback;
|
||||
}
|
||||
}
|
||||
|
||||
// 师傅脱敏称呼与评分
|
||||
$masterName = '优匠师傅';
|
||||
$masterScore = 5.00;
|
||||
if ((int)$order['master_id'] > 0) {
|
||||
$m = Db::name('master')->where('id', (int)$order['master_id'])->find();
|
||||
if ($m) {
|
||||
$rName = trim((string)$m['realname']);
|
||||
if ($rName !== '' && strpos($rName, '师傅') === false) {
|
||||
$masterName = mb_substr($rName, 0, 1) . '师傅';
|
||||
} elseif ($rName !== '') {
|
||||
$masterName = $rName;
|
||||
}
|
||||
$masterScore = (float)$m['score'] > 0 ? round((float)$m['score'], 2) : 5.00;
|
||||
}
|
||||
}
|
||||
|
||||
$score = $rev ? max(1, min(5, (int)$rev['score'])) : 5;
|
||||
$tags = ($rev && !empty($rev['tags'])) ? (string)$rev['tags'] : '准时,专业,值得推荐';
|
||||
$content = ($rev && !empty($rev['content'])) ? (string)$rev['content'] : '师傅准时上门,施工专业细致,安装效果很满意!';
|
||||
|
||||
$payload = [
|
||||
'order_id' => $orderId,
|
||||
'user_id' => (int)$order['user_id'],
|
||||
'user_name' => $userName,
|
||||
'user_avatar' => $userAvatar,
|
||||
'cat_id' => (int)$order['cat_id'],
|
||||
'cat_name' => $cat ? (string)$cat['name'] : '上门服务',
|
||||
'service_id' => (int)$order['service_id'],
|
||||
'service_name' => $order['service_name'] ?: ($service ? (string)$service['name'] : '上门服务'),
|
||||
'region' => $region,
|
||||
'cover_image' => $cover,
|
||||
'images' => !empty($imgList) ? json_encode(array_values($imgList), JSON_UNESCAPED_UNICODE) : '',
|
||||
'score' => $score,
|
||||
'tags' => $tags,
|
||||
'content' => mb_substr($content, 0, 500),
|
||||
'master_name' => $masterName,
|
||||
'master_score' => $masterScore,
|
||||
'status' => 'normal',
|
||||
'updatetime' => $now,
|
||||
];
|
||||
|
||||
$existing = Db::name('showcase')->where('order_id', $orderId)->find();
|
||||
if ($existing) {
|
||||
Db::name('showcase')->where('id', (int)$existing['id'])->update($payload);
|
||||
return (int)$existing['id'];
|
||||
} else {
|
||||
$payload['createtime'] = !empty($order['showcase_time']) ? (int)$order['showcase_time'] : $now;
|
||||
return (int)Db::name('showcase')->insertGetId($payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
文件差异内容过多而无法显示
加载差异
@@ -248,33 +248,92 @@ class Merchant extends Base
|
||||
}
|
||||
|
||||
/**
|
||||
* 开票记录
|
||||
* 开票记录与可开票额度(个人用户与认证商家通用)
|
||||
*/
|
||||
public function invoices()
|
||||
{
|
||||
$merchant = $this->requireMerchant();
|
||||
$list = Db::name('invoice')->where('merchant_id', $merchant['id'])->order('id desc')->limit(30)->select();
|
||||
$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);
|
||||
$this->success('', ['list' => $list]);
|
||||
|
||||
$userRow = Db::name('user')->where('id', $this->uid())->field('nickname,mobile')->find();
|
||||
$personalDefaultTitle = ($userRow && !empty($userRow['nickname'])) ? (string)$userRow['nickname'] : '个人';
|
||||
|
||||
$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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请开票
|
||||
* 申请开票(支持个人用户与认证商家)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function invoice()
|
||||
{
|
||||
$merchant = $this->requireMerchant();
|
||||
$merchant = $this->merchantInfo();
|
||||
$isPassMerchant = ($merchant && $merchant['verify_status'] === 'pass');
|
||||
|
||||
$amount = round((float)$this->request->post('amount'), 2);
|
||||
$type = $this->request->post('type', 'normal');
|
||||
$email = trim((string)$this->request->post('email'));
|
||||
$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('请填写开票金额');
|
||||
@@ -283,21 +342,64 @@ class Merchant extends Base
|
||||
$this->error('发票类型错误');
|
||||
}
|
||||
|
||||
$paid = (float)Db::name('order')->where('user_id', 'in', $this->merchantUserIds($merchant))->where('pay_status', 1)->sum('pay_amount');
|
||||
$used = (float)Db::name('invoice')->where('merchant_id', $merchant['id'])->where('status', 'in', ['wait', 'issued'])->sum('amount');
|
||||
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' => (int)$merchant['id'],
|
||||
'merchant_id' => $isPassMerchant ? (int)$merchant['id'] : 0,
|
||||
'user_id' => $this->uid(),
|
||||
'amount' => $amount,
|
||||
'type' => $type,
|
||||
'title' => $merchant['invoice_title'] ?: $merchant['name'],
|
||||
'tax_no' => $merchant['invoice_tax_no'],
|
||||
'title' => $title,
|
||||
'tax_no' => $taxNo,
|
||||
'email' => $email,
|
||||
'remark' => $remark,
|
||||
'status' => 'wait',
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
|
||||
@@ -18,6 +18,118 @@ class Order extends Base
|
||||
$this->success('', ['list' => OrderStatus::all()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 物流单号智能识别物流公司(支持线上快递100识别 + 本地单号特征规则库双保险)
|
||||
*/
|
||||
public function detectExpress()
|
||||
{
|
||||
$raw = strtoupper(trim((string)$this->request->param('express_no', $this->request->param('logistics_no', ''))));
|
||||
$no = preg_replace('/[^A-Z0-9]/', '', $raw);
|
||||
if ($no === '' || strlen($no) < 5) {
|
||||
$this->error('请输入正确的物流单号');
|
||||
}
|
||||
|
||||
$codeMap = [
|
||||
'shunfeng' => '顺丰速运',
|
||||
'debangwuliu' => '德邦快递',
|
||||
'debangkuaidi' => '德邦快递',
|
||||
'jd' => '京东快递',
|
||||
'kuayue' => '跨越速运',
|
||||
'shentong' => '申通快递',
|
||||
'yuantong' => '圆通速递',
|
||||
'zhongtong' => '中通快递',
|
||||
'yunda' => '韵达',
|
||||
'huitongkuaidi' => '百世快递',
|
||||
'baishiwuliu' => '百世快递',
|
||||
'tiantian' => '天天快递',
|
||||
'ems' => '邮政特快专递',
|
||||
'youzhengguonei' => '中国邮政',
|
||||
'youzhengbk' => '中国邮政',
|
||||
'zhaijisong' => '宅急送',
|
||||
'jtexpress' => '极兔速递',
|
||||
'annengwuliu' => '安能物流',
|
||||
];
|
||||
|
||||
$companyName = '';
|
||||
|
||||
// 1. 优先匹配高确定性的标准物流单号前缀规则
|
||||
if (strpos($no, 'SF') === 0) {
|
||||
$companyName = '顺丰速运';
|
||||
} elseif (strpos($no, 'DP') === 0 || strpos($no, 'DB') === 0) {
|
||||
$companyName = '德邦快递';
|
||||
} elseif (strpos($no, 'JD') === 0 || strpos($no, 'KK') === 0) {
|
||||
$companyName = '京东快递';
|
||||
} elseif (strpos($no, 'KY') === 0) {
|
||||
$companyName = '跨越速运';
|
||||
} elseif (strpos($no, 'YT') === 0) {
|
||||
$companyName = '圆通速递';
|
||||
} elseif (strpos($no, 'ST') === 0 || strpos($no, '77') === 0) {
|
||||
$companyName = '申通快递';
|
||||
} elseif (strpos($no, 'ZT') === 0 || preg_match('/^(73|75|76|78)\d{10,12}$/', $no)) {
|
||||
$companyName = '中通快递';
|
||||
} elseif (strpos($no, 'YD') === 0 || preg_match('/^(31|43|46)\d{11,13}$/', $no)) {
|
||||
$companyName = '韵达';
|
||||
} elseif (strpos($no, 'BS') === 0 || strpos($no, 'HT') === 0 || preg_match('/^55\d{11,13}$/', $no)) {
|
||||
$companyName = '百世快递';
|
||||
} elseif (strpos($no, 'TT') === 0 || preg_match('/^66\d{10,12}$/', $no)) {
|
||||
$companyName = '天天快递';
|
||||
} elseif (preg_match('/^(EMS|EA|EB|EC|ED|EE|EF|10|11)\d{7,11}/', $no)) {
|
||||
$companyName = '邮政特快专递';
|
||||
} elseif (preg_match('/^(98|99|YZ)\d{9,13}$/', $no)) {
|
||||
$companyName = '中国邮政';
|
||||
} elseif (strpos($no, 'ZJS') === 0) {
|
||||
$companyName = '宅急送';
|
||||
} elseif (strpos($no, 'JT') === 0) {
|
||||
$companyName = '极兔速递';
|
||||
} elseif (strpos($no, 'ANE') === 0) {
|
||||
$companyName = '安能物流';
|
||||
}
|
||||
|
||||
// 2. 若前缀未命中,尝试快递100智能单号识别接口(1.5秒超时保护)
|
||||
if ($companyName === '') {
|
||||
try {
|
||||
$ctx = stream_context_create([
|
||||
'http' => ['timeout' => 1.5, 'ignore_errors' => true],
|
||||
'ssl' => ['verify_peer' => false, 'verify_peer_name' => false],
|
||||
]);
|
||||
$resp = @file_get_contents('https://www.kuaidi100.com/autonumber/autoComNum?text=' . urlencode($no), false, $ctx);
|
||||
if ($resp) {
|
||||
$json = json_decode($resp, true);
|
||||
if (!empty($json['auto']) && is_array($json['auto'])) {
|
||||
foreach ($json['auto'] as $item) {
|
||||
$code = isset($item['comCode']) ? strtolower((string)$item['comCode']) : '';
|
||||
if (isset($codeMap[$code])) {
|
||||
$companyName = $codeMap[$code];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {}
|
||||
}
|
||||
|
||||
// 3. 常见纯数字位数兜底识别
|
||||
if ($companyName === '') {
|
||||
if (preg_match('/^\d{15}$/', $no)) {
|
||||
$companyName = '德邦快递';
|
||||
} elseif (preg_match('/^\d{12}$/', $no)) {
|
||||
$companyName = '顺丰速运';
|
||||
} elseif (preg_match('/^\d{13}$/', $no)) {
|
||||
$companyName = '圆通速递';
|
||||
} elseif (preg_match('/^\d{14}$/', $no)) {
|
||||
$companyName = '中通快递';
|
||||
} else {
|
||||
$companyName = '德邦快递';
|
||||
}
|
||||
}
|
||||
|
||||
$this->success('已识别物流公司', [
|
||||
'express_no' => $no,
|
||||
'company_name' => $companyName,
|
||||
'matched' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下单(创建待支付订单)
|
||||
* @ApiMethod (POST)
|
||||
@@ -203,6 +315,15 @@ class Order extends Base
|
||||
$goodsImagesPayload = json_encode($extraPayload, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
$orderContact = trim((string)$this->request->post('order_contact', ''));
|
||||
$expressNo = trim((string)$this->request->post('express_no', $this->request->post('logistics_no', '')));
|
||||
$expressCompany = trim((string)$this->request->post('express_company', $this->request->post('logistics_company', '')));
|
||||
$goodsArrived = (int)$this->request->post('goods_arrived', 1);
|
||||
$orderSource = trim((string)$this->request->post('order_source', ''));
|
||||
$sourceOrderNo = trim((string)$this->request->post('source_order_no', ''));
|
||||
$qualReq = trim((string)$this->request->post('qual_req', ''));
|
||||
$masterReq = trim((string)$this->request->post('master_req', ''));
|
||||
|
||||
$id = Db::name('order')->insertGetId([
|
||||
'order_no' => $orderNo,
|
||||
'user_id' => $this->uid(),
|
||||
@@ -227,6 +348,14 @@ class Order extends Base
|
||||
'address' => $address['address'],
|
||||
'lng' => $address['lng'],
|
||||
'lat' => $address['lat'],
|
||||
'order_contact' => mb_substr($orderContact, 0, 100),
|
||||
'express_no' => mb_substr($expressNo, 0, 64),
|
||||
'express_company' => mb_substr($expressCompany, 0, 64),
|
||||
'goods_arrived' => $goodsArrived ? 1 : 0,
|
||||
'order_source' => mb_substr($orderSource, 0, 100),
|
||||
'source_order_no' => mb_substr($sourceOrderNo, 0, 64),
|
||||
'qual_req' => mb_substr($qualReq, 0, 255),
|
||||
'master_req' => mb_substr($masterReq, 0, 255),
|
||||
'appoint_time' => $appointTime ?: null,
|
||||
'base_amount' => $amount['base_amount'],
|
||||
'floor_amount' => $amount['floor_amount'],
|
||||
@@ -510,6 +639,93 @@ class Order extends Base
|
||||
$this->success('订单已删除');
|
||||
}
|
||||
|
||||
/**
|
||||
* 一键晒单 / 取消晒单(仅限已完成订单,控制是否在首页服务动态公开展示)
|
||||
* @ApiMethod (POST)
|
||||
*/
|
||||
public function showcase()
|
||||
{
|
||||
$orderId = (int)$this->request->post('order_id');
|
||||
$isShowcase = (int)$this->request->post('is_showcase', 1) ? 1 : 0;
|
||||
$content = trim((string)$this->request->post('content', ''));
|
||||
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
if ((int)$order['status'] !== OrderStatus::COMPLETED) {
|
||||
$this->error('仅已完成的订单支持晒单操作');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($isShowcase === 1) {
|
||||
$existingReview = Db::name('review')->where('order_id', $orderId)->find();
|
||||
if ($existingReview) {
|
||||
$updateRev = ['is_showcase' => 1];
|
||||
if ($content !== '') {
|
||||
$updateRev['content'] = mb_substr($content, 0, 500);
|
||||
}
|
||||
Db::name('review')->where('id', (int)$existingReview['id'])->update($updateRev);
|
||||
} else {
|
||||
// 若尚未评价直接点击一键晒单,自动复用完工验收图生成五星好评晒单
|
||||
$mediaUrls = Db::name('order_media')
|
||||
->where('order_id', $orderId)
|
||||
->order('id desc')
|
||||
->limit(6)
|
||||
->column('url');
|
||||
$imagesStr = !empty($mediaUrls) ? json_encode(array_values($mediaUrls), JSON_UNESCAPED_UNICODE) : '';
|
||||
$defaultContent = $content !== '' ? mb_substr($content, 0, 500) : '师傅准时上门,施工专业细致,验收非常满意,主动晒单推荐!';
|
||||
Db::name('review')->insert([
|
||||
'order_id' => $orderId,
|
||||
'user_id' => $this->uid(),
|
||||
'master_id' => (int)$order['master_id'],
|
||||
'score' => 5,
|
||||
'tags' => '准时,专业,值得推荐',
|
||||
'content' => $defaultContent,
|
||||
'images' => $imagesStr,
|
||||
'is_anonymous' => 0,
|
||||
'is_showcase' => 1,
|
||||
'status' => 'normal',
|
||||
'createtime' => $now,
|
||||
]);
|
||||
$masterId = (int)$order['master_id'];
|
||||
if ($masterId > 0) {
|
||||
$avg = Db::name('review')->where('master_id', $masterId)->where('status', 'normal')->avg('score');
|
||||
Db::name('master')->where('id', $masterId)->update([
|
||||
'score' => round((float)$avg, 2),
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'is_review' => 1,
|
||||
'is_showcase' => 1,
|
||||
'showcase_time' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
$showcaseId = $this->syncOrderShowcase($order, 1);
|
||||
} else {
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'is_showcase' => 0,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
Db::name('review')->where('order_id', $orderId)->update([
|
||||
'is_showcase' => 0,
|
||||
]);
|
||||
$showcaseId = $this->syncOrderShowcase($order, 0);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('操作失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
$this->success($isShowcase ? '已公开晒单至首页服务动态' : '已取消公开晒单', [
|
||||
'is_showcase' => $isShowcase,
|
||||
'showcase_id' => isset($showcaseId) ? (int)$showcaseId : 0,
|
||||
'is_review' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情(含状态轨迹与师傅信息)
|
||||
*/
|
||||
@@ -600,7 +816,8 @@ class Order extends Base
|
||||
if (empty($goodsList)) {
|
||||
$goodsList = [
|
||||
[
|
||||
'goods_name' => $formattedOrder['goods_name'] ?: $formattedOrder['service_name'],
|
||||
// fa_order 表无 goods_name 字段,回退使用 service_name
|
||||
'goods_name' => $formattedOrder['service_name'],
|
||||
'goods_model' => $formattedOrder['goods_model'] ?: '',
|
||||
'goods_num' => max(1, (int)$formattedOrder['goods_num']),
|
||||
'goods_image' => $formattedOrder['goods_image_url'],
|
||||
@@ -610,6 +827,39 @@ class Order extends Base
|
||||
];
|
||||
}
|
||||
|
||||
$review = null;
|
||||
$revRow = Db::name('review')->where('order_id', $orderId)->where('status', 'normal')->find();
|
||||
if ($revRow) {
|
||||
$revTags = [];
|
||||
if (!empty($revRow['tags'])) {
|
||||
$decTags = json_decode($revRow['tags'], true);
|
||||
$revTags = is_array($decTags) ? $decTags : array_values(array_filter(explode(',', (string)$revRow['tags'])));
|
||||
}
|
||||
$revImgs = [];
|
||||
if (!empty($revRow['images'])) {
|
||||
$decImgs = json_decode($revRow['images'], true);
|
||||
$rawImgs = is_array($decImgs) ? $decImgs : array_values(array_filter(explode(',', (string)$revRow['images'])));
|
||||
foreach ($rawImgs as $rImg) {
|
||||
if ($rImg) {
|
||||
$revImgs[] = cdnurl(trim($rImg), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
$review = [
|
||||
'id' => (int)$revRow['id'],
|
||||
'score' => (int)$revRow['score'],
|
||||
'tags' => $revTags,
|
||||
'content' => (string)$revRow['content'],
|
||||
'images' => $revImgs,
|
||||
'is_anonymous' => (int)$revRow['is_anonymous'],
|
||||
'is_showcase' => isset($revRow['is_showcase']) ? (int)$revRow['is_showcase'] : (int)$formattedOrder['is_showcase'],
|
||||
'time_text' => date('Y-m-d H:i', (int)$revRow['createtime']),
|
||||
];
|
||||
}
|
||||
|
||||
$showcaseRow = Db::name('showcase')->where('order_id', $orderId)->where('status', 'normal')->find();
|
||||
$formattedOrder['showcase_id'] = $showcaseRow ? (int)$showcaseRow['id'] : 0;
|
||||
|
||||
$this->success('', [
|
||||
'order' => $formattedOrder,
|
||||
'goods_list' => $goodsList,
|
||||
@@ -617,6 +867,7 @@ class Order extends Base
|
||||
'logs' => $logs,
|
||||
'media' => $media,
|
||||
'quotes' => $quotes,
|
||||
'review' => $review,
|
||||
'can_choose_quote' => $order['price_type'] === 'quote'
|
||||
&& (int)$order['status'] === OrderStatus::QUOTING
|
||||
&& (int)$order['master_id'] === 0,
|
||||
@@ -941,6 +1192,8 @@ class Order extends Base
|
||||
'can_delete' => in_array($status, [OrderStatus::CANCELLED, OrderStatus::COMPLETED], true),
|
||||
'can_verify' => $status === OrderStatus::PENDING_VERIFY,
|
||||
'can_review' => $status === OrderStatus::COMPLETED && (int)$row['is_review'] === 0,
|
||||
'can_showcase' => $status === OrderStatus::COMPLETED,
|
||||
'is_showcase' => isset($row['is_showcase']) ? (int)$row['is_showcase'] : 0,
|
||||
'can_pay' => $status === OrderStatus::PENDING_PAY,
|
||||
'pay_amount' => (float)$row['pay_amount'],
|
||||
'total_amount' => (float)$row['total_amount'],
|
||||
|
||||
@@ -22,8 +22,9 @@ class Review extends Base
|
||||
$score = (int)$this->request->post('score', 5);
|
||||
$content = trim((string)$this->request->post('content'));
|
||||
$tags = $this->request->post('tags', '');
|
||||
$images = $this->request->post('images', '');
|
||||
$images = $this->request->post('images', '', null);
|
||||
$anonymous = (int)$this->request->post('is_anonymous', 0);
|
||||
$isShowcase = (int)$this->request->post('is_showcase', 1);
|
||||
|
||||
$order = $this->ownOrder($orderId, 'user');
|
||||
if ((int)$order['status'] !== OrderStatus::COMPLETED) {
|
||||
@@ -36,6 +37,7 @@ class Review extends Base
|
||||
$this->error('请选择 1-5 星');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
Db::name('review')->insert([
|
||||
@@ -47,10 +49,16 @@ class Review extends Base
|
||||
'content' => mb_substr($content, 0, 500),
|
||||
'images' => is_array($images) ? json_encode($images, JSON_UNESCAPED_UNICODE) : (string)$images,
|
||||
'is_anonymous' => $anonymous ? 1 : 0,
|
||||
'is_showcase' => $isShowcase ? 1 : 0,
|
||||
'status' => 'normal',
|
||||
'createtime' => time(),
|
||||
'createtime' => $now,
|
||||
]);
|
||||
Db::name('order')->where('id', $orderId)->update([
|
||||
'is_review' => 1,
|
||||
'is_showcase' => $isShowcase ? 1 : 0,
|
||||
'showcase_time' => $isShowcase ? $now : null,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
Db::name('order')->where('id', $orderId)->update(['is_review' => 1, 'updatetime' => time()]);
|
||||
|
||||
// 刷新师傅综合评分
|
||||
$masterId = (int)$order['master_id'];
|
||||
@@ -61,6 +69,9 @@ class Review extends Base
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
// 同步至独立晒单表 fa_showcase
|
||||
$this->syncOrderShowcase($order, $isShowcase ? 1 : 0);
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
在新工单中引用
屏蔽一个用户