ThinkPHP 公共函数整理
程序员文章站
2022-05-03 19:34:38
//通用增加
function add($dataName,$data){
return M($dataName)->add($data);
}
//通用...
//通用增加 function add($dataName,$data){ return M($dataName)->add($data); } //通用删除 function del($dataName,$where){ return M($dataName)->where($where)->delete(); } //通用修改 function edit($dataName,$where,$data){ return M($dataName)->where($where)->save($data); //echo M($dataName)->_sql();die;//输出sql语句 }
/** * 用户二维码图片 * @param $user_id [用户id] * @param $recommend[推荐码] * @param $data[链接地址] */ //生成图片二维码 $data链接 $id user_id; function code($url,$user_id,$recommend){ $user=M('users')->field('user_name')->where('user_id='.$user_id)->find(); $user_name=$user['user_name']; $canshu="?r_ec=$recommend"; $url=$url.$canshu; vendor("phpqrcode.phpqrcode"); // 纠错级别:L、M、Q、H $level = 'H'; // 点的大小:1到10,用于手机端4就可以了 $size = 4; // 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false $path = "Public/code/".date("ymd")."/"; if(!is_dir($path)) { mkdir($path); } $fileName = $path.$user_name.'.png'; $data['code']=$fileName; M('users')->where('user_id='.$user_id)->save($data); ob_end_clean();//清空缓冲区 QRcode::png($url, $fileName, $level, $size,1); $furl=C('REMOTE_ROOT').$fileName; ftp_upload($furl,$fileName); } /** * 商品历史浏览记录 * $data 商品记录信息 */ function _history($data) { if(!$data || !is_array($data)) { return false; } //判断cookie类里面是否有浏览记录 if(!empty($_COOKIE['history'])) { $history = unserialize($_COOKIE['history']); array_unshift($history, $data); //在浏览记录顶部加入 /* 去除重复记录 */ $rows = array(); foreach ($history as $v) { if(in_array($v, $rows)) { continue; } $rows[] = $v; } /* 如果记录数量多余5则去除 */ while (count($rows) > 5) { array_pop($rows); //弹出 } setcookie('history',serialize($rows),time() + 3600 * 24 * 30,'/'); } else { $history = serialize(array($data)); setcookie('history',$history,time() + 3600 * 24 * 30,'/'); } } //ue编辑器通过FTP上传图片($str代表从表单接收到的content字符串),返回处理后的content字符串 function ftp_image_upload($str){ $local_root = $_SERVER['DOCUMENT_ROOT']; $host = C('FTP_SEVER'); $remote_root = C('REMOTE_ROOT'); $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg|\.mp4]))[\'|\"].*?[\/]?>/"; preg_match_all($pattern,$str,$match); foreach ($match[1] as $k => $v) { $localfile = $local_root.$v; $remotefile = $remote_root.$v; ftp_upload($remotefile,$localfile); } return str_replace("src=\"/Public", "src=\"$host/Public", $str); } //FTP上传文件函数 function ftp_upload($remotefile,$localfile){ $ftp = new \Think\Ftp(); $data['server'] = C('FTP_HOST'); $data['username'] = C('FTP_NAME');//ftp帐户 $data['password'] = C('FTP_PWD');//ftp密码 $data['port'] = C('FTP_PORT');//ftp端口,默认为21 $data['pasv'] = C('FTP_PASV');//是否开启被动模式,true开启,默认不开启 $data['ssl'] = C('FTP_SSL');//ssl连接,默认不开启 $data['timeout'] = C('FTP_TIMEOUT');//超时时间,默认60,单位 s $info = $ftp->start($data); if($info){ if($ftp->put($remotefile,$localfile)){} } $ftp->close(); }
/** * $param 数组 传入的参数需要与API的模板对应 * * $mobile 手机号码 */ function send_verify($mobile='18831605741'){ $rand=md5(rand(000000,999999)); cookie('code',$rand,time()+6000,"/"); $param['code']=$rand; $appkey = "23496841"; $secret = "f3b9840f579585feeb37b02474fe6bd0"; vendor('Alimsg.top.TopClient'); vendor('Alimsg.top.ResultSet'); vendor('Alimsg.top.RequestCheckUtil'); vendor('Alimsg.top.TopLogger'); vendor('Alimsg.top.request.AlibabaAliqinFcSmsNumSendRequest'); vendor('Alimsg.TopSdk.php'); $c = new \TopClient; $c->appkey = $appkey; $c->secretKey = $secret; $req = new \AlibabaAliqinFcSmsNumSendRequest; $req->setSmsTemplateCode("SMS_24575052"); $req->setExtend("3"); $req->setSmsType("normal"); $req->setSmsFreeSignName("简单主义"); $req->setSmsParam($param); $req->setRecNum($mobile); $resp = $c->execute($req); }
/** * 微信扫码支付 * @param array $order 订单 必须包含支付所需要的参数 body(产品描述)、total_fee(订单金额)、out_trade_no(订单号)、product_id(产品id) */ function weixinpay($order){ $order['trade_type']='NATIVE'; Vendor('Weixinpay.Weixinpay'); $weixinpay=new \Weixinpay(); return $weixinpay->pay($order); } /** * 生成二维码 * @param string $url url连接 * @param integer $size 尺寸 纯数字 */ function qrcode($url,$size=4){ vendor("phpqrcode.phpqrcode"); QRcode::png($url,false,QR_ECLEVEL_L,$size,2,false,0xFFFFFF,0x000000); } function is_weixin(){ if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') === false ){ return false; }else{ return ture; } }
/** * 使用curl获取远程数据 * @param string $url url连接 * @return string 获取到的数据 */ function curl_get_contents($url){ $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址 // curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息 curl_setopt($ch, CURLOPT_TIMEOUT, 5); //设置超时 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //用户访问代理 User-Agent curl_setopt($ch, CURLOPT_REFERER,$_SERVER['HTTP_HOST']); //设置 referer curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果 $r=curl_exec($ch); curl_close($ch); return $r; } //获取用户ip地址 function get_ip() { static $ip; if (isset($_SERVER)) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $ip = $_SERVER["HTTP_CLIENT_IP"]; } else { $ip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $ip = getenv("HTTP_CLIENT_IP"); } else { $ip = getenv("REMOTE_ADDR"); } } if (preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $ip)) { return $ip; } else { return '127.0.0.1'; } }
//显示图片 function showImage($url, $width = '', $height = '') { $ic = C('FTP_SEVER').C('REMOTE_ROOT'); if($width) $width = "width='$width'"; if($height) $height = "height='$height'"; echo "<img ".$width. " ".$height." src="."'".$ic.$url."' />"; } ** * 公用的方法 进行信息的提示 */ function showMsg($status,$message,$data=array()){ $result = array( 'status' => $status, 'message' =>$message, 'data' =>$data ); exit(json_encode($result)); } /** * 对象转化为数组 * @param $e * @return array|void */ function objectToArray($e){ $e=(array)$e; foreach($e as $k=>$v){ if( gettype($v)=='resource' ) return; if( gettype($v)=='object' || gettype($v)=='array' ) $e[$k]=(array)objectToArray($v); } return $e; } function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n","\r");$hou=array("","","","",""); return str_replace($qian,$hou,$str); } /** * excel表数据转化为数组 * @param string $filePath * @param int $sheet * @return array|void */ function format_excel2array($filePath='',$sheet=0){ if(empty($filePath) or !file_exists($filePath)){die('file not exists');} $PHPReader = new PHPExcel_Reader_Excel2007(); //建立reader对象 if(!$PHPReader->canRead($filePath)){ $PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($filePath)){ echo 'no Excel'; return ; } } $PHPExcel = $PHPReader->load($filePath); //建立excel对象 $currentSheet = $PHPExcel->getSheet($sheet); //**读取excel文件中的指定工作表*/ $allColumn = $currentSheet->getHighestColumn(); //**取得最大的列号*/ $allRow = $currentSheet->getHighestRow(); //**取得一共有多少行*/ $data = array(); for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ //循环读取每个单元格的内容。注意行从1开始,列从A开始 for($colIndex='A';$colIndex<=$allColumn;$colIndex++){ $addr = $colIndex.$rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); if($cell instanceof PHPExcel_RichText){ //富文本转换字符串 $cell = $cell->__toString(); } $data[$rowIndex][$colIndex] = $cell; } } return $data; }
上一篇: 村民看病成笑话
下一篇: js操作select