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 @@ +