123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- /*
- |--------------------------------------------------------------------------
- | aliyun.php 阿里云
- |--------------------------------------------------------------------------
- */
- namespace Token\Lib;
- use Dever;
- class Aliyun
- {
- /**
- * config
- *
- * @var array
- */
- private $config;
-
- /**
- * project
- *
- * @var string
- */
- private $project;
-
- /**
- * 构造函数 初始化
- *
- * @return mixed
- */
- public function __construct($project = false)
- {
- if (!$project) {
- $appid = Dever::input('appid');
- if ($appid) {
- $project = Dever::db('token/project')->one(array('option_type' => $type, 'option_appid' => $appid));
- }
- if (!$project) {
- $project = Dever::input('project', 1);
- }
- }
- if (is_numeric($project)) {
- $project = Dever::db('token/project')->one(array('option_type' => 8, 'option_id' => $project));
- }
- if (!$project) {
- Dever::alert('project is not exits!');
- }
- $this->project = $project;
- }
-
- /**
- * 获取当前站点的配置
- *
- * @return mixed
- */
- public function project()
- {
- return $this->project;
- }
- /**
- * 获取当前基本的配置
- *
- * @return mixed
- */
- public function config()
- {
- return $this->config;
- }
- /**
- * 更新数据库
- * state true为强制更新数据库中的token数据
- *
- * @return mixed
- */
- private function save($type = 'token', $state = false, $interval = 4000)
- {
- $db = Dever::db('token/token');
-
- $where['project_id'] = $this->project['id'];
- $info = $db->one($where);
- $update = false;
-
- if ($state == true) {
- $update = true;
- } elseif ($info && time() - $info['mdate'] >= $info['expires']) {
- $update = true;
- } elseif($info) {
- return $info;
- }
- if (!$info) {
- $update = false;
- }
- Dever::load('aliyun/');
- \AlibabaCloud\Client\AlibabaCloud::accessKeyClient($this->project['appid'],$this->project['secret'])->regionId("cn-shanghai")->asDefaultClient();
- $response = \AlibabaCloud\Client\AlibabaCloud::nlsCloudMeta()->v20180518()->createToken()->request();
-
- if ($response && isset($response['Token']) && $response['Token']) {
- $data['value'] = $response['Token']['Id'];
- $data['expires'] = $response['Token']['ExpireTime'];
- $data['project_id'] = $this->project['id'];
- $expires = $data['expires'] - time() - $interval;
- if ($expires <= 0) {
- $data['expires'] = $data['expires'] - 300;
- } else {
- $data['expires'] = $expires;
- }
- if ($update == true) {
- $data['where_id'] = $info['id'];
- $id = $info['id'];
- $db->update($data);
- } else {
- $id = $db->insert($data);
- }
- $data['id'] = $id;
- } elseif($info && $info['value']) {
- $data = $info;
- $data['value'] = $info['value'];
- }
- return $data;
- }
- /**
- * 获取最新的token
- *
- * @return mixed
- */
- public function token($state = false)
- {
- $result = $this->save('token', $state);
- return $result['value'];
- }
- }
|