AuthController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by IntelliJ IDEA.
  4. * User: solu
  5. * Date: 2019/3/7
  6. * Time: 11:07 AM
  7. */
  8. class AuthController extends BaseController {
  9. protected $ajaxLoginActions = [
  10. 'getAccessToken',
  11. ];
  12. public function __construct()
  13. {
  14. parent::__construct(true);
  15. }
  16. /**
  17. * 授权页面
  18. * @author solu
  19. * @param $args
  20. */
  21. public function actionLogin($args) {
  22. $this->tpl->display('');
  23. }
  24. /**
  25. * 获取access_token
  26. * @author solu
  27. * @param $args
  28. * @return array
  29. */
  30. public function actionGetAccessToken($args) {
  31. $rules = [
  32. 'app_id' => ['string', 'desc' => 'appid'],
  33. 'ts' => ['int', 'desc' => '发起请求时间戳'],
  34. 'sign' => ['string', 'desc' => '签名'],
  35. ];
  36. Param::checkParam2($rules, $args);
  37. $user_id = User::getUserId();
  38. try {
  39. (new AppInfo())->verify($args['app_id'], $args['ts'], $args['sign']);
  40. } catch (Exception $e) {
  41. Response::error($e->getCode(), $e->getMessage());
  42. }
  43. $access_token = AppInfo::genAccessToken($args['app_id'], $user_id);
  44. $ttl = AppInfo::ACCESS_TOKEN_TTL;
  45. return compact('user_id', 'access_token', 'ttl');
  46. }
  47. /**
  48. * 校验access_token有效性
  49. * @author solu
  50. * @param $args
  51. * @return array
  52. */
  53. public function actionCheckAccessToken($args) {
  54. $rules = [
  55. 'app_id' => ['string', 'desc' => 'appid'],
  56. 'user_id' => ['int', 'desc' => '用户id'],
  57. 'access_token' => ['string', 'desc' => 'access_token'],
  58. ];
  59. Param::checkParam2($rules, $args);
  60. $valid = AppInfo::checkAccessToken($args['app_id'], $args['user_id'], $args['access_token']);
  61. return compact('valid');
  62. }
  63. /**
  64. * 用户信息
  65. * @author solu
  66. * @param $args
  67. * @return array
  68. */
  69. public function actionGetUserInfo($args) {
  70. $rules = [
  71. 'app_id' => ['string', 'desc' => 'appid'],
  72. 'user_id' => ['int', 'desc' => '用户id'],
  73. 'access_token' => ['string', 'desc' => 'access_token'],
  74. ];
  75. Param::checkParam2($rules, $args);
  76. if (!AppInfo::checkAccessToken($args['app_id'], $args['user_id'], $args['access_token'])) {
  77. Response::error(CODE_PARAM_ERROR, 'access token timeout');
  78. }
  79. return User::getUserInfo($args['user_id'], 0, 0);
  80. }
  81. }