TdcAccount.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Env;
  12. use think\Log;
  13. use think\Model;
  14. class TdcAccount extends Model
  15. {
  16. // 表名
  17. protected $table = 'tdc_account';
  18. // 自动写入时间戳字段
  19. protected $autoWriteTimestamp = 'int';
  20. // 定义时间戳字段名
  21. protected $createTime = 'createtime';
  22. protected $updateTime = 'updatetime';
  23. // 追加属性
  24. protected $append = [
  25. ];
  26. /**
  27. * 获取account_id 和 access_token
  28. * @param $admin_id
  29. * @param bool $fromDb
  30. * @return array
  31. * @throws \think\Exception
  32. */
  33. public function getInfoByAdminId($fromDb = false)
  34. {
  35. $redisKey = "TDCTI:0";
  36. $result = [
  37. 'account_id' => 0,
  38. 'access_token' => '',
  39. ];
  40. if ($fromDb == false) {
  41. $data = Redis::instance()->hMGet($redisKey, ['account_id', 'access_token', 'access_token_expire_time']);
  42. if ($data['account_id'] !== false) {
  43. if ($data['account_id'] > 0) {
  44. if ($data['access_token_expire_time'] <= time()) {
  45. $fromDb = true;
  46. } else {
  47. $result['account_id'] = $data['account_id'];
  48. $result['access_token'] = $data['access_token'];
  49. }
  50. }
  51. } else {
  52. $fromDb = true;
  53. }
  54. }
  55. if ($fromDb) {
  56. $row = $this->find();
  57. if ($row) {
  58. $data = $row->toArray();
  59. //需要检测token有效期
  60. if ($data['access_token_expire_time'] <= time()) {
  61. $config = Env::get("tdc");
  62. $access_token = GdtService::instance()->apiUpdateAccessToken($data['account_id'], $data['refresh_token'], $config);
  63. if (is_null($access_token)) {
  64. Log::error("TDC刷新access_token失败:admin_id:0");
  65. $result = [
  66. 'account_id' => 0,
  67. 'access_token' => '',
  68. ];
  69. } else {
  70. //更新数据库
  71. $this->update(['access_token' => $access_token, 'access_token_expire_time' => time() + 85400, 'updatetime' => time()], ['id' => $data['id']]);
  72. $data['access_token'] = $access_token;
  73. $data['access_token_expire_time'] = time() + 85400;
  74. $result = [
  75. 'account_id' => $data['account_id'],
  76. 'access_token' => $access_token,
  77. ];
  78. }
  79. } else {
  80. $result = [
  81. 'account_id' => $data['account_id'],
  82. 'access_token' => $data['access_token'],
  83. ];
  84. }
  85. } else {
  86. $data = [
  87. 'admin_id' => 0,
  88. 'account_id' => 0,
  89. 'access_token' => '',
  90. 'access_token_expire_time' => 0,
  91. ];
  92. }
  93. Redis::instance()->hMSet($redisKey, $data);
  94. Redis::instance()->expire($redisKey, 83400);
  95. }
  96. return $result;
  97. }
  98. }