helpers.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_unique(explode($glue, $str));
  14. }
  15. function arr2str($arr, $glue = ',')
  16. {
  17. return implode($glue, array_unique(Arr::wrap($arr)));
  18. }
  19. function php2js($arr)
  20. {
  21. return json_encode($arr, true);
  22. }
  23. function js2php($json)
  24. {
  25. return json_decode($json, true);
  26. }
  27. /**
  28. * 取数组的值
  29. * @param $key
  30. * @param $arr
  31. * @param string $default
  32. * @return mixed|string
  33. * Author: Mead
  34. */
  35. function get_arr_val($key, $arr = [], $default = '')
  36. {
  37. if (array_key_exists($key, $arr) && $arr[$key] != null) {
  38. return $arr[$key];
  39. }
  40. return $default;
  41. }
  42. /**
  43. * 路径转地址
  44. * @param $path
  45. * @param string $disk
  46. * @return mixed
  47. * Author: Mead
  48. */
  49. function path_to_url($path, $disk = 'public')
  50. {
  51. if (empty($path)) return null;
  52. // 如果 image 字段本身就已经是完整的 url 就直接返回
  53. if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
  54. return $path;
  55. }
  56. return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
  57. }
  58. function is_user_login()
  59. {
  60. return auth('api')->check();
  61. }
  62. function login_user()
  63. {
  64. return auth('api')->user();
  65. }
  66. function login_admin()
  67. {
  68. return auth('admins')->user();
  69. }
  70. function login_user_id()
  71. {
  72. return auth('api')->id();
  73. }
  74. function login_admin_id()
  75. {
  76. return auth('admins')->id();
  77. }
  78. function is_admin_login()
  79. {
  80. return auth('admins')->check();
  81. }
  82. /**
  83. * 数组转树状
  84. * @param array $data
  85. * @param string $primary
  86. * @param string $parent
  87. * @param string $children
  88. * @return array
  89. * Author: Mead
  90. */
  91. function toTree($data = [], $primary = 'id', $parent = 'parent', $children = 'children')
  92. {
  93. if (!isset($data[0][$parent])) {
  94. return [];
  95. }
  96. $items = array();
  97. // $pids = [];
  98. foreach ($data as $v) {
  99. $items[$v[$primary]] = $v;
  100. // $pids[] = $v[$parent];
  101. }
  102. $tree = array();
  103. foreach ($items as &$item) {
  104. // if (in_array($item['id'], $pids)) {
  105. // $item['is_base'] = false;
  106. // } else {
  107. // $item['is_base'] = true;
  108. // }
  109. if (isset($items[$item[$parent]])) {
  110. $items[$item[$parent]][$children][] = &$items[$item[$primary]];
  111. } else {
  112. $tree[] = &$items[$item[$primary]];
  113. }
  114. }
  115. return $tree;
  116. }
  117. /**
  118. * 根据key获取流程配置
  119. * @param $key
  120. * @return bool|mixed
  121. * Author: Mead
  122. */
  123. function byKeyGetJoinPartyFlow($key)
  124. {
  125. $data = getJoinPartyFlowLists([3]);
  126. $index = array_search($key, array_column($data, 'key'));
  127. if ($index < 0) return false;
  128. return $data[$index];
  129. }
  130. /**
  131. * 获取流程keys
  132. * @param array $level
  133. * @return array
  134. * Author: Mead
  135. */
  136. function getJoinPartyFlowKeys($level = [3])
  137. {
  138. $data = getJoinPartyFlowLists($level);
  139. return array_column($data, 'key');
  140. }
  141. /**
  142. * 获取流程
  143. * @param array $level
  144. * @return array
  145. * Author: Mead
  146. */
  147. function getJoinPartyFlowLists($level = [1, 2, 3])
  148. {
  149. $flow = config('party.join_party_flow');
  150. $flowLists = [];
  151. foreach ($flow as $a) {
  152. foreach ($a['child'] as $b) {
  153. if (in_array(3, $level)) {
  154. foreach ($b['child'] as $c) {
  155. $c['level'] = 3;
  156. $flowLists[] = $c;
  157. }
  158. }
  159. if (in_array(2, $level)) {
  160. $b['level'] = 2;
  161. unset($b['child']);
  162. $flowLists[] = $b;
  163. }
  164. }
  165. if (in_array(1, $level)) {
  166. $a['level'] = 1;
  167. unset($a['child']);
  168. $flowLists[] = $a;
  169. }
  170. }
  171. if (count($level) === 3) return arraySort($flowLists, 'level', SORT_ASC);
  172. return $flowLists;
  173. }
  174. /**
  175. * 二维数组根据某个字段排序
  176. * @param array $array 要排序的数组
  177. * @param string $keys 要排序的键字段
  178. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  179. * @return array 排序后的数组
  180. */
  181. function arraySort($array, $keys, $sort = SORT_DESC)
  182. {
  183. $keysValue = [];
  184. foreach ($array as $k => $v) {
  185. $keysValue[$k] = $v[$keys];
  186. }
  187. array_multisort($keysValue, $sort, $array);
  188. return $array;
  189. }
  190. function match_chinese($chars, $encoding = 'utf8')
  191. {
  192. $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9_.]/u' : '/[\x80-\xFF]/';
  193. preg_match_all($pattern, $chars, $result);
  194. $temp = join('', $result[0]);
  195. return $temp;
  196. }
  197. function arr_str($arr)
  198. {
  199. return '-' . arr2str($arr, '-') . '-';
  200. }
  201. function str_arr($str)
  202. {
  203. return str2arr(trim($str, '-'), '-');
  204. }
  205. function wechat_fee($money)
  206. {
  207. $m = (int)($money * 100);
  208. if (login_user_id() > 3) {
  209. if ($money <= 0) {
  210. return 1;
  211. }
  212. }
  213. return $m;
  214. }
  215. function orderType2BoxType($order_type)
  216. {
  217. switch ($order_type) {
  218. case \App\Repositories\Enums\OrderWorkTypeCodeEnum::A:
  219. return 1;
  220. break;
  221. case \App\Repositories\Enums\OrderWorkTypeCodeEnum::AB:
  222. return 2;
  223. break;
  224. case \App\Repositories\Enums\OrderWorkTypeCodeEnum::B:
  225. return 3;
  226. break;
  227. }
  228. }
  229. function arr2type($arr)
  230. {
  231. return '-' . arr2str($arr, '-') . '-';
  232. }
  233. function type2arr($str)
  234. {
  235. return str2arr(trim($str, '-'), '-');
  236. }
  237. function money($value)
  238. {
  239. return str_replace(',', '', $value);
  240. }