This commit is contained in:
2025-10-29 15:32:26 +08:00
parent d90614805b
commit b7462657cd
78921 changed files with 2753938 additions and 71 deletions

View File

@@ -0,0 +1,219 @@
# thinkphp-jump
适用于thinkphp6.0的跳转扩展
## 安装
~~~php
composer require liliuwei/thinkphp-jump
~~~
## 配置
~~~php
// 安装之后会在config目录里生成jump.php配置文件
return[
// 默认跳转页面对应的模板文件
'dispatch_success_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
'dispatch_error_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
];
~~~
## 用法示例
使用 use \liliuwei\think\Jump;
在所需控制器内引用该扩展即可:
~~~php
<?php
namespace app\controller;
class Index
{
use \liliuwei\think\Jump;
public function index()
{
//return $this->error('error');
//return $this->success('success','index/index');
//return $this->redirect('/admin/index/index');
return $this->result(['username' => 'liliuwei', 'sex' => '男']);
}
}
~~~
下面示例我在框架自带的BaseController里引入以后所有需要使用跳转的类继承自带的基类即可
以下是自带的基类
~~~php
<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
use \liliuwei\think\Jump;
}
~~~
这里继承BaseController后即可使用success、error、redirect、result方法
~~~php
<?php
namespace app\admin\controller;
class Index extends \app\BaseController
{
public function demo1()
{
/**
* 操作成功跳转的快捷方法
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
*/
// 一般用法
return $this->success('登录成功', 'index/index');
//完整用法
//return $this->success($msg = '登录成功', $url = 'index/index', $data = '', $wait = 3, $header = []);
}
public function demo2()
{
/**
* 操作错误跳转的快捷方法
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
*/
// 一般用法
return $this->error('登录失败');
//return $this->success('登录失败','login/index');
//完整用法
//return $this->error($msg = '登录失败', $url = 'login/index', $data = '', $wait = 3, $header = []);
}
public function demo3()
{
/**
* URL重定向
* @param string $url 跳转的URL表达式
* @param integer $code http code
* @param array $with 隐式传参
*/
//一般用法
//第一种方式:直接使用完整地址(/打头)
//return $this->redirect('/admin/index/index');
//第二种方式如果你需要自动生成URL地址应该在调用之前调用url函数先生成最终的URL地址。
return $this->redirect(url('index/index', ['name' => 'think']));
//return $this->redirect('http://www.thinkphp.cn');
//完整用法
//return $this->redirect($url= '/admin/index/index', $code = 302, $with = ['data' => 'hello']);
}
public function demo4()
{
/**
* 返回封装后的API数据到客户端
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
*/
//一般用法
return $this->result(['username' => 'liliuwei', 'sex' => '男']);
//完整用法
//return $this->result($data=['username' => 'liliuwei', 'sex' => '男'], $code = 0, $msg = '', $type = '', $header = []);
}
}
~~~

View File

@@ -0,0 +1,30 @@
{
"name": "liliuwei/thinkphp-jump",
"description": "适用于thinkphp6.0的跳转扩展",
"keywords": ["thinkphp", "think-jump", "success","error","result","redirect"],
"license": "Apache-2.0",
"type": "think-extend",
"authors": [
{
"name": "liliuwei",
"email": "974829947@qq.com"
}
],
"require": {
"php": ">=7.1.0",
"topthink/framework": "^6.0",
"topthink/think-view": "^1.0"
},
"autoload": {
"psr-4": {
"liliuwei\\think\\": "src/"
}
},
"extra": {
"think": {
"config": {
"jump": "src/config/jump.php"
}
}
}
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* Created by PhpStorm.
* User: liliuwei
* Date: 2019/5/23
* Time: 22:43
*/
namespace liliuwei\think;
use think\exception\HttpResponseException;
use think\App;
use think\Response;
trait Jump
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function success($msg = '', string $url = null, $data = '', int $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 : (string)$this->app->route->buildUrl($url);
}
$result = [
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
// 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式
if ('html' == strtolower($type)) {
$type = 'view';
$response = Response::create($this->app->config->get('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header);
} else {
$response = Response::create($result, $type)->header($header);
}
throw new HttpResponseException($response);
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param mixed $msg 提示信息
* @param string $url 跳转的URL地址
* @param mixed $data 返回的数据
* @param integer $wait 跳转等待时间
* @param array $header 发送的Header信息
* @return void
*/
protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
{
if (is_null($url)) {
$url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
} elseif ($url) {
$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)$this->app->route->buildUrl($url);
}
$result = [
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url,
'wait' => $wait,
];
$type = $this->getResponseType();
if ('html' == strtolower($type)) {
$type = 'view';
$response = Response::create($this->app->config->get('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header);
} else {
$response = Response::create($result, $type)->header($header);
}
throw new HttpResponseException($response);
}
/**
* 返回封装后的API数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param string $type 返回数据格式
* @param array $header 发送的Header信息
* @return void
*/
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 integer $code http code
* @param array $with 隐式传参
* @return void
*/
protected function redirect($url, $code = 302, $with = [])
{
$response = Response::create($url, 'redirect');
$response->code($code)->with($with);
throw new HttpResponseException($response);
}
/**
* 获取当前的response 输出类型
* @access protected
* @return string
*/
protected function getResponseType()
{
return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
}
}

View File

@@ -0,0 +1,12 @@
<?php
/**
* Created by PhpStorm.
* User: liliuwei
* Date: 2019/5/23
* Time: 22:50
*/
return[
// 默认跳转页面对应的模板文件
'dispatch_success_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
'dispatch_error_tmpl' => app()->getRootPath().'/vendor/liliuwei/thinkphp-jump/src/tpl/dispatch_jump.tpl',
];

View File

@@ -0,0 +1,74 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<title>跳转提示</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
body{ background: #fff; font-family: "Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif; color: #333; font-size: 16px; }
/*.system-message{ padding: 24px 48px; }*/
.system-message h1{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
.system-message .jump{ padding-top: 10px; }
.system-message .jump a{ color: #333; }
.system-message .success,.system-message .error{ line-height: 1.8em; font-size: 36px; }
.system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px; display: none; }
#error{
width:1000px;
height:100vh;
min-height:450px;
display: flex;
justify-content: space-between;
margin: auto;
align-items: center;
}
#errorImg{
width:60%;
}
#errorText{
width:40%;
line-height:50px;
margin-left: 50px;
}
</style>
</head>
<body>
<div class="system-message">
<?php switch ($code) {?>
<?php case 1:?>
<h1>:)</h1>
<p class="success"><?php echo(strip_tags($msg));?></p>
<p class="detail"></p>
<p class="jump">
<a id="href" href="<?php echo($url);?>"></a> <b id="wait"><?php echo($wait);?></b>
</p>
<?php break;?>
<?php case 0:?>
<div id='error'>
<img id="errorImg" src="STATIC_IMG/error.png">
<div id="errorText">
<h2 style="color:#7B7B7B;"><?php echo(strip_tags($msg));?></h2>
<!-- <a href="<?php echo($url);?>" style="color:#6195FD;text-decoration:none;"><p> ></p></a>-->
<span style="color:#7B7B7B;"> <a style="color:#6195FD;text-decoration:none;" id="href" href="<?php echo($url);?>"></a> <span style="color:#7B7B7B;"> <b id="wait"><?php echo($wait);?></b></span>
</div>
</div>
<?php break;?>
<?php } ?>
</div>
<script type="text/javascript">
(function(){
var wait = document.getElementById('wait'),
href = document.getElementById('href').href;
var interval = setInterval(function(){
var time = --wait.innerHTML;
if(time <= 0) {
location.href = href;
clearInterval(interval);
};
}, 1000);
})();
</script>
</body>
</html>