123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- <?php
- use think\Db;
- function requestPost($url , $post_data = array() ){
- // 1. 初始化一个cURL会话
- $ch = curl_init();
- // 2. 设置请求选项, 包括具体的url
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- // 设置请求为post类型
- curl_setopt($ch, CURLOPT_POST, 1);
- // 添加post数据到请求中
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- // 3. 执行一个cURL会话并且获取相关回复
- $response = curl_exec($ch);
- // 4. 释放cURL句柄,关闭一个cURL会话
- curl_close($ch);
- return $response;
- }
- function requestGet($url , $msg = ''){
- // 1. 初始化一个cURL会话
- $ch = curl_init();
- //设置选项,包括URL
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- //执行并获取HTML文档内容
- $response = curl_exec($ch);
- // 4. 释放cURL句柄,关闭一个cURL会话
- curl_close($ch);
- return $response;
- }
- function http_curl($url,$type='get',$res='json',$arr=''){
- $headers = array();
- //根据API的要求,定义相对应的Content-Type
- array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_FAILONERROR, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_HEADER, false);
- $output = curl_exec($curl);
- curl_close($curl);
- if($res=='json'){
- if($output === false){
- //请求失败,返回错误信息
- return curl_error($curl);
- }else{
- //请求成功,返回信息
- return json_decode($output,true);
- }
- }
- }
- function curl_get($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- if($output === false){
- return curl_error($ch);
- }else{
- return json_decode($output,true);
- }
- }
- // 获取模板区域
- function get_city_area()
- {
- $field=['id','pid','name'];
- $list=Db::name('china_area')->where('pid',0)->field($field)->select();
- foreach ($list as $k=>&$v){
- $v['children']= Db::name('china_area')->where('pid',$v['id'])->field($field)->select();
- }
- return $list;
- }
- //判断字段存在并不为空
- function isset_full($arr, $key)
- {
- if (isset($arr[$key]) && !empty($arr[$key])) {
- return true;
- } else {
- return false;
- }
- }
- //判断字段存在并不为空 并且等于验证值
- function isset_full_check($arr, $key,$check_val)
- {
- if (isset($arr[$key]) && !empty($arr[$key]) && $arr[$key] == $check_val) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 把返回的数据集转换成Tree
- * @param array $list 要转换的数据集
- * @param string $pk 自增字段(栏目id)
- * @param string $pid parent标记字段
- * @return array
- */
- function make_tree($list,$pk='id',$pid='pid',$child='children',$root=0){
- if(is_object($list)) $list = $list->toArray();
- $tree=array();
- $packData=array();
- foreach ($list as $data) {
- $packData[$data[$pk]] = $data;
- }
- foreach ($packData as $key =>$val){
- if($val[$pid]==$root){//代表跟节点
- $tree[]=& $packData[$key];
- }else{
- $packData[$val[$pid]][$child][]=& $packData[$key]; //找到其父类
- }
- }
- return $tree;
- }
- /**
- * 用于测试打印数组数据
- **/
- function p($arr)
- {
- header('content-type:text/html;charset=utf-8');
- echo '<pre>';
- print_r($arr);
- echo '</pre>';
- }
- /**
- * 返回JSON统一格式
- *
- * @param int $code 返回状态
- * @param string $msg 返回提示信息
- * @param array $data 返回对象
- * @return array
- */
- function V($code, $msg, $data = array())
- {
- return array('code' => $code, 'msg' => $msg, 'data' => $data);
- }
- /**
- * 格式化表格导入的时间为php时间
- **/
- function excel_time_conversion($data){
- // excel软件中的日期是从 1900-01-01 开始计算的
- //但是php 是从 1970-01-01开始计算的。
- //这两者间有一个天数差 25569
- $d = 25569; // excel和php之间相差的时间
- $t = 24 * 60 * 60; // 一天24小时
- return gmdate('Y-m-d', ($data - $d) *$t);
- }
- /**
- * @desc 时间生成
- * @param $type 1、本日 2、本周 3、本月 4、本年 5、上个月 6、指定月份
- * @return array
- */
- function mk_time($type,$date){
- switch($type){
- case 1://本日
- $start = mktime(0,0,0,date('m'), date('d'), date('Y'));
- $end = mktime(23,59,59,date('m'),date('d'),date('Y'));
- break;
- case 2://本周
- $date_w = date('w');
- if($date_w == 0) $date_w = 7;
- $start = mktime(0,0,0,date('m'),date('d')-$date_w+1,date('Y'));
- $end = mktime(23,59,59,date('m'),date('d')-$date_w+7,date('Y'));
- break;
- case 3://本月
- $start = mktime(0,0,0,date('m'),1,date('Y'));
- $end = mktime(23,59,59,date('m'),date('t'),date('Y'));
- break;
- case 4://本年
- $start = mktime(0,0,0,1,1,date('Y'));
- $end = mktime(0,0,0,12,31,date('Y'));
- break;
- case 5://上个月
- $start = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
- $end = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));
- break;
- case 6://指定月份
- $y = $date[0];
- $m = $date[1];
- $d = date('t', strtotime($y.'-'.$m));
- $start = strtotime($y.'-'.$m);
- $end = mktime(23,59,59,$m,$d,$y);
- break;
- default:
- $t = mk_time(1,[]);
- $start = $t['start'];
- $end = $t['end'];
- break;
- }
- return array('start' => $start, 'end' => $end);
- }
- /**
- * 生成log文件
- * @param $dir
- * @param $content
- */
- function user_log($dir,$content){
- $log_dir=RUNTIME_PATH.'/'.$dir;
- if(!is_dir($log_dir)){
- mkdir($log_dir,0777,true);
- }
- $filename=date('Y-m-d').'.log';
- if(is_array($content)){
- $content=json_encode($content,JSON_UNESCAPED_UNICODE);
- }
- file_put_contents($log_dir.'/'.$filename,date('Y-m-d H:i:s ').$content.PHP_EOL,FILE_APPEND);
- }
- /**
- * 获取订单号
- **/
- function get_order_sn($user_id)
- {
- $rand = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
- $order_sn = date('Yhis') . $rand;
- $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);
- return $order_sn . $id;
- }
- /**
- * 一维数组转为二维数组
- *
- * @param array $arr
- * @param string $str1
- * @param string $str2
- * @return array
- */
- function get_one_two_array($arr, $str1, $str2)
- {
- $item = [];
- foreach ($arr as $k => $v) {
- $item[] = [
- $str1 => $k,
- $str2 => $v,
- ];
- }
- return $item;
- }
- /**
- * 秒转换为天
- *
- * @param integer $remain_time
- */
- function get_stay_time($remain_time, $is_hour = 1, $is_minutes = 1)
- {
- $day = floor($remain_time / (3600*24));
- $day = $day > 0 ? $day.'天' : '';
- $hour = floor(($remain_time % (3600*24)) / 3600);
- $hour = $hour > 0 ? $hour.'小时' : '';
- if($is_hour && $is_minutes) {
- $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
- $minutes = $minutes > 0 ? $minutes.'分钟' : '';
- return $day.$hour.$minutes;
- }
- if($hour) {
- return $day.$hour;
- }
- return $day;
- }
- /**
- * +----------------------------------------------------------
- * 生成随机字符串
- * +----------------------------------------------------------
- * @param int $length 要生成的随机字符串长度
- * @param string $type 随机码类型:0,数字+大小写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符
- * +----------------------------------------------------------
- * @return string
- * +----------------------------------------------------------
- */
- function randCode($length = 5, $type = 0)
- {
- $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
- if ($type == 0) {
- array_pop($arr);
- $string = implode("", $arr);
- } elseif ($type == "-1") {
- $string = implode("", $arr);
- } else {
- $string = $arr[$type];
- }
- $count = strlen($string) - 1;
- $code = '';
- for ($i = 0; $i < $length; $i++) {
- $code .= $string[rand(0, $count)];
- }
- return $code;
- }
- /**
- * 事务开始
- *
- * @return void
- */
- function db_start()
- {
- Db::startTrans();
- }
- /**
- * 事务提交
- *
- * @return void
- */
- function db_commit()
- {
- Db::commit();
- }
- /**
- * 事务回滚
- *
- * @return void
- */
- function db_rollback()
- {
- Db::rollback();
- }
- /**
- * 抛出响应异常(返回信息)
- *
- * @param string $msg 记录日志的错误信息, 如果returnMsg不配置, 则也为返回错误信息
- * @param integer $type: 1.不记录日志只返回错误信息, 2.事务回退,记录日志并返回错误信息, 3.记录日志并返回错误信息
- * @param Throwable|null $e 异常, 可为null
- * @param string $returnMsg 返回错误信息, 可不配置, 默认为msg
- * @param string $code 错误码
- * @param array $headers 头部信息
- * @param mixed $data 返回数据
- */
- function except($msg = '', $type = 1, $e = null, $returnMsg = '', $code = 0, array $header = [], $data = null)
- {
- $type == 1 && mexcept($msg, $code, $header, $data);
- $type == 2 && db_rollback();
- // 返回信息
- $returnMsg && $msg = $returnMsg;
- mexcept($msg, $code, $header, $data);
- }
- /**
- * 抛出异常(真正)
- *
- * @param string $msg
- * @param integer $code
- * @param array $headers
- * @param mixed $data
- * @return void
- */
- function mexcept($msg = '', $code = 0, $headers = [], $data = null)
- {
- $time = (int)request()->server('REQUEST_TIME') ?: time();
- abort(json(compact('code', 'msg', 'data', 'time'), null, $headers));
- }
|