LoginController.php 6.0 KB

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