ThinkAdmin/application/common.php
2017-02-09 05:08:06 -05:00

53 lines
1.4 KiB
PHP

<?php
use think\Config;
use Wechat\Loader;
use Wechat\WechatReceive;
/**
* 打印输出数据到文件
* @param mixed $data
* @param bool $replace
* @param string|null $pathname
*/
function p($data, $replace = false, $pathname = NULL) {
is_null($pathname) && $pathname = RUNTIME_PATH . date('Ymd') . '.txt';
$str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, TRUE) : var_export($data, TRUE)) . "\n";
$replace ? file_put_contents($pathname, $str) : file_put_contents($pathname, $str, FILE_APPEND);
}
/**
* 获取微信操作对象
* @param string $type
* @return WechatReceive
*/
function & load_wechat($type = '') {
static $wechat = array();
$index = md5(strtolower($type));
if (!isset($wechat[$index])) {
$config = Config::get('wechat');
$config['cachepath'] = CACHE_PATH . 'wechat' . DS;
$wechat[$index] = &Loader::get($type, $config);
}
return $wechat[$index];
}
/**
* 安全URL编码
* @param array $data
* @return string
*/
function encode($data) {
return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode(serialize($data)));
}
/**
* 安全URL解码
* @param string $string
* @return string
*/
function decode($string) {
$data = str_replace(array('-', '_'), array('+', '/'), $string);
$mod4 = strlen($data) % 4;
($mod4) && $data .= substr('====', $mod4);
return unserialize(base64_decode($data));
}