function.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. if (!function_exists('getValidatorError')) {
  12. /**
  13. * 错误验证
  14. * @param $error
  15. * @return mixed
  16. */
  17. function getValidatorError($validator)
  18. {
  19. $errors = $validator->errors()->all();
  20. return isset($errors[0]) ? $errors[0] : 1013;
  21. }
  22. }
  23. /**
  24. * 是否是手机号码
  25. */
  26. if (!function_exists('isPhone')) {
  27. function isPhone($mobile)
  28. {
  29. return preg_match("/^1[3-9][0-9]]{9}/", $mobile);
  30. }
  31. }
  32. if (!function_exists('moneyFormat')) {
  33. /**
  34. * 金钱格式化小数点
  35. * @param $money
  36. * @param int $decimal 小数点位数,默认系统配置2位
  37. * @return float
  38. */
  39. function moneyFormat($money, $decimal = null)
  40. {
  41. $formatConfig = [];
  42. $charset = !empty($formatConfig['charset']) ? trim($formatConfig['charset']) : 'utf-8';
  43. if ($decimal === null) {
  44. $decimal = isset($formatConfig['moneyDecimal']) ? intval($formatConfig['moneyDecimal']) : 2;
  45. }
  46. $money = round($money, $decimal + 1);
  47. $data = explode('.', $money);
  48. $money = isset($data[0]) ? $data[0] : 0;
  49. $float = isset($data[1]) ? $data[1] : '';
  50. $len = $float ? mb_strlen($float, $charset) : 0;
  51. $decimal = $decimal >= 0 ? intval($decimal) : 2;
  52. $num1 = isset($data[1]) ? $data[1] : '';
  53. $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
  54. $money = $money . '.' . $float;
  55. return number_format($money, $decimal, '.', '');
  56. }
  57. }
  58. if (!function_exists('paramsFormat')) {
  59. /**
  60. * 格式化请求数组参数为&连接字符串
  61. * @param array $data 参数数组
  62. * @param false $filter 是否过滤空值字段
  63. * @return string
  64. */
  65. function paramsFormat(array $data, $filter=false){
  66. $result = [];
  67. ksort($data);
  68. foreach($data as $k => $v){
  69. if($filter && !empty($v) || !$filter){
  70. $result[] = "{$k}={$v}";
  71. }
  72. }
  73. return implode('&', $result);
  74. }
  75. }
  76. if (!function_exists('xmlToArray')) {
  77. /**
  78. * xml转数组
  79. * @param $xml
  80. * @return array
  81. */
  82. function xmlToArray($xml){
  83. if(!$xml){
  84. return [];
  85. }
  86. $result = [];
  87. $xml = (array)simplexml_load_string($xml);
  88. foreach($xml as $k => $v){
  89. if(!is_object($v)){
  90. $result[$k] = $v;
  91. }else{
  92. $result[$k] = xmlToArray((array)$v);
  93. }
  94. }
  95. return $result;
  96. }
  97. }
  98. if (!function_exists('curl_api')) {
  99. /**
  100. * curl请求(POST)
  101. * @param $url 请求地址
  102. * @param array $data 请求参数
  103. * @return bool|string 返回结果
  104. * @author laravel开发员
  105. * @date 2019/6/5
  106. */
  107. function curl_api($url, $data = [], $header=[], $timeout=20)
  108. {
  109. // 初始化
  110. $ch = curl_init();
  111. // 设置post方式提交
  112. curl_setopt($ch, CURLOPT_POST, 1);
  113. // 设置头文件的信息作为数据流输出
  114. curl_setopt($ch, CURLOPT_HEADER, 0);
  115. // 超时
  116. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  117. // 是否要求返回数据
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  119. // 请求头
  120. if($header){
  121. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  122. }
  123. // 设置抓取的url
  124. curl_setopt($ch, CURLOPT_URL, $url);
  125. // 提交的数据
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  127. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  129. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  130. // 执行命令
  131. $result = curl_exec($ch);
  132. // 关闭URL请求(释放句柄)
  133. curl_close($ch);
  134. return $result;
  135. }
  136. }
  137. if (!function_exists('formatTime')) {
  138. /**
  139. * 格式化到期时间段
  140. * @param int $interval 时间秒
  141. * @return string 输出格式化时间
  142. * @author laravel开发员
  143. * @date 2019/5/23
  144. */
  145. function formatTime($interval)
  146. {
  147. $format = array(
  148. '31536000' => '年',
  149. '2592000' => '个月',
  150. '604800' => '星期',
  151. '86400' => '天',
  152. '3600' => '小时',
  153. '60' => '分钟',
  154. '1' => '秒',
  155. );
  156. foreach ($format as $key => $val) {
  157. $match = floor($interval / (int)$key);
  158. if (0 != $match) {
  159. return $match . $val;
  160. }
  161. }
  162. return date('Y-m-d', $interval);
  163. }
  164. }
  165. if (!function_exists('showJson')) {
  166. /**
  167. * 消息JSON
  168. * @param string $msg 提示文字
  169. * @param bool $success 是否成功true或false
  170. * @param array $data 结果数据
  171. * @param int $code 编码
  172. * @return array 返回结果
  173. * @author laravel开发员
  174. * @date 2019/5/28
  175. */
  176. function showJson($msg = "1002", $success = true, $data = [], $code = 0)
  177. {
  178. $result = ['success' => $success, 'msg' => lang($msg), 'data' => $data];
  179. if ($success) {
  180. $result['code'] = 0;
  181. } else {
  182. $result['code'] = $code ? $code : -1;
  183. }
  184. return json_encode($result, 256);
  185. }
  186. }
  187. if(!function_exists('getCode')){
  188. /**
  189. * 生成唯一字符串
  190. * @param string $prefix 长度最小8位
  191. * @param int $length 长度最小9位,越小唯一性越差,9位百万唯一
  192. * @return false|string
  193. */
  194. function getCode($prefix='',$length=11){
  195. $length = $length<9? 9 : $length;
  196. $code = strtoupper(md5(uniqid(mt_rand(), true).time()));
  197. $start = rand(1, strlen($code)-$length);
  198. return $prefix.mb_substr($code, $start, $length);
  199. }
  200. }
  201. if(!function_exists('getChatKey')){
  202. /**
  203. * 获取聊天窗口KEY
  204. * @param $fromUid
  205. * @param $toUid
  206. * @return string
  207. */
  208. function getChatKey($fromUid, $toUid){
  209. $arr = array_values([$fromUid,$toUid]);
  210. asort($arr);
  211. return implode('', $arr);
  212. }
  213. }
  214. if(!function_exists('checkPassword')){
  215. /**
  216. * 验证密码
  217. * @param $password
  218. * @return string
  219. */
  220. function checkPassword($password){
  221. if(!preg_match("/^[0-9a-zA-Z\W_]{8,30}$/", $password)){
  222. return false;
  223. }
  224. if(!preg_match("/[a-zA-Z]{1}/", $password)){
  225. return false;
  226. }
  227. if(!preg_match("/[0-9]{1}/", $password)){
  228. return false;
  229. }
  230. if(!preg_match("/([[:punct:]]){1}/", $password)){
  231. return false;
  232. }
  233. return true;
  234. }
  235. }