Oauth.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php namespace Api\Api;
  2. use Dever;
  3. use Dever\Helper\Secure;
  4. class Oauth
  5. {
  6. public function __construct()
  7. {
  8. $this->t = Dever::input('t', 'is_string', 't');
  9. $this->account = Dever::input('account', 'is_string', '通信账户');
  10. }
  11. # 获取code
  12. public function code()
  13. {
  14. $refer = Dever::input('refer');
  15. $scope = Dever::input('scope', 'is_string', 'scope', 'snsapi_base');
  16. $param['scope'] = $scope;
  17. $param['redirect_uri'] = urlencode(Dever::url('api/oauth.token', array
  18. (
  19. 't' => $this->t,
  20. 'account' => $this->account,
  21. 'refer' => Secure::encode($refer),
  22. )
  23. ));
  24. Dever::load('api/account')->run($this->account, 'oauth_code', $param, 1, 'jump');
  25. }
  26. # 获取token
  27. public function token()
  28. {
  29. $param['code'] = Dever::input('code', 'is_string', 'code');
  30. $data = Dever::load('api/account')->run($this->account, 'oauth_token', $param);
  31. if ($data && isset($data['openid'])) {
  32. if ($t = Secure::checkLogin($this->t)) {
  33. if ($t['uid'] && $t['uid'] > 0) {
  34. $update['uid'] = $t['uid'];
  35. $update['account_id'] = $data['account_id'];
  36. $update['env'] = 3;
  37. $info = Dever::db('api/openid')->find($update);
  38. if (!$info) {
  39. $update['openid'] = $data['openid'];
  40. Dever::db('api/openid')->insert($update);
  41. }
  42. }
  43. }
  44. if (isset($data['scope']) && $data['scope'] == 'snsapi_userinfo') {
  45. $user = $this->user($data);
  46. }
  47. }
  48. $refer = Secure::decode(Dever::input('refer'));
  49. if ($refer) {
  50. header('location:' . $refer);
  51. }
  52. }
  53. # 获取用户信息
  54. public function user($data)
  55. {
  56. $param['access_token'] = $data['access_token'];
  57. $param['openid'] = $data['openid'];
  58. $data = Dever::load('api/account')->run($this->account, 'oauth_user', $param);
  59. if ($data) {
  60. # 获取到用户了
  61. }
  62. }
  63. # 根据refresh_token获取token
  64. public function refreshToken($id)
  65. {
  66. }
  67. }