1
0

first commit

This commit is contained in:
2020-08-06 14:58:51 +08:00
commit 17096657dc
780 changed files with 92857 additions and 0 deletions

104
extend/tools/Initialize.php Normal file
View File

@@ -0,0 +1,104 @@
<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace tools;
use cjango\Wechat;
use cjango\Wechat\Token;
use think\Cache;
use think\Config;
use think\Controller;
use think\Session;
/**
* 所有模块的公共方法
*/
class Initialize extends Controller
{
/**
* 判断是否登录返回当前UID
* @return integer
*/
final protected static function isLogin()
{
$user = Session::get('user_auth');
if (empty($user)) {
return 0;
} else {
return Session::get('user_auth_sign') == Crypt::dataAuthSign($user) ? $user['uid'] : 0;
}
}
/**
* 初始化微信
* @return void
*/
protected function initWechat()
{
$token = Cache::get('wechat_access_token');
$config = Config::get('wechat');
if (!$token) {
Wechat::instance($config);
$token = Token::get();
Cache::set('wechat_access_token', $token, 7200);
$config['access_token'] = $token;
Wechat::instance($config, true);
} else {
$config['access_token'] = $token;
Wechat::instance($config);
}
}
/**
* 微信分享
*/
protected function jsWechat()
{
$ticket = Cache::get('Wechat_ticket');
$config = Config::get('wechat_config');
self::initWechat();
if (!$ticket) {
$ticket = Token::ticket();
Cache::set('Wechat_ticket', $ticket, 7000);
}
$now_time = time();
$wx['appid'] = $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;
}
/**
* 模板变量赋值
* @param string $name 变量名
* @param mixed $value 变量值
*/
final public function __set($name, $value)
{
$this->view->$name = $value;
}
/**
* 取得模板显示变量的值
* @param string $name 模板变量
* @return mixed|null
*/
final public function __get($name)
{
return $this->view->$name ?? null;
}
}