dever 4 years ago
parent
commit
7f630bffd9
3 changed files with 149 additions and 4 deletions
  1. 4 3
      database/project.php
  2. 1 1
      index.php
  3. 144 0
      lib/Aliyun.php

+ 4 - 3
database/project.php

@@ -9,6 +9,7 @@ $type = array
 	5 => '第三方平台',
 	6 => '开发者',
 	7 => '有赞',
+	8 => '阿里云',
 );
 
 return array
@@ -60,9 +61,9 @@ return array
 		'appid'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '微信appid',
+			'name' 		=> 'appid-一般为微信appid、阿里的appkey',
 			'default' 	=> '',
-			'desc' 		=> '请输入微信appid',
+			'desc' 		=> '请输入appid',
 			'match' 	=> 'is_string',
 			'search'	=> 'order,fulltext',
 			'update'	=> 'text',
@@ -73,7 +74,7 @@ return array
 		'secret'		=> array
 		(
 			'type' 		=> 'varchar-150',
-			'name' 		=> '微信secret',
+			'name' 		=> 'secret',
 			'default' 	=> '',
 			'desc' 		=> '请输入微信secret',
 			'match' 	=> 'is_string',

+ 1 - 1
index.php

@@ -1,7 +1,7 @@
 <?php
 
 define('DEVER_APP_NAME', 'token');
-define('DEVER_APP_LANG', '微信开发管理');
+define('DEVER_APP_LANG', 'Token管理');
 define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
 define('DEVER_MANAGE_ORDER', -1);
 define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-road layui-icon-engine');

+ 144 - 0
lib/Aliyun.php

@@ -0,0 +1,144 @@
+<?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'];
+	}
+}