diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index cd5fef7..13fd88d 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -4,6 +4,7 @@ namespace app\common\controller; use app\common\controller\BaseController; use app\common\model\UsedCar; use app\common\model\UsedCarPdfLog; +use app\common\model\User; use app\common\service\OssService; use app\common\traits\crud\CrudControllerTrait; use app\common\service\UsedCarService; @@ -138,8 +139,14 @@ class UsedCarController extends BaseController } $data['attachment'] = $attachments; $data['price'] = $price; - - $pdf = (new MpdfDriver())->exportUsedCarPdf($data); + $user = User::where('id',$request->user['id'])->first(); + $contact = $user['email'] ?? $user['phone'] ?? $user['whatsapp'] ?? null; + if(!empty($user['social_code'])){ + $ossService = new OssService(); + $user['social_code'] = $ossService->generatePrivateUrl($user['social_code']); + $user['social_code'] = $this->service->downloadImage($user['social_code']); + } + $pdf = (new MpdfDriver())->exportUsedCarPdf($data,$user,$contact); UsedCar::where('id', $data['id'])->update(['pdf_src' => $pdf]); array_map('unlink', glob(runtime_path('pdf/images/*.jpg'))); diff --git a/app/common/controller/UserController.php b/app/common/controller/UserController.php index 38c768b..cdaa385 100644 --- a/app/common/controller/UserController.php +++ b/app/common/controller/UserController.php @@ -2,8 +2,10 @@ namespace app\common\controller; use app\common\controller\BaseController; +use app\common\service\OssService; use app\common\traits\crud\CrudControllerTrait; use app\common\service\UserService; +use support\Request; class UserController extends BaseController { @@ -30,4 +32,66 @@ class UserController extends BaseController // $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); // return $data; // } + + /** + * 更新 + * + * @param Request $request 请求对象 + * @return mixed 返回更新成功的数据或信息 + */ + public function edit(Request $request) + { + try { + $data = $request->post(); + $request->setPost('id',$request->user['id']); + $res = $this->service->serviceUpdate($request, $data); + return $this->success([], '更新成功'); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + + /** + * 获取详情 + * + * @param Request $request 请求对象 + * @param int|string $id 资源 ID + * @return mixed 返回资源详情数据 + */ + public function show(Request $request) + { + try { + $request->setGet('id',$request->user['id']); + $raw = $this->service->serviceShow($request); + $data = $this->transformData($raw, method_exists($this, 'showFormat') + ? [$this, 'showFormat'] + : null); + return $this->success($data); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + + /** + * 列表页数据格式化 + * + * @param array $data + * @return array + */ + protected function showFormat($data) + { + if (isset($data['created_at'])) + $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']); + if (isset($data['updated_at'])) + $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); + unset($data['union_id']); + unset($data['app_id']); + + $ossservice = new OssService(); + $data['user']['avatar_pull'] = $data['user']['avatar']; + if(!str_contains($data['user']['avatar'], 'http') && !str_contains($data['user']['avatar'], 'www.') && !empty($data['user']['avatar'])){ + $data['user']['avatar_pull'] = $ossservice->generatePrivateUrl($data['user']['avatar']); + } + return $data; + } } \ No newline at end of file diff --git a/app/common/logic/AuthLogic.php b/app/common/logic/AuthLogic.php index f4510f1..da75795 100644 --- a/app/common/logic/AuthLogic.php +++ b/app/common/logic/AuthLogic.php @@ -218,10 +218,12 @@ class AuthLogic } + Log::info('userInfo:' ,[$userInfo]); User::firstOrCreate(['union_id'=>$userInfo['data']['union_id']],[ 'union_id'=>$userInfo['data']['union_id'], 'name'=>$userInfo['data']['name'], - 'app_id'=>$data['app_id'] + 'app_id'=>$data['app_id'], + 'avatar'=>$userInfo['avatar_url'] ]); return $userInfo['data'] + $data; } diff --git a/app/common/model/UsedCar.php b/app/common/model/UsedCar.php index 567ae08..bf6ac24 100644 --- a/app/common/model/UsedCar.php +++ b/app/common/model/UsedCar.php @@ -40,7 +40,7 @@ class UsedCar extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'user_id', 'product_name', 'vin', 'price', 'source_place', 'plate_place', 'plate_time', 'displacement','mileage','power', 'body_color', 'images', 'condition_desc', 'attachment', 'crawl_title', 'crawl_brand', 'crawl_series', 'crawl_year', 'crawl_msrp', 'crawl_car_id', 'status', 'sort', 'created_at', 'updated_at']; + protected $fillable = ['id','code', 'user_id', 'product_name', 'vin', 'price', 'source_place', 'plate_place', 'plate_time', 'displacement','mileage','power', 'body_color', 'images', 'condition_desc', 'attachment', 'crawl_title', 'crawl_brand', 'crawl_series', 'crawl_year', 'crawl_msrp', 'crawl_car_id', 'status', 'sort', 'created_at', 'updated_at']; /** * 不允许自动填充模型字段 diff --git a/app/common/model/User.php b/app/common/model/User.php index 58e9baa..0110f68 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -40,7 +40,7 @@ class User extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'union_id', 'app_id', 'name']; + protected $fillable = ['id', 'union_id', 'app_id', 'name', 'avatar', 'phone', 'email', 'whatsapp', 'social', 'social_code']; /** * 不允许自动填充模型字段 diff --git a/app/common/service/UsedCarService.php b/app/common/service/UsedCarService.php index bffec12..b7afd1e 100644 --- a/app/common/service/UsedCarService.php +++ b/app/common/service/UsedCarService.php @@ -9,6 +9,7 @@ use GuzzleHttp\Client; use support\exception\BusinessException; use support\Log; use support\Request; +use app\common\model\User; class UsedCarService { @@ -85,24 +86,93 @@ class UsedCarService public function beforeServiceStore($data, Request $request): array { - if(empty($data['product_name'])){ + if (empty($data['product_name'])) { throw new BusinessException('产品名称不能为空'); } - if(empty($data['images'])){ + if (empty($data['images'])) { throw new BusinessException('产品图片不能为空'); } - if(empty($data['price'])){ + if (empty($data['price'])) { $data['price'] = 0; } - if(!empty($data['attachment'])){ + if (!empty($data['attachment'])) { $data['attachment'] = json_encode($data['attachment']); }else{ $data['attachment'] = ''; } $data['user_id'] = $request->user['id']; + $data['code'] = $this->getcode($request->user['id']); return $data; } + public function getcode($userId){ + $todayStart = strtotime('today'); + $count = UsedCar::where('user_id',$userId)->where('created_at','>=',$todayStart)->count(); + $countCode = str_pad($count, 3, '0', STR_PAD_LEFT); + $userCode = str_pad($userId, 3, '0', STR_PAD_LEFT); + $user = User::where('id',$userId)->first(); + $nameInitials = $this->getNameInitials($user['name']); + return $nameInitials . $userCode . date('Ymd') . $countCode; + } + + + /** + * 获取中文名拼音首字母 + */ + function getNameInitials(string $name): string + { + $initials = ''; + $len = mb_strlen($name); + for ($i = 0; $i < $len; $i++) { + $char = mb_substr($name, $i, 1); + $initials .= $this->getFirstChar($char); + } + return strtoupper($initials); + } + + function getFirstChar(string $char): string + { + $s = iconv('UTF-8', 'GB2312//IGNORE', $char); + $asc = ord($s[0]); + + if ($asc >= 65 && $asc <= 90) return $char; + + $code = ord($s[0]) * 256 + ord($s[1]) - 65536; + + $map = [ + [-20319, -20284, 'A'], + [-20283, -19776, 'B'], + [-19775, -19219, 'C'], + [-19218, -18711, 'D'], + [-18710, -18527, 'E'], + [-18526, -18240, 'F'], + [-18239, -17923, 'G'], + [-17922, -17418, 'H'], + [-17417, -16475, 'J'], + [-16474, -16213, 'K'], + [-16212, -15641, 'L'], + [-15640, -15166, 'M'], + [-15165, -14923, 'N'], + [-14922, -14915, 'O'], + [-14914, -14631, 'P'], + [-14630, -14150, 'Q'], + [-14149, -14091, 'R'], + [-14090, -13319, 'S'], + [-13318, -12839, 'T'], + [-12838, -12557, 'W'], + [-12556, -11848, 'X'], + [-11847, -11056, 'Y'], + [-11055, -10247, 'Z'] + ]; + + foreach ($map as [$start, $end, $letter]) { + if ($code >= $start && $code <= $end) { + return $letter; + } + } + return ''; + } + /** @@ -122,30 +192,30 @@ class UsedCarService $href = $crawlService->modelSearch($keyword); $seriesId = $this->extractSeriesId($href); //懂车帝车系id -// if (empty($seriesId)) { //没有抓取到懂车帝系列id时,按关键词为品牌名抓取所有的系列名 -// $crawlSeries = $crawlService->brandSeries($keyword); -// } + // if (empty($seriesId)) { //没有抓取到懂车帝系列id时,按关键词为品牌名抓取所有的系列名 + // $crawlSeries = $crawlService->brandSeries($keyword); + // } //按关键词为品牌名抓取所有的系列名 -// $crawlSeries = $crawlService->brandSeries($keyword); + // $crawlSeries = $crawlService->brandSeries($keyword); if (empty($seriesId)) { throw new \Exception('抓取失败,请使用更精确的关键词'); } -// $brandSerieses = []; -// if(empty($seriesId) && !empty($crawlSeries) || count($crawlSeries)>1){ //关键词为品牌名时抓取所有车系的懂车帝id -// $href = $crawlService->modelSearch($crawlSeries[0]); -// $seriesId = $this->extractSeriesId($href); //懂车帝车系id -// //抓取品牌与系列 -// $brandSeries = $crawlService->crawlBrandSeries($seriesId); -// foreach ($crawlSeries as $item) { -//// $brandSerieses[] = $crawlService->brandSeries($keyword); -// $brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $item]; -// } -// }elseif (!empty($seriesId) && empty($crawlSeries)){ -// //抓取品牌与系列 -// $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId); -// } + // $brandSerieses = []; + // if(empty($seriesId) && !empty($crawlSeries) || count($crawlSeries)>1){ //关键词为品牌名时抓取所有车系的懂车帝id + // $href = $crawlService->modelSearch($crawlSeries[0]); + // $seriesId = $this->extractSeriesId($href); //懂车帝车系id + // //抓取品牌与系列 + // $brandSeries = $crawlService->crawlBrandSeries($seriesId); + // foreach ($crawlSeries as $item) { + //// $brandSerieses[] = $crawlService->brandSeries($keyword); + // $brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $item]; + // } + // }elseif (!empty($seriesId) && empty($crawlSeries)){ + // //抓取品牌与系列 + // $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId); + // } //抓取品牌与系列 $brandSerieses[] = $crawlService->crawlBrandSeries($seriesId); @@ -158,7 +228,7 @@ class UsedCarService //获取车型数据 $item['model'] = []; - if (count($brandSerieses)<2) { + if (count($brandSerieses) < 2) { $item['model'] = $this->crawlSeriesModel($seriesId); } $result[] = $item; @@ -189,30 +259,30 @@ class UsedCarService ] ]); $data = json_decode($response->getBody()->getContents(), true); - if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ + if ($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))) { Log::error('抓取车型数据失败' . $data['message'], $data); throw new \Exception('抓取车型数据失败' . $data['message']); } $models = []; foreach ($data['data']['tab_list'] as $item) { - if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ + if ($item['tab_text'] == '在售' || $item['tab_text'] == '停售') { unset($item['data'][0]); $models = array_merge($models, $item['data']); } } foreach ($models as $key => $model) { - if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ + if (!isset($model["info"]['car_name']) || $model['type'] != "1115") { unset($models[$key]); continue; } $official_price = 0; - if(!isset($model["info"]['official_price'])){ + if (!isset($model["info"]['official_price'])) { $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; - }else{ + } else { $official_price = $model["info"]['official_price'] * 10000; } $models[$key] = [ - 'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], + 'title' => $model["info"]['year'] . '款 ' . $model["info"]['car_name'], 'brand_id' => $model["info"]['brand_id'], 'brand_name' => $model["info"]['brand_name'], 'series_id' => $model["info"]['series_id'], @@ -224,7 +294,6 @@ class UsedCarService } return array_values($models); - } catch (\Exception $e) { throw new \Exception($e->getMessage()); } @@ -235,7 +304,8 @@ class UsedCarService * @param string $url 完整的URL * @return string|null series ID或null */ - function extractSeriesId($url) { + function extractSeriesId($url) + { // 解析URL获取路径部分 $path = parse_url($url, PHP_URL_PATH); @@ -291,4 +361,4 @@ class UsedCarService return $path; } -} \ No newline at end of file +} diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index 89c5725..b2e2f45 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -5,6 +5,7 @@ namespace app\common\service; use app\common\validate\UserValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\User; +use support\Request; class UserService { @@ -28,4 +29,12 @@ class UserService { return new $this->validator; } + + public function beforeServiceUpdate($data, Request $request){ + unset($data['union_id']); + unset($data['app_id']); + unset($data['name']); + + return $data; + } } \ No newline at end of file diff --git a/app/common/utils/pdf/service/MpdfDriver.php b/app/common/utils/pdf/service/MpdfDriver.php index c303693..436cb34 100644 --- a/app/common/utils/pdf/service/MpdfDriver.php +++ b/app/common/utils/pdf/service/MpdfDriver.php @@ -116,7 +116,7 @@ class MpdfDriver return [$css, $html]; } - public function exportUsedCarPdf($car) + public function exportUsedCarPdf($car,$user,$contact) { /*$car = [ 'title' => '奥迪Q3 2013款 35 TFSI 舒适型', @@ -154,12 +154,35 @@ class MpdfDriver 'default_font' => 'sun-exta' ]); + $mpdf->SetProtection(//限制篡改或盗用 + ['print'], // 允许的权限 copy:允许复制、print:允许打印 + '', // 用户密码(可留空) + 'Admin@2026@!#' // 拥有者密码(一定要设置) + ); + + //设置水印 + $mpdf->SetWatermarkText( + $user['name'] .' '. $contact, + 0.08 + ); + $mpdf->showWatermarkText = true; + + // 1️⃣ 车辆基本信息 HTML $mpdf->WriteHTML($html); + // 2️⃣ 追加附件pdf foreach ($car['attachment'] as $attachment) { $this->appendPdf($mpdf,$attachment); } + ob_start(); + $cardTpl = base_path('app/common/utils/pdf/template/contactCard.stub'); + include $cardTpl; + $cardHtml = ob_get_clean(); + // 3️⃣ 新增一页:名片页 + $mpdf->AddPage(); + $mpdf->WriteHTML($cardHtml); + $pdf = $mpdf->Output('车辆详情.pdf', 'S'); $dir = runtime_path('pdf'); if (!is_dir($dir)) mkdir($dir, 0777, true); diff --git a/app/common/utils/pdf/template/contactCard.stub b/app/common/utils/pdf/template/contactCard.stub new file mode 100644 index 0000000..5b8c4f4 --- /dev/null +++ b/app/common/utils/pdf/template/contactCard.stub @@ -0,0 +1,84 @@ + + +
+ + + + + +|
+ |
+
+
+