Aliyun.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | aliyun.php 阿里云
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Token\Lib;
  8. use Dever;
  9. class Aliyun
  10. {
  11. /**
  12. * config
  13. *
  14. * @var array
  15. */
  16. private $config;
  17. /**
  18. * project
  19. *
  20. * @var string
  21. */
  22. private $project;
  23. /**
  24. * 构造函数 初始化
  25. *
  26. * @return mixed
  27. */
  28. public function __construct($project = false)
  29. {
  30. if (!$project) {
  31. $appid = Dever::input('appid');
  32. if ($appid) {
  33. $project = Dever::db('token/project')->one(array('option_type' => $type, 'option_appid' => $appid));
  34. }
  35. if (!$project) {
  36. $project = Dever::input('project', 1);
  37. }
  38. }
  39. if (is_numeric($project)) {
  40. $project = Dever::db('token/project')->one(array('option_type' => 8, 'option_id' => $project));
  41. }
  42. if (!$project) {
  43. Dever::alert('project is not exits!');
  44. }
  45. $this->project = $project;
  46. }
  47. /**
  48. * 获取当前站点的配置
  49. *
  50. * @return mixed
  51. */
  52. public function project()
  53. {
  54. return $this->project;
  55. }
  56. /**
  57. * 获取当前基本的配置
  58. *
  59. * @return mixed
  60. */
  61. public function config()
  62. {
  63. return $this->config;
  64. }
  65. /**
  66. * 更新数据库
  67. * state true为强制更新数据库中的token数据
  68. *
  69. * @return mixed
  70. */
  71. private function save($type = 'token', $state = false, $interval = 4000)
  72. {
  73. $db = Dever::db('token/token');
  74. $where['project_id'] = $this->project['id'];
  75. $info = $db->one($where);
  76. $update = false;
  77. if ($state == true) {
  78. $update = true;
  79. } elseif ($info && time() - $info['mdate'] >= $info['expires']) {
  80. $update = true;
  81. } elseif($info) {
  82. return $info;
  83. }
  84. if (!$info) {
  85. $update = false;
  86. }
  87. Dever::load('aliyun/');
  88. \AlibabaCloud\Client\AlibabaCloud::accessKeyClient($this->project['appid'],$this->project['secret'])->regionId("cn-shanghai")->asDefaultClient();
  89. $response = \AlibabaCloud\Client\AlibabaCloud::nlsCloudMeta()->v20180518()->createToken()->request();
  90. if ($response && isset($response['Token']) && $response['Token']) {
  91. $data['value'] = $response['Token']['Id'];
  92. $data['expires'] = $response['Token']['ExpireTime'];
  93. $data['project_id'] = $this->project['id'];
  94. $expires = $data['expires'] - time() - $interval;
  95. if ($expires <= 0) {
  96. $data['expires'] = $data['expires'] - 300;
  97. } else {
  98. $data['expires'] = $expires;
  99. }
  100. if ($update == true) {
  101. $data['where_id'] = $info['id'];
  102. $id = $info['id'];
  103. $db->update($data);
  104. } else {
  105. $id = $db->insert($data);
  106. }
  107. $data['id'] = $id;
  108. } elseif($info && $info['value']) {
  109. $data = $info;
  110. $data['value'] = $info['value'];
  111. }
  112. return $data;
  113. }
  114. /**
  115. * 获取最新的token
  116. *
  117. * @return mixed
  118. */
  119. public function token($state = false)
  120. {
  121. $result = $this->save('token', $state);
  122. return $result['value'];
  123. }
  124. }