helper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. if (!function_exists('dd')) {
  3. function dd($obj)
  4. {
  5. halt($obj);
  6. }
  7. }
  8. if (!function_exists('fail')) {
  9. /**
  10. * 主动抛错
  11. */
  12. function fail($message, $data = [], $code = 0)
  13. {
  14. $result = [
  15. 'code' => $code,
  16. 'data' => $data,
  17. 'msg' => is_array($message) ? json_encode($message) : $message,
  18. ];
  19. request_log_update($result);
  20. // 如果未设置类型则自动判断
  21. $type = 'json';
  22. $response = \think\Response::create($result, $type, 200);
  23. throw new \think\exception\HttpResponseException($response);
  24. }
  25. }
  26. if (!function_exists('succ')) {
  27. /**
  28. * 成功返回
  29. */
  30. function succ($data = [], $message = '')
  31. {
  32. $result = [
  33. 'code' => 1,
  34. 'data' => $data,
  35. 'msg' => $message,
  36. ];
  37. request_log_update($result);
  38. // 如果未设置类型则自动判断
  39. $type = 'json';
  40. $response = \think\Response::create($result, $type, 200);
  41. throw new \think\exception\HttpResponseException($response);
  42. }
  43. }
  44. if (!function_exists('getConfig')) {
  45. /**
  46. * 获取配置
  47. * @param string $field 配置组名
  48. * @param string $key 字段
  49. * @param string $default 字段默认值
  50. * @param boolean $refresh 是否刷新缓存
  51. * @return mixed
  52. */
  53. function getConfig(string $field, $key = '', $default = '', $refresh = true)
  54. {
  55. $config = \think\Cache::get($field);
  56. if (!$config || $refresh) {
  57. $config = \think\Db::name('exam_config_info')->order('id')->limit(1)->value($field);
  58. if (!$config) {
  59. return null;
  60. }
  61. $config = json_decode($config, true);
  62. //存入缓存
  63. \think\Cache::set($field, $config);
  64. }
  65. if ($key) {
  66. return $config[$key] ?? $default;
  67. }
  68. return $config;
  69. }
  70. }
  71. if (!function_exists('getCurl')) {
  72. /**
  73. * get请求
  74. * @param $url
  75. * @return bool|string
  76. */
  77. function getCurl($url)
  78. {
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_URL, $url);
  81. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  82. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  83. $data = curl_exec($ch);
  84. curl_close($ch);
  85. return $data;
  86. }
  87. }
  88. if (!function_exists('postCurl')) {
  89. /**
  90. * post请求
  91. * @param $url
  92. * @param string $data
  93. * @param string $type
  94. * @return bool|string
  95. */
  96. function postCurl($url, $data = '', $type = 'json')
  97. {
  98. if ($type == 'json') {
  99. $data = json_encode($data); //对数组进行json编码
  100. $header = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
  101. }
  102. $curl = curl_init();
  103. curl_setopt($curl, CURLOPT_URL, $url);
  104. curl_setopt($curl, CURLOPT_POST, 1);
  105. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  106. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  107. if (!empty($data)) {
  108. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  109. }
  110. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  111. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  112. $res = curl_exec($curl);
  113. if (curl_errno($curl)) {
  114. echo 'Error+' . curl_error($curl);
  115. }
  116. curl_close($curl);
  117. return $res;
  118. }
  119. }
  120. if (!function_exists('only_keys')) {
  121. /**
  122. * 只取数组部分key数据
  123. * @param array $array
  124. * @param array $keys
  125. * @return array
  126. */
  127. function only_keys(array $array, array $keys)
  128. {
  129. $result = [];
  130. foreach ($array as $k => $value) {
  131. if (in_array($k, $keys)) {
  132. $result[$k] = $value;
  133. }
  134. }
  135. return $result;
  136. }
  137. }
  138. if (!function_exists('hidden_keys')) {
  139. /**
  140. * 隐藏数组部分key数据
  141. * @param array $array
  142. * @param array $keys
  143. * @return array
  144. */
  145. function hidden_keys(array $array, array $keys)
  146. {
  147. $result = [];
  148. foreach ($array as $k => $value) {
  149. if (in_array($k, $keys)) {
  150. unset($value[$k]);
  151. $result[$k] = $value;
  152. }
  153. }
  154. return $result;
  155. }
  156. }
  157. if (!function_exists('hidden_list_keys')) {
  158. /**
  159. * 隐藏数组部分key数据
  160. * @param array $list
  161. * @param array $keys
  162. * @return array
  163. */
  164. function hidden_list_keys(array $list, array $keys)
  165. {
  166. $list = collection($list)->toArray();
  167. $result = [];
  168. foreach ($list as $i => $item) {
  169. foreach ($item as $k => $value) {
  170. if (in_array($k, $keys)) {
  171. unset($item[$k]);
  172. }
  173. }
  174. $result[$i] = $item;
  175. }
  176. return $result;
  177. }
  178. }
  179. if (!function_exists('is_empty_in_array')) {
  180. /**
  181. * 数组内是否包含且存在字段值
  182. * @param $array
  183. * @param $field
  184. * @return bool
  185. */
  186. function is_empty_in_array($array, $field)
  187. {
  188. if (!isset($array[$field]) || !$array[$field]) {
  189. return true;
  190. }
  191. return false;
  192. }
  193. }
  194. if (!function_exists('cache_data')) {
  195. /**
  196. * 获取/设置缓存数据
  197. * @param string $cache_key 缓存key名
  198. * @param Closure $fun 用户函数,获取并返回数据
  199. * @param int $expire_time 缓存过期时间
  200. * @return mixed
  201. */
  202. function cache_data(string $cache_key, Closure $fun, int $expire_time = 0, bool $refresh = false)
  203. {
  204. // 固定前缀
  205. $cache_key = "exam:{$cache_key}";
  206. // 存在缓存,返回缓存
  207. if (!$refresh && $cache = cache($cache_key)) {
  208. return $cache;
  209. }
  210. // 执行数据获取
  211. $data = $fun();
  212. $data = is_array($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
  213. // 设置缓存
  214. cache($cache_key, $data, $expire_time);
  215. return $data;
  216. }
  217. }
  218. if (!function_exists('getUser')) {
  219. /**
  220. * 获取Api用户信息
  221. * @return mixed
  222. */
  223. function getUser()
  224. {
  225. if (\app\common\library\Auth::instance()->isLogin()) {
  226. return \app\common\library\Auth::instance();
  227. }
  228. return null;
  229. }
  230. }
  231. if (!function_exists('getUserId')) {
  232. /**
  233. * 获取Api用户ID
  234. * @return mixed
  235. */
  236. function getUserId()
  237. {
  238. if ($user = getUser()) {
  239. return $user->id;
  240. }
  241. return 0;
  242. }
  243. }
  244. if (!function_exists('str_trim')) {
  245. /**
  246. * 字符串去除空格
  247. * @return string
  248. */
  249. function str_trim($str)
  250. {
  251. return str_replace(' ', '', $str);
  252. }
  253. }
  254. if (!function_exists('generate_no')) {
  255. /**
  256. * 根据时间生成编号
  257. * @return string
  258. */
  259. function generate_no($pre = '')
  260. {
  261. $date = date('YmdHis', time());
  262. $u_timestamp = microtime(true);
  263. $timestamp = floor($u_timestamp);
  264. $milliseconds = round(($u_timestamp - $timestamp) * 100); // 改这里的数值控制毫秒位数
  265. return $pre . $date . date(preg_replace('`(?<!\\\\)u`', $milliseconds, 'u'), $timestamp);
  266. }
  267. }
  268. function request_log_update($log_result){
  269. /*if ($this->logType === 1){
  270. if (strlen(json_encode($log_result['data'])) > 1000) {
  271. $log_result['data'] = '数据太多,不记录';
  272. }
  273. LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
  274. }else{*/
  275. if(defined('API_REQUEST_ID')) { //记录app正常返回结果
  276. if(strlen(json_encode($log_result['data'])) > 1000) {
  277. $log_result['data'] = '数据太多,不记录';
  278. }
  279. db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
  280. }
  281. /*}*/
  282. }