Login.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\system\GrantVoucher;
  4. use app\api\model\Channel;
  5. use app\api\service\UserService;
  6. use app\api\service\WxService;
  7. use app\api\validate\BaseApiValidate;
  8. use app\api\validate\LoginValidate;
  9. use app\common\controller\Api;
  10. use app\common\library\token\driver\Redis;
  11. use redis\RedisClient;
  12. use redis\RedLock;
  13. /**
  14. * Module 登录模块
  15. * Class Login
  16. * @package app\api\controller
  17. */
  18. class Login extends Api
  19. {
  20. protected $noNeedLogin = ["user", "wxAppLogin", "wxWebLogin"];
  21. /**
  22. * 用户登录
  23. */
  24. public function user()
  25. {
  26. $params = (new LoginValidate())->checkBody();
  27. $check = \app\common\library\Sms::check($params["username"], $params["sms_code"] ?? "1234", "user_login");
  28. if (!$check)
  29. $this->error("短信验证码不正确!");
  30. $userModel = new \app\api\model\User();
  31. $user = $userModel->where("mobile", $params["username"])->find();
  32. $parent_id = isset($params["parent_id"]) ? $params["parent_id"] : null;
  33. if ($parent_id > 0) {
  34. $parent = $userModel->get($parent_id);
  35. if (is_null($parent))
  36. $this->error("推荐人不存在!");
  37. }
  38. $invite_store_id = isset($params["invite_store_id"]) ? $params["invite_store_id"] : null;
  39. if (is_null($user)) {
  40. $params['password'] = '123456';
  41. $result = $this->auth->register("U{$params["username"]}", $params['password'], null, $params["username"], [
  42. "parent_id" => $parent_id,
  43. "invite_store_id" => $invite_store_id,
  44. ]);
  45. if (!$result)
  46. $this->error("手机号码数据库中已经存在!");
  47. $login = $this->auth->login($params["username"], $params['password']);
  48. if (!$login)
  49. $this->error("账号或者密码错误!");
  50. $user = $this->auth->getUser();
  51. GrantVoucher::grant_voucher("register", $user->id);
  52. if (!is_null($parent_id)) {
  53. GrantVoucher::grant_voucher("invite", $parent_id);
  54. }
  55. }
  56. $login = $this->auth->direct($user->id);
  57. if (!$login)
  58. $this->error("账号或者密码错误!");
  59. $user = \app\api\model\User::fmtUser($user);
  60. $user['token'] = $this->auth->getToken();
  61. if ($invite_store_id) {
  62. RedisClient::of()->set("scan:qr:codes:{$user["id"]}", $invite_store_id, 60 * 10);
  63. }
  64. $this->success($user);
  65. }
  66. /**
  67. * 微信APP登录
  68. */
  69. public function wxAppLogin()
  70. {
  71. $params = (new BaseApiValidate([
  72. "openId" => "require",
  73. "unionId" => "require",
  74. "nickName" => "require",
  75. "avatarUrl" => "require",
  76. "parent_id" => "number",
  77. ]))->checkBody();
  78. $userModel = new \app\api\model\User();
  79. $parent_id = isset($params["parent_id"]) ? $params["parent_id"] : null;
  80. if ($parent_id > 0) {
  81. $parent = $userModel->get($parent_id);
  82. if (is_null($parent))
  83. $this->error("推荐人不存在!");
  84. }
  85. $invite_store_id = isset($params["invite_store_id"]) ? $params["invite_store_id"] : null;
  86. $existUser = $userModel->findByUnionId($params["unionId"]);
  87. if (!$existUser) {
  88. $username = "U" . time();
  89. $result = $this->auth->register($username, "123456", null, "", [
  90. "app_openid" => $params["openId"],
  91. "union_id" => $params["unionId"],
  92. "group_id" => 1,
  93. "avatar" => $params["avatarUrl"],
  94. "parent_id" => $parent_id,
  95. "invite_store_id" => $invite_store_id,
  96. ], $params["nickName"]);
  97. if (!$result)
  98. $this->error("手机号码数据库中已经存在!");
  99. $login = $this->auth->login($username, "123456");
  100. if (!$login)
  101. $this->error("账号或者密码错误!");
  102. $user = $this->auth->getUser();
  103. GrantVoucher::grant_voucher("register", $user->id);
  104. if (!is_null($parent_id)) {
  105. GrantVoucher::grant_voucher("invite", $parent_id);
  106. }
  107. } else {
  108. $userModel->update([
  109. "app_openid" => $params["openId"],
  110. "avatar" => $params["avatarUrl"],
  111. "nickname" => $params["nickName"]
  112. ], ["id" => $existUser["id"]]);
  113. $user = $userModel->get($existUser["id"]);
  114. }
  115. $this->auth->direct($user->id);
  116. $user = \app\api\model\User::fmtUser($user);
  117. $user['token'] = $this->auth->getToken();
  118. if ($invite_store_id) {
  119. RedisClient::of()->set("scan:qr:codes:{$user["id"]}", $invite_store_id, 60 * 10);
  120. }
  121. $this->success($user);
  122. }
  123. /**
  124. * 微信Web登录
  125. */
  126. public function wxWebLogin()
  127. {
  128. $params = (new BaseApiValidate([
  129. "code" => "require"
  130. ]))->checkBody();
  131. $r = (new WxService())->wxLoginByWeb($params["code"]);
  132. if (0 === $r->code())
  133. $this->error($r->msg());
  134. $userinfo = $r->data();
  135. if (!isset($userinfo["unionid"]) || !isset($userinfo["openid"]))
  136. $this->error("微信登录错误! unionid|openid 不能为空!");
  137. $userModel = new \app\api\model\User();
  138. $parent_id = isset($params["parent_id"]) ? $params["parent_id"] : null;
  139. if ($parent_id > 0) {
  140. $parent = $userModel->get($parent_id);
  141. if (is_null($parent))
  142. $this->error("推荐人不存在!");
  143. }
  144. $invite_store_id = isset($params["invite_store_id"]) ? $params["invite_store_id"] : null;
  145. $existUser = $userModel->findByUnionId($userinfo["unionid"]);
  146. if (!$existUser) {
  147. $username = "U" . time();
  148. $result = $this->auth->register($username, "123456", null, "", [
  149. "web_openid" => $userinfo["openid"],
  150. "union_id" => $userinfo["unionid"],
  151. "group_id" => 1,
  152. "avatar" => $userinfo["headimgurl"],
  153. "parent_id" => $parent_id,
  154. "invite_store_id" => $invite_store_id
  155. ], $userinfo["nickname"]);
  156. if (!$result)
  157. $this->error("手机号码数据库中已经存在!");
  158. $login = $this->auth->login($username, "123456");
  159. if (!$login)
  160. $this->error("账号或者密码错误!");
  161. $user = $this->auth->getUser();
  162. GrantVoucher::grant_voucher("register", $user->id);
  163. if (!is_null($parent_id)) {
  164. GrantVoucher::grant_voucher("invite", $parent_id);
  165. }
  166. } else {
  167. $userModel->update([
  168. "web_openid" => $userinfo["openid"],
  169. "avatar" => $userinfo["headimgurl"],
  170. "nickname" => $userinfo["nickname"]
  171. ], ["id" => $existUser["id"]]);
  172. $user = $userModel->get($existUser["id"]);
  173. }
  174. $this->auth->direct($user["id"]);
  175. $user = \app\api\model\User::fmtUser($user);
  176. $user['token'] = $this->auth->getToken();
  177. if ($invite_store_id) {
  178. RedisClient::of()->set("scan:qr:codes:{$user["id"]}", $invite_store_id, 60 * 10);
  179. }
  180. $this->success($user);
  181. }
  182. }