From 0e3687e697e3a3fa75091534286f0f47b6688121 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Fri, 14 Aug 2020 17:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7unionid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + app/Controller/UserController.php | 31 ++++++++++++++ app/Request/UserUnionidRequest.php | 45 ++++++++++++++++++++ app/Service/UserService.php | 63 ++++++++++++++++++++++++++++ app/Service/UserServiceInterface.php | 18 ++++++++ config/routes.php | 23 ++++++++++ 6 files changed, 181 insertions(+) create mode 100644 app/Controller/UserController.php create mode 100644 app/Request/UserUnionidRequest.php create mode 100644 app/Service/UserService.php create mode 100644 app/Service/UserServiceInterface.php diff --git a/.gitignore b/.gitignore index 9091e68..8088c1b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ vendor/ *.lock .phpunit* /watch +.vscode/settings.json diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php new file mode 100644 index 0000000..d494d83 --- /dev/null +++ b/app/Controller/UserController.php @@ -0,0 +1,31 @@ +request->input('openid',''); + $unionid = $this->request->input('unionid',''); + + return $this->success($this->userService->saveUserUnionid($openid,$unionid)); + } + +} diff --git a/app/Request/UserUnionidRequest.php b/app/Request/UserUnionidRequest.php new file mode 100644 index 0000000..939940f --- /dev/null +++ b/app/Request/UserUnionidRequest.php @@ -0,0 +1,45 @@ + 'required|nonempty', + 'unionid' => 'required|nonempty', + ]; + } + + public function messages(): array + { + return [ + 'openid.*' => ':attribute必须', + 'unionid.*' => ':attribute必须', + ]; + } + + public function attributes(): array + { + return [ + 'openid' => '用户openid', + 'unionid' => '用户unionid', + ]; + } +} diff --git a/app/Service/UserService.php b/app/Service/UserService.php new file mode 100644 index 0000000..87606c1 --- /dev/null +++ b/app/Service/UserService.php @@ -0,0 +1,63 @@ +where(['user_id' => $user_id]) + ->where(function ($query){ + $query->where('state', 'in', [4,5,10]) + ->orWhere('dm_state', 'in', [2,3]); + }) + ->exists(); + + return !$exist; + } + + /** + * 根据用户的openid更新unionid信息 + * 如果没有找到用户,则不做任何处理 + * @param $openid + * @param $unionid + * @return array + */ + public function saveUserUnionid($openid,$unionid) + { + $result = [ + 'status' => false, + 'msg' => '用户不存在或者已存在相同unionid' + ]; + + // 查询用户是否存在 + $userinfo = Users::select('id','unionid')->where('openid',$openid)->first(); + if($userinfo && $userinfo->unionid != $unionid){ + $userinfo->unionid = $unionid; + if($res = $userinfo->save()){ + $result['status'] = true; + $result['msg'] = '更改用户unionid信息成功'; + $result['res'] = $res; + }else{ + $result['msg'] = '更改用户unionid信息失败'; + } + } + + return $result; + } + + +} \ No newline at end of file diff --git a/app/Service/UserServiceInterface.php b/app/Service/UserServiceInterface.php new file mode 100644 index 0000000..06c1ea0 --- /dev/null +++ b/app/Service/UserServiceInterface.php @@ -0,0 +1,18 @@ +