|
|
<?php
|
|
|
// 應用公共文件
|
|
|
use think\facade\Db;
|
|
|
|
|
|
function getUrl(){
|
|
|
// $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
|
|
|
$protocol = 'https://';
|
|
|
$http = $_SERVER['HTTP_HOST'];
|
|
|
|
|
|
return $protocol.$http;
|
|
|
}
|
|
|
|
|
|
function getIdByUid($uid){
|
|
|
$id =Db::name('user')
|
|
|
->where('user_id',$uid)
|
|
|
->value('id');
|
|
|
if(!$id){
|
|
|
return false;
|
|
|
}
|
|
|
return $id;
|
|
|
}
|
|
|
|
|
|
function getIdByLid($lid){
|
|
|
$id =Db::name('user')
|
|
|
->where('line_id',$lid)
|
|
|
->value('id');
|
|
|
if(!$id){
|
|
|
return false;
|
|
|
}
|
|
|
return $id;
|
|
|
}
|
|
|
|
|
|
function getUseridByCuid($cuid){
|
|
|
$id =Db::name('user_sno')
|
|
|
->where('serial_no',$cuid)
|
|
|
->value('user_id');
|
|
|
|
|
|
if(!$id){
|
|
|
return false;
|
|
|
}
|
|
|
return $id;
|
|
|
}
|
|
|
|
|
|
function genUniqid($prefix='mc'){
|
|
|
$is_get = false;
|
|
|
while(!$is_get){
|
|
|
$uniqid = $prefix.uniqid();
|
|
|
|
|
|
$result1 = Db::name('user')
|
|
|
->where('user_id',$uniqid)
|
|
|
->count();
|
|
|
|
|
|
$result2 = Db::name('precard')
|
|
|
->where('user_id',$uniqid)
|
|
|
->count();
|
|
|
|
|
|
if(!$result1 && !$result2){
|
|
|
$is_get=true;
|
|
|
}
|
|
|
}
|
|
|
return $uniqid;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取得開通序號
|
|
|
*
|
|
|
* @author Wayne <wwayne.hsu@gmail.com>
|
|
|
*
|
|
|
* @param integer $num 數量
|
|
|
*
|
|
|
* @return array
|
|
|
*/
|
|
|
function genSerialNo($len=8){
|
|
|
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
$serialNumber = '';
|
|
|
$max = strlen($characters) - 1;
|
|
|
|
|
|
for ($i = 0; $i < $len; $i++) {
|
|
|
$randomIndex = random_int(0, $max);
|
|
|
$serialNumber .= $characters[$randomIndex];
|
|
|
}
|
|
|
|
|
|
//檢查是否存在DB,若存在則重新產生
|
|
|
$result = Db::name('precard')
|
|
|
->where('serial_no',$serialNumber)
|
|
|
->count();
|
|
|
|
|
|
if($result){
|
|
|
$serialNumber = genSerialNo($len);
|
|
|
}
|
|
|
|
|
|
return $serialNumber;
|
|
|
}
|
|
|
|
|
|
function genSNo($len=8){
|
|
|
//產生$len長度序號
|
|
|
$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
|
|
$rand = $code[rand(0,25)]
|
|
|
.strtoupper(dechex(date('m')))
|
|
|
.date('d').substr(time(),-5)
|
|
|
.substr(microtime(),2,5)
|
|
|
.sprintf('%02d',rand(0,99));
|
|
|
|
|
|
for(
|
|
|
$a = md5( $rand, true ),
|
|
|
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
|
|
$d = '',
|
|
|
$f = 0;
|
|
|
$f < $len;
|
|
|
$g = ord( $a[ $f ] ),
|
|
|
$d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
|
|
|
$f++
|
|
|
);
|
|
|
return $d;
|
|
|
}
|
|
|
|
|
|
function encodeRefer($userId)
|
|
|
{
|
|
|
$sourceString = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
|
|
|
$code = '';
|
|
|
while ($userId > 0) {
|
|
|
$mod = $userId % 35;
|
|
|
$userId = ($userId - $mod) / 35;
|
|
|
$code = $sourceString[$mod] . $code;
|
|
|
}
|
|
|
if (strlen($code) < 8)
|
|
|
$code = str_pad($code, 8, '0', STR_PAD_LEFT);
|
|
|
return $code;
|
|
|
}
|
|
|
|
|
|
function decodeRefer($code)
|
|
|
{
|
|
|
$sourceString = 'E5FCDG3HQA4B1NOPIJ2RSTUV67MWX89KLYZ';
|
|
|
//移除左側的 0
|
|
|
if (strrpos($code, '0') !== false)
|
|
|
$code = substr($code, strrpos($code, '0') + 1);
|
|
|
$len = strlen($code);
|
|
|
$code = strrev($code);
|
|
|
$num = 0;
|
|
|
for ($i = 0; $i < $len; $i++) {
|
|
|
$num += strpos($sourceString, $code[$i]) * pow(35, $i);
|
|
|
}
|
|
|
return $num;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能:生成二維碼
|
|
|
* @param string $qrData 手機掃描後要跳轉的網址
|
|
|
* @param string $qrLevel 預設糾錯比例 分為L、M、Q、H四個等級,H代表最高糾錯能力
|
|
|
* @param string $qrSize 二維碼圖大小,1-10可選,數字越大圖片尺寸越大
|
|
|
* @param string $savePath 圖片儲存路徑
|
|
|
* @param string $savePrefix 圖片名稱字首
|
|
|
*/
|
|
|
function createQRcode($savePath, $qrData = 'QR Code :)', $qrLevel = 'L', $qrSize = 4, $savePrefix = 'qrcode')
|
|
|
{
|
|
|
if (!isset($savePath)) return '';
|
|
|
|
|
|
//設定生成png圖片的路徑
|
|
|
$PNG_TEMP_DIR = $savePath;
|
|
|
//檢測並建立生成資料夾
|
|
|
if (!file_exists($PNG_TEMP_DIR)) {
|
|
|
mkdir($PNG_TEMP_DIR);
|
|
|
}
|
|
|
|
|
|
$filename = $PNG_TEMP_DIR . '/qrcode_temp.png';
|
|
|
$errorCorrectionLevel = 'L';
|
|
|
if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
|
|
|
$errorCorrectionLevel = $qrLevel;
|
|
|
}
|
|
|
$matrixPointSize = 4;
|
|
|
if (isset($qrSize)) {
|
|
|
$matrixPointSize = min(max((int)$qrSize, 1), 10);
|
|
|
}
|
|
|
if (isset($qrData)) {
|
|
|
if (trim($qrData) == '') {
|
|
|
die('data cannot be empty!');
|
|
|
}
|
|
|
//生成檔名 檔案路徑+圖片名字字首+md5(名稱)+.png
|
|
|
$filename = $PNG_TEMP_DIR . $savePrefix . '_qrcode.png';
|
|
|
|
|
|
//開始生成
|
|
|
\PHPQRCode\QRcode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
|
|
|
} else {
|
|
|
//預設生成
|
|
|
\PHPQRCode\QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
|
|
|
}
|
|
|
if (file_exists($PNG_TEMP_DIR . basename($filename)))
|
|
|
return basename($filename);
|
|
|
else
|
|
|
return FALSE;
|
|
|
}
|
|
|
|
|
|
function genQrCode($url,$user_id,$prefix)
|
|
|
{
|
|
|
//$savePath 圖片儲存路徑
|
|
|
$savePath = $_SERVER['DOCUMENT_ROOT'].'/storage/'.$user_id.'/';
|
|
|
//路徑
|
|
|
$webPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $savePath);
|
|
|
|
|
|
//$qrData 手機掃描後要跳轉的網址
|
|
|
$qrData = $url;
|
|
|
|
|
|
// $qrLevel 預設糾錯比例 分為L、M、Q、H四個等級,H代表最高糾錯能力
|
|
|
$qrLevel = 'H';
|
|
|
//$qrSize 二維碼圖大小,1-10可選,數字越大圖片尺寸越大
|
|
|
$qrSize = '4';
|
|
|
// $savePrefix 圖片名稱字首
|
|
|
$savePrefix = $user_id.'_'.$prefix;
|
|
|
$pic = '';
|
|
|
$filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix);
|
|
|
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
|
|
|
$pic = $webPath . $filename;
|
|
|
return $pic;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
function asc_trim($val){
|
|
|
if(!is_array($val)){
|
|
|
return trim($val);
|
|
|
}else{
|
|
|
return $val;
|
|
|
}
|
|
|
}
|