Login.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace addons\cms\controller\api;
  3. use app\common\library\Sms;
  4. use app\common\library\Ems;
  5. use fast\Random;
  6. use think\Validate;
  7. use fast\Http;
  8. use addons\third\model\Third;
  9. use addons\third\library\Service;
  10. use app\common\library\Auth;
  11. use think\Config;
  12. use think\Session;
  13. class Login extends Base
  14. {
  15. protected $noNeedLogin = ['*'];
  16. /**
  17. * 会员登录
  18. *
  19. * @param string $account 账号
  20. * @param string $password 密码
  21. */
  22. public function login()
  23. {
  24. $account = $this->request->post('account', '');
  25. $password = $this->request->post('password', '');
  26. if (!$account || !$password) {
  27. $this->error(__('Invalid parameters'));
  28. }
  29. $ret = $this->auth->login($account, $password);
  30. if ($ret) {
  31. $this->success(__('Logged in successful'), [
  32. 'token' => $this->auth->getToken(),
  33. 'user_id' => $this->auth->id
  34. ]);
  35. } else {
  36. $this->error($this->auth->getError());
  37. }
  38. }
  39. /**
  40. * 重置密码
  41. *
  42. * @param string $mobile 手机号
  43. * @param string $newpassword 新密码
  44. * @param string $captcha 验证码
  45. */
  46. public function resetpwd()
  47. {
  48. if(!$this->request->isPost()){
  49. $this->error('请求错误');
  50. }
  51. $type = $this->request->param("type");
  52. $mobile = $this->request->param("mobile");
  53. $email = $this->request->param("email");
  54. $newpassword = $this->request->param("newpassword");
  55. $captcha = $this->request->param("captcha");
  56. if (!$newpassword || !$captcha) {
  57. $this->error(__('Invalid parameters'));
  58. }
  59. if ($type == 'mobile') {
  60. if (!Validate::regex($mobile, "^1\d{10}$")) {
  61. $this->error(__('Mobile is incorrect'));
  62. }
  63. $user = \app\common\model\User::getByMobile($mobile);
  64. if (!$user) {
  65. $this->error(__('User not found'));
  66. }
  67. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  68. if (!$ret) {
  69. $this->error(__('Captcha is incorrect'));
  70. }
  71. Sms::flush($mobile, 'resetpwd');
  72. } else {
  73. if (!Validate::is($email, "email")) {
  74. $this->error(__('Email is incorrect'));
  75. }
  76. $user = \app\common\model\User::getByEmail($email);
  77. if (!$user) {
  78. $this->error(__('User not found'));
  79. }
  80. $ret = Ems::check($email, $captcha, 'resetpwd');
  81. if (!$ret) {
  82. $this->error(__('Captcha is incorrect'));
  83. }
  84. Ems::flush($email, 'resetpwd');
  85. }
  86. //模拟一次登录
  87. $this->auth->direct($user->id);
  88. $ret = $this->auth->changepwd($newpassword, '', true);
  89. if ($ret) {
  90. $this->success(__('Reset password successful'));
  91. } else {
  92. $this->error($this->auth->getError());
  93. }
  94. }
  95. /**
  96. * 手机验证码登录
  97. *
  98. * @param string $mobile 手机号
  99. * @param string $captcha 验证码
  100. */
  101. public function mobilelogin()
  102. {
  103. $mobile = $this->request->post('mobile');
  104. $captcha = $this->request->post('captcha');
  105. if (!$mobile || !$captcha) {
  106. $this->error(__('Invalid parameters'));
  107. }
  108. if (!Validate::regex($mobile, "^1\d{10}$")) {
  109. $this->error(__('Mobile is incorrect'));
  110. }
  111. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  112. // $this->error(__('Captcha is incorrect'));
  113. }
  114. $user = \app\common\model\User::getByMobile($mobile);
  115. if ($user) {
  116. if ($user->status != 'normal') {
  117. $this->error(__('Account is locked'));
  118. }
  119. //如果已经有账号则直接登录
  120. $ret = $this->auth->direct($user->id);
  121. } else {
  122. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  123. }
  124. if ($ret) {
  125. Sms::flush($mobile, 'mobilelogin');
  126. $data = ['token' => $this->auth->getToken()];
  127. $this->success(__('Logged in successful'), $data);
  128. } else {
  129. $this->error($this->auth->getError());
  130. }
  131. }
  132. /**
  133. * 注册会员
  134. *
  135. * @param string $username 用户名
  136. * @param string $password 密码
  137. * @param string $email 邮箱
  138. * @param string $mobile 手机号
  139. * @param string $code 验证码
  140. */
  141. public function register()
  142. {
  143. if(!$this->request->isPost()){
  144. $this->error('请求错误');
  145. }
  146. $username = $this->request->param('username');
  147. $password = $this->request->param('password');
  148. $mobile = $this->request->param('mobile');
  149. $code = $this->request->param('code');
  150. if (!$username || !$password) {
  151. $this->error(__('Invalid parameters'));
  152. }
  153. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  154. $this->error(__('Mobile is incorrect'));
  155. }
  156. $ret = Sms::check($mobile, $code, 'register');
  157. if (!$ret) {
  158. $this->error(__('Captcha is incorrect'));
  159. }
  160. $ret = $this->auth->register($username, $password, '', $mobile, []);
  161. if ($ret) {
  162. $this->success(__('Sign up successful'), [
  163. 'token' => $this->auth->getToken(),
  164. 'user_id' => $this->auth->id
  165. ]);
  166. } else {
  167. $this->error($this->auth->getError());
  168. }
  169. }
  170. /**
  171. * Undocumented function
  172. * 第三方登录[绑定] 小程序
  173. * @return void
  174. */
  175. public function wxLogin()
  176. {
  177. $code = $this->request->post("code");
  178. $WxUser = $this->request->post("rawData/a", '', 'trim');
  179. if (!$code || !$WxUser) {
  180. $this->error("参数不正确");
  181. }
  182. $third = get_addon_info('third');
  183. if (!$third || !$third['state']) {
  184. $this->error("请在后台插件管理安装并配置第三方登录插件");
  185. }
  186. $params = [
  187. 'appid' => Config::get('cms.wxappid'),
  188. 'secret' => Config::get('cms.wxappsecret'),
  189. 'js_code' => $code,
  190. 'grant_type' => 'authorization_code'
  191. ];
  192. $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
  193. if ($result['ret']) {
  194. $json = (array)json_decode($result['msg'], true);
  195. if (isset($json['openid'])) {
  196. $userinfo = [
  197. 'platform' => 'wechat',
  198. 'apptype' => 'miniapp',
  199. 'openid' => $json['openid'],
  200. 'userinfo' => [
  201. 'nickname' => $WxUser['nickName'],
  202. 'avatar' => $WxUser['avatarUrl']
  203. ],
  204. 'openname' => $WxUser['nickName'],
  205. 'access_token' => $json['session_key'],
  206. 'refresh_token' => '',
  207. 'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
  208. 'unionid' => isset($json['unionid']) ? $json['unionid'] : ''
  209. ];
  210. $third = [
  211. 'nickname' => $WxUser['nickName'],
  212. 'avatar' => $WxUser['avatarUrl']
  213. ];
  214. $user = null;
  215. if ($this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid'])) {
  216. Service::connect($userinfo['platform'], $userinfo);
  217. $user = $this->auth->getUserinfo();
  218. } else {
  219. Session::set('third-userinfo', $userinfo);
  220. }
  221. $this->success('授权成功!', ['user' => $user, 'third' => $third]);
  222. }
  223. }
  224. $this->error("授权失败,".$result['msg']);
  225. }
  226. /**
  227. * app登录
  228. * @return void
  229. */
  230. public function appLogin()
  231. {
  232. $code = $this->request->post("code");
  233. $scope = $this->request->post("scope");
  234. if (!$code) {
  235. $this->error("参数不正确");
  236. }
  237. $third = get_addon_info('third');
  238. if (!$third || !$third['state']) {
  239. $this->error("请在后台插件管理安装并配置第三方登录插件");
  240. }
  241. Session::set('state', $code);
  242. $config = [
  243. 'app_id' => Config::get('cms.app_id'),
  244. 'app_secret' => Config::get('cms.app_secret'),
  245. 'scope' => $scope
  246. ];
  247. $wechat = new \addons\third\library\Wechat($config);
  248. $userinfo = $wechat->getUserInfo(['code' => $code, 'state' => $code]);
  249. if (!$userinfo) {
  250. $this->error(__('操作失败'));
  251. }
  252. //判断是否需要绑定
  253. $userinfo['apptype'] = 'native';
  254. $userinfo['platform'] = 'wechat';
  255. $third = [
  256. 'avatar' => $userinfo['userinfo']['avatar'],
  257. 'nickname' => $userinfo['userinfo']['nickname']
  258. ];
  259. $user = null;
  260. if ($this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid'])) {
  261. Service::connect($userinfo['platform'], $userinfo);
  262. $user = $this->auth->getUserinfo();
  263. } else {
  264. Session::set('third-userinfo', $userinfo);
  265. }
  266. $this->success('授权成功!', ['user' => $user, 'third' => $third]);
  267. }
  268. /**
  269. * Undocumented function
  270. * 微信授权手机号码登录
  271. * @return void
  272. */
  273. public function getWechatMobile()
  274. {
  275. $encryptedData = $this->request->post('encryptedData');
  276. $iv = $this->request->post('iv');
  277. $code = $this->request->post("code");
  278. if (!$encryptedData || !$iv) {
  279. $this->error('参数错误!');
  280. }
  281. if (strlen($iv) != 24) {
  282. $this->error('iv不正确!');
  283. }
  284. $third = Session::get('third-userinfo');
  285. $params = [
  286. 'appid' => Config::get('cms.wxappid'),
  287. 'secret' => Config::get('cms.wxappsecret'),
  288. 'js_code' => $code,
  289. 'grant_type' => 'authorization_code'
  290. ];
  291. $result = Http::sendRequest("https://api.weixin.qq.com/sns/jscode2session", $params, 'GET');
  292. if ($result['ret']) {
  293. $json = (array)json_decode($result['msg'], true);
  294. $aesIV = base64_decode($iv);
  295. $aesCipher = base64_decode($encryptedData);
  296. $aesKey = base64_decode($json['session_key']);
  297. $re = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  298. $data = json_decode($re, true);
  299. if ($data) {
  300. $user = \app\common\model\User::getByMobile($data['phoneNumber']);
  301. if ($user) {
  302. if ($user->status != 'normal') {
  303. $this->error(__('Account is locked'));
  304. }
  305. //如果已经有账号则直接登录
  306. $ret = $this->auth->direct($user->id);
  307. } else {
  308. $ret = $this->auth->register($data['phoneNumber'], Random::alnum(), '', $data['phoneNumber'], []);
  309. }
  310. if ($ret) {
  311. //绑定第三方
  312. Service::connect('wechat', $third);
  313. $data = ['token' => $this->auth->getToken()];
  314. $this->success(__('Logged in successful'), $data);
  315. } else {
  316. $this->error($this->auth->getError());
  317. }
  318. } else {
  319. $this->error("配置参数错误");
  320. }
  321. } else {
  322. $this->error("授权失败");
  323. }
  324. }
  325. }