SiteController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. public function beforeAction($action): bool
  15. {
  16. $this->checkRequest();
  17. return true;
  18. }
  19. /**
  20. * Displays the login page
  21. */
  22. public function actionLogin()
  23. {
  24. $model=new LoginForm;
  25. if (isset($_POST['username'])) {
  26. $model->attributes=$_POST;
  27. if($model->validate() && $model->login()){
  28. $this->clearAuth();
  29. Helper::ok(['token' => \Yii::app()->session->getSessionID(), 'refreshToken' => '']);
  30. } else {
  31. Helper::error('登入失败');
  32. }
  33. }
  34. Helper::error('参数错误');
  35. }
  36. /**
  37. * 发送验证码
  38. */
  39. public function actionSendCode()
  40. {
  41. $phone = Helper::getPostString('phone', '');
  42. if (!Helper::isPhone($phone)) {
  43. Helper::error('手机号码格式错误');
  44. }
  45. // 验证码发送限制
  46. Helper::dealCommonResult(Helper::limitSmsSend(10, $phone, 5), false);
  47. if (!DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'))) {
  48. Helper::error('该手机号用户不存在');
  49. }
  50. $code = (string)random_int(100000,999999);
  51. RedisInstance::getInstance()->set('user_code:'.$phone, $code, 600);
  52. // 发送短信
  53. Helper::dealCommonResult(SMS::getInstance()->send($phone, '2094847', [$code]));
  54. }
  55. /**
  56. * 找回密码
  57. */
  58. public function actionSetPassword()
  59. {
  60. $phone = Helper::getPostString('phone');
  61. $code = Helper::getPostString('code');
  62. $password = Helper::getPostString('password');
  63. if (!Helper::isPhone($phone)) {
  64. Helper::error('手机号码格式错误');
  65. }
  66. if (!$code || !$password) {
  67. Helper::error('参数错误');
  68. }
  69. if (RedisInstance::getInstance()->get('user_code:'.$phone) != $code) {
  70. Helper::error('验证码错误');
  71. }
  72. $id = DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'));
  73. if (!$id) {
  74. Helper::error('该手机号用户不存在');
  75. }
  76. DB::updateById('useradmin', ['password' => md5($password)], $id);
  77. Helper::ok();
  78. }
  79. /******************************* 测试相关代码 ***************************************/
  80. public function actionPhp()
  81. {
  82. (new DBTable(Helper::getGetString('t1')))->echoEditPhp();
  83. }
  84. public function actionTs()
  85. {
  86. echo (new DBTable(Helper::getGetString('t1')))->getTsInterFace();
  87. }
  88. public function actionForm()
  89. {
  90. (new DBTable(Helper::getGetString('t1')))->editVue();
  91. }
  92. public function actionTable()
  93. {
  94. echo (new DBTable(Helper::getGetString('t1')))->getTableHtml();
  95. }
  96. public function actionInfo()
  97. {
  98. echo (new DBTable(Helper::getGetString('t1')))->getDetailHtml();
  99. }
  100. public function actionDefault()
  101. {
  102. $data = 'id: number
  103. name: string // 名称
  104. distinct: string[] // 地区,
  105. canteens: number[] // 地区,
  106. province: string // 省
  107. city: string // 市
  108. area: string // 区
  109. address: string // 详细地址
  110. bind_user_id?: number // 负责人
  111. memo: string // 备注';
  112. $data = explode("\n", $data);
  113. $data = array_map(function ($item) {
  114. $item = trim($item);
  115. $item = explode(':', $item);
  116. $name = trim($item[0], '? ');
  117. $type = trim(explode('//', $item[1])[0]);
  118. $value = "''";
  119. switch ($type) {
  120. case '0|1':
  121. case 'number':
  122. $value = 0;
  123. break;
  124. case 'string[]':
  125. case 'number[]':
  126. $value = '[]';
  127. break;
  128. }
  129. echo "{$name}: {$value}, <br/>";
  130. }, $data);
  131. }
  132. }