SiteController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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);
  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. $userName = Helper::getPostString('username', '');
  25. $password = Helper::getPostString('password', '');
  26. if (!($userName && $password)) {
  27. Helper::error('参数错误');
  28. }
  29. $user = DB::getInfoWithCriteria('useradmin', DbCriteria::simpleCompare(['username' => $userName, 'status' => 1])->setSelect('id, password'));
  30. if (!$user || $user['password'] != md5($password)) {
  31. Helper::error('登入失败');
  32. }
  33. $token = Helper::getRandomString(32);
  34. RedisInstance::getInstance()->set('user_token:'.$token, ['id' => $user['id']], 86400);
  35. $this->clearAuth($user['id']);
  36. Helper::ok(['token' => $token, 'refreshToken' => '']);
  37. }
  38. /**
  39. * 发送验证码
  40. */
  41. public function actionSendCode()
  42. {
  43. $phone = Helper::getPostString('phone', '');
  44. if (!Helper::isPhone($phone)) {
  45. Helper::error('手机号码格式错误');
  46. }
  47. // 验证码发送限制
  48. Helper::dealCommonResult(Helper::limitSmsSend(10, $phone, 5), false);
  49. if (!DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'))) {
  50. Helper::error('该手机号用户不存在');
  51. }
  52. $code = (string)random_int(100000,999999);
  53. RedisInstance::getInstance()->set('user_code:'.$phone, $code, 600);
  54. // 发送短信
  55. Helper::dealCommonResult(SMS::getInstance()->send($phone, '2094847', [$code]));
  56. }
  57. /**
  58. * 找回密码
  59. */
  60. public function actionSetPassword()
  61. {
  62. $phone = Helper::getPostString('phone');
  63. $code = Helper::getPostString('code');
  64. $password = Helper::getPostString('password');
  65. if (!Helper::isPhone($phone)) {
  66. Helper::error('手机号码格式错误');
  67. }
  68. if (!$code || !$password) {
  69. Helper::error('参数错误');
  70. }
  71. if (RedisInstance::getInstance()->get('user_code:'.$phone) != $code) {
  72. Helper::error('验证码错误');
  73. }
  74. $id = DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['phone' => $phone])->setSelect('id'));
  75. if (!$id) {
  76. Helper::error('该手机号用户不存在');
  77. }
  78. DB::updateById('useradmin', ['password' => md5($password)], $id);
  79. Helper::ok();
  80. }
  81. /******************************* 测试相关代码 ***************************************/
  82. /*public function actionPhp()
  83. {
  84. (new DBTable(Helper::getGetString('t1')))->echoEditPhp();
  85. }
  86. public function actionTs()
  87. {
  88. echo (new DBTable(Helper::getGetString('t1')))->getTsInterFace();
  89. }
  90. public function actionForm()
  91. {
  92. (new DBTable(Helper::getGetString('t1')))->editVue();
  93. }
  94. public function actionTable()
  95. {
  96. echo (new DBTable(Helper::getGetString('t1')))->getTableHtml();
  97. }
  98. public function actionInfo()
  99. {
  100. echo (new DBTable(Helper::getGetString('t1')))->getDetailHtml();
  101. }
  102. public function actionJson()
  103. {
  104. $newData = [];
  105. $data = file_get_contents(PROJECT_PATH . '/protected/runtime/city.json');
  106. foreach (json_decode($data, true) as $province) {
  107. foreach ($province['citys'] as $city) {
  108. $newData[] = $city['city'];
  109. }
  110. }
  111. echo json_encode($newData, JSON_UNESCAPED_UNICODE);
  112. \CVarDumper::dump(count($newData), 6, 1);die;
  113. }
  114. public function actionDefault()
  115. {
  116. $data = 'id: number
  117. name: string // 名称
  118. distinct: string[] // 地区,
  119. canteens: number[] // 地区,
  120. province: string // 省
  121. city: string // 市
  122. area: string // 区
  123. address: string // 详细地址
  124. bind_user_id?: number // 负责人
  125. memo: string // 备注';
  126. $data = explode("\n", $data);
  127. $data = array_map(function ($item) {
  128. $item = trim($item);
  129. $item = explode(':', $item);
  130. $name = trim($item[0], '? ');
  131. $type = trim(explode('//', $item[1])[0]);
  132. $value = "''";
  133. switch ($type) {
  134. case '0|1':
  135. case 'number':
  136. $value = 0;
  137. break;
  138. case 'string[]':
  139. case 'number[]':
  140. $value = '[]';
  141. break;
  142. }
  143. echo "{$name}: {$value}, <br/>";
  144. }, $data);
  145. }*/
  146. }