AuthController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = (int)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. // public function actionTest($args) {
  82. // $appId = 'XJQ2Qc24bMJnAZWg9p43JUsH';
  83. // $secret = 'uQhF3M8t3MkNKG2g';
  84. //
  85. // $ts = time();
  86. // $sign = AppInfo::doSign($appId, $secret, $ts);
  87. //
  88. // return compact('appId', 'ts', 'sign');
  89. // }
  90. }