helper.php 7.1 KB

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