Auth.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\openapi;
  12. use crmeb\basic\BaseController;
  13. use crmeb\exceptions\AuthException;
  14. use crmeb\services\JwtTokenService;
  15. use Firebase\JWT\JWT;
  16. use think\exception\ValidateException;
  17. use think\facade\Cache;
  18. use think\facade\Config;
  19. /**
  20. * Class Auth
  21. * @package app\controller\api
  22. * @author xaboy
  23. * @day 2020-05-06
  24. */
  25. class Auth extends BaseController
  26. {
  27. /**
  28. * 对外接口获取token
  29. * @return array
  30. * @author Qinii
  31. * @day 2023/8/14
  32. */
  33. public function auth()
  34. {
  35. $auth = $this->request->openAuthInfo();
  36. $service = new JwtTokenService();
  37. $valid_exp = intval(Config::get('admin.openapi_token_valid_exp', 15));
  38. $exp = strtotime("+ {$valid_exp}hour");
  39. $token = $service->createToken($auth->id, 'openapi', $exp);
  40. Cache::store('file')->set('openapi_' . $token['token'], time() + $token['out'], $token['out']);
  41. return app('json')->success(['token' => $token['token'],'exp' => $token['out']]);
  42. }
  43. }