LoginController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenkuan
  5. * Date: 2017/11/7
  6. * Time: 上午11:10
  7. */
  8. namespace backend\controllers;
  9. use backend\helpers\DateTimeHelper;
  10. use backend\helpers\MailHelper;
  11. use backend\helpers\RandomHelper;
  12. use backend\models\Config;
  13. use backend\models\Member;
  14. use backend\models\Signin;
  15. class LoginController extends BaseController
  16. {
  17. /**
  18. * 登录验证方法
  19. * @return \yii\web\Response
  20. */
  21. public function actionLoginPost()
  22. {
  23. $data = \Yii::$app->getRequest()->post();
  24. $checkData = $this->checkLoginInfo($data['username'], $data['password'], $data['type'], $data['ip']);
  25. if ($checkData->data['code'] == 1) {
  26. return $this->outJson(1, $checkData->data['data'], 'LOGIN SUCCESS');
  27. } else {
  28. return $this->outJson(0, false, 'LOGIN FAILED');
  29. }
  30. }
  31. /**
  32. * @title 校验登录信息
  33. * @param string $username
  34. * @param string $password
  35. * @param int $type
  36. * @param string $ip
  37. * @return \yii\web\Response
  38. */
  39. private function checkLoginInfo($username = '', $password = '', $type, $ip)
  40. {
  41. if (empty($username) || empty($password)) {
  42. return $this->outJson(0, [], '用户名或秘密不能为空');
  43. } else {
  44. $member = new Member();
  45. $model = $member->findByUserName($username, $type);
  46. $string = print_r($model,true);
  47. file_put_contents('logininfo.txt',$string);
  48. if ($type == Member::MEMBER_TYPE_IB && $model == null) {
  49. $model = $member->findByIbOldLoginName($username);
  50. }
  51. if (!empty($model) && ($model->password == md5($password))
  52. && intval($model->type) == $type && intval($model->is_enable) == 1
  53. ) {
  54. $signin = new Signin();
  55. $signin->member_id = $model->id;
  56. $signin->ip = $ip;
  57. $signin->in_time = DateTimeHelper::microtime_float();
  58. $signin->save();
  59. $model->ip = $ip;
  60. if ($model->save()) {
  61. return $this->outJson(1, $model->getAttributes(null, ['password']), "LOGIN SUCCESS");
  62. }
  63. } else {
  64. return $this->outJson(0, false, 'LOGIN FAILED');
  65. }
  66. }
  67. }
  68. /**
  69. * 找回密码-修改密码发送验证码
  70. */
  71. public function actionPwd1()
  72. {
  73. $request = \Yii::$app->getRequest()->post();
  74. $username = $request['username'];
  75. $type = isset($request['type']) ? intval($request['type']) : 1;
  76. $member = new Member();
  77. $m = $member->findByUserName($username, $type);
  78. if ($m == null) {
  79. return $this->outJson(0, [], '用户不存在');
  80. }
  81. $code = RandomHelper::getRandomNo(6);
  82. $paramArray = ['code' => $code];
  83. $config = Config::findOne(1);
  84. $t = MailHelper::sendMail("找回密码验证码", $username, $paramArray, 'do.not.reply', $config->mail_code);
  85. if (!$t) {
  86. return $this->outJson(0, [], '邮件发送失败');
  87. }
  88. $m->random_code = $code;
  89. $m->random_code_time = DateTimeHelper::microtime_float();
  90. $rs = $m->save();
  91. if ($rs) {
  92. return $this->outJson(1, [$rs], '操作成功');
  93. }
  94. }
  95. /**
  96. * 找回密码-修改密码
  97. */
  98. public function actionModifyPwd1()
  99. {
  100. $request = \Yii::$app->getRequest()->post();
  101. $username = $request['username'];
  102. $password = $request['password'];
  103. $rePassword = $request['rePassword'];
  104. $code = $request['code'];
  105. $type = isset($request['type']) ? intval($request['type']) : 1;
  106. if (empty($username)) {
  107. return $this->outJson(0, [], '电子邮箱格式错误');
  108. }
  109. if (empty($code)) {
  110. return $this->outJson(0, [], '请输入邮箱验证码');
  111. }
  112. if (empty($password)) {
  113. return $this->outJson(0, [], '请输入新密码');
  114. }
  115. if (empty($rePassword)) {
  116. return $this->outJson(0, [], '请输入重复密码');
  117. }
  118. if ($password != $rePassword) {
  119. return $this->outJson(0, [], '2次密码不一致');
  120. }
  121. $member = new Member();
  122. $m = $member->findByUserName($username, $type);
  123. if ($m == null) {
  124. return $this->outJson(0, [], '用户不存在');
  125. }
  126. if ($m->random_code == null || $m->random_code_time == null) {
  127. return $this->outJson(0, [], '验证码错误');
  128. }
  129. if ($m->random_code != $code) {
  130. return $this->outJson(0, [], '验证码错误');
  131. }
  132. if ($m->random_code_time + 1800000 < DateTimeHelper::microtime_float()) {
  133. return $this->outJson(0, [], '验证码已过期');
  134. }
  135. $m->password = md5($password);
  136. $m->random_code = null;
  137. $m->random_code_time = null;
  138. $rs = $m->save();
  139. if ($rs) {
  140. return $this->outJson(1, [$rs], '修改成功');
  141. }
  142. }
  143. public function actionLoginByIdPassword()
  144. {
  145. $id = trim(\Yii::$app->getRequest()->post('id'));
  146. $password = trim(\Yii::$app->getRequest()->post('password'));
  147. if (empty($id)) {
  148. return $this->outJson(0, [], 'id不能为空');
  149. }
  150. if (empty($password)) {
  151. return $this->outJson(0, [], '密码不能为空');
  152. }
  153. /** @var Member $member */
  154. $member = Member::find()->where(['id' => $id])->limit(1)->one();
  155. if ($member == null) {
  156. return $this->outJson(0, [], '用户不存在');
  157. }
  158. if ($member['password'] !== $password) {
  159. return $this->outJson(0, [], '密码不正确');
  160. }
  161. return $this->outJson(1, $member->getAttributes(null, ['password']), 'OK');
  162. }
  163. }