請把以下方法放到 項目目錄\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);
}
}
~~~