请把以下方法放到 项目目录\application\Common.php中
~~~
if (!function_exists('getOrderSn')) {
/**创建订单号
* author Eric
* createDay: 2019/4/30
* createTime: 11:16
* return string
*/
function getOrderSn()
{
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$orderSn = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . sprintf('%02d', rand(1000, 9999)) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
$orderSn = $yCode[rand(0, 9)] . date('Ymd') . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
return $orderSn;
}
}
~~~
~~~
if (!function_exists('putMst')) {
/**输出信息
* @param int $code 状态码
* @param null $msg 提示信息
* @param null $data 返回数据
*/
function putMsg($code = 0, $msg = null, $data = null)
{
header('Content-Type:application/json;charset=utf-8');
$CodeData = [
'1' => ['1', '操作成功'],
'0' => ['0', '操作失败'],
'-1' => ['-1', 'hash参数无效'],
'-2' => ['-2', '应用AppID非法'],
'-3' => ['-3', '缺少AppToken令牌'],
'-4' => ['-4', 'AppToken令牌无效'],
'-5' => ['-5', 'AppToken令牌过期'],
'-6' => ['-6', '缺少UserToken令牌'],
'-7' => ['-7', 'UserToken令牌无效'],
'-8' => ['-8', 'UserToken令牌过期'],
'-230' => ['-230', '应用已禁用'],
'-340' => ['-340', '请求AppToken令牌次数超额'],
'-800' => ['-800', '没有数据'],
'-900' => ['-900', '参数错误'],
'-999' => ['-999', '系统错误'],
];
if (is_array($msg) || is_object($msg)) {
$data = $msg;
$msg = null;
}
$result = array(
'code' => $code
);
if (!empty($msg)) {
$result['msg'] = $msg;
} else {
$result['msg'] = $CodeData[$code][1];
}
if (isset($data)) {
$result['data'] = $data;
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
EXIT;
}
}
~~~
~~~
if (!function_exists('time_tran')) {
/**
* Notes:转换时间
* author: Eric
* date 2019/10/21 0021
* time 14:13
* @param $timer
* @return string
*/
function time_tran($timer)
{
$diff = $_SERVER['REQUEST_TIME'] - $timer;
$day = floor($diff / 86400);
$free = $diff % 86400;
if ($day > 0) {
return $day . " 天前";
} else {
if ($free > 0) {
$hour = floor($free / 3600);
$free = $free % 3600;
if ($hour > 0) {
return $hour . " 小时前";
} else {
if ($free > 0) {
$min = floor($free / 60);
$free = $free % 60;
if ($min > 0) {
return $min . " 分钟前";
} else {
if ($free > 0) {
return $free . " 秒前";
} else {
return '刚刚';
}
}
} else {
return '刚刚';
}
}
} else {
return '刚刚';
}
}
}
}
~~~
~~~
if (!function_exists('get_week')) {
/**
* Notes:根据日期获取星期几
* author: Eric
* date 2019/10/21 0021
* time 14:13
* @param string $time
* @return mixed
*/
function get_week($time = '')
{
$time = $time != '' ? $time : time();
$weekarray = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
$date = date('w', $time);
return $weekarray[$date];
}
}
~~~
~~~
if (!function_exists('get_date_time')) {
/**
* Notes:获取指定月份的开始时间和结束时间
* author: Eric
* date 2019/10/21 0021
* time 14:13
* @param null $date
* @return mixed
*/
function get_date_time($date = null)
{
//设定日期时间
if ($date == null) {
//不设定日期则设置为本月的开始和结束时间
$time['start_time'] = strtotime(date('Y-m-01 00:00:00'));
$time['mdays'] = date('t', time());
$time['end_time'] = strtotime(date('Y-m-' . $time['mdays'] . ' 23:59:59'));
} else {
//设置为指定月份的开始和结束时间
if ($date != date('Y-m', strtotime($date))) {
$this->error('参数不合法');
}
$time['start_time'] = strtotime(date($date . '-01 00:00:00'));
$time['mdays'] = date('t', $time['start_time']);
$time['end_time'] = strtotime(date($date . '-' . $time['mdays'] . ' 23:59:59'));
}
return $time;
}
}
~~~
~~~
if (!function_exists('secToTime')) {
/**
* Notes:转换时分秒
* author: Eric
* date 2019/10/21 0021
* time 14:13
* @param int $times
* @return string
*/
function secToTime($times)
{
$result = '00:00';
if ($times > 0) {
$hour = floor($times / 60);
if ($hour == 0) {
$hour = '00';
}
$minute = floor($times - 60 * $hour);
if ($minute == 0) {
$minute = '00';
}
$second = floor((($times - 3600 * $hour) - 60 * $minute) % 60);
if ($second < 10) {
$second = '0' . $second;
}
$result = $hour . ':' . $minute . ':' . $second;
}
return $result;
}
}
~~~
~~~
if (!function_exists('filter')) {
/**
* Notes:数据过滤转换
* User: EricYan
* @param $data
* @param null $whiteList
* @param string $format
* @return array
*/
function filter($data, $whiteList = null, $format = 'Y-m-d H:i:s')
{
$newData = array();
if (is_string($whiteList)) {
$whiteList = explode(',', $whiteList);
}
foreach ($data as $item) {
$newData[] = filter_data($item,$whiteList,$format);
}
return $newData;
}
}
if (!function_exists('filter_data')) {
/**
* Notes:数据过滤转换
* User: EricYan
* @param $data 原始数组/对象
* @param null $whiteList 过滤显示字段
* @param string $format 时间戳转换形式/方法名称
* @return array
*/
function filter_data($data, $whiteList = null, $format = 'Y-m-d H:i:s')
{
$newData = array();
if (is_string($whiteList)) {
$whiteList = explode(',', $whiteList);
}
foreach ($whiteList as $key) {
switch ($key) {
case 'create_time':
case 'update_time':
case 'start_time':
case 'end_time':
case 'createtime':
if($format == 'time_tran'){
$newData[$key] = time_tran($data[$key]);
}else{
$newData[$key] = datetime($data[$key],$format);
}
break;
case 'images':
$newData[$key] = explode(',', $data[$key]);
break;
default:
$newData[$key] = $data[$key];
}
}
return $newData;
}
}
~~~
~~~
if(!function_exists('set_cookie_history')){
/**
* Notes:记录浏览记录
* author: Eric
* date 2019/10/8 0008
* time 11:08
* @param $type 存储历史记录类型
* @param $id 数据ID
* @return bool
*/
function set_cookie_history($type, $id)
{
//设置初始数据
$set_limit = 20; //浏览记录的容量限制
$cookieName = 'cookie_history_' . $type;
//初始数据过滤
if (!in_array($type, ['goods', 'forum', 'news'])) {
return false;
}
//获取cookie记录
if(!isset($_COOKIE[$cookieName])){
$history_array = [];
}else{
$history_array = unserialize($_COOKIE[$cookieName]);
if (!$history_array){
$history_array = [];
}
}
//浏览记录存在
if (in_array($id, $history_array)) {
unset($history_array[array_search($id, $history_array)]); //删除存在
array_unshift($history_array, $id);//重新放在第一个
} else {
//浏览记录不存在
//没有超过记录的容量限制,直接放在第一个
if (count($history_array) < $set_limit) {
array_unshift($history_array, $id);
//超过记录的容量限制,删除最后一个,然后放在第一个
} else {
array_pop($history_array);
array_unshift($history_array, $id);
}
}
//将浏览数组序列化后写入cookie
$expire_time = 3600 * 24 * 30; //过期时间
$history_array = serialize($history_array);
setcookie($cookieName, $history_array, time() + $expire_time, '/');
}
}
if(!function_exists('get_cookie_history')){
/**
* Notes:读取浏览记录
* author: Eric
* date 2019/10/8 0008
* time 11:08
* @param $type历史记录类型
* @return array|mixed
*/
function get_cookie_history($type){
$cookieName = 'cookie_history_' . $type;
//设置初始返回数据
$return_data = [];
//获取cookie记录
$history_array = unserialize($_COOKIE[$cookieName]);
if(!$history_array){
return $return_data;
}
//非法获取数据,直接返回
return $history_array;
}
}
~~~
~~~
if (!function_exists('curl_http')) {
/**
* curl操作
* @param string $url 请求地址
* @param string $method 请求类型
* @param string $postfields 请求参数
* @param string $headers 头信息
* @param string $debug 调试模式
* @return string
*/
function curl_http($url, $method = 'get', $postfields = null, $headers = array(), $debug = false)
{
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ci, CURLOPT_NOBODY, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
//$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info=====' . "\r\n";
print_r(curl_getinfo($ci));
echo '=====$response=====' . "\r\n";
print_r($response);
curl_close($ci);
exit;
}
curl_close($ci);
return $response;
}
}
if(!function_exists('hiddenMobile')){
/**
* Notes:隐藏手机号
* author: Eric
* date 2019/10/16 0016
* time 10:38
* @param $mobile
* @return mixed
*/
function hiddenMobile($mobile){
return substr_replace($mobile,'****',3,4);
}
}
~~~