This commit is contained in:
318
WeChatShare/Action.php
Normal file
318
WeChatShare/Action.php
Normal file
@@ -0,0 +1,318 @@
|
||||
<?php
|
||||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||||
|
||||
class WeChatShare_Action extends Typecho_Widget implements Widget_Interface_Do
|
||||
{
|
||||
private $db;
|
||||
private $prefix;
|
||||
private $wx_config;
|
||||
private $system_options;
|
||||
private $widget_options;
|
||||
private $user;
|
||||
|
||||
public function action()
|
||||
{
|
||||
$this->init();
|
||||
$this->on($this->request->is('do=insert'))->insertWxShare();
|
||||
$this->on($this->request->is('do=ajax-get'))->ajaxGetWxShare();
|
||||
}
|
||||
|
||||
/*
|
||||
* 编辑或者新增文章的时候把微信分享的数据插入到wx_share表
|
||||
*/
|
||||
public function insertWxShare()
|
||||
{
|
||||
if($this->user->group != 'administrator') {
|
||||
throw new Typecho_Plugin_Exception(_t('f**k,别瞎jb搞'));
|
||||
}
|
||||
$data = [];
|
||||
//接收数据
|
||||
$data['wx_title'] = trim($_POST['wx_title']);
|
||||
|
||||
$data['wx_description'] = trim($_POST['wx_description']);
|
||||
|
||||
$data['wx_image'] = trim($_POST['wx_image']);
|
||||
|
||||
$data['wx_url'] = !empty(trim($_POST['wx_url'])) ? trim($_POST['wx_url']) :'';
|
||||
|
||||
$cid= !empty(trim($_POST['cid'])) ? trim($_POST['cid']) :'';
|
||||
|
||||
if($cid) {
|
||||
//取出数据
|
||||
$wx_share_data= $this->db->fetchAll($this->db->select()->from($this->prefix.'wx_share')->where('cid = ?', $cid));
|
||||
if($wx_share_data) {
|
||||
/** 更新数据 */
|
||||
$this->db->query($this->db->update($this->prefix.'wx_share')->rows($data)->where('cid=?',$cid));
|
||||
} else {
|
||||
/** 插入数据 */
|
||||
$this->db->query($this->db->insert($this->prefix.'wx_share')->rows($data));
|
||||
}
|
||||
|
||||
}else {
|
||||
/** 插入数据 */
|
||||
$this->db->query($this->db->insert($this->prefix.'wx_share')->rows($data));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 前台ajax获取微信分享信息
|
||||
*/
|
||||
public function ajaxGetWxShare()
|
||||
{
|
||||
|
||||
if(!$this->request->isPost()) {
|
||||
|
||||
throw new Typecho_Plugin_Exception(_t('f**k,别瞎jb搞'));
|
||||
}
|
||||
|
||||
$params = $this->request->from(['cid','parameter_type','title','signature_url']);
|
||||
|
||||
extract($params);
|
||||
|
||||
if(empty($title) || empty($signature_url) || empty($parameter_type)) {
|
||||
|
||||
throw new Typecho_Plugin_Exception(_t('f**k,别瞎jb搞'));
|
||||
}
|
||||
|
||||
$wx_share = [];
|
||||
|
||||
switch ($parameter_type) {
|
||||
|
||||
case 'index':
|
||||
|
||||
$wx_share['wx_title'] = $this->system_options->title;
|
||||
|
||||
$wx_share['wx_description'] = $this->system_options->description;
|
||||
|
||||
$wx_share['wx_url'] = $this->system_options->siteUrl;
|
||||
|
||||
break;
|
||||
|
||||
case 'post' == $parameter_type || 'page' == $parameter_type:
|
||||
|
||||
//取出数据
|
||||
$wx_share_data= $this->db->fetchAll($this->db->select()->from($this->prefix.'wx_share')->where('cid = ?', $cid));
|
||||
|
||||
if(!$wx_share_data) {
|
||||
|
||||
$wx_share['wx_title'] = $title;
|
||||
|
||||
$wx_share['wx_description'] = $this->system_options->description;
|
||||
|
||||
$wx_share['wx_url'] = $signature_url;
|
||||
|
||||
}else {
|
||||
|
||||
$wx_share['wx_title'] = $wx_share_data[0]['wx_title'];
|
||||
|
||||
$wx_share['wx_description'] = $wx_share_data[0]['wx_description'];
|
||||
|
||||
$wx_share['wx_image'] = $wx_share_data[0]['wx_image'];
|
||||
|
||||
$wx_share['wx_url'] = $wx_share_data[0]['wx_url'];
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$wx_share['wx_title'] = $title;
|
||||
|
||||
$wx_share['wx_description'] = $this->system_options->description;
|
||||
|
||||
$wx_share['wx_url'] = $signature_url;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
!empty($wx_share['wx_image']) || $wx_share['wx_image'] = $this->wx_config->wx_image;
|
||||
|
||||
$signPackage = $this->getSignPackage($signature_url);
|
||||
|
||||
$wx_share['appId'] = $this->wx_config->wx_AppID;
|
||||
|
||||
$wx_share['timestamp'] = $signPackage['timestamp'];
|
||||
|
||||
$wx_share['nonceStr'] = $signPackage['nonceStr'];
|
||||
|
||||
$wx_share['signature'] = $signPackage['signature'];
|
||||
|
||||
_e(json_encode($wx_share));
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取微信分享配置SignPackage值
|
||||
*/
|
||||
public function getSignPackage($url) {
|
||||
|
||||
$this->getJsApiTicket();
|
||||
|
||||
$timestamp = time();
|
||||
|
||||
$nonceStr = $this->createNonceStr();
|
||||
|
||||
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
|
||||
$string = 'jsapi_ticket='.$this->wx_config->jsapi_ticket.'&noncestr='.$nonceStr.'×tamp='.$timestamp.'&url='.$url;
|
||||
|
||||
$signature = sha1($string);
|
||||
|
||||
$signPackage = [
|
||||
"nonceStr" => $nonceStr,
|
||||
"timestamp" => $timestamp,
|
||||
"signature" => $signature,
|
||||
"rawString" => $string,
|
||||
];
|
||||
return $signPackage;
|
||||
}
|
||||
|
||||
/*
|
||||
* 生成随机字符串
|
||||
*/
|
||||
private function createNonceStr($length = 16) {
|
||||
|
||||
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
|
||||
$str = '';
|
||||
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
|
||||
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
|
||||
|
||||
}
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 获取JsApiTicket,如果过时就重新获取
|
||||
*/
|
||||
private function getJsApiTicket() {
|
||||
|
||||
if ($this->wx_config->jsapi_ticket_expire_time < time()) {
|
||||
|
||||
$this->getAccessToken();
|
||||
|
||||
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.$this->wx_config->access_token;
|
||||
|
||||
$res = json_decode($this->httpGet($url));
|
||||
|
||||
$ticket = $res->ticket;
|
||||
|
||||
if ($ticket) {
|
||||
|
||||
$this->wx_config->jsapi_ticket_expire_time = time() + 7000;
|
||||
|
||||
$this->wx_config->jsapi_ticket = $ticket;
|
||||
|
||||
$this->updateWxConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取AccessToken,如果过时就重新获取
|
||||
*/
|
||||
private function getAccessToken() {
|
||||
|
||||
if ($this->wx_config->access_token_expire_time < time()) {
|
||||
|
||||
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->wx_config->wx_AppID.'&secret='.$this->wx_config->wx_AppSecret;
|
||||
|
||||
$res = json_decode($this->httpGet($url));
|
||||
|
||||
$access_token = $res->access_token;
|
||||
|
||||
if ($access_token) {
|
||||
|
||||
$this->wx_config->access_token_expire_time = time() + 7000;
|
||||
|
||||
$this->wx_config->access_token = $access_token;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*服务器与微信服务器通信
|
||||
*/
|
||||
private function httpGet($url) {
|
||||
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
|
||||
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
|
||||
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
|
||||
|
||||
curl_setopt($curl, CURLOPT_CAINFO,'usr/plugins/WeChatShare/cacert.pem');
|
||||
|
||||
curl_setopt($curl, CURLOPT_URL, $url);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取微信插件的配置
|
||||
*/
|
||||
|
||||
public function getWxConfig()
|
||||
{
|
||||
|
||||
$this->wx_config = json_decode(json_encode(unserialize(Helper::options()->plugin('WeChatShare'))));
|
||||
|
||||
//判断是否配置好APPID
|
||||
if (empty($this->wx_config->wx_AppID)) {
|
||||
|
||||
throw new Typecho_Plugin_Exception(_t('微信AppID未配置'));
|
||||
}
|
||||
if (empty($this->wx_config->wx_AppSecret)) {
|
||||
|
||||
throw new Typecho_Plugin_Exception(_t('微信AppSecret密钥未配置'));
|
||||
}
|
||||
//判断是否设置了默认图片URL
|
||||
if(empty($this->wx_config->wx_image)) {
|
||||
|
||||
$this->wx_config->wx_image = Typecho_Common::url('usr/plugins/WeChatShare/nopic.jpg', $this->system_options->siteUrl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 更新微信插件的配置
|
||||
*/
|
||||
public function updateWxConfig()
|
||||
{
|
||||
|
||||
$data = ['value'=>serialize($this->wx_config)];
|
||||
|
||||
$this->db->query($this->db->update($this->prefix.'options')->rows($data)->where('name = ?', 'plugin:WeChatShare'));
|
||||
|
||||
}
|
||||
/*
|
||||
* 初始化
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->getWxConfig();
|
||||
|
||||
$this->db = Typecho_Db::get();
|
||||
|
||||
$this->prefix = $this->db->getPrefix();
|
||||
|
||||
$this->widget_options = Typecho_Widget::widget('Widget_Options');
|
||||
|
||||
$this->user = Typecho_Widget::widget('Widget_User');
|
||||
|
||||
$this->system_options = Helper::options();
|
||||
|
||||
}
|
||||
}
|
||||
21
WeChatShare/LICENSE
Normal file
21
WeChatShare/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 fuzqing
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
9
WeChatShare/Mysql.sql
Normal file
9
WeChatShare/Mysql.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE `typecho_wx_share` (
|
||||
`wx_id` int(10) unsigned NOT NULL auto_increment COMMENT 'wx_share表主键',
|
||||
`cid` int(10) unsigned default 0 COMMENT '页面的id',
|
||||
`wx_title` varchar(200) default NULL COMMENT '微信分享标题',
|
||||
`wx_url` varchar(200) default NULL COMMENT '微信分享链接',
|
||||
`wx_image` varchar(200) default NULL COMMENT '微信分享小图标',
|
||||
`wx_description` varchar(200) default NULL COMMENT '微信分享摘要',
|
||||
PRIMARY KEY (`wx_id`)
|
||||
) ENGINE=MYISAM DEFAULT CHARSET=%charset%;
|
||||
357
WeChatShare/Plugin.php
Normal file
357
WeChatShare/Plugin.php
Normal file
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||||
/**
|
||||
* WeChat Share for Typecho
|
||||
*
|
||||
* @package WeChatShare
|
||||
* @author Fuzqing
|
||||
* @version 1.0.6
|
||||
* @link https://huangweitong.com
|
||||
*
|
||||
*
|
||||
* version 1.0.6 at 2018-12-19
|
||||
* 删除在线更新
|
||||
* 删除自定义字段
|
||||
*
|
||||
*/
|
||||
class WeChatShare_Plugin implements Typecho_Plugin_Interface
|
||||
{
|
||||
|
||||
/**
|
||||
* 插件版本号
|
||||
* @var string
|
||||
*/
|
||||
const _VERSION = '1.0.6';
|
||||
/**
|
||||
* 激活插件方法,如果激活失败,直接抛出异常
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static Function activate()
|
||||
{
|
||||
$info = WeChatShare_Plugin::wechatShareInstall();
|
||||
Helper::addAction('wx-share', 'WeChatShare_Action');
|
||||
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('WeChatShare_Plugin', 'render');
|
||||
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('WeChatShare_Plugin', 'render');
|
||||
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare');
|
||||
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare');
|
||||
Typecho_Plugin::factory('Widget_Archive')->footer = array('WeChatShare_Plugin','addWxShareScript');
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public static function deactivate(){
|
||||
|
||||
Helper::removeAction('wx-share');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form 配置面板
|
||||
* @return void
|
||||
*/
|
||||
public static function config(Typecho_Widget_Helper_Form $form)
|
||||
{
|
||||
$options = Typecho_Widget::widget('Widget_Options');
|
||||
|
||||
$up_action_url = Typecho_Common::url('/index.php/action/wx-share?do=update-plugin', $options->siteUrl);
|
||||
|
||||
/** 公众号配置 */
|
||||
$wx_AppID = new Typecho_Widget_Helper_Form_Element_Text('wx_AppID', NULL, NULL, _t('APPID'),'请登录微信公众号获取');
|
||||
|
||||
$form->addInput($wx_AppID);
|
||||
|
||||
$wx_AppSecret = new Typecho_Widget_Helper_Form_Element_Text('wx_AppSecret', NULL, NULL, _t('密钥'),'请登录微信公众号获取');
|
||||
|
||||
$form->addInput($wx_AppSecret);
|
||||
|
||||
$wx_image = new Typecho_Widget_Helper_Form_Element_Text('wx_image', NULL, NULL, _t('默认图标URL'),'请注意图标大小不要超过32KB');
|
||||
|
||||
$form->addInput($wx_image);
|
||||
|
||||
$access_token_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('access_token_expire_time', NULL, NULL, _t('AccessToken 过期时间'),'隐藏');
|
||||
|
||||
$form->addInput($access_token_expire_time);
|
||||
|
||||
$access_token = new Typecho_Widget_Helper_Form_Element_Hidden('access_token', NULL, NULL, _t('AccessToken'),'隐藏');
|
||||
|
||||
$form->addInput($access_token);
|
||||
|
||||
$jsapi_ticket_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('jsapi_ticket_expire_time', NULL, NULL, _t('JsapiTicket 过期时间'),'隐藏');
|
||||
|
||||
$form->addInput($jsapi_ticket_expire_time);
|
||||
|
||||
$jsapi_ticket = new Typecho_Widget_Helper_Form_Element_Text('jsapi_ticket', NULL, NULL, _t('JsapiTicket'),'方便出错调试,不用填写自动更新');
|
||||
|
||||
$form->addInput($jsapi_ticket);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人用户的配置面板
|
||||
*
|
||||
* @access public
|
||||
* @param Typecho_Widget_Helper_Form $form
|
||||
* @return void
|
||||
*/
|
||||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||||
|
||||
/**
|
||||
* 添加微信分享信息输入框到编辑文章/页面的页面,添加自定义摘要自定义字段
|
||||
*/
|
||||
public static function render()
|
||||
{
|
||||
$options = Typecho_Widget::widget('Widget_Options');
|
||||
$wx_action_url = Typecho_Common::url('/index.php/action/wx-share?do=insert', $options->siteUrl);
|
||||
$cid_res = preg_match('/cid=(\d+)/',$_SERVER['REQUEST_URI'],$match);
|
||||
|
||||
$description = '';$wx_title = '';$wx_description = '';$wx_url = '';$wx_image = '';
|
||||
|
||||
if($cid_res) {
|
||||
/** 取出数据 */
|
||||
$db = Typecho_Db::get();
|
||||
$prefix = $db->getPrefix();
|
||||
$desc_res= $db->fetchAll($db->select()->from($prefix.'fields')->where('cid = ?', $match['1'])->where('name = ?', 'description'));
|
||||
$wx_share= $db->fetchAll($db->select()->from($prefix.'wx_share')->where('cid = ?', $match['1']));
|
||||
if($desc_res) {
|
||||
$description = $desc_res[0]['str_value'];
|
||||
}
|
||||
if($wx_share) {
|
||||
$wx_title = $wx_share[0]['wx_title'];
|
||||
$wx_description = $wx_share[0]['wx_description'];
|
||||
$wx_url = $wx_share[0]['wx_url'];
|
||||
$wx_image = $wx_share[0]['wx_image'];
|
||||
}
|
||||
}
|
||||
|
||||
$data = '<style>:-moz-placeholder {color: #E0E0E0; opacity:1;}::-moz-placeholder {color: #E0E0E0;opacity:1;}input:-ms-input-placeholder{color: #E0E0E0;opacity:1;}input::-webkit-input-placeholder{color: #E0E0E0;opacity:1;}</style><form id="wx_share" ><fieldset><legend>微信分享</legend><ol style="list-style-type:none;><li style="padding-bottom: 5px;"><label for="wx_title">标题:</label><input style="width: 80%" type="text" class="wx_title" value="'.$wx_title.'" name="wx_title" ></li><li style="padding-bottom: 5px;"><label for="wx_url">链接:</label><input type="text" style="width: 80%" value="'.$wx_url.'" name="wx_url"></li><li style="padding-bottom: 5px;display:block;"><span style="float:left" for="wx_describe">摘要:</span><textarea rows="4" class="wx_describe" name="wx_description" style="width: 80%" >'.$wx_description.'</textarea></li><li style="padding-bottom: 5px;"><label for="wx_image">图标:</label><input type="text" class="wx_image" value="'.$wx_image.'" style="width: 80%" name="wx_image"></li></ol></fieldset><input type="hidden" name="cid" value="'.$match['1'].'"></form>';
|
||||
|
||||
?>
|
||||
|
||||
<script>
|
||||
$(document).ready(function(){
|
||||
|
||||
//添加微信分享
|
||||
$("#custom-field").after('<?php echo $data ?>');
|
||||
|
||||
//监控文章的输入,同步更新微信标题
|
||||
$("#title").bind(' input propertychange ',function () {
|
||||
var wx_title = $(".wx_title").val();
|
||||
if($("#title").val()) {
|
||||
if (wx_title !== null || wx_title !== undefined || wx_title !== '') {
|
||||
|
||||
$(".wx_title").val($("#title").val());
|
||||
}
|
||||
}
|
||||
});
|
||||
$("#text").blur(function(){
|
||||
var text = $("#text").val();
|
||||
var images = text.match(/http.*?(png|jpg|jpeg|gif)$/gi);
|
||||
if(images){
|
||||
var wx_image = $(".wx_image").val();
|
||||
if (wx_image !== null || wx_image !== undefined || wx_image !== '') {
|
||||
|
||||
$(".wx_image").val(images[0]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
$("#btn-submit").click(function () {
|
||||
var wx_data = $("#wx_share").serialize();
|
||||
$.post("<?php echo $wx_action_url;?>", wx_data,success,"");
|
||||
function success(data){
|
||||
//console.log(data);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新wx_share的wx_url字段
|
||||
*/
|
||||
public static function updateWxShare($contents,$class)
|
||||
{
|
||||
$db = Typecho_Db::get();
|
||||
|
||||
$prefix = $db->getPrefix();
|
||||
|
||||
$wx_share = $db->fetchAll($db->select('wx_id,wx_url')->from('table.wx_share')->order('wx_id',Typecho_Db::SORT_DESC)->limit(1));
|
||||
|
||||
$wx_id = $wx_share[0]['wx_id'];
|
||||
|
||||
!empty($wx_share[0]['wx_url']) || $data['wx_url'] = $class->permalink;
|
||||
|
||||
$data['cid'] = $class->cid;
|
||||
|
||||
//更新数据,执行后,返回收影响的行数。
|
||||
$db->query($db->update($prefix.'wx_share')->rows($data)->where('wx_id = ?',$wx_id));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将微信分享的需要的信息写入wx_share.js文件中
|
||||
*/
|
||||
public static function addWxShareScript($archive)
|
||||
{
|
||||
|
||||
echo '<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/qzact/common/share/share.js"></script>';
|
||||
|
||||
$options = Typecho_Widget::widget('Widget_Options');
|
||||
|
||||
$ajax_wx_share_url = Typecho_Common::url('/index.php/action/wx-share?do=ajax-get', $options->siteUrl);
|
||||
|
||||
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
|
||||
|
||||
$signature_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
|
||||
// pjax
|
||||
$signature_url = preg_replace('/\?_pjax=.*/','',$signature_url);
|
||||
|
||||
$version = self::_VERSION;
|
||||
|
||||
$wx_script = <<<SCRIPT
|
||||
|
||||
WX_Custom_Share = function(){
|
||||
|
||||
var xhr = null;
|
||||
var url = '{$ajax_wx_share_url}';
|
||||
var formData = {
|
||||
title: '{$archive->title}',
|
||||
parameter_type: '{$archive->parameter->type}',
|
||||
cid: '{$archive->cid}',
|
||||
signature_url: '{$signature_url}'
|
||||
};
|
||||
|
||||
this.init = function(){
|
||||
if( window.XMLHttpRequest ){
|
||||
xhr = new XMLHttpRequest();
|
||||
}
|
||||
else if( window.ActiveXObject ){
|
||||
xhr = new ActiveXObject('Microsoft.XMLHTTP');
|
||||
};
|
||||
|
||||
get_share_info();
|
||||
};
|
||||
function formatPostData( obj ){
|
||||
|
||||
var arr = new Array();
|
||||
for (var attr in obj ){
|
||||
arr.push( encodeURIComponent( attr ) + '=' + encodeURIComponent( obj[attr] ) );
|
||||
};
|
||||
|
||||
return arr.join( '&' );
|
||||
};
|
||||
|
||||
function get_share_info(){
|
||||
|
||||
if( xhr == null ) return;
|
||||
|
||||
xhr.onreadystatechange = function(){
|
||||
if( xhr.readyState == 4 && xhr.status == 200 ){
|
||||
|
||||
var data = eval('(' + xhr.responseText + ')');
|
||||
if( data == null ){
|
||||
return;
|
||||
}
|
||||
//console.log(data);
|
||||
var info = {
|
||||
title: data.wx_title,
|
||||
summary: data.wx_description,
|
||||
pic: data.wx_image,
|
||||
url: data.wx_url
|
||||
};
|
||||
|
||||
|
||||
//info.url = data.wx_url;
|
||||
|
||||
|
||||
if( data.error ){
|
||||
console.error( data.error );
|
||||
} else if( data.appId ){
|
||||
info.WXconfig = {
|
||||
swapTitleInWX: true,
|
||||
appId: data.appId,
|
||||
timestamp: data.timestamp,
|
||||
nonceStr: data.nonceStr,
|
||||
signature: data.signature
|
||||
};
|
||||
};
|
||||
|
||||
setShareInfo( info );
|
||||
}
|
||||
};
|
||||
|
||||
xhr.open( 'POST', url, true);
|
||||
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
|
||||
xhr.send( formatPostData( formData ) );
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
new WX_Custom_Share().init();
|
||||
console.log("%c", "padding:100px 200px;line-height:220px;background:url('https://hiphotos.baidu.com/feed/pic/item/b999a9014c086e06606a9d0009087bf40bd1cbbf.jpg') no-repeat;");
|
||||
console.log("%c WeChatShare v{$version} %c By Fuzqing https://huangweitong.com ","color:#444;background:#eee;padding:5px 0;","color:#eee;background:#444;padding:5px 0;");
|
||||
SCRIPT;
|
||||
|
||||
file_put_contents('usr/plugins/WeChatShare/wx_share.js',$wx_script);
|
||||
|
||||
$wx_script_js = Typecho_Common::url('/usr/plugins/WeChatShare/wx_share.js', $options->siteUrl);
|
||||
|
||||
echo '<script type="text/javascript">'.$wx_script.'</script>';
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* WeChatShare插件安装方法
|
||||
*/
|
||||
public static function wechatShareInstall()
|
||||
{
|
||||
$installDb = Typecho_Db::get();
|
||||
$type = explode('_', $installDb->getAdapterName());
|
||||
$type = array_pop($type);
|
||||
$prefix = $installDb->getPrefix();
|
||||
$scripts = file_get_contents('usr/plugins/WeChatShare/'.$type.'.sql');
|
||||
$scripts = str_replace('typecho_', $prefix, $scripts);
|
||||
$scripts = str_replace('%charset%', 'utf8', $scripts);
|
||||
$scripts = explode(';', $scripts);
|
||||
try {
|
||||
foreach ($scripts as $script) {
|
||||
$script = trim($script);
|
||||
if ($script) {
|
||||
$installDb->query($script, Typecho_Db::WRITE);
|
||||
}
|
||||
}
|
||||
return '建立微信分享数据表,插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url';
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
$code = $e->getCode();
|
||||
if(('Mysql' == $type && 1050 == $code) || ('Mysql' == $type && '42S01' == $code) ||
|
||||
('SQLite' == $type && ('HY000' == $code || 1 == $code))) {
|
||||
try {
|
||||
$script = 'SELECT `wx_id`, `wx_title`, `wx_url`, `wx_image`, `wx_description` ,`cid` from `' . $prefix . 'wx_share`';
|
||||
$installDb->query($script, Typecho_Db::READ);
|
||||
return '检测到微信分享数据表,微信分享插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url';
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
$code = $e->getCode();
|
||||
throw new Typecho_Plugin_Exception('数据表检测失败,微信分享插件启用失败。错误号:'.$code);
|
||||
}
|
||||
} else {
|
||||
throw new Typecho_Plugin_Exception('数据表建立失败,微信分享插件启用失败。错误号:'.$code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
33
WeChatShare/README.md
Normal file
33
WeChatShare/README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Typecho 微信分享插件 WeChatShare
|
||||
|
||||
# 不再维护,谁愿意维护此插件的可以fork过去
|
||||
|
||||
## 插件简介
|
||||
|
||||
使用此插件可方便地自定义微信和QQ分享链接中的标题、描述、小图标和URL,让你的分享链接不再单调;
|
||||
|
||||
## 本人博客环境
|
||||
军哥的lnmp一键包,PHP7.1,开启了curl扩展
|
||||
Typecho 版本 1.2 (18.1.29)
|
||||
|
||||
## 安装方法
|
||||
|
||||
1. 至[releases](https://github.com/fuzqing/WeChatShare/releases)中下载最新版本插件;
|
||||
2. 将下载的压缩包进行解压,文件夹重命名为`WeChatShare`,上传至`Typecho`插件目录中;
|
||||
3. 检查`WeChatShare`插件目录文件是否有读写权限,如果没有请增加;
|
||||
4. 后台激活插件;
|
||||
5. 到插件配置页面填写插件配置信息。
|
||||
|
||||
## 注意:由于微信更新,如果你想要在微信中直接分享,请按以下步骤操作:
|
||||
|
||||
1. 公众号通过微信认证;
|
||||
2. 填写 AppID 和 AppSecret (开发 > 基本配置);
|
||||
3. 添加服务器IP 到 IP白名单中 (开发 > 基本配置 > IP白名单);
|
||||
4. 添加域名到 JS安全域名中(设置 > 公众号设置 > 功能设置 > JS接口安全域名)。
|
||||
|
||||
## 更新日志
|
||||
|
||||
### 2018.12.19
|
||||
|
||||
删除在线更新
|
||||
删除自定义字段
|
||||
8
WeChatShare/SQLite.sql
Normal file
8
WeChatShare/SQLite.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE `typecho_wx_share` (
|
||||
`wx_id` INTEGER NOT NULL PRIMARY KEY,
|
||||
`cid` int(10) default '0',
|
||||
`wx_title` varchar(200) default NULL,
|
||||
`wx_url` varchar(200) default NULL,
|
||||
`wx_image` varchar(200) default NULL,
|
||||
`wx_description` varchar(200) default NULL,
|
||||
);
|
||||
3338
WeChatShare/cacert.pem
Normal file
3338
WeChatShare/cacert.pem
Normal file
File diff suppressed because it is too large
Load Diff
BIN
WeChatShare/nopic.jpg
Normal file
BIN
WeChatShare/nopic.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
Reference in New Issue
Block a user