|
|
@@ -0,0 +1,463 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+
|
|
|
+use app\extra\service\system\SystemService;
|
|
|
+use support\Response;
|
|
|
+use Webman\Event\Event;
|
|
|
+
|
|
|
+if (!function_exists("between_time"))
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 格式化起止时间(为了兼容前端RangePicker组件)
|
|
|
+ * 2020-04-01T08:15:08.891Z => 1585670400
|
|
|
+ * @param array $times
|
|
|
+ * @param bool $isWithTime 是否包含时间
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ function between_time(array $times, bool $isWithTime = false): array
|
|
|
+ {
|
|
|
+ foreach ($times as &$time) {
|
|
|
+ $time = trim($time, '"');
|
|
|
+ $time = str2date($time, $isWithTime);
|
|
|
+ }
|
|
|
+ return ['start_time' => current($times), 'end_time' => next($times)];
|
|
|
+ }
|
|
|
+}
|
|
|
+if (!function_exists("str2date"))
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 日期转换时间戳
|
|
|
+ * 例如: 2020-04-01 08:15:08 => 1585670400
|
|
|
+ * @param string $date
|
|
|
+ * @param bool $isWithTime 是否包含时间
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ function str2date(string $date, bool $isWithTime = false): int
|
|
|
+ {
|
|
|
+ if (!$isWithTime) {
|
|
|
+ $date = date('Y-m-d', strtotime($date));
|
|
|
+ }
|
|
|
+ return strtotime($date);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if(!function_exists("hide_mobile")){
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机号码脱敏
|
|
|
+ * @param string $mobile
|
|
|
+ * @param int $len 4 末尾4位 6 末尾2位
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ function hide_mobile(string $mobile,int $len = 6): string
|
|
|
+ {
|
|
|
+ return substr_replace($mobile, ($len==4 ? '****' : '******'), 3, $len);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('hide_str')) {
|
|
|
+ /**
|
|
|
+ * 将一个字符串部分字符用*替代隐藏
|
|
|
+ * @param string $string 待转换的字符串
|
|
|
+ * @param int $begin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度
|
|
|
+ * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度
|
|
|
+ * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串中间用***代替
|
|
|
+ * @param string $glue 分割符
|
|
|
+ * @return string 处理后的字符串
|
|
|
+ */
|
|
|
+ function hide_str(string $string, int $begin = 3, int $len = 4, int $type = 0, string $glue = "@"): string
|
|
|
+ {
|
|
|
+ $array = array();
|
|
|
+ if ($type == 0 || $type == 1 || $type == 4) {
|
|
|
+ $strlen = $length = mb_strlen($string);
|
|
|
+ while ($strlen) {
|
|
|
+ $array[] = mb_substr($string, 0, 1, "utf8");
|
|
|
+ $string = mb_substr($string, 1, $strlen, "utf8");
|
|
|
+ $strlen = mb_strlen($string);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($type == 0) {
|
|
|
+ for ($i = $begin; $i < ($begin + $len); $i++) {
|
|
|
+ if (isset($array[$i])) {
|
|
|
+ $array[$i] = "*";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $string = implode("", $array);
|
|
|
+ } elseif ($type == 1) {
|
|
|
+ $array = array_reverse($array);
|
|
|
+ for ($i = $begin; $i < ($begin + $len); $i++) {
|
|
|
+ if (isset($array[$i])) {
|
|
|
+ $array[$i] = "*";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $string = implode("", array_reverse($array));
|
|
|
+ } elseif ($type == 2) {
|
|
|
+ $array = explode($glue, $string);
|
|
|
+ if (isset($array[0])) {
|
|
|
+ $array[0] = hide_str($array[0], $begin, $len, 1);
|
|
|
+ }
|
|
|
+ $string = implode($glue, $array);
|
|
|
+ } elseif ($type == 3) {
|
|
|
+ $array = explode($glue, $string);
|
|
|
+ if (isset($array[1])) {
|
|
|
+ $array[1] = hide_str($array[1], $begin, $len, 0);
|
|
|
+ }
|
|
|
+ $string = implode($glue, $array);
|
|
|
+ } elseif ($type == 4) {
|
|
|
+ $left = $begin;
|
|
|
+ $right = $len;
|
|
|
+ $tem = array();
|
|
|
+ for ($i = 0; $i < ($length - $right); $i++) {
|
|
|
+ if (isset($array[$i])) {
|
|
|
+ $tem[] = $i >= $left ? "" : $array[$i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $tem[] = '*****';
|
|
|
+ $array = array_chunk(array_reverse($array), $right);
|
|
|
+ $array = array_reverse($array[0]);
|
|
|
+ for ($i = 0; $i < $right; $i++) {
|
|
|
+ if (isset($array[$i])) {
|
|
|
+ $tem[] = $array[$i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $string = implode("", $tem);
|
|
|
+ }
|
|
|
+ return $string;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('list_sort_by')) {
|
|
|
+ /**
|
|
|
+ *----------------------------------------------------------
|
|
|
+ * 对查询结果集进行排序
|
|
|
+ *----------------------------------------------------------
|
|
|
+ * @access public
|
|
|
+ *----------------------------------------------------------
|
|
|
+ * @param array $list 查询结果
|
|
|
+ * @param string $field 排序的字段名
|
|
|
+ * @param string $sortBy 排序类型
|
|
|
+ * @switch string asc正向排序 desc逆向排序 nat自然排序
|
|
|
+ *----------------------------------------------------------
|
|
|
+ * @return array
|
|
|
+ *----------------------------------------------------------
|
|
|
+ */
|
|
|
+ function list_sort_by(array $list, string $field, string $sortBy = 'asc'): array
|
|
|
+ {
|
|
|
+ if (!empty($list)) {
|
|
|
+ $refer = $resultSet = array();
|
|
|
+ foreach ($list as $i => $data)
|
|
|
+ $refer[$i] = &$data[$field];
|
|
|
+ switch ($sortBy) {
|
|
|
+ case 'asc': // 正向排序
|
|
|
+ asort($refer);
|
|
|
+ break;
|
|
|
+ case 'desc':// 逆向排序
|
|
|
+ arsort($refer);
|
|
|
+ break;
|
|
|
+ case 'nat': // 自然排序
|
|
|
+ natcasesort($refer);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ foreach ($refer as $key => $val)
|
|
|
+ $resultSet[] = &$list[$key];
|
|
|
+ return $resultSet;
|
|
|
+ }
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+if (!function_exists('supplement_id')) {
|
|
|
+ /**
|
|
|
+ * 用户ID风格
|
|
|
+ * @param string $id
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ function supplement_id(string $id): string
|
|
|
+ {
|
|
|
+ $len = strlen($id);
|
|
|
+ $buf = '000000';
|
|
|
+ return $len < 6 ? substr($buf, 0, (6 - $len)) . $id : $id;
|
|
|
+ }
|
|
|
+}
|
|
|
+if (!function_exists('createOrderId')) {
|
|
|
+ /**
|
|
|
+ * 生成订单号
|
|
|
+ * @param string $letter
|
|
|
+ * @param int $length
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ function createOrderId(string $letter = '', int $length = 3): string
|
|
|
+ {
|
|
|
+ $gradual = 0;
|
|
|
+ $orderId = date('YmdHis') . mt_rand(10000000, 99999999);
|
|
|
+ $lengths = strlen($orderId);
|
|
|
+
|
|
|
+ // 循环处理随机数
|
|
|
+ for ($i = 0; $i < $lengths; $i++) {
|
|
|
+ $gradual += (int)(substr($orderId, $i, 1));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($letter)) {
|
|
|
+ $letter = get_order_letter($length);
|
|
|
+ }
|
|
|
+
|
|
|
+ $code = (100 - $gradual % 100) % 100;
|
|
|
+ return $letter . $orderId . str_pad((string)$code, 2, '0', STR_PAD_LEFT);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('get_order_letter')) {
|
|
|
+ /**
|
|
|
+ * 生成订单短ID
|
|
|
+ * @param int $length
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ function get_order_letter(int $length = 2): string
|
|
|
+ {
|
|
|
+ $letter_all = range('A', 'Z');
|
|
|
+ shuffle($letter_all);
|
|
|
+ $letter_array = array_diff($letter_all, ['I', 'O']);
|
|
|
+ $letter = array_rand(array_flip($letter_array), $length);
|
|
|
+ return implode('', $letter);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 过滤emoji表情
|
|
|
+ * @param string $str
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+if (!function_exists('removeEmoji')) {
|
|
|
+ function removeEmoji(string $str = '') : string
|
|
|
+ {
|
|
|
+ $str = preg_replace('/[\x{1F600}-\x{1F64F}]/u', '', $str);
|
|
|
+ $str = preg_replace('/[\x{1F300}-\x{1F5FF}]/u', '', $str);
|
|
|
+ $str = preg_replace('/[\x{1F680}-\x{1F6FF}]/u', '', $str);
|
|
|
+ $str = preg_replace('/[\x{2600}-\x{26FF}]/u', '', $str);
|
|
|
+ return preg_replace('/[\x{2700}-\x{27BF}]/u', '', $str);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('object_array')) {
|
|
|
+ /**
|
|
|
+ * @param $array
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ function object_array($array): array
|
|
|
+ {
|
|
|
+ if(is_object($array)) {
|
|
|
+ $array = (array)$array;
|
|
|
+ }
|
|
|
+ if(is_array($array)) {
|
|
|
+ foreach($array as $key=>$value) {
|
|
|
+ $array[$key] = object_array($value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $array;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("getDateFull"))
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @param string $format
|
|
|
+ * @param string $time
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
|
|
|
+ {
|
|
|
+ if (empty($time)) $time = time();
|
|
|
+ return date($format??'Y-m-d H:i:s',$time);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("events"))
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 事件发布
|
|
|
+ * @param $name
|
|
|
+ * @param $data
|
|
|
+ * @return array|mixed|null
|
|
|
+ */
|
|
|
+ function events($name,$data): mixed
|
|
|
+ {
|
|
|
+ return Event::dispatch($name,$data);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("success")) {
|
|
|
+ /**
|
|
|
+ * @param string $msg
|
|
|
+ * @param array $data
|
|
|
+ * @param int $code
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ function success(string $msg = "", array $data = [], int $code = 1): Response
|
|
|
+ {
|
|
|
+ return json(compact('code', 'data', 'msg'));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("successTrans")) {
|
|
|
+ /**
|
|
|
+ * 消息返回
|
|
|
+ * @param string $message
|
|
|
+ * @param array $data
|
|
|
+ * @param int $code
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ function successTrans(string $message, array $data = [], int $code = 1): Response
|
|
|
+ {
|
|
|
+ $msg = trans($message);
|
|
|
+ return json(compact("msg", "code", "data"));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("error")) {
|
|
|
+ /**
|
|
|
+ * @param $msg
|
|
|
+ * @param array $data
|
|
|
+ * @param int $code
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ function error($msg, array $data = [], int $code = 0): Response
|
|
|
+ {
|
|
|
+ return json(compact('code', 'data', 'msg'));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("errorTrans")) {
|
|
|
+ /**
|
|
|
+ * 消息返回
|
|
|
+ * @param string $message
|
|
|
+ * @param array $data
|
|
|
+ * @param int $code
|
|
|
+ * @return Response
|
|
|
+ */
|
|
|
+ function errorTrans(string $message = "", array $data = [], int $code = 0): Response
|
|
|
+ {
|
|
|
+ $msg = trans($message);
|
|
|
+ return json(compact("msg", "code", "data"));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists("pageFormat")) {
|
|
|
+ /**
|
|
|
+ * @param $data
|
|
|
+ * @param int $size
|
|
|
+ * @return array {page:"",pageSize:"",rows:[],total:""}
|
|
|
+ */
|
|
|
+ function pageFormat($data, int $size = 10): array
|
|
|
+ {
|
|
|
+ if (empty($data)) return [];
|
|
|
+ return [
|
|
|
+ 'total' => $data->total(),
|
|
|
+ 'page' => $data->currentPage(),
|
|
|
+ 'pageSize' => $size,
|
|
|
+ 'rows' => $data->items()
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+if(!function_exists('format_money')){
|
|
|
+ function format_money($str,$len = '2',$append = ""): string
|
|
|
+ {
|
|
|
+ if (empty($str)) {
|
|
|
+ return "0.00";
|
|
|
+ }
|
|
|
+ return number_format($str, $len, ".", $append);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+if (!function_exists('sConf')) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取或配置系统参数
|
|
|
+ * @param string $name 参数名称
|
|
|
+ * @param string|null $value 参数内容
|
|
|
+ */
|
|
|
+ function sConf(string $name = '', string $value = null)
|
|
|
+ {
|
|
|
+ if (is_null($value) && is_string($name)) {
|
|
|
+ return SystemService::get($name);
|
|
|
+ } else {
|
|
|
+ return SystemService::set($name, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (!function_exists('sData')) {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * JSON 数据读取与存储
|
|
|
+ * @param string $name 数据名称
|
|
|
+ * @param mixed $value 数据内容
|
|
|
+ */
|
|
|
+ function sData(string $name,mixed $value = null)
|
|
|
+ {
|
|
|
+ if (is_null($value)) {
|
|
|
+ return SystemService::getData($name);
|
|
|
+ } else {
|
|
|
+ return SystemService::setData($name, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+if (!function_exists('sOplog')) {
|
|
|
+ /**
|
|
|
+ * 写入系统日志
|
|
|
+ * @param string $action 日志行为
|
|
|
+ * @param string $content 日志内容
|
|
|
+ * @param string $userName
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ function sOplog(string $action, string $content, string $userName): bool
|
|
|
+ {
|
|
|
+ return SystemService::setOplog($action, $content,$userName);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if(!function_exists('store_region')){
|
|
|
+ /**
|
|
|
+ * 返回附近司机数据
|
|
|
+ * @param $latitude
|
|
|
+ * @param $longitude
|
|
|
+ * @param $type 1 返回查询影响记录数;2 返回数据数组
|
|
|
+ */
|
|
|
+ function store_region($latitude,$longitude,$type = 1,$where = 'status = 1',$field="*"){
|
|
|
+ $sql = "select ".$field." from(
|
|
|
+SELECT id,poi_id,poi_name,status,poi_address,longitude,latitude,
|
|
|
+ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$latitude}*PI()/180-latitude*PI()/180)/2),2)+COS({$latitude}*PI()/180)*COS(latitude*PI()/180)*POW(SIN(({$longitude}*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli
|
|
|
+FROM saas_store_shop where ".$where.") as tmp_table_name order by juli asc";
|
|
|
+ if($type == 1){
|
|
|
+ return \think\facade\Db::execute($sql);
|
|
|
+ }
|
|
|
+ return \think\facade\Db::query($sql);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if(!function_exists('getHourlyTimeSlots')){
|
|
|
+ function getHourlyTimeSlots($startHour = 9, $endHour = 22, $format = 'H:i'): array
|
|
|
+ {
|
|
|
+ $slots = [];
|
|
|
+
|
|
|
+ for ($hour = $startHour; $hour < $endHour; $hour++) {
|
|
|
+ $startTime = sprintf('%02d:00', $hour);
|
|
|
+ $endTime = sprintf('%02d:00', $hour + 1);
|
|
|
+
|
|
|
+ $slots[] = [
|
|
|
+ 'start' => $startTime,
|
|
|
+ 'end' => $endTime,
|
|
|
+ 'display' => $startTime . ' - ' . $endTime
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $slots;
|
|
|
+ }
|
|
|
+}
|