common.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. function pre($content, $is_die = true)
  13. {
  14. header('Content-type: text/html; charset=utf-8');
  15. echo '<pre>' . print_r($content, true);
  16. $is_die && die();
  17. }
  18. /**
  19. * 异常处理函数
  20. * @param string $code
  21. * @param string $msg
  22. */
  23. function json_error_exception($code = '', $msg = '')
  24. {
  25. $error = config('errorCode.');
  26. $errorCode = isset($error[$code]) ? $code : 10000;
  27. $errorMsg = $msg != '' ? (isset($error[$code]) ? $error[$code] . " :" . $msg : $msg) : (isset($error[$code]) ? $error[$code] : "请求错误");
  28. return array('code' => $errorCode, 'msg' => $errorMsg);
  29. }
  30. /**
  31. * 成功Json返回函数
  32. * @param string $code
  33. * @param array $data
  34. * @param string $msg
  35. */
  36. function json_return($code,$data=[],$msg='')
  37. {
  38. exit(json_encode(['code'=>$code,'data'=>$data,'msg'=>$msg],JSON_UNESCAPED_UNICODE ));
  39. }
  40. function json_success($data=[],$msg='ok')
  41. {
  42. json_return(0, $data, $msg);
  43. }
  44. function json_error($code,$msg='')
  45. {
  46. json_return(json_error_exception($code,$msg));
  47. }
  48. /**
  49. * php发送http请求
  50. * @param array $params 相关请求参数
  51. * @param booble $is_json 数据格式,默认false为数组,true为json
  52. * @return string
  53. */
  54. function _curl(array $params,$is_json=false )
  55. {
  56. $_data = $params['data'] ?? null;
  57. $url = $params['url'];
  58. $defaults = [
  59. CURLOPT_HEADER => 0,
  60. CURLOPT_URL => $params['url'],
  61. CURLOPT_FRESH_CONNECT => 1,
  62. CURLOPT_RETURNTRANSFER => 1,
  63. CURLOPT_FORBID_REUSE => 1,
  64. CURLOPT_TIMEOUT => 60,
  65. CURLOPT_SSL_VERIFYHOST=>2,
  66. CURLOPT_SSL_VERIFYPEER=>0,
  67. ];
  68. switch ($params['method'])
  69. {
  70. case 'post':
  71. $defaults[CURLOPT_CUSTOMREQUEST] = "POST";
  72. break;
  73. case 'get':
  74. $defaults[CURLOPT_CUSTOMREQUEST] = "GET";
  75. break;
  76. case 'put':
  77. $defaults[CURLOPT_CUSTOMREQUEST] = "PUT";
  78. break;
  79. case 'delete':
  80. $defaults[CURLOPT_CUSTOMREQUEST] = "DELETE";
  81. break;
  82. }
  83. if ($_data && $params['method']!='get') {
  84. if($is_json){
  85. $defaults[CURLOPT_POSTFIELDS] = json_encode($_data);
  86. $defaults[CURLOPT_HTTPHEADER] = [
  87. 'Content-Type: application/json',
  88. 'Content-Length: ' . strlen(json_encode($_data))
  89. ];
  90. }else{
  91. $defaults[CURLOPT_POSTFIELDS] = $_data;
  92. }
  93. }elseif($params['method']=='get'){
  94. if(is_array($_data)){
  95. $param = '';
  96. foreach ($_data as $k=>$v){
  97. $param .= $k."=".$v."&";
  98. }
  99. $param = rtrim($param ,"&");
  100. if(!strpos($url,"?")){
  101. $url .= "?".$param;
  102. }else{
  103. $url .= "&".$param;
  104. }
  105. }
  106. $defaults[CURLOPT_URL] = $url;
  107. }
  108. $ch = curl_init();
  109. curl_setopt_array($ch, ($defaults));
  110. $result = curl_exec($ch);
  111. curl_close($ch);
  112. return $result;
  113. }
  114. /**
  115. * 整理菜单住方法
  116. * @param $param
  117. * @return array
  118. */
  119. function prepareMenu($param)
  120. {
  121. $param = objToArray($param);
  122. $parent = []; //父类
  123. $child = []; //子类
  124. foreach($param as $key=>$vo){
  125. if(0 == $vo['type_id']){
  126. $vo['href'] = '#';
  127. $parent[] = $vo;
  128. }else{
  129. $vo['href'] = url($vo['control_name'] .'/'. $vo['action_name']); //跳转地址
  130. $child[] = $vo;
  131. }
  132. }
  133. foreach($parent as $key=>$vo){
  134. foreach($child as $k=>$v){
  135. if($v['type_id'] == $vo['id']){
  136. $parent[$key]['child'][] = $v;
  137. }
  138. }
  139. }
  140. unset($child);
  141. return $parent;
  142. }
  143. /**
  144. * 统一返回信息
  145. * @param $code
  146. * @param $data
  147. * @param $msge
  148. */
  149. function msg($code, $data, $msg)
  150. {
  151. return compact('code', 'data', 'msg');
  152. }
  153. /**
  154. * 整理出tree数据 --- layui tree
  155. * @param $pInfo
  156. * @param $spread
  157. */
  158. function getTree($pInfo, $spread = true)
  159. {
  160. $res = [];
  161. $tree = [];
  162. //整理数组
  163. foreach($pInfo as $key=>$vo){
  164. if($spread){
  165. $vo['spread'] = true; //默认展开
  166. }
  167. $res[$vo['id']] = $vo;
  168. $res[$vo['id']]['children'] = [];
  169. }
  170. unset($pInfo);
  171. //查找子孙
  172. foreach($res as $key=>$vo){
  173. if(0 != $vo['pid']){
  174. $res[$vo['pid']]['children'][] = &$res[$key];
  175. }
  176. }
  177. //过滤杂质
  178. foreach( $res as $key=>$vo ){
  179. if(0 == $vo['pid']){
  180. $tree[] = $vo;
  181. }
  182. }
  183. unset( $res );
  184. return $tree;
  185. }
  186. /**
  187. * 对象转换成数组
  188. * @param $obj
  189. */
  190. function objToArray($obj)
  191. {
  192. return json_decode(json_encode($obj), true);
  193. }
  194. /**
  195. * 权限检测
  196. * @param $rule
  197. */
  198. function authCheck($rule)
  199. {
  200. $control = explode('/', $rule)['0'];
  201. if(in_array($control, ['login', 'index'])){
  202. return true;
  203. }
  204. if(in_array($rule, cache(session('role_id')))){
  205. return true;
  206. }
  207. return false;
  208. }