rabin 1 year ago
parent
commit
c81c3f9515
3 changed files with 56 additions and 5 deletions
  1. 10 0
      database/project.php
  2. 14 5
      lib/Wechat.php
  3. 32 0
      src/Api.php

+ 10 - 0
database/project.php

@@ -104,6 +104,16 @@ return array
 			'update'	=> 'text',
 		),
 
+		'url'		=> array
+		(
+			'type' 		=> 'varchar-500',
+			'name' 		=> '获取最新access_token-输入url即可,为了方便测试使用,如测试环境和正式环境都同时使用一套appid时可以填入,url拼接为host/package/token?api.token',
+			'default' 	=> '',
+			'desc' 		=> '获取最新access_token',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+		),
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',

+ 14 - 5
lib/Wechat.php

@@ -143,12 +143,21 @@ class Wechat
 		}
 
 		if (is_array($value) || (is_string($value) && strstr($value, 'http://'))) {
-			if ($refresh) {
-				$result = $this->curl(false, $value, false, $type);
-			} else {
-				$result = $this->curl(false, $value, true, $type);
+
+			# 为方便测试,这里可以直接用接口返回的token
+			$result = array();
+			if ($type == 'token' && isset($this->project['url']) && $this->project['url']) {
+				$param = array('type' => $type, 'appid' => $this->project['appid']);
+				$param = Dever::token($param, $this->project['secret']);
+				$result = Dever::curl($this->project['url'], $param);
+			}
+			if (!$result) {
+				if ($refresh) {
+					$result = $this->curl(false, $value, false, $type);
+				} else {
+					$result = $this->curl(false, $value, true, $type);
+				}
 			}
-			
 
 			$key = $type;
 			if ($result && isset($result[$key])) {

+ 32 - 0
src/Api.php

@@ -0,0 +1,32 @@
+<?php
+namespace Token\Lib;
+
+use Dever;
+
+class Api
+{
+	private function getSecret()
+	{
+		if (!$appid = Dever::input('appid')) {
+            Dever::alert('appid不能为空');
+        }
+        $this->project = Dever::db('token/project')->find(array('appid' => $appid));
+        if ($this->project) {
+        	return $this->project['secret'];
+        }
+	}
+	# 兼容测试环境 设置token
+    public function token_secure_api_token(){return $this->getSecret();}
+    # 兼容测试环境 获取最新token
+    public function token_secure_api()
+    {
+    	$result = array();
+    	$token = Dever::db('token/token')->find(array('project_id' => $this->project['id']));
+    	if ($token) {
+    		$result['token'] = $token['value'];
+    		$result['expires_in'] = $token['expires'];
+    	}
+    	
+        return $result;
+    }
+}