Skip to content

Commit ba8cd09

Browse files
committed
移除重复公共函数
1 parent 05e0fc4 commit ba8cd09

File tree

1 file changed

+0
-194
lines changed

1 file changed

+0
-194
lines changed

app/vuecmf/common.php

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1 @@
11
<?php
2-
// +----------------------------------------------------------------------
3-
// | Copyright (c) 2019~2022 http://www.vuecmf.com All rights reserved.
4-
// +----------------------------------------------------------------------
5-
// | Licensed ( https://github.com/vuecmf/framework/blob/main/LICENSE )
6-
// +----------------------------------------------------------------------
7-
// | Author: vuecmf <tulihua2004@126.com>
8-
// +----------------------------------------------------------------------
9-
10-
/**
11-
* ajax请求成功返回的数据结构
12-
* @param string $msg 提示信息
13-
* @param array|boolean|string $data 返回的数据
14-
* @param int $code 状态码
15-
* @return \think\response\Json
16-
*/
17-
function ajaxSuccess(string $msg = '', $data = [], int $code = 0): \think\response\Json
18-
{
19-
return json([
20-
'data' => $data,
21-
'msg' => $msg,
22-
'code' => $code
23-
]);
24-
}
25-
26-
/**
27-
* ajax请求失败返回的数据结构
28-
* @param object|string $exception 异常对象|异常信息
29-
* @param int $code 状态码
30-
* @param array $data 返回的数据
31-
* @return \think\response\Json
32-
*/
33-
function ajaxFail($exception, int $code = 1000, array $data = []): \think\response\Json
34-
{
35-
$msg = '异常:';
36-
37-
if(is_object($exception)){
38-
$msg .= config('app.show_error_msg') == true ? $exception->getMessage() . ' 在文件' . $exception->getFile() . '中第' . $exception->getLine() . '': $exception->getMessage();
39-
}else if(is_string($exception)){
40-
$msg .= $exception;
41-
}
42-
43-
return json([
44-
'data' => $data,
45-
'msg' => $msg,
46-
'code' => $code
47-
]);
48-
}
49-
50-
/**
51-
* 获取路由信息
52-
* @param object $request 请求对象实例
53-
* @return array
54-
*/
55-
function getRouteInfo($request): array
56-
{
57-
return [
58-
'app_name' => strtolower(app()->http->getName()), //应用路由名称
59-
'controller' => toUnderline($request->controller()), //控制器路由名称
60-
'action' => $request->action(true), //操作路由名称
61-
];
62-
}
63-
64-
/**
65-
* 下划线转驼峰
66-
* @param string $str 待转换的字符串
67-
* @param string $separator 分隔符
68-
* @return string
69-
*/
70-
function toHump(string $str, string $separator='_'): string
71-
{
72-
$str = str_replace($separator, ' ', strtolower($str));
73-
return str_replace(' ','', ucwords($str));
74-
}
75-
76-
/**
77-
* 驼峰转下划线
78-
* @param string $str 待转换的字符串
79-
* @param string $separator 分隔符
80-
* @return string
81-
*/
82-
function toUnderline(string $str, string $separator='_'): string
83-
{
84-
return strtolower(trim(preg_replace("/[A-Z]/", $separator . "$0", $str), $separator));
85-
}
86-
87-
/**
88-
* 生成登录访问令牌: 当天有效
89-
* @param string $username 用户名
90-
* @param string $pwd 密码加密后的字符串
91-
* @param string $ip 当前登录IP地址
92-
* @return string
93-
*/
94-
function makeToken(string $username, string $pwd , string $ip): string
95-
{
96-
return strtolower(md5($username . '_' . $pwd . '_' . $ip . date('Y-m-d') . 'vuecmf'));
97-
}
98-
99-
/**
100-
* 获取表格的树形列表
101-
* @param object $model 模型实例
102-
* @param int $pid 父级ID
103-
* @param string $keywords 过滤关键词
104-
* @param string $pid_field 父级字段名
105-
* @param string $search_field 过滤字段名
106-
* @param string $order_field 排序字段名
107-
* @param int $total 总条数
108-
* @return array
109-
*/
110-
function getTreeList($model, int $pid, string $keywords, string $pid_field = 'pid', string $search_field = 'title', string $order_field = 'sort_num', int &$total = 0): array
111-
{
112-
$query = $model->where($pid_field, $pid);
113-
!empty($keywords) && $query->whereLike($search_field, '%'.$keywords.'%');
114-
!empty($order_field) && $query->order($order_field);
115-
$res = $query->select()->toArray();
116-
$total += count($res);
117-
foreach ($res as &$val){
118-
$child = getTreeList($model, $val['id'], $keywords, $pid_field, $search_field, $order_field, $total);
119-
$val[$pid_field] == 0 && $val[$pid_field] = '';
120-
!empty($child['data']) && $val['children'] = $child['data'];
121-
}
122-
return ['data' => $res, 'total' => $total];
123-
}
124-
125-
126-
/**
127-
* 格式化下拉树型列表
128-
* @param array $tree 存储返回的结果
129-
* @param object $model 模型实例
130-
* @param int $pid 父级ID
131-
* @param string $label 标题字段名
132-
* @param string $pid_field 父级字段名
133-
* @param string $order_field 排序字段名
134-
* @param int $level 层级数
135-
* @return array
136-
*/
137-
function formatTree(array &$tree, $model, int $pid = 0, string $label = 'title', string $pid_field = 'pid', string $order_field = 'sort_num', int $level = 1): array
138-
{
139-
$index_key = $model->getPk();
140-
$query = $model->where($pid_field,$pid)
141-
->where('status', 10);
142-
!empty($order_field) && $query->order($order_field);
143-
$res = $query->column($label, $index_key);
144-
145-
if(!empty($res)){
146-
foreach($res as $key => $val){
147-
148-
$prefix = str_repeat('',$level-1);
149-
150-
$keys = array_keys($res);
151-
$child = $model->where($pid_field, $key)
152-
->where('status', 10)
153-
->count();
154-
155-
if($child || $key != end($keys)){
156-
$prefix .= '┊┈ ';
157-
}else{
158-
$prefix .= '└─ ';
159-
}
160-
161-
$item = [];
162-
$item['id'] = $key;
163-
$item['label'] = $prefix . $val;
164-
$item['level'] = $level;
165-
166-
$tree[] = $item;
167-
168-
formatTree($tree, $model, $key, $label, $pid_field, $order_field,$level + 1);
169-
}
170-
}
171-
172-
return $tree;
173-
}
174-
175-
176-
/**
177-
* 获取目录树的层级路径
178-
* @param string $id_path 层级路径
179-
* @param object $model 模型实例
180-
* @param int $pid 父级ID
181-
* @param string $pid_field 父级ID的字段名称
182-
* @return mixed|string
183-
*/
184-
function getTreeIdPath(string &$id_path, $model, int $pid = 0, string $pid_field = 'pid')
185-
{
186-
$pid_val = $model->where('id',$pid)->where('status', 10)->value($pid_field);
187-
188-
$pid_val != 0 && $id_path = $pid_val . ',' . $id_path;
189-
190-
if($pid_val != 0){
191-
$id_path = getTreeIdPath($id_path, $model, $pid_val, $pid_field);
192-
}
193-
194-
return $id_path;
195-
}

0 commit comments

Comments
 (0)