SiteController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * 这个方法不会检测登入和权限,如果需要登入后操作的请放到 CommonController
  4. */
  5. class SiteController extends Controller
  6. {
  7. /**
  8. * This is the action to handle external exceptions.
  9. */
  10. public function actionError()
  11. {
  12. Helper::error('系统错误', 500, Yii::app()->errorHandler->error);
  13. }
  14. /**
  15. * Displays the login page
  16. */
  17. public function actionLogin()
  18. {
  19. $model=new LoginForm;
  20. if (isset($_POST['username'])) {
  21. $model->attributes=$_POST;
  22. if($model->validate() && $model->login()){
  23. Helper::ok(['token' => \Yii::app()->session->getSessionID(), 'refreshToken' => '']);
  24. } else {
  25. Helper::error('登入失败');
  26. }
  27. }
  28. Helper::error('参数错误');
  29. }
  30. /**
  31. * 发送验证码
  32. */
  33. public function actionSendCode()
  34. {
  35. $phone = Helper::getPostString('phone', '');
  36. if (!Helper::isPhone($phone)) {
  37. Helper::error('手机号码格式错误');
  38. }
  39. // 验证码发送限制
  40. Helper::dealCommonResult(Helper::limitSmsSend(10, $phone, 5), false);
  41. if (!DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'))) {
  42. Helper::error('该手机号用户不存在');
  43. }
  44. $code = (string)random_int(100000,999999);
  45. RedisInstance::getInstance()->set('user_code:'.$phone, $code, 600);
  46. // 发送短信
  47. Helper::dealCommonResult(SMS::getInstance()->send($phone, '2094847', [$code]));
  48. }
  49. /**
  50. * 找回密码
  51. */
  52. public function actionSetPassword()
  53. {
  54. $phone = Helper::getPostString('phone');
  55. $code = Helper::getPostString('code');
  56. $password = Helper::getPostString('password');
  57. if (!Helper::isPhone($phone)) {
  58. Helper::error('手机号码格式错误');
  59. }
  60. if (!$code || !$password) {
  61. Helper::error('参数错误');
  62. }
  63. if (RedisInstance::getInstance()->get('user_code:'.$phone) != $code) {
  64. Helper::error('验证码错误');
  65. }
  66. $id = DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'));
  67. if (!$id) {
  68. Helper::error('该手机号用户不存在');
  69. }
  70. DB::updateById('useradmin', ['password' => md5($password)], $id);
  71. Helper::ok();
  72. }
  73. }