functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * Here is your custom functions.
  4. */
  5. if(!function_exists('KsortAscll')){
  6. /**
  7. * Notes:ascll正序排序
  8. * @param array $params
  9. * @return string
  10. * User: yym
  11. * Date: 2022/7/23
  12. */
  13. function KsortAscll($params = array()){
  14. $str = '';
  15. if(!empty($params)){
  16. $p = ksort($params);
  17. if($p){
  18. foreach ($params as $k=>$val){
  19. if(!$val){
  20. continue;
  21. }
  22. $str .= $k .'=' . $val . '&';
  23. }
  24. $str = rtrim($str, '&');
  25. }
  26. }
  27. return $str;
  28. }
  29. }
  30. if(!function_exists('randCode')){
  31. /**
  32. * Notes:生成随机验证码
  33. * @param int $num 生成位数
  34. * @param bool $isLetter 是否带有字母 true-纯数字 false-带有字母
  35. * @return int|string
  36. * User: yym
  37. * Date: 2022/7/23
  38. */
  39. function randCode(int $num, $isLetter = false)
  40. {
  41. if($isLetter){
  42. $codeMin = '';
  43. $codeMax = '';
  44. for($i=1;$i<=$num;$i++){
  45. $codeMin.=0;
  46. $codeMax.=9;
  47. }
  48. return rand(1 . substr($codeMin, 0, $num-1),$codeMax);
  49. }
  50. if(!$isLetter){
  51. //如果想调整权重,自己可以根据需求修改$codeArr这个一位数组
  52. $codeArr = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i',
  53. 'j','k','l','m','n','o','p','q','e','s','t','u','v','w','x','y','z',
  54. '1','2','3','4','5','6','7','8','9','0'];
  55. $codeKeys = array_rand($codeArr,$num);
  56. shuffle($codeKeys);
  57. $codeStr = '';
  58. foreach ($codeKeys as $codeValue){
  59. $codeStr .= $codeArr[$codeValue];
  60. }
  61. return $codeStr;
  62. }
  63. }
  64. }
  65. if(!function_exists('curlGet')){
  66. /**
  67. * Notes:curl Get请求
  68. * @param $url
  69. * @return mixed
  70. * User: yym
  71. * Date: 2022/7/26
  72. */
  73. function curlGet($url){
  74. $ch = curl_init();
  75. curl_setopt($ch, CURLOPT_URL, $url);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  77. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  79. $output = curl_exec($ch);
  80. curl_close($ch);
  81. return json_decode($output, true);
  82. }
  83. }
  84. if(!function_exists('imgToBase64')){
  85. /**
  86. * Notes:图片转base64
  87. * @param $path
  88. * @return string
  89. * @throws Exception
  90. * User: yym
  91. * Date: 2022/7/28
  92. */
  93. function imgToBase64($path){
  94. //对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
  95. $img_data = "";
  96. if (substr($path,0,strlen("http")) === "http" || substr($path,0,strlen("https")) === "https"){
  97. $img_data = $path;
  98. }else{
  99. if($fp = fopen($path, "rb", 0)) {
  100. $binary = fread($fp, filesize($path)); // 文件读取
  101. fclose($fp);
  102. $img_data = base64_encode($binary); // 转码
  103. }else{
  104. throw new Exception('图片不存在');
  105. }
  106. }
  107. return $img_data;
  108. }
  109. }
  110. if(!function_exists('curlRequest')){
  111. /**
  112. * Notes:CURL post请求
  113. * @param $url
  114. * @param $method
  115. * @param $headers
  116. * @param $body
  117. * @return false|string
  118. * @throws Exception
  119. * User: yym
  120. * Date: 2022/7/28
  121. */
  122. function curlRequest($url,$method = 'POST',$headers,$body)
  123. {
  124. $curl = curl_init();
  125. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  126. curl_setopt($curl, CURLOPT_URL, $url);
  127. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  128. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  129. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  130. curl_setopt($curl, CURLOPT_HEADER, true);
  131. if (1 == strpos("$".$url, "https://")){
  132. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  133. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  134. }
  135. curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
  136. $result = curl_exec($curl);
  137. $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  138. $rheader = substr($result, 0, $header_size);
  139. $rbody = substr($result, $header_size);
  140. $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
  141. if($httpCode == 200){
  142. return $rbody;
  143. }else{
  144. throw new Exception('错误:header-' . $rheader . "\n body:" . $rbody, $httpCode);
  145. }
  146. }
  147. }
  148. if(!function_exists('getOcrInfo')){
  149. /**
  150. * Notes:身份证ocr识别
  151. * @param $img_path
  152. * @param string $type
  153. * @return mixed
  154. * @throws Exception
  155. * User: yym
  156. * Date: 2022/7/28
  157. */
  158. function getOcrInfo($img_path, $type = 'face')
  159. {
  160. $url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
  161. $appcode = config('app')['aly_ocr']['app_code'] ?? '';
  162. $headers = array();
  163. array_push($headers, "Authorization:APPCODE " . $appcode);
  164. //根据API的要求,定义相对应的Content-Type
  165. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  166. //如果没有configure字段,config设为空
  167. $config = array(
  168. "side"=>$type
  169. );
  170. $img_data = imgToBase64($img_path);
  171. $request = array(
  172. "image" => "$img_data"
  173. );
  174. if(count($config) > 0){
  175. $request["configure"] = json_encode($config);
  176. }
  177. $body = json_encode($request);
  178. $result = curlRequest($url, 'POST', $headers, $body);
  179. $res = json_decode($result, true);
  180. return $res;
  181. }
  182. }
  183. if(!function_exists('createOrderSn')){
  184. /**
  185. * Notes:生成订单
  186. * @param int $length
  187. * @return string
  188. * User: yym
  189. * Date: 2022/7/29
  190. */
  191. function createOrderSn(int $length)
  192. {
  193. $date = date("YmdHis");
  194. $rand = $date . randCode($length - strlen($date), true);
  195. if(strlen($rand) > $length)
  196. {
  197. createOrderSn($length);
  198. }
  199. if(\support\Redis::exists('order:order_sn:' . $rand))
  200. {
  201. sleep(1);
  202. createOrderSn($length);
  203. }
  204. \support\Redis::setEx('order:order_sn:' . $rand, 10, 1);
  205. return $rand;
  206. }
  207. }
  208. if(!function_exists('unique2dArrayByKey')){
  209. /**
  210. * Notes:二位数组按照某个健去重
  211. * @param $_2d_array
  212. * @param $unique_key
  213. * @return array
  214. * User: yym
  215. * Date: 2022/7/30
  216. */
  217. function unique2dArrayByKey($_2d_array, $unique_key)
  218. {
  219. $tmp_key[] = array();
  220. foreach ($_2d_array as $key => &$item) {
  221. if (is_array($item) && isset($item[$unique_key])) {
  222. if (in_array($item[$unique_key], $tmp_key)) {
  223. unset($_2d_array[$key]);
  224. } else {
  225. $tmp_key[] = $item[$unique_key];
  226. }
  227. }
  228. }
  229. //重置一下二维数组的索引
  230. return array_slice($_2d_array, 0, count($_2d_array), false);
  231. }
  232. }
  233. if(!function_exists('plog'))
  234. {
  235. /**
  236. * Notes:
  237. * @param $type
  238. * @param $name
  239. * @param string $content
  240. * @return false|true
  241. * User: yym
  242. * Date: 2022/9/21
  243. */
  244. function plog($type, $name, $content = '')
  245. {
  246. if(empty($type) || empty($name)){
  247. return false;
  248. }
  249. $log = array(
  250. 'log_admin_id' => request()->admin_id ?? 0,
  251. 'log_name' => $name,
  252. 'log_type' => $type,
  253. 'log_content' => $content,
  254. 'log_ip' => get_ip(),
  255. 'log_create_time' => time()
  256. );
  257. \app\admin\model\SystemPermLog::insert($log);
  258. return true;
  259. }
  260. }
  261. if(!function_exists('timeDifference'))
  262. {
  263. function timeDifference($start_time, $end_time)
  264. {
  265. //计算两个时间段差
  266. $difference = strtotime(date("Y-m-d", time()) . $end_time) - strtotime(date("Y-m-d", time()) . $start_time);
  267. $time_slot = 30 * 60;
  268. $count = ceil($difference / $time_slot);
  269. $data = array();
  270. for ($i = 0; $i < $count; $i ++)
  271. {
  272. $time = '';
  273. $time = date("H:i", (strtotime(date("Y-m-d", time()) . $start_time) + $i * $time_slot));
  274. $data[$time] = 0;
  275. }
  276. return $data;
  277. }
  278. }
  279. if (!function_exists('get_tree_children')) {
  280. /**
  281. * tree 子菜单
  282. * @param array $data 数据
  283. * @param string $childrenname 子数据名
  284. * @param string $keyName 数据key名
  285. * @param string $pidName 数据上级key名
  286. * @return array
  287. * User: ZQ
  288. * Date: 2022/9/30
  289. */
  290. function get_tree_children(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid')
  291. {
  292. $list = array();
  293. foreach ($data as $value) {
  294. $list[$value[$keyName]] = $value;
  295. }
  296. static $tree = array(); //格式化好的树
  297. foreach ($list as $item) {
  298. if (isset($list[$item[$pidName]])) {
  299. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  300. } else {
  301. $tree[] = &$list[$item[$keyName]];
  302. }
  303. }
  304. return $tree;
  305. }
  306. }
  307. if(!function_exists('appointment_data'))
  308. {
  309. /**
  310. * Notes:医生设置时间处理
  311. * @param array $data
  312. * @param $key
  313. * @return array|object
  314. * User: yym
  315. * Date: 2022/10/13
  316. */
  317. function appointment_data(array $data, $key)
  318. {
  319. if(empty($data) || empty($key))
  320. {
  321. return (object)array();
  322. }
  323. $appointment_data = json_decode($data[$key], true);
  324. if(empty($appointment_data))
  325. {
  326. return (object)array();
  327. }
  328. $morning = array_keys($appointment_data['morning_time_data']);
  329. $afternoon = array_keys($appointment_data['afternoon_time_data']);
  330. $appointment = array(
  331. 'morning_start' => empty($morning) ? '' : array_shift($morning),
  332. 'morning_end' => empty($morning) ? '' : date("H:i", strtotime(array_pop($morning)) + 30 * 60),
  333. '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),
  334. 'afternoon_start' => empty($afternoon) ? '' : array_shift($afternoon),
  335. 'afternoon_end' => empty($afternoon) ? '' : date("H:i", strtotime(array_pop($afternoon)) + 30 * 60),
  336. );
  337. return $appointment;
  338. }
  339. }
  340. if(!function_exists('getRandomString')){
  341. /**
  342. * Notes:生成指定位数唯一字符串
  343. * @param $len
  344. * @param null $chars
  345. * @return string
  346. * User: yym
  347. * Date: 2022/10/21
  348. */
  349. function getRandomString($len, $chars=null)
  350. {
  351. if (is_null($chars)){
  352. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  353. }
  354. mt_srand(10000000*(double)microtime());
  355. for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
  356. $str .= $chars[mt_rand(0, $lc)];
  357. }
  358. return $str;
  359. }
  360. }
  361. if(!function_exists('curlPost')){
  362. /**
  363. * Notes:CURL https 请求
  364. * @param $url
  365. * @param $data
  366. * @return bool|string
  367. * User: yym
  368. * Date: 2022/9/5
  369. */
  370. function curlPost($url, $data){
  371. $header = array(
  372. 'Accept: application/json',
  373. );
  374. $curl = curl_init(); // 启动一个CURL会话
  375. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  376. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  377. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
  378. // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  379. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  380. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  381. // 设置请求头
  382. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  383. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  384. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  385. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  386. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  387. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  388. $tmpInfo = curl_exec($curl); // 执行操作
  389. if (curl_errno($curl)) {
  390. echo 'Errno'.curl_error($curl);//捕抓异常
  391. }
  392. curl_close($curl); // 关闭CURL会话
  393. return $tmpInfo; // 返回数据
  394. }
  395. }