From 152d150774dc3d680e40e552920d9e0c2f5c0fc9 Mon Sep 17 00:00:00 2001 From: unknown <736250432@qq.com> Date: Mon, 18 Sep 2023 17:04:42 +0800 Subject: [PATCH 1/2] update --- app/adminapi/logic/user/UserLogic.php | 2 +- app/api/controller/IndexController.php | 3 + .../controller/user/UserBalanceController.php | 43 +++++++++++++++ app/api/logic/LoginLogic.php | 7 +++ app/common/model/user/ShopMerchant.php | 11 ++++ app/common/model/user/ShopUser.php | 11 ++++ app/common/model/user/UserBalance.php | 22 ++++++++ .../validate/user/UserBalanceValidate.php | 55 +++++++++++++++++++ config/log.php | 2 +- runtime/.gitignore | 2 - 10 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 app/api/controller/user/UserBalanceController.php create mode 100644 app/common/model/user/ShopMerchant.php create mode 100644 app/common/model/user/ShopUser.php create mode 100644 app/common/model/user/UserBalance.php create mode 100644 app/common/validate/user/UserBalanceValidate.php delete mode 100644 runtime/.gitignore diff --git a/app/adminapi/logic/user/UserLogic.php b/app/adminapi/logic/user/UserLogic.php index 98233090..f8a85dd5 100644 --- a/app/adminapi/logic/user/UserLogic.php +++ b/app/adminapi/logic/user/UserLogic.php @@ -10,7 +10,7 @@ class UserLogic extends BaseLogic //用户详情 public static function detail(int $userId): array { - $field = ['id','user_account','user_nickname','user_avatar','user_realname','user_gender','user_phone','user_status','user_last_login_time','user_last_login_ip','create_time']; + $field = ['id','user_phone','user_avatar','user_name','user_gender','user_age','user_status','user_last_login_time','user_last_login_ip','create_time']; $user = User::field($field)->where(['id' => $userId])->findOrEmpty(); $user['user_gender_text'] = $user->user_gender_text; $user['user_status_text'] = $user->user_status_text; diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index 9e0a216e..1eb5f392 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -4,6 +4,7 @@ namespace app\api\controller; use app\common\enum\notice\NoticeEnum; use app\common\model\user\User; +use think\facade\Log; use think\response\Json; /** @@ -46,6 +47,8 @@ class IndexController extends BaseApiController ]); return $this->success($result[0]); }catch(\Exception $e){ + //记录日志 + Log::error($e->getMessage()); return $this->fail($e->getMessage()); } } diff --git a/app/api/controller/user/UserBalanceController.php b/app/api/controller/user/UserBalanceController.php new file mode 100644 index 00000000..6011972b --- /dev/null +++ b/app/api/controller/user/UserBalanceController.php @@ -0,0 +1,43 @@ +post()->goCheck('add'); + // 添加数据 + try { + $result = UserBalance::create([ + 'user_id' => $params['user_id'], + 'record_id' => $params['record_id'], + 'record_table' => $params['record_table'], + 'amount' => $params['amount'], + 'type' => $params['type'], + 'pay_type' => $params['pay_type'], + 'mark' => $params['mark'], + 'appid' => $this->request->header('appid'), + 'create_time' => time(), + ]); + if(!empty($result->id)){ + return $this->success('添加成功'); + }else{ + return $this->fail('添加失败'); + } + }catch (\Exception $e) { + //记录日志 + Log::error($e->getMessage()); + return $this->fail('系统错误'); + } + } +} \ No newline at end of file diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index a2c500e0..b0913b91 100644 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -8,6 +8,7 @@ use app\common\model\systems\System; use app\common\service\ConfigService; use app\common\model\user\User; use think\facade\Config; +use think\facade\Log; /** * 登录逻辑 @@ -46,6 +47,8 @@ class LoginLogic extends BaseLogic ]); return true; } catch (\Exception $e) { + //记录日志 + Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } @@ -69,6 +72,8 @@ class LoginLogic extends BaseLogic 'token' => $userInfo['token'], ]; } catch (\Exception $e) { + //记录日志 + Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } @@ -100,6 +105,8 @@ class LoginLogic extends BaseLogic 'token' => $userInfo['token'], ]; } catch (\Exception $e) { + //记录日志 + Log::error($e->getMessage()); self::setError($e->getMessage()); return false; } diff --git a/app/common/model/user/ShopMerchant.php b/app/common/model/user/ShopMerchant.php new file mode 100644 index 00000000..b80805ea --- /dev/null +++ b/app/common/model/user/ShopMerchant.php @@ -0,0 +1,11 @@ +'支出',1=>'收入']; + return $type[$data['type']]; + } + + public function getPayTypeTextAttr($value,$data): string + { + $pay_type = [ 0=>'微信支付', 2=>'支付宝支付', 3=>'银行卡支付', 4=>'余额支付', 5=>'其他']; + return $pay_type[$data['pay_type']]; + } +} \ No newline at end of file diff --git a/app/common/validate/user/UserBalanceValidate.php b/app/common/validate/user/UserBalanceValidate.php new file mode 100644 index 00000000..cb257f90 --- /dev/null +++ b/app/common/validate/user/UserBalanceValidate.php @@ -0,0 +1,55 @@ + 'require', + 'user_id' => 'require|checkUser', + 'record_id' => 'require', + 'record_table' => 'require', + 'amount' => 'require|float|gt:0', + 'type' => 'require|in:0,1', + 'pay_type' => 'require|in:0,1,2,3,4,5', + 'mark' => 'require', + ]; + + protected $message = [ + 'id.require' => '缺少数据主键', + 'user_id.require' => '请选择用户', + 'user_id.checkUser' => '用户不存在', + 'record_id.require' => '请填写本地记录id', + 'record_table.require' => '请填写本地记录表名', + 'amount.require' => '请填写金额', + 'amount.float' => '金额数据格式错误', + 'amount.gt' => '金额必须大于零', + 'type.require' => '请选择金额变更类型', + 'type.in' => '金额变更类型值错误', + 'pay_type.require' => '请选择支付方式', + 'pay_type.in' => '支付方式值错误', + 'mark.require' => '请填写金额变更具体详情', + ]; + + public function sceneAdd(): UserBalanceValidate + { + return $this->only(['user_id','record_id','record_table','amount','type','pay_type','mark']); + } + + public function sceneDetail(): UserBalanceValidate + { + return $this->only(['id']); + } + + public function checkUser($uid): bool + { + $userInfo = User::where('id',$uid)->field('id')->findOrEmpty(); + if($userInfo->isEmpty()){ + return false; + } + return true; + } +} \ No newline at end of file diff --git a/config/log.php b/config/log.php index ea24ff9d..6bab73c0 100644 --- a/config/log.php +++ b/config/log.php @@ -25,7 +25,7 @@ return [ // 单文件日志写入 'single' => false, // 独立日志级别 - 'apart_level' => [], + 'apart_level' => ['error','sql'], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录 diff --git a/runtime/.gitignore b/runtime/.gitignore deleted file mode 100644 index c96a04f0..00000000 --- a/runtime/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file -- 2.40.1 From ce3a570a2566fa68ab34333138ab0bc0be7e4919 Mon Sep 17 00:00:00 2001 From: unknown <736250432@qq.com> Date: Mon, 18 Sep 2023 17:51:25 +0800 Subject: [PATCH 2/2] update --- app/api/controller/LoginController.php | 1 - .../http/middleware/VerifySignMiddleware.php | 8 +-- app/api/logic/LoginLogic.php | 68 ++++++++++++------- app/common/cache/UserTokenCache.php | 4 +- .../validate/login/LoginAccountValidate.php | 12 ++-- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/app/api/controller/LoginController.php b/app/api/controller/LoginController.php index 7eaaa5b6..e844b6e1 100644 --- a/app/api/controller/LoginController.php +++ b/app/api/controller/LoginController.php @@ -22,7 +22,6 @@ class LoginController extends BaseApiController public function register(): Json { $params = (new RegisterValidate())->post()->goCheck(); - $params['appid'] = $this->request->header('appid'); $result = LoginLogic::register($params); if (true === $result) { return $this->success('注册成功', [], 1, 1); diff --git a/app/api/http/middleware/VerifySignMiddleware.php b/app/api/http/middleware/VerifySignMiddleware.php index f22ae7f5..d5643fc1 100644 --- a/app/api/http/middleware/VerifySignMiddleware.php +++ b/app/api/http/middleware/VerifySignMiddleware.php @@ -21,18 +21,18 @@ class VerifySignMiddleware return JsonService::fail('缺少请求头参数', [], 0); } //获取子系统信息 - $system = System::field('system_appid,system_appkey,system_url,system_status')->where('system_appid',$appid)->where('system_url',$url)->findOrEmpty(); + $system = System::field('app_id,app_key,url,status')->where('app_id',$appid)->where('url',$url)->findOrEmpty(); if($system->isEmpty()){ return JsonService::fail('应用ID无效', [], 0); } - if($url != $system['system_url']){ + if($url != $system['url']){ return JsonService::fail('请求来源异常', [], 0); } - if($system['system_status'] != 0){ + if($system['status'] != 0){ return JsonService::fail('应用已被禁用或注销', [], 0); } //验证签名 - $checkSign = ApiSignService::verifySign(['appid'=>$appid,'url'=>$url,'timestamp'=>$timestamp,'sign'=>$sign],$system['system_appkey']); + $checkSign = ApiSignService::verifySign(['appid'=>$appid,'url'=>$url,'timestamp'=>$timestamp,'sign'=>$sign],$system['app_key']); if($checkSign['code'] == 0){ return JsonService::fail($checkSign['msg'],[],0); } diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index b0913b91..ee8b5262 100644 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -4,10 +4,11 @@ namespace app\api\logic; use app\common\cache\UserTokenCache; use app\common\logic\BaseLogic; use app\api\service\UserTokenService; -use app\common\model\systems\System; +use app\common\model\auth\Admin; use app\common\service\ConfigService; use app\common\model\user\User; use think\facade\Config; +use think\facade\Db; use think\facade\Log; /** @@ -20,34 +21,49 @@ class LoginLogic extends BaseLogic // 账号密码注册 public static function register(array $params): bool { - //获取子系统信息 - $system = System::field('id')->where('system_appid',$params['appid'])->findOrEmpty(); - if($system->isEmpty()){ - self::setError('应用appid错误'); - return false; - } //创建密码和默认头像 $passwordSalt = Config::get('project.unique_identification'); - $password = create_password($params['password'], $passwordSalt); + $adminPassword = create_password($params['phone'], $passwordSalt); + $userPassword = create_password($params['password'], $passwordSalt); $avatar = env('project.web_domain').'/'.ConfigService::get('default_image', 'user_avatar'); + (new Db)->startTrans(); try { - User::create([ - 'user_phone' => $params['phone'], - 'user_password' => $password, - 'user_avatar' => $avatar, - 'user_age' => $params['age'], - 'user_gender' => $params['gender'], - 'user_province' => $params['province'], - 'user_city' => $params['city'], - 'user_area' => $params['area'], - 'user_street' => $params['street'], - 'user_village' => $params['village'], - 'user_brigade' => $params['brigade'], - 'user_address' => $params['address'], + //添加到管理员表 + $admin = Admin::create([ + 'root' => 0, + 'name' => '用户'.$params['phone'], + 'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'], + 'account' => $params['phone'], + 'password' => $adminPassword, + 'multipoint_login' => 1, + 'disable' => 0 ]); - return true; + //添加到用户表 + $user = User::create([ + 'admin_id' => $admin['id'], + 'phone' => $params['phone'], + 'nickname' => '用户'.$params['phone'], + 'password' => $userPassword, + 'avatar' => empty($params['avatar']) ? $avatar : $params['avatar'], + 'age' => $params['age'], + 'gender' => $params['gender'], + 'province' => $params['province'], + 'city' => $params['city'], + 'area' => $params['area'], + 'street' => $params['street'], + 'village' => $params['village'], + 'brigade' => $params['brigade'], + 'address' => $params['address'], + ]); + if(!empty($admin['id']) && !empty($user['id'])){ + (new Db)->commit(); + return true; + }else{ + (new Db)->rollback(); + return false; + } } catch (\Exception $e) { - //记录日志 + (new Db)->rollback(); Log::error($e->getMessage()); self::setError($e->getMessage()); return false; @@ -58,11 +74,11 @@ class LoginLogic extends BaseLogic public static function login($params): bool|array { try { - $where = ['user_phone' => $params['account']]; + $where = ['phone' => $params['account']]; $user = User::field('id')->where($where)->findOrEmpty(); //更新登录信息 - $user->user_last_login_time = time(); - $user->user_last_login_ip = request()->ip(); + $user->last_login_time = time(); + $user->last_login_ip = request()->ip(); $user->save(); //设置token $userInfo = UserTokenService::setToken($user->id,0); diff --git a/app/common/cache/UserTokenCache.php b/app/common/cache/UserTokenCache.php index 365d5b81..b9882e7d 100644 --- a/app/common/cache/UserTokenCache.php +++ b/app/common/cache/UserTokenCache.php @@ -69,8 +69,8 @@ class UserTokenCache extends BaseCache $user = User::where('id', '=', $userSession->user_id)->find(); $userInfo = [ 'user_id' => $user->id, - 'phone' => $user->user_phone, - 'avatar' => $user->user_avatar, + 'phone' => $user->phone, + 'avatar' => $user->avatar, 'token' => $token, 'terminal' => $userSession->terminal, 'expire_time' => $userSession->expire_time, diff --git a/app/common/validate/login/LoginAccountValidate.php b/app/common/validate/login/LoginAccountValidate.php index 7dba70f2..7e89e644 100644 --- a/app/common/validate/login/LoginAccountValidate.php +++ b/app/common/validate/login/LoginAccountValidate.php @@ -55,19 +55,19 @@ class LoginAccountValidate extends BaseValidate if (!$userAccountSafeCache->isSafe()) { return '密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试'; } - $userInfo = User::where('user_phone',$data['account'])->field('user_password,user_status')->findOrEmpty(); + $userInfo = User::where('phone',$data['account'])->field('password,status')->findOrEmpty(); if ($userInfo->isEmpty()) { return '用户不存在'; } - if ($userInfo['user_status'] != 0) { + if ($userInfo['status'] != 0) { return '用户已冻结或删除'; } - if (empty($userInfo['user_password'])) { + if (empty($userInfo['password'])) { $userAccountSafeCache->record(); return '密码不存在'; } $passwordSalt = Config::get('project.unique_identification'); - if ($userInfo['user_password'] !== create_password($password, $passwordSalt)) { + if ($userInfo['password'] !== create_password($password, $passwordSalt)) { $userAccountSafeCache->record(); return '密码错误'; } @@ -77,11 +77,11 @@ class LoginAccountValidate extends BaseValidate public function checkCode($code, $rule, $data): bool|string { - $userInfo = User::where('user_phone',$data['account'])->field('id,user_status')->findOrEmpty(); + $userInfo = User::where('phone',$data['account'])->field('id,status')->findOrEmpty(); if ($userInfo->isEmpty()) { return '用户不存在'; } - if ($userInfo['user_status'] != 0) { + if ($userInfo['status'] != 0) { return '用户已冻结或删除'; } $smsDriver = new SmsDriver(); -- 2.40.1