Files
2020-08-06 15:26:41 +08:00

92 lines
2.6 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\common\service;
use app\common\model\Wechat as WechatModel;
use cjango\Wechat as Wexin;
use cjango\Wechat\Token;
use think\Cache;
class Wechat extends _Init
{
public static $config = [];
public static $Wechat = null;
public static function set_init()
{
self::$Wechat = WechatModel::find(1);
self::$config = [
'appid' => self::$Wechat->appid,
'secret' => self::$Wechat->secret,
'access_token' => self::$Wechat->access_token,
'mch_id' => self::$Wechat->mch_id,
'paykey' => self::$Wechat->paykey,
'token' => self::$Wechat->token,
];
}
/**
* [instance description]
* @param object $agent Member模型数据
* @return [type] [description]
*/
public static function instance()
{
self::set_init();
if (empty(self::$Wechat->access_token) || self::$Wechat->access_time < (time() - 7000)) {
Wexin::instance(self::$config, true);
$token = Wexin\Token::get();
WechatModel::update(['access_token' => $token, 'access_time' => time()], ['id' => 1]);
self::$config['access_token'] = $token;
Wexin::instance(self::$config, true);
} else {
Wexin::instance(self::$config);
}
}
/**
* 微信分享
*/
public static function jsWechat()
{
$ticket = Cache::get('Wechat_ticket');
self::instance();
if (!$ticket) {
$ticket = Token::ticket();
Cache::set('Wechat_ticket', $ticket, 7000);
}
$now_time = time();
$wx['appid'] = self::$config['appid'];
$wx['timestamp'] = $now_time;
$wx['noncestr'] = $noncestr = \tools\Str::random(32);
$sign = array(
'noncestr' => $noncestr,
'jsapi_ticket' => $ticket,
'timestamp' => $now_time,
'url' => __SELF__,
);
ksort($sign);
$signStr = sha1(urldecode(http_build_query($sign)));
$wx['signature'] = $signStr;
return $wx;
}
public static function sendT($uid, $data)
{
}
}