helpers.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. use Illuminate\Support\Arr;
  11. function str2arr($str, $glue = ',')
  12. {
  13. return array_filter(array_unique(explode($glue, $str)));
  14. }
  15. function arr2str($arr, $glue = ',')
  16. {
  17. return implode($glue, array_unique(Arr::wrap($arr)));
  18. }
  19. function str2arr0($str, $glue = ',')
  20. {
  21. return explode($glue, $str);
  22. }
  23. function arr2str0($arr, $glue = ',')
  24. {
  25. return implode($glue, (Arr::wrap($arr)));
  26. }
  27. function php2js($arr)
  28. {
  29. return json_encode($arr, true);
  30. }
  31. function js2php($json)
  32. {
  33. return json_decode($json, true);
  34. }
  35. /**
  36. * 取数组的值
  37. * @param $key
  38. * @param $arr
  39. * @param string $default
  40. * @return mixed|string
  41. * Author: Mead
  42. */
  43. function get_arr_val($key, $arr = [], $default = '')
  44. {
  45. if (array_key_exists($key, $arr) && $arr[$key] != null) {
  46. return $arr[$key];
  47. }
  48. return $default;
  49. }
  50. /**
  51. * 路径转地址
  52. * @param $path
  53. * @param string $disk
  54. * @return mixed
  55. * Author: Mead
  56. */
  57. function path_to_url($path, $disk = 'public')
  58. {
  59. if (empty($path)) return null;
  60. // 如果 image 字段本身就已经是完整的 url 就直接返回
  61. if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
  62. return $path;
  63. }
  64. return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
  65. }
  66. function is_user_login()
  67. {
  68. return auth('api')->check();
  69. }
  70. function login_user()
  71. {
  72. return auth('api')->user();
  73. }
  74. function login_admin()
  75. {
  76. return auth('admins')->user();
  77. }
  78. function login_user_id()
  79. {
  80. return auth('api')->id();
  81. }
  82. function login_admin_id()
  83. {
  84. return auth('admins')->id();
  85. }
  86. function is_admin_login()
  87. {
  88. return auth('admins')->check();
  89. }
  90. /**
  91. * 数组转树状
  92. * @param array $data
  93. * @param string $primary
  94. * @param string $parent
  95. * @param string $children
  96. * @return array
  97. * Author: Mead
  98. */
  99. function toTree($data = [], $primary = 'id', $parent = 'parent_id', $children = 'children')
  100. {
  101. if (!isset($data[0][$parent])) {
  102. return [];
  103. }
  104. $items = array();
  105. // $pids = [];
  106. foreach ($data as $v) {
  107. $items[$v[$primary]] = $v;
  108. // $pids[] = $v[$parent];
  109. }
  110. $tree = array();
  111. foreach ($items as &$item) {
  112. // if (in_array($item['id'], $pids)) {
  113. // $item['is_base'] = false;
  114. // } else {
  115. // $item['is_base'] = true;
  116. // }
  117. if (isset($items[$item[$parent]])) {
  118. $items[$item[$parent]][$children][] = &$items[$item[$primary]];
  119. } else {
  120. $tree[] = &$items[$item[$primary]];
  121. }
  122. }
  123. return $tree;
  124. }
  125. /**
  126. * 二维数组根据某个字段排序
  127. * @param array $array 要排序的数组
  128. * @param string $keys 要排序的键字段
  129. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  130. * @return array 排序后的数组
  131. */
  132. function arraySort($array, $keys, $sort = SORT_DESC)
  133. {
  134. $keysValue = [];
  135. foreach ($array as $k => $v) {
  136. $keysValue[$k] = $v[$keys];
  137. }
  138. array_multisort($keysValue, $sort, $array);
  139. return $array;
  140. }
  141. function match_chinese($chars, $encoding = 'utf8')
  142. {
  143. $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9_.]/u' : '/[\x80-\xFF]/';
  144. preg_match_all($pattern, $chars, $result);
  145. $temp = join('', $result[0]);
  146. return $temp;
  147. }
  148. function arr_str($arr)
  149. {
  150. return '-' . arr2str($arr, '-') . '-';
  151. }
  152. function str_arr($str)
  153. {
  154. return str2arr(trim($str, '-'), '-');
  155. }
  156. function wechat_fee($money)
  157. {
  158. $m = (int)($money * 100);
  159. if (login_admin_id() > 3) {
  160. if ($money <= 0) {
  161. return 1;
  162. }
  163. }
  164. return $m;
  165. }
  166. function arr2type($arr)
  167. {
  168. return '-' . arr2str($arr, '-') . '-';
  169. }
  170. function type2arr($str)
  171. {
  172. return str2arr(trim($str, '-'), '-');
  173. }
  174. function T($key)
  175. {
  176. if (\Illuminate\Support\Facades\App::getLocale() == 'zh_CN') {
  177. return __("messages.{$key}");
  178. } else {
  179. return __($key);
  180. }
  181. }
  182. if (!function_exists('config_path')) {
  183. /**
  184. * Get the configuration path.
  185. *
  186. * @param string $path
  187. * @return string
  188. */
  189. function config_path($path = '')
  190. {
  191. return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
  192. }
  193. }
  194. function sec2time($sec)
  195. {
  196. $mes = '';
  197. if ($sec >= 3600) {
  198. $hours = str_pad(floor($sec / 3600), 2, "0", STR_PAD_LEFT);
  199. $sec = ($sec % 3600);
  200. $mes .= "{$hours}:";
  201. }
  202. if ($sec >= 60) {
  203. $minutes = str_pad(floor($sec / 60), 2, "0", STR_PAD_LEFT);;
  204. $sec = ($sec % 60);
  205. $mes .= "{$minutes}:";
  206. }
  207. $seconds = str_pad(floor($sec), 2, "0", STR_PAD_LEFT);;
  208. $mes .= "{$seconds}";
  209. return $mes;
  210. }
  211. function str_is_null()
  212. {
  213. return '--';
  214. }