GdtAccount.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/2/27
  6. * Time: 14:04
  7. */
  8. namespace app\common\model;
  9. use app\common\library\Redis;
  10. use app\main\service\GdtService;
  11. use think\Log;
  12. use think\Model;
  13. class GdtAccount extends Model
  14. {
  15. // 表名
  16. protected $table = 'gdt_account';
  17. // 自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. // 追加属性
  23. protected $append = [
  24. ];
  25. /**
  26. * 获取account_id 和 access_token
  27. * @param $admin_id
  28. * @param bool $fromDb
  29. * @return array
  30. * @throws \think\Exception
  31. */
  32. public function getInfoByAdminId($admin_id, $fromDb = false)
  33. {
  34. $redisKey = "GDTI:".$admin_id;
  35. $result = [
  36. 'account_id' => 0,
  37. 'access_token' => '',
  38. ];
  39. if ($fromDb == false) {
  40. $data = Redis::instance()->hMGet($redisKey, ['account_id', 'access_token', 'access_token_expire_time']);
  41. if ($data['account_id'] !== false) {
  42. if ($data['account_id'] > 0) {
  43. if ($data['access_token_expire_time'] <= time()) {
  44. $fromDb = true;
  45. } else {
  46. $result['account_id'] = $data['account_id'];
  47. $result['access_token'] = $data['access_token'];
  48. }
  49. }
  50. } else {
  51. $fromDb = true;
  52. }
  53. }
  54. if ($fromDb) {
  55. $row = $this->where('admin_id', 'eq', $admin_id)->find();
  56. if ($row) {
  57. $data = $row->toArray();
  58. //需要检测token有效期
  59. if ($data['access_token_expire_time'] <= time()) {
  60. $access_token = GdtService::instance()->apiUpdateAccessToken($data['account_id'], $data['refresh_token'], ['admin_id' => $data['admin_id']]);
  61. if (is_null($access_token)) {
  62. Log::error("GDT刷新access_token失败:admin_id:{$admin_id}");
  63. $result = [
  64. 'account_id' => 0,
  65. 'access_token' => '',
  66. ];
  67. } else {
  68. //更新数据库
  69. $this->update(['access_token' => $access_token, 'access_token_expire_time' => time() + 85400, 'updatetime' => time()], ['id' => $data['id']]);
  70. $data['access_token'] = $access_token;
  71. $data['access_token_expire_time'] = time() + 85400;
  72. $result = [
  73. 'account_id' => $data['account_id'],
  74. 'access_token' => $access_token,
  75. ];
  76. }
  77. } else {
  78. $result = [
  79. 'account_id' => $data['account_id'],
  80. 'access_token' => $data['access_token'],
  81. ];
  82. }
  83. } else {
  84. $data = [
  85. 'admin_id' => $admin_id,
  86. 'account_id' => 0,
  87. 'access_token' => '',
  88. 'access_token_expire_time' => 0,
  89. ];
  90. }
  91. Redis::instance()->hMSet($redisKey, $data);
  92. Redis::instance()->expire($redisKey, 3600);
  93. }
  94. return $result;
  95. }
  96. }