Skip to content

Commit 438a9d3

Browse files
committed
Fix #1.
修复Access Token获取的问题.
1 parent 9b94d24 commit 438a9d3

File tree

3 files changed

+55
-48
lines changed

3 files changed

+55
-48
lines changed

Action.php

-15
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,6 @@ private function baseText($postObj, $contentStr=''){
203203
$resultStr = sprintf($this->_textTpl, $fromUsername, $toUsername, $time, $contentStr);
204204
return $resultStr;
205205
}
206-
/** 机器人对话 **/
207-
private function chatText($postObj, $info){
208-
if($info != null){
209-
$key = $this->_tulingApi ;
210-
$url = "http://www.tuling123.com/openapi/api?key=$key&info=$info";
211-
$content = file_get_contents($url);
212-
$content = json_decode($content, true);
213-
$contentStr = $content['text'];
214-
}
215-
$fromUsername = $postObj->FromUserName;
216-
$toUsername = $postObj->ToUserName;
217-
$time = time();
218-
$resultStr = sprintf($this->_textTpl, $fromUsername, $toUsername, $time, $contentStr);
219-
return $resultStr;
220-
}
221206

222207
/** 最新日志 **/
223208
private function newPost($postObj){

Plugin.php

+47-28
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
2+
23
/**
3-
* Typecho 微信助手插件
4-
* 在冰剑(https://github.com/binjoo/WeChatHelper)的基础上精简了一些内容
4+
* Typecho 微信助手
5+
* 对冰剑(https://github.com/binjoo/WeChatHelper)进行精简的版本
56
*
67
* @package WeChatHelper
78
* @author Holmesian
8-
* @version 2.0
9+
* @version 2.2.2
910
* @link https://holmesian.org
1011
*/
1112
class WeChatHelper_Plugin implements Typecho_Plugin_Interface
1213
{
1314
/**
1415
* 激活插件方法,如果激活失败,直接抛出异常
15-
*
16+
*
1617
* @access public
1718
* @return void
1819
* @throws Typecho_Plugin_Exception
@@ -21,8 +22,8 @@ public static function activate()
2122
{
2223
Helper::addAction('wechatHelper', 'WeChatHelper_Action');
2324
Helper::addRoute('wechat', '/wechat', 'WeChatHelper_Action', 'link');
24-
$db = Typecho_Db::get();
25-
if("Pdo_Mysql" === $db->getAdapterName() || "Mysql" === $db->getAdapterName()){
25+
$db = Typecho_Db::get();
26+
if ("Pdo_Mysql" === $db->getAdapterName() || "Mysql" === $db->getAdapterName()) {
2627
/**
2728
* 创建自定义菜单表
2829
*/
@@ -38,21 +39,27 @@ public static function activate()
3839
`created` int(10) DEFAULT '0',
3940
PRIMARY KEY (`mid`)
4041
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
41-
$db->query("INSERT INTO `". $db->getPrefix() . 'wch_menus' ."` (`mid`, `level`, `name`, `type`, `value`, `sort`, `order`, `parent`, `created`) VALUES
42+
$db->query("INSERT INTO `" . $db->getPrefix() . 'wch_menus' . "` (`mid`, `level`, `name`, `type`, `value`, `sort`, `order`, `parent`, `created`) VALUES
4243
(1, 'button', '首页', 'view', 'https://holmesian.org/', 10, 1, 0, 1503804104),
4344
(3, 'button', '搜索', 'view', 'https://m.holmesian.org/', 20, 2, 0, 1503804141),
4445
(4, 'button', '其他', 'click', NULL, 50, 5, 0, 1503804153),
4546
(6, 'sub_button', '最新文章', 'click', 'n', 51, 1, 4, 1503804247),
4647
(7, 'sub_button', '随机文章', 'click', 'r', 52, 2, 4, 1503807202),
4748
(8, 'sub_button', '手气不错', 'click', 'l', 54, 4, 4, 1503824995);");
48-
}else{
49+
50+
$db->query($db->sql()->insert('table.options')->rows(array("name"=>"WCH_access_token","user"=>"0","value"=>"0")));
51+
$db->query($db->sql()->insert('table.options')->rows(array("name"=>"WCH_expires_in","user"=>"0","value"=>"0")));
52+
53+
} else {
4954
throw new Typecho_Plugin_Exception(_t('对不起, 本插件仅支持MySQL数据库。'));
5055
}
56+
5157
Helper::addAction('WeChat', 'WeChatHelper_Action');
52-
Helper::addPanel(1, 'WeChatHelper/Page/Menus.php', '自定义菜单', '自定义菜单', 'administrator');
53-
return('微信助手已经成功激活,请进入设置Token!');
58+
Helper::addPanel(1, 'WeChatHelper/Page/Menus.php', '公众号菜单', '公众号菜单', 'administrator');
59+
60+
return ('微信助手已经成功激活,请进入设置Token!');
5461
}
55-
62+
5663
/**
5764
* 禁用插件方法,如果禁用失败,直接抛出异常
5865
*
@@ -63,32 +70,30 @@ public static function activate()
6370
*/
6471
public static function deactivate()
6572
{
66-
$db = Typecho_Db::get();
73+
$db = Typecho_Db::get();
6774
$options = Typecho_Widget::widget('Widget_Options');
68-
if("Pdo_Mysql" === $db->getAdapterName() || "Mysql" === $db->getAdapterName()){
69-
$db->query("drop table ".$db->getPrefix()."wch_menus");
70-
$db->query($db->sql()->delete('table.options')->where('name like ?', "WeChatHelper_%"));
71-
}
75+
if ("Pdo_Mysql" === $db->getAdapterName() || "Mysql" === $db->getAdapterName()) {
76+
$db->query("drop table " . $db->getPrefix() . "wch_menus");
77+
$db->query($db->sql()->delete('table.options')->where('name like ?', "WCH_%"));
78+
}
7279
Helper::removePanel(1, 'WeChatHelper/Page/Menus.php');
7380

7481
Helper::removeRoute('wechat');
7582
Helper::removeAction('wechatHelper');
7683
}
77-
84+
7885
/**
7986
* 获取插件配置面板
80-
*
87+
*
8188
* @access public
8289
* @param Typecho_Widget_Helper_Form $form 配置面板
8390
* @return void
8491
*/
8592
public static function config(Typecho_Widget_Helper_Form $form)
8693
{
87-
/** Token **/
88-
$token = new Typecho_Widget_Helper_Form_Element_Text('token', NULL, '', _t('微信Token'), '可以任意填写,用作生成签名。');
89-
$form->addInput($token);
94+
9095
/** 用户添加订阅欢迎语 **/
91-
$welcome = new Typecho_Widget_Helper_Form_Element_Textarea('welcome', NULL, '欢迎!'.chr(10).'发送\'h\'让小的给您介绍一下!', '订阅欢迎语', '用户订阅之后主动发送的一条欢迎语消息。');
96+
$welcome = new Typecho_Widget_Helper_Form_Element_Textarea('welcome', NULL, '欢迎!' . chr(10) . '发送\'h\'让小的给您介绍一下!', '订阅欢迎语', '用户订阅之后主动发送的一条欢迎语消息。');
9297
$form->addInput($welcome);
9398
/** 返回最大结果条数 **/
9499
$imageDefault = new Typecho_Widget_Helper_Form_Element_Text('imageDefault', NULL, 'https://holmesian.org/usr/themes/Holmesian/image/avatar.jpg', _t('默认显示图片'), '图片链接,支持JPG、PNG格式,推荐图为80*80。');
@@ -101,17 +106,31 @@ public static function config(Typecho_Widget_Helper_Form $form)
101106
$subMaxNum = new Typecho_Widget_Helper_Form_Element_Text('subMaxNum', NULL, '200', _t('日志截取字数'), '显示单条日志时,截取日志内容字数。');
102107
$subMaxNum->input->setAttribute('class', 'mini');
103108
$form->addInput($subMaxNum);
104-
/** 图灵机器人api **/
105-
//$tulingApi = new Typecho_Widget_Helper_Form_Element_Text('tulingApi', NULL, '', _t('图灵机器人api'), '这里填入图灵机器人Api');
106-
//$form->addInput($tulingApi);
109+
110+
/** Token **/
111+
$token = new Typecho_Widget_Helper_Form_Element_Text('token', NULL, '', _t('令牌(Token)'), '需要与开发模式服务器配置中填写一致。服务器地址(URL):' . Helper::options()->index . '/wechat');
112+
$form->addInput($token);
113+
114+
/** APP_ID **/
115+
$appid = new Typecho_Widget_Helper_Form_Element_Text('WCH_appid', NULL, NULL,
116+
_t('APP_ID'), _t('需要管理菜单时填写,与开发模式服务器配置中填写一致。'));
117+
118+
/** APP_Secret **/
119+
$form->addInput($appid);
120+
$appsecret = new Typecho_Widget_Helper_Form_Element_Text('WCH_appsecret', NULL, NULL,
121+
_t('APP_Secret'), _t('需要管理菜单时填写,与开发模式服务器配置中填写一致。'));
122+
$form->addInput($appsecret);
123+
107124
}
108-
125+
109126
/**
110127
* 个人用户的配置面板
111-
*
128+
*
112129
* @access public
113130
* @param Typecho_Widget_Helper_Form $form
114131
* @return void
115132
*/
116-
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
133+
public static function personalConfig(Typecho_Widget_Helper_Form $form)
134+
{
135+
}
117136
}

Widget/Utils.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ public static function getSystemMsg(){
3030

3131
public static function getAccessToken(){
3232
$db = Typecho_Db::get();
33-
$options = Typecho_Widget::widget('Widget_Options');
33+
$WCH_expires_in = Typecho_Widget::widget('Widget_Options')->WCH_expires_in;
34+
$WCH_access_token = Typecho_Widget::widget('Widget_Options')->WCH_access_token;
35+
$options =Helper::options()->plugin('WeChatHelper');
3436
if(isset($options->WCH_appid) && isset($options->WCH_appsecret)){
35-
if(isset($options->WCH_access_token) && isset($options->WCH_expires_in) && $options->WCH_expires_in > time()){
36-
return $options->WCH_access_token;
37+
var_dump($options);
38+
if(isset($WCH_access_token) && isset($WCH_expires_in) && $WCH_expires_in > time()){
39+
return $WCH_access_token;
3740
}else{
3841
$client = Typecho_Http_Client::get();
3942
$params = array('grant_type' => 'client_credential',
@@ -44,8 +47,8 @@ public static function getAccessToken(){
4447
//throw new Typecho_Plugin_Exception(_t('对不起,请求错误。ErrCode:'.$response->errcode.' - ErrMsg:'.$response->errmsg));
4548
return NULL;
4649
}else{
47-
$db->query($db->update('table.options')->rows(array('value' => $response->access_token))->where('name = ?', 'WeChatHelper_access_token'));
48-
$db->query($db->update('table.options')->rows(array('value' => time() + $response->expires_in))->where('name = ?', 'WeChatHelper_expires_in'));
50+
$db->query($db->update('table.options')->rows(array('value' => $response->access_token))->where('name = ?', 'WCH_access_token'));
51+
$db->query($db->update('table.options')->rows(array('value' => time() + $response->expires_in))->where('name = ?', 'WCH_expires_in'));
4952
return $response->access_token;
5053
}
5154
}

0 commit comments

Comments
 (0)