123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 应用公共文件
- /*
- * 用户是否登录
- */
- function is_login($type){
- $user = session($type.'_auth');
- if (empty($user)) {
- return false;
- }
- return $user;
- }
- function is_admin(){
- $user = is_login('admin');
- $role_id = $user['role']['id'];
- if ($role_id == 1) {
- return true;
- }
- return false;
- }
- /**
- * 获取本站token
- */
- function getlocaltoken($name){
- $where['name'] = $name;
- $user = $User->where($where)->find();
- if (!$user) {
- return false;
- }
- $time = $this->request->time();
- $token_expire = $user['token_expire'];
- if (!empty($token_expire)) {
- list($token,$expire) = explode('|', $token_expire);
- if ($time < $expire) {
- return $token;
- }
- }
- $token = md5(guid());
- $expire = $this->request->time() + 7200;
- $user->token = $token .'|'.$expire;
- $user->save();
- return $token;
- }
- /**
- * 是否是微信浏览器
- * @return bool
- */
- function isWxClient(){
- return strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
- }
- /**
- * 判断是否支付宝内置浏览器访问
- * @return bool
- */
- function isAliClient(){
- return strpos($_SERVER['HTTP_USER_AGENT'], 'Alipay') !== false;
- }
- /**
- * 远程获取数据
- */
- function curl($url, $data = '',$headers = [],$timeout = 60,$issplit = false) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- @curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- if ($data) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSLVERSION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
- if (!empty($headers) && is_array($headers)) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- }
- ob_start();
- $res = curl_exec($ch);
- $headersize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
- $header = substr($res, 0, $headersize);
- $body = substr($res, $headersize);
- ob_end_clean();
- curl_close($ch);
- unset($ch);
- if ($issplit) {
- return ['header'=>$header,'body'=>$body];
- }
- return $res;
- }
- /**
- * 获取随机数
- */
- function random($length, $numeric = FALSE) {
- $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
- $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
- if ($numeric) {
- $hash = '';
- } else {
- $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
- $length--;
- }
- $max = strlen($seed) - 1;
- for ($i = 0; $i < $length; $i++) {
- $hash .= $seed{mt_rand(0, $max)};
- }
- $hash=strtoupper($hash);
- return $hash;
- }
- function getNonceStr($length = 32) {
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
- /*生成全球唯一标识*/
- function guid($fix=true){
- $left=$fix?chr(123):'';
- $right=$fix?chr(125):'';
- if (function_exists('com_create_guid')){
- $uuid=com_create_guid();
- if(!$fix){
- $uuid=substr($uuid,1,-1);
- }
- return $uuid;
- }else{
- mt_srand((double)microtime()*10000);
- $charid = strtoupper(md5(uniqid(rand(), true)));
- $hyphen = chr(45);
- $uuid = $left
- .substr($charid, 0, 8).$hyphen
- .substr($charid, 8, 4).$hyphen
- .substr($charid,12, 4).$hyphen
- .substr($charid,16, 4).$hyphen
- .substr($charid,20,12)
- .$right;
- return $uuid;
- }
- }
- //生成二维码
- function createqrcode($data,$level = 'L',$size=4){
- $name = md5($data);
- vendor("phpqrcode.phpqrcode");
- $relativePath = 'uploads'.DS.date('Ymd',request()->time());
- $absolutePath = ROOT_PATH.'public'.DS. $relativePath;
- $file = $absolutePath.DS.$name.".png";
- if (!is_file($file)) {
- is_dir($absolutePath)?'':mkdir($absolutePath, 0755, true);
- $object = new \QRcode();
- $object->png($data,$file, 'L',6);
- }
- return request()->domain(). DS.$relativePath.DS.$name.".png";
- }
- //redis连接
- function redis_connect(){
- vendor("Predis.autoload");
- $redis = new \Predis\Client(array(
- 'scheme' => 'tcp',
- 'host' => '127.0.0.1',
- 'port' => 6379,
- ));
- $password = \think\Config::get('redis.password');
- if (!empty($password)) {
- $redis->auth($password);
- }
- return $redis;
- }
- /**
- * 自增长单号
- */
- function aino($prefix='',$format='Ymd',$length=3){;
- $redis = redis_connect();
- $date = empty($format)?'':date($format);
- $key = $prefix.$date;
- $no = $redis->incr($key);
- if ($no == 1) {
- $redis->expireat($key,strtotime($date) + 86400);
- }
- $no = str_pad($no,$length,"0",STR_PAD_LEFT);
- $no = $key.$no;
- return $no;
- }
- /**
- * 获取文件路径
- */
- function getfileurl($id){
- $File = model('File');
- $file = $File->where(['id'=>$id])->find();
- return DS. 'public' . DS . 'uploads' .DS .$file['name'];
- }
- /**
- * 获取配置信息
- */
- function cnf($key,$value = null){
- if (is_null($value) && is_string($key)) {
- $value = model('Config')->where(['key'=>$key])->value("value");
- $value = json_decode($value,true);
- return $value;
- } else {
- $value = json_encode($value,JSON_UNESCAPED_UNICODE);
- return model('Config')->where(['key'=>$key])->update(["value"=>$value]);
- }
- }
- /**
- * 字符串截取,支持中文和其他编码
- * @static
- * @access public
- * @param string $str 需要转换的字符串
- * @param string $start 开始位置
- * @param string $length 截取长度
- * @param string $charset 编码格式
- * @param string $suffix 截断显示字符
- * @return string
- */
- function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
- if(function_exists("mb_substr"))
- $slice = mb_substr($str, $start, $length, $charset);
- elseif(function_exists('iconv_substr')) {
- $slice = iconv_substr($str,$start,$length,$charset);
- if(false === $slice) {
- $slice = '';
- }
- }else{
- $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
- $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
- $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
- $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
- preg_match_all($re[$charset], $str, $match);
- $slice = join("",array_slice($match[0], $start, $length));
- }
- return $suffix ? $slice.'...' : $slice;
- }
- function amount_format($amount){
- $amount = trim($amount);
- return sprintf("%.2f",substr(sprintf("%.3f", $amount), 0, -1));
- }
- function sendtomobile($mobile,$content,$ajax = true){
- }
- function sendtoemail($email,$content,$ajax = true){
- $sms_cnf = cnf('sms');
- $email_cnf = $sms_cnf['mobile'];
- require_once VENDOR_PATH.'PHPMailer/src/Exception.php';
- require_once VENDOR_PATH.'PHPMailer/src/PHPMailer.php';
- require_once VENDOR_PATH.'PHPMailer/src/SMTP.php';
- $mail = new \PHPMailer\PHPMailer\PHPMailer(true); //实例化
- try {
- $sms_cnf = cnf('sms');
- $email_cnf = $sms_cnf['email'];
- $mail->isSMTP();
- $mail->CharSet = "UTF-8";
- $mail->Host = $email_cnf['smtp'];
- $mail->Port = 465;
- $mail->SMTPAuth = true;
- $mail->SMTPSecure = "ssl";
- $mail->CharSet = "UTF-8";
- $mail->Encoding = "base64";
- $mail->Username = $email_cnf['account'];
- $mail->Password = $email_cnf['password'];
- $mail->From = $email_cnf['account'];
- $mail->FromName = $email_cnf['from'];
- $mail->AddAddress($email, "Hello");
- $mail->IsHTML(true);
- $mail->Subject = '【'.$email_cnf['from'].'】';
- $mail->Body = $content;
- $mail->Send();
- if ($ajax) {
- output(0,'发送成功');
- }
- return true;
- }catch (\PHPMailer\PHPMailer\Exception $e) {
- output(1,'发送失败1');
- }
- }
- function output($code,$msg,$data = [],$count = 0,$pages = 1){
- try {
- // 返回JSON数据格式到客户端 包含状态信息
- $count = $count == 0?count($data):$count;
- $data = json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count,'pages'=>$pages], JSON_UNESCAPED_UNICODE);
- if ($data === false) {
- throw new \InvalidArgumentException(json_last_error_msg());
- }
- header('Content-type: application/json');
- echo $data;exit();
- } catch (\Exception $e) {
- if ($e->getPrevious()) {
- throw $e->getPrevious();
- }
- throw $e;
- }
- }
- function oplog($position,$user_id){
- $request = request();
- $Log = model('Log');
- $Log->user_id = $user_id;
- $Log->action = $request->module() . '/' . $request->controller() . '/' . $request->action();
- $Log->params = $request->param();
- $Log->position = $position;
- $Log->save();
- }
- function common_curl($url, $param, $dataType = 0, $header = [])
- {
- $headers = [
- "Content-type:application/json;charset=utf-8",
- "Accept:*/*",
- "Connection:close",
- "User-Agent:DSMall/6.1.0;PHP-Curl",
- ];
- //dataType 1是 x-www-form形式 0是默认的json形式
- if ($dataType == 1) {
- $post_data = http_build_query($param);//重点
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- // curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据
- curl_setopt($curl, CURLOPT_TIMEOUT, (int)10);//超时设置
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);//重点
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
- // curl_setopt(curl,CURLOPTHTTPHEADER,$header);
- $result = curl_exec($curl);
- curl_close($curl);
- $result = json_decode($result, 1);
- return $result;
- } else {
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
- $result = curl_exec($ch);
- if (curl_errno($ch)) $result = curl_error($ch);
- curl_close($ch);
- $result = json_decode($result, 1);
- return $result;
- }
- }
|