$val){ if(!$val){ continue; } $str .= $k .'=' . $val . '&'; } $str = rtrim($str, '&'); } } return $str; } } if(!function_exists('randCode')){ /** * Notes:生成随机验证码 * @param int $num 生成位数 * @param bool $isLetter 是否带有字母 true-纯数字 false-带有字母 * @return int|string * User: yym * Date: 2022/7/23 */ function randCode(int $num, $isLetter = false) { if($isLetter){ $codeMin = ''; $codeMax = ''; for($i=1;$i<=$num;$i++){ $codeMin.=0; $codeMax.=9; } return rand(1 . substr($codeMin, 0, $num-1),$codeMax); } if(!$isLetter){ //如果想调整权重,自己可以根据需求修改$codeArr这个一位数组 $codeArr = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','e','s','t','u','v','w','x','y','z', '1','2','3','4','5','6','7','8','9','0']; $codeKeys = array_rand($codeArr,$num); shuffle($codeKeys); $codeStr = ''; foreach ($codeKeys as $codeValue){ $codeStr .= $codeArr[$codeValue]; } return $codeStr; } } } if(!function_exists('curlGet')){ /** * Notes:curl Get请求 * @param $url * @return mixed * User: yym * Date: 2022/7/26 */ function curlGet($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } } if(!function_exists('imgToBase64')){ /** * Notes:图片转base64 * @param $path * @return string * @throws Exception * User: yym * Date: 2022/7/28 */ function imgToBase64($path){ //对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回 $img_data = ""; if (substr($path,0,strlen("http")) === "http" || substr($path,0,strlen("https")) === "https"){ $img_data = $path; }else{ if($fp = fopen($path, "rb", 0)) { $binary = fread($fp, filesize($path)); // 文件读取 fclose($fp); $img_data = base64_encode($binary); // 转码 }else{ throw new Exception('图片不存在'); } } return $img_data; } } if(!function_exists('curlRequest')){ /** * Notes:CURL post请求 * @param $url * @param $method * @param $headers * @param $body * @return false|string * @throws Exception * User: yym * Date: 2022/7/28 */ function curlRequest($url,$method = 'POST',$headers,$body) { $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); 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, true); if (1 == strpos("$".$url, "https://")){ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $result = curl_exec($curl); $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $rheader = substr($result, 0, $header_size); $rbody = substr($result, $header_size); $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE); if($httpCode == 200){ return $rbody; }else{ throw new Exception('错误:header-' . $rheader . "\n body:" . $rbody, $httpCode); } } } if(!function_exists('getOcrInfo')){ /** * Notes:身份证ocr识别 * @param $img_path * @param string $type * @return mixed * @throws Exception * User: yym * Date: 2022/7/28 */ function getOcrInfo($img_path, $type = 'face') { $url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"; $appcode = config('app')['aly_ocr']['app_code'] ?? ''; $headers = array(); array_push($headers, "Authorization:APPCODE " . $appcode); //根据API的要求,定义相对应的Content-Type array_push($headers, "Content-Type".":"."application/json; charset=UTF-8"); //如果没有configure字段,config设为空 $config = array( "side"=>$type ); $img_data = imgToBase64($img_path); $request = array( "image" => "$img_data" ); if(count($config) > 0){ $request["configure"] = json_encode($config); } $body = json_encode($request); $result = curlRequest($url, 'POST', $headers, $body); $res = json_decode($result, true); return $res; } } if(!function_exists('createOrderSn')){ /** * Notes:生成订单 * @param int $length * @return string * User: yym * Date: 2022/7/29 */ function createOrderSn(int $length) { $date = date("YmdHis"); $rand = $date . randCode($length - strlen($date), true); if(strlen($rand) > $length) { createOrderSn($length); } if(\support\Redis::exists('order:order_sn:' . $rand)) { sleep(1); createOrderSn($length); } \support\Redis::setEx('order:order_sn:' . $rand, 10, 1); return $rand; } } if(!function_exists('unique2dArrayByKey')){ /** * Notes:二位数组按照某个健去重 * @param $_2d_array * @param $unique_key * @return array * User: yym * Date: 2022/7/30 */ function unique2dArrayByKey($_2d_array, $unique_key) { $tmp_key[] = array(); foreach ($_2d_array as $key => &$item) { if (is_array($item) && isset($item[$unique_key])) { if (in_array($item[$unique_key], $tmp_key)) { unset($_2d_array[$key]); } else { $tmp_key[] = $item[$unique_key]; } } } //重置一下二维数组的索引 return array_slice($_2d_array, 0, count($_2d_array), false); } } if(!function_exists('plog')) { /** * Notes: * @param $type * @param $name * @param string $content * @return false|true * User: yym * Date: 2022/9/21 */ function plog($type, $name, $content = '') { if(empty($type) || empty($name)){ return false; } $log = array( 'log_admin_id' => request()->admin_id ?? 0, 'log_name' => $name, 'log_type' => $type, 'log_content' => $content, 'log_ip' => get_ip(), 'log_create_time' => time() ); \app\admin\model\SystemPermLog::insert($log); return true; } } if(!function_exists('timeDifference')) { function timeDifference($start_time, $end_time) { //计算两个时间段差 $difference = strtotime(date("Y-m-d", time()) . $end_time) - strtotime(date("Y-m-d", time()) . $start_time); $time_slot = 30 * 60; $count = ceil($difference / $time_slot); $data = array(); for ($i = 0; $i < $count; $i ++) { $time = ''; $time = date("H:i", (strtotime(date("Y-m-d", time()) . $start_time) + $i * $time_slot)); $data[$time] = 0; } return $data; } } if (!function_exists('get_tree_children')) { /** * tree 子菜单 * @param array $data 数据 * @param string $childrenname 子数据名 * @param string $keyName 数据key名 * @param string $pidName 数据上级key名 * @return array * User: ZQ * Date: 2022/9/30 */ function get_tree_children(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid') { $list = array(); foreach ($data as $value) { $list[$value[$keyName]] = $value; } static $tree = array(); //格式化好的树 foreach ($list as $item) { if (isset($list[$item[$pidName]])) { $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]]; } else { $tree[] = &$list[$item[$keyName]]; } } return $tree; } } if(!function_exists('appointment_data')) { /** * Notes:医生设置时间处理 * @param array $data * @param $key * @return array|object * User: yym * Date: 2022/10/13 */ function appointment_data(array $data, $key) { if(empty($data) || empty($key)) { return (object)array(); } $appointment_data = json_decode($data[$key], true); if(empty($appointment_data)) { return (object)array(); } $morning = array_keys($appointment_data['morning_time_data']); $afternoon = array_keys($appointment_data['afternoon_time_data']); $appointment = array( 'morning_start' => empty($morning) ? '' : array_shift($morning), 'morning_end' => empty($morning) ? '' : date("H:i", strtotime(array_pop($morning)) + 30 * 60), 'appointment_sum' => !empty($appointment_data['morning_time_data']) ? array_shift($appointment_data['morning_time_data']) : (!empty($appointment_data['afternoon_time_data']) ? array_shift($appointment_data['afternoon_time_data']) : 0), 'afternoon_start' => empty($afternoon) ? '' : array_shift($afternoon), 'afternoon_end' => empty($afternoon) ? '' : date("H:i", strtotime(array_pop($afternoon)) + 30 * 60), ); return $appointment; } } if(!function_exists('getRandomString')){ /** * Notes:生成指定位数唯一字符串 * @param $len * @param null $chars * @return string * User: yym * Date: 2022/10/21 */ function getRandomString($len, $chars=null) { if (is_null($chars)){ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; } mt_srand(10000000*(double)microtime()); for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){ $str .= $chars[mt_rand(0, $lc)]; } return $str; } } if(!function_exists('curlPost')){ /** * Notes:CURL https 请求 * @param $url * @param $data * @return bool|string * User: yym * Date: 2022/9/5 */ function curlPost($url, $data){ $header = array( 'Accept: application/json', ); $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在 // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer // 设置请求头 curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $tmpInfo = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $tmpInfo; // 返回数据 } }