From 43897af989e02647ad0f988a5aeccd48860553aa Mon Sep 17 00:00:00 2001 From: "HESU\\HESU" <1696136552@qq.com> Date: Mon, 17 Mar 2025 18:01:53 +0800 Subject: [PATCH] 11 --- webman/app/controller/AreaController.php | 20 ++-- webman/app/controller/CarTypeController.php | 3 +- webman/app/controller/CustomerController.php | 36 ++++-- webman/app/controller/FollowController.php | 20 ++-- webman/app/controller/UploadController.php | 2 +- webman/app/controller/UserController.php | 108 ++++++++++++++---- webman/app/model/CarType.php | 4 +- webman/app/model/Channel.php | 48 ++++++++ webman/app/model/Follow.php | 3 +- webman/app/model/User.php | 13 +++ webman/app/service/MailService.php | 2 +- webman/app/util/Util.php | 3 + .../resource/translations/zh_CN/messages.php | 7 +- 13 files changed, 210 insertions(+), 59 deletions(-) create mode 100644 webman/app/model/Channel.php diff --git a/webman/app/controller/AreaController.php b/webman/app/controller/AreaController.php index 7ba730d..ee6addd 100644 --- a/webman/app/controller/AreaController.php +++ b/webman/app/controller/AreaController.php @@ -14,12 +14,11 @@ class AreaController $parentId = Util::getParam($request,'parent_id'); $level = Util::getParam($request,'level')?:1; $list = $this->getRegions($parentId, $level); - return json($list); + return Util::success($list); } - - - function getRegions($parentId = null, $level = 1) { + //递归查询 + function getRegions($parentId = null, $level = 1, $child = true) { $regions = []; $where= array(); @@ -30,13 +29,20 @@ class AreaController $results = Area::where($where)->get(); foreach ($results as $row) { -// $row['level'] = $level; // 添加层级信息 -// $regions = array_merge($regions, $this->getRegions($row['id'], $level + 1)); // 递归调用 - $row['children'] = $this->getRegions($row['id'], $row['level'] + 1); + if($child){ + $row['children'] = $this->getRegions($row['id'], $row['level'] + 1); + } $regions[] = $row; } return $regions; } + + public function getCountry (Request $request) + { + $level = Util::getParam($request,'level')?:1; + $list = $this->getRegions(null, $level,false); + return Util::success($list); + } } \ No newline at end of file diff --git a/webman/app/controller/CarTypeController.php b/webman/app/controller/CarTypeController.php index 7a69582..573c39b 100644 --- a/webman/app/controller/CarTypeController.php +++ b/webman/app/controller/CarTypeController.php @@ -4,6 +4,7 @@ namespace app\controller; use app\model\CarType; use app\model\Customer; +use app\util\Util; use PDO; use support\Db; use support\Redis; @@ -15,7 +16,7 @@ class CarTypeController public function getAll(Request $request) { $list = CarType::getAll(); - return json($list); + return Util::success($list); } } \ No newline at end of file diff --git a/webman/app/controller/CustomerController.php b/webman/app/controller/CustomerController.php index 1719c92..ba0066d 100644 --- a/webman/app/controller/CustomerController.php +++ b/webman/app/controller/CustomerController.php @@ -2,6 +2,7 @@ namespace app\controller; +use app\model\Channel; use app\model\Customer; use app\util\Util; use config\constants\Constants; @@ -10,7 +11,6 @@ use support\Request; class CustomerController { - /** * 客户分级 * @param Request $request @@ -21,28 +21,40 @@ class CustomerController $grade = []; foreach (Constants::GRADE as $key => $value) { $grade[] = [ - 'status' => $key, - 'text' => $value, + 'id' => $key, + 'name' => $value, ]; } - return json($grade); + return Util::success($grade); } /** - * 客户偏好 + * 商机 * @param Request $request * @return \support\Response */ - public function getCarType(Request $request) + public function getMagnitude(Request $request) { $grade = []; - foreach (Constants::GRADE as $key => $value) { + foreach (Constants::MAGNITUDE as $key => $value) { $grade[] = [ - 'status' => $key, - 'text' => $value, + 'id' => $key, + 'name' => $value, ]; } - return json($grade); + return Util::success($grade); + } + + /** + * 客户渠道 + * @param Request $request + * @return \support\Response + */ + public function getChannel(Request $request) + { + $list = Channel::getAll(); + return Util::success($list); + } /** @@ -158,9 +170,9 @@ class CustomerController } if ($res) { - return Util::success($res); + return Util::success($res,trans('save_success')); } else { - return Util::fail($res); + return Util::fail($res,trans('save_fail')); } } diff --git a/webman/app/controller/FollowController.php b/webman/app/controller/FollowController.php index 0ec5b25..3e8223b 100644 --- a/webman/app/controller/FollowController.php +++ b/webman/app/controller/FollowController.php @@ -70,8 +70,11 @@ class FollowController ->orderBy('id', 'desc') ->paginate($perPage, ['*'], 'page', $page); + +// $host = $request->host(); + $url = 'http:'.$request->url(); foreach ($data->items() as &$value){ - $value = Follow::getFollow($value); + $value = Follow::getFollow($value,$url); } return Util::page($data); @@ -112,6 +115,7 @@ class FollowController public function save(Request $request) { $customer_id = Util::getParam($request,'customer_id'); + if(Util::isEmpty($customer_id)){ return Util::fail([],'customer_id'.trans('not_null')); } @@ -132,9 +136,9 @@ class FollowController 'remark'=>Util::getParam($request,'remark',''), 'audio'=>Util::getParam($request,'audio',''), 'audio_text'=>Util::getParam($request,'audio',''), - 'lat'=>Util::getParam($request,'lat'), - 'lng'=>Util::getParam($request,'lng'), - 'address'=>Util::getParam($request,'address'), + 'lat'=>Util::getParam($request,'lat',0), + 'lng'=>Util::getParam($request,'lng',0), + 'address1'=>Util::getParam($request,'address'), ]; $res = Follow::create($data); @@ -143,10 +147,10 @@ class FollowController $customer->follow_last = $res->created_at; $customer->save(); - if($res){ - return Util::success($res); - }else{ - return Util::fail($res); + if ($res) { + return Util::success($res,trans('save_success')); + } else { + return Util::fail($res,trans('save_fail')); } } diff --git a/webman/app/controller/UploadController.php b/webman/app/controller/UploadController.php index eb2b38e..343577d 100644 --- a/webman/app/controller/UploadController.php +++ b/webman/app/controller/UploadController.php @@ -78,7 +78,7 @@ class UploadController 'size' => $fileSize ]); - $result[] = [ + $result = [ 'original_name' => $file->getUploadName(), 'saved_name' => $filename, 'path' => 'uploads/' . date('Ym') . '/' . $filename, diff --git a/webman/app/controller/UserController.php b/webman/app/controller/UserController.php index 3a92b55..fccc7d6 100644 --- a/webman/app/controller/UserController.php +++ b/webman/app/controller/UserController.php @@ -14,26 +14,24 @@ class UserController /** * 不需要登录的方法 */ - protected $noNeedLogin = ['login','sendCode']; + protected $noNeedLogin = ['login', 'sendCode', 'register']; public function login(Request $request) { -// Util::isEmpty($request->post('email')); - $email = Util::getParam($request,'email'); - if(Util::isEmpty($email)){ - return Util::fail([],'email'.trans('not_null')); + $email = Util::getParam($request, 'email'); + if (Util::isEmpty($email)) { + return Util::fail([], 'email' . trans('not_null')); } -// Util::isEmpty($request->post('password')); - $password = Util::getParam($request,'password'); - if(Util::isEmpty($password)){ - return Util::fail([],'password'.trans('not_null')); + $password = Util::getParam($request, 'password'); + if (Util::isEmpty($password)) { + return Util::fail([], 'password' . trans('not_null')); } - $user = User::select(['id','name','sex','role','avatar','email','password']) - ->where('status',0) - ->where('email',$request->post('email')) + $user = User::select(['id', 'name', 'sex', 'role', 'avatar', 'email', 'password']) + ->where('status', 0) + ->where('email', $request->post('email')) ->first(); - if(!$user){ - return Util::fail(['user'=>$user],trans('user_none')); + if (!$user) { + return Util::fail(['user' => $user], trans('user_none')); } $user = $user->toArray(); @@ -44,12 +42,12 @@ class UserController //验证密码 if (!password_verify($userPassword, $hashedPassword)) { - return Util::fail([],trans('password_error')); + return Util::fail([], trans('password_error')); } $userId = $user['id']; // 实际从数据库获取 - Redis::set('user_'.$userId, $user); + Redis::set('user_' . $userId, $user); //密码验证成功后加密生成 Token $token = JwtUtil::generateToken($userId); @@ -57,7 +55,7 @@ class UserController unset($result['password']); $result['token'] = $token; - return Util::success($result,trans('login_success')); + return Util::success($result, trans('login_success')); } /** @@ -68,10 +66,9 @@ class UserController */ public function sendCode(Request $request) { -// $email = $request->input('email'); - $email = '1696136552@qq.com'; + $email = Util::getParam($request,'email'); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - return json(['code' => 400, 'msg' => '邮箱格式错误']); + return Util::fail([], 'email' . trans('not_null')); } // 生成验证码 @@ -82,13 +79,76 @@ class UserController if ($mailService->sendVerificationCode($email, $code)) { // 存储验证码到 Redis Redis::set("verification_code:{$email}", $code, 300); - return Util::success([],'验证码已发送'); + return Util::success([], '验证码已发送'); } - return Util::fail([],'邮件发送失败'); + return Util::fail([], '邮件发送失败'); + } + + /** + * 注册 + * @param Request $request + * @return \support\Response + * @throws \Random\RandomException + */ + public function register(Request $request) + { + $email = Util::getParam($request,'email','asdf'); + $code = Util::getParam($request,'code'); + $sex = Util::getParam($request,'sex',0); + $name = Util::getParam($request,'name',0); + $password = Util::getParam($request,'password'); + $mobile = Util::getParam($request,'mobile'); + $id = Util::getParam($request,'id'); + + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + return Util::fail([], 'email'.$email . trans('not_null')); + } + + $res = User::select('id', 'email') + ->where('email', $email) + ->first(); +// if (!Util::isEmpty($res)) { +// return Util::fail([], trans('email_repeat')); +// } + + if (Util::isEmpty($name)) { + return Util::fail([], 'name' . trans('not_null')); + } + + $verification_code = Redis::get("verification_code:{$email}"); + + if ($code != $verification_code) { + return Util::fail([], trans('code_error')); + } + + //加密 + $hashedPassword = password_hash($password, PASSWORD_DEFAULT); + + $data = [ + 'mobile' => $mobile, + 'email' => $email, + 'name' => $name, + 'sex' => $sex, + 'role' => 1, + 'password' => $hashedPassword, + ]; + + if (Util::isEmpty($id)) { + $res = User::create($data); + } else { + $res = User::where('id', $id) + ->update($data); +// Log::info('update', ['res' => $res]); + (!$res)?: $res=User::find($id)->toArray(); + } + + if ($res) { + return Util::success($res); + } else { + return Util::fail($res); + } } - - } \ No newline at end of file diff --git a/webman/app/model/CarType.php b/webman/app/model/CarType.php index cde1877..a4a4f95 100644 --- a/webman/app/model/CarType.php +++ b/webman/app/model/CarType.php @@ -5,8 +5,6 @@ namespace app\model; use support\Model; -use Illuminate\Database\Eloquent\SoftDeletes; - class CarType extends Model { /** @@ -32,7 +30,7 @@ class CarType extends Model public static function getAll() { - $list = CarType::get()->select('id','name','status')->where('status',0)->toArray(); + $list = self::get()->select('id','name','status')->where('status',0)->toArray(); return $list; } } \ No newline at end of file diff --git a/webman/app/model/Channel.php b/webman/app/model/Channel.php new file mode 100644 index 0000000..a9bba9e --- /dev/null +++ b/webman/app/model/Channel.php @@ -0,0 +1,48 @@ + 'datetime:Y-m-d H:i:s', // 指定序列化格式 +// 'created_at' => 'datetime:Y-m-d H:i:s', // 指定序列化格式 + 'updated_at' => 'timestamp', // 指定序列化格式 + 'created_at' => 'timestamp', // 指定序列化格式 + ]; + + public static function getAll() + { + $list = self::get()->select('id','name','status')->where('status',0)->toArray(); + return $list; + } +} \ No newline at end of file diff --git a/webman/app/model/Follow.php b/webman/app/model/Follow.php index 09993a0..aed113d 100644 --- a/webman/app/model/Follow.php +++ b/webman/app/model/Follow.php @@ -43,9 +43,10 @@ class Follow extends Model 'created_at' => 'timestamp', // 指定序列化格式 ]; - public static function getFollow($follow) + public static function getFollow($follow,$domain) { $follow['type_txt'] = Constants::FOLLOW[$follow['type']]; + $follow['oss_picture'] = $domain.'/'.$follow['picture']; return $follow; } } \ No newline at end of file diff --git a/webman/app/model/User.php b/webman/app/model/User.php index 28fe73e..118d3b2 100644 --- a/webman/app/model/User.php +++ b/webman/app/model/User.php @@ -30,6 +30,19 @@ class User extends Model */ public $timestamps = true; + // 指定日期字段存储格式为时间戳 + protected $dateFormat = 'U'; + + //设置黑名单 + protected $guarded = ['id','created_at','updated_at']; + + protected $casts = [ +// 'updated_at' => 'datetime:Y-m-d H:i:s', // 指定序列化格式 +// 'created_at' => 'datetime:Y-m-d H:i:s', // 指定序列化格式 + 'updated_at' => 'timestamp', // 指定序列化格式 + 'created_at' => 'timestamp', // 指定序列化格式 + ]; + public static function getUser($userId) { $user = User::first()->select(['id','name','sex','role','avatar','email','password']) diff --git a/webman/app/service/MailService.php b/webman/app/service/MailService.php index 864a147..03f1104 100644 --- a/webman/app/service/MailService.php +++ b/webman/app/service/MailService.php @@ -39,7 +39,7 @@ class MailService public function sendVerificationCode(string $toEmail, string $code): bool { try { - $this->mailer->setFrom('1696136552@qq.com', 'XinYouHe Code'); // 发件人 + $this->mailer->setFrom('1696136552@qq.com', 'XinYouHe'); // 发件人 $this->mailer->addAddress($toEmail); // 邮件内容 diff --git a/webman/app/util/Util.php b/webman/app/util/Util.php index bc20f1a..daa6a22 100644 --- a/webman/app/util/Util.php +++ b/webman/app/util/Util.php @@ -30,6 +30,9 @@ class Util public static function getParam(Request $request,$param,$default='') { $param = $request->input($param,$default); + if(empty($default)&&$default!==''){ + $param = $default; + } return $param; } diff --git a/webman/resource/translations/zh_CN/messages.php b/webman/resource/translations/zh_CN/messages.php index 93229a6..e6890f0 100644 --- a/webman/resource/translations/zh_CN/messages.php +++ b/webman/resource/translations/zh_CN/messages.php @@ -2,10 +2,15 @@ return [ 'success' => '成功', - 'fail' => '成功', + 'fail' => '失败', 'login_success' => '登录成功', 'password_error' => '密码错误', 'user_none' => '用户不存在', 'not_null' => '不能为空', + 'code_error' => '验证码错误', + 'format_error' => '格式错误', + 'email_repeat' => '邮箱重复', + 'save_success' => '添加成功', + 'save_fail' => '添加失败', ]; \ No newline at end of file