后端常用功能
# 函数方法
## 查询数据
### 模板一
```PHP
//渲染页面
public function index()
{
if ($this->request->isAjax()) {
$where = [];
//课堂学号
if ($fxid = input("fxid")) {
//$where["name"] = ["like", "%" . $name . "%"];
$where[] = ["fxid", "=", $fxid];
}
//联系方式
if ($contact = input("contact")) {
$where[] = ["contact", "like", "%" . $contact . "%"];
}
//名字
if ($nickname = input("nickname")) {
$where[] = ["nickname", "like", "%" . $nickname . "%"];
}
//备注
if ($remark = input("remark")) {
$where[] = ["remark", "like", "%" . $remark . "%"];
}
//团长
if(($group_id = input("group_id/d", -1))!==-1){
$where[] = ["group_id", "=", $group_id];
}
$page = input("page/d", "1");
$limits = input("limit/d", "10");
$userdb = new UserModel();
//获取所有数据
$userlist = $userdb->getAllByWhere($where, "create_time desc", $page, $limits);
$userlist->withAttr("groupname", function ($value, $data) {
//获取当前团长的ID
$groupid = $data["group_id"];
if ($groupid == 0) {
return "其他人";
}
$groupName = Db::table("dzf_user")->where("id", $groupid)->column("nickname");
if (empty($groupName)) {
return "无团长";
}
return $groupName;
})->append(["groupname"]);
return $this->returnJson("获取用户列表成功", 200, $userlist->toArray(),["count"=>$userdb->getAllCount($where)]);
}
return $this->fetch("user/user");
```
# traitas
## thinkphp页面跳转
```php
<?php
namespace app\common\traits;
use think\exception\HttpResponseException;
use think\Response;
/**
* Trait JumpTrait
* @package app\common\traits
*/
trait JumpTrait
{
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param string $url 跳转的 URL 地址
* @param int $wait 跳转等待时间
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function success($msg = '', $data = '', $url = null, $wait = 3, array $header = [])
{
if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
$url = $_SERVER["HTTP_REFERER"];
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $type = $this->getResponseType();
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_success_tmpl'), $result);
} elseif ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param mixed $data 返回的数据
* @param string $url 跳转的 URL 地址
* @param int $wait 跳转等待时间
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function error($msg = '', $data = '', $url = null, $wait = 3, array $header = [])
{
if (is_null($url)) {
$url = request()->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url)->__toString();
}
$type = $this->getResponseType();
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
if ($type == 'html') {
$response = view(app('config')->get('app.dispatch_error_tmpl'), $result);
} elseif ($type == 'json') {
$response = json($result);
}
throw new HttpResponseException($response);
}
/**
* 返回封装后的 API 数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param int $code 返回的 code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data,
];
$type = $type ?: $this->getResponseType();
$response = Response::create($result, $type)->header($header);
throw new HttpResponseException($response);
}
/**
* URL 重定向
* @access protected
* @param string $url 跳转的 URL 表达式
* @param array|int $params 其它 URL 参数
* @param int $code http code
* @param array $with 隐式传参
* @return void
* @throws HttpResponseException
*/
protected function redirect($url = [], $params = [], $code = 302)
{
if (is_integer($params)) {
$code = $params;
$params = [];
}
$response = Response::create($url, 'redirect', $code);
throw new HttpResponseException($response);
}
/**
* 获取当前的 response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return (request()->isJson() || request()->isAjax() || request()->isPost()) ? 'json' : 'html';
}
}
```
-----