validator; } /** * 构造 CrudUtil 查询配置 */ protected function buildQueryConfig(Request $request, string $scene = 'default'): array { $p = $request->all(); $config = []; $config['model'] = $this->modelClass; switch ($scene) { case 'default': if (isset($p['keyword']) && $p['keyword']) { $k = '%' . $p['keyword'] . '%'; foreach (['model','content'] as $col) { $config['orWhere'][] = [$col, 'like', $k]; } } $config['where']['status'] = 1; $config['order']['sort'] = 'desc'; $config['order']['id'] = 'desc'; break; case 'index': $config['where']['status'] = 1; $config['order']['sort'] = 'desc'; $config['order']['id'] = 'desc'; break; case 'show': $config['where']['id'] = $p['id']; $config['where']['status'] = 1; break; case 'store': break; case 'update': $config['where']['id'] = $p['id']; break; case 'destroy': $config['where']['id'] = $p['id']; break; } return $config; } /** * 添加前置处理 */ public function beforeServiceStore($data, Request $request) { $protocol = $request->header('x-forwarded-proto', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'); $domain = $protocol . '://' . $request->host(); // $spec = []; // foreach ($data['spec'] as $key => $value) { // $specItem = []; // $specItem['category_id'] = $data['category_id']; // $specItem['attr_id'] = $value['attr_id']; // $specItem['attr_name'] = $value['attr_name']; // $specItem['attr_value'] = $value['attr_value']; // $spec[] = $specItem; // } // $spec_json = json_encode($spec,JSON_UNESCAPED_UNICODE); // Redis::set('sku_attr_'.$request->user->uid, $spec_json,30); unset($data['spec']); // $data['spec_json'] = $spec_json; $data['user_id'] = $request->user->uid; $data['code'] = $this->generateUniqueCode(); $data['image'] = implode(',',$data['image']); // 将OSS直连地址替换为代理地址 // $ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/'; // $proxyPath = '/proxy/ossurl?file='; // // $data['desc'] = str_replace( // $ossDomain, // $proxyPath, // $data['desc'] // ); // 将OSS直连地址替换为代理地址 $ossDomain = 'https://static.hnzhwlkj.cn/'; $proxyPath = $domain.'/proxy/ossurl?file='; $data['desc'] = str_replace( $ossDomain, $proxyPath, $data['desc'] ); $data['desc'] = str_replace( '?Expires=', '&Expires=', $data['desc'] ); return $data; } /** * 添加后置处理 */ public function afterServiceStore($res) { // $specs = Redis::get('sku_attr_'.$res['user_id']); // $specs= json_decode($specs,true); // $spec = []; // foreach ($specs as $key => $value) { // $spec['category_id'] = $res['category_id']; // $spec['sku_id'] = $res['id']; // $spec['attr_id'] = $value['attr_id']; // $spec['attr_name'] = $value['attr_name']; // $spec['attr_value'] = $value['attr_value']; // } return $res; } /** * 添加前置处理 */ public function beforeServiceUpdate($data, Request $request) { $protocol = $request->header('x-forwarded-proto', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'); $domain = $protocol . '://' . $request->host(); // $spec = []; // foreach ($data['spec'] as $key => $value) { // $specItem = []; // $specItem['category_id'] = $data['category_id']; // $specItem['sku_id'] = $data['id']; // $specItem['attr_id'] = $value['attr_id']; // $specItem['attr_name'] = $value['attr_name']; // $specItem['attr_value'] = $value['attr_value']; // $spec[] = $specItem; // } // $spec_json = json_encode($spec,JSON_UNESCAPED_UNICODE); unset($data['spec']); // $data['spec_json'] = $spec_json; $data['user_id'] = $request->user->uid; $data['image'] = implode(',',$data['image']); // 将OSS直连地址替换为代理地址 // $ossDomain = 'https://vehicle-album.oss-cn-beijing.aliyuncs.com/'; // $proxyPath = '/proxy/ossurl?file='; // $data['desc'] = str_replace( // $ossDomain, // $proxyPath, // $data['desc'] // ); // 将OSS直连地址替换为代理地址 $ossDomain = 'https://static.hnzhwlkj.cn/'; $proxyPath = $domain.'/proxy/ossurl?file='; $data['desc'] = str_replace( $ossDomain, $proxyPath, $data['desc'] ); $data['desc'] = str_replace( '?Expires=', '&Expires=', $data['desc'] ); return $data; } /** * 生成唯一的7位颜色编码 * 格式:C + (O/I) + 5位字母数字组合 * * @param int $type 类型:2=I,其他=O * @return string 7位唯一编码 */ public function generateUniqueCode() { $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $maxAttempts = 10; // 防止无限循环 for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { // 生成后5位随机字符 $suffix = ''; for ($i = 0; $i < 5; $i++) { $suffix .= $chars[rand(0, strlen($chars) - 1)]; } $colorCode = 'PN' . $suffix; // 检查是否已存在 $existing = Sku::where('code', $colorCode)->first(); if (!$existing) { return $colorCode; } } // 如果1000次尝试后仍然重复,使用时间戳确保唯一性 $timestamp = substr(time(), -5); return 'PN' . $timestamp; } }