Common.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Captcha;
  4. use ba\ClickCaptcha;
  5. use ba\Random;
  6. use app\common\facade\Token;
  7. use app\common\controller\Api;
  8. class Common extends Api
  9. {
  10. public function captcha()
  11. {
  12. $captchaId = $this->request->request('id');
  13. $config = array(
  14. 'codeSet' => '123456789', // 验证码字符集合
  15. 'fontSize' => 22, // 验证码字体大小(px)
  16. 'useCurve' => false, // 是否画混淆曲线
  17. 'useNoise' => true, // 是否添加杂点
  18. 'length' => 4, // 验证码位数
  19. 'bg' => array(255, 255, 255), // 背景颜色
  20. );
  21. $captcha = new Captcha($config);
  22. return $captcha->entry($captchaId);
  23. }
  24. public function clickCaptcha()
  25. {
  26. $id = $this->request->request('id/s');
  27. $captcha = new ClickCaptcha();
  28. $this->success('', $captcha->creat($id));
  29. }
  30. public function checkClickCaptcha()
  31. {
  32. $id = $this->request->post('id/s');
  33. $info = $this->request->post('info/s');
  34. $unset = $this->request->post('unset/b', false);
  35. $captcha = new ClickCaptcha();
  36. if ($captcha->check($id, $info, $unset)) $this->success();
  37. $this->error();
  38. }
  39. public function refreshToken()
  40. {
  41. $refreshToken = $this->request->post('refresh_token');
  42. $refreshToken = Token::get($refreshToken, false);
  43. if (!$refreshToken || $refreshToken['expiretime'] < time()) {
  44. $this->error(__('Login expired, please login again.'));
  45. }
  46. $newToken = Random::uuid();
  47. if ($refreshToken['type'] == 'admin-refresh') {
  48. $baToken = $this->request->server('HTTP_BATOKEN', $this->request->request('batoken', ''));
  49. if (!$baToken) {
  50. $this->error(__('Invalid token'));
  51. }
  52. Token::delete($baToken);
  53. Token::set($newToken, 'admin', $refreshToken['user_id'], 86400);
  54. } elseif ($refreshToken['type'] == 'user-refresh') {
  55. $baUserToken = $this->request->server('HTTP_BA_USER_TOKEN', $this->request->request('ba-user-token', ''));
  56. if (!$baUserToken) {
  57. $this->error(__('Invalid token'));
  58. }
  59. Token::delete($baUserToken);
  60. Token::set($newToken, 'user', $refreshToken['user_id'], 86400);
  61. }
  62. $this->success('', [
  63. 'type' => $refreshToken['type'],
  64. 'token' => $newToken
  65. ]);
  66. }
  67. }