You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
7.3 KiB
345 lines
7.3 KiB
<?php |
|
/** |
|
* |
|
* Js.php |
|
* User: ChenLong |
|
* DateTime: 2020/3/12 13:01 |
|
*/ |
|
|
|
|
|
namespace sdModule\makeAdminBasics\htmlUnit; |
|
|
|
use sdModule\common\Sc; |
|
use sdModule\makeAdminBasics\Basics; |
|
use sdModule\makeAdminBasics\Make; |
|
|
|
class Js |
|
{ |
|
const LIST_HEAD_TEMPLATE = 'table_head'; |
|
const LIST_LINE_TEMPLATE = 'table_line'; |
|
const LIST_ALL_TEMPLATE = '-'; |
|
|
|
/** |
|
* layui加载的模块 |
|
* @var array |
|
*/ |
|
public $layUIModule = [ |
|
'form', '$' => 'jquery' |
|
]; |
|
|
|
/** |
|
* layui js 代码块 |
|
* @var string |
|
*/ |
|
public $layUICode = ''; |
|
|
|
/** |
|
* @var string 编辑重置代码 |
|
*/ |
|
public $resetCode; |
|
|
|
/** |
|
* js 代码块 |
|
* @var string |
|
*/ |
|
public $normalCode = ''; |
|
|
|
/** |
|
* 加载JS的块 |
|
* @var string |
|
*/ |
|
public $loadJS = ''; |
|
|
|
|
|
/** |
|
* 模板组 |
|
* @var array |
|
*/ |
|
public $template = []; |
|
|
|
/** |
|
* @var array |
|
*/ |
|
public $templateHtml = ''; |
|
|
|
|
|
|
|
/** |
|
* @var Js |
|
*/ |
|
private static $instance; |
|
|
|
private function __construct(){} |
|
|
|
private function __clone(){} |
|
|
|
/** |
|
* @return Js |
|
*/ |
|
public static function getInstance() |
|
{ |
|
if (empty(self::$instance)){ |
|
self::$instance = new self(); |
|
} |
|
return self::$instance; |
|
} |
|
|
|
/** |
|
* 重置JS代码 |
|
*/ |
|
private function reset() |
|
{ |
|
self::$instance = null; |
|
} |
|
|
|
/** |
|
* 代码组建 |
|
* @param string $scene 场景值 |
|
* @param Make $make |
|
* @return string |
|
*/ |
|
public function JsCompose(string $scene, Make $make) |
|
{ |
|
$sceneJs = $this->scene($scene, $make); |
|
$js = <<<JSA |
|
|
|
{$this->templateHtml} |
|
{$this->loadJS} |
|
<script> |
|
{$this->normalCode} |
|
layui.use({$this->module()}, function () { |
|
{$this->moduleLoad()}; |
|
{$this->layUICode} |
|
{$sceneJs} |
|
}); |
|
</script> |
|
JSA; |
|
$this->reset(); |
|
return $js; |
|
} |
|
|
|
/** |
|
* 加载的layui模块 |
|
* @return string |
|
*/ |
|
private function module() |
|
{ |
|
return "['" . implode("', '", $this->layUIModule) . "']"; |
|
} |
|
|
|
/** |
|
* layui js模块加载 |
|
* @return string |
|
*/ |
|
private function moduleLoad() |
|
{ |
|
array_walk($this->layUIModule, function (&$value, $key) { |
|
if (is_string($key)) { |
|
$value = $key . ' = layui.' . $value; |
|
}else{ |
|
$value = $value . ' = layui.' . $value; |
|
} |
|
}); |
|
|
|
return 'var ' . implode(", ", $this->layUIModule); |
|
} |
|
|
|
/** |
|
* 注册layui模块 |
|
* @param string $module |
|
*/ |
|
public function registerLayUIModule(string $module) |
|
{ |
|
if ($module && !in_array($module, $this->layUIModule)) { |
|
$this->layUIModule[] = $module; |
|
} |
|
} |
|
|
|
/** |
|
* layui js 代码集结 |
|
* @param string $code |
|
*/ |
|
public function layUICode(string $code) |
|
{ |
|
$code and $this->layUICode .= $code; |
|
} |
|
|
|
/** |
|
* 重置代码 |
|
* @param string $code |
|
*/ |
|
public function resetCode(string $code) |
|
{ |
|
$code and $this->resetCode .= $code; |
|
} |
|
|
|
/** |
|
* 普通js |
|
* @param string $code |
|
*/ |
|
public function normalJs(string $code) |
|
{ |
|
$code and $this->normalCode .= $code; |
|
} |
|
|
|
/** |
|
* 加载 |
|
* @param $url |
|
*/ |
|
public function loadJs($url) |
|
{ |
|
$url and $this->loadJS .= Basics::indentAndLineFeed(0, Basics::BEFORE) |
|
. "<script type=\"text/javascript\" src=\"__PUBLIC__/{$url}\"></script>"; |
|
} |
|
|
|
/** |
|
* 模板加载 |
|
* @param string $event |
|
* @param string $code |
|
* @param string $js |
|
* @param string $template |
|
*/ |
|
public function loadTemplate($event, $code, $js, $template) |
|
{ |
|
$this->template[$template][$event] = [ |
|
'template' => Basics::indentAndLineFeed(1, Basics::BEFORE) . $code, |
|
'js' => Basics::indentAndLineFeed(3, Basics::BEFORE) . $js |
|
]; |
|
} |
|
|
|
/** |
|
* @param $html |
|
*/ |
|
public function templateHtml($html) |
|
{ |
|
$this->templateHtml .= $html; |
|
} |
|
|
|
|
|
/** |
|
* 不同的场景对应不同的js |
|
* @param $scene |
|
* @param Make $make |
|
* @return mixed |
|
*/ |
|
private function scene($scene, Make $make) |
|
{ |
|
return call_user_func([$this, $scene . 'Scene'], $make); |
|
} |
|
|
|
/** |
|
* 新增页面 |
|
* @return string |
|
*/ |
|
protected function createScene() |
|
{ |
|
return $this->submitData('create'); |
|
} |
|
|
|
/** |
|
* 修改页面 |
|
* @return string |
|
*/ |
|
protected function editScene() |
|
{ |
|
|
|
return <<<EJS |
|
|
|
(window.formVal = function(){ |
|
form.val('sd', {:json_encode(\$data)}); |
|
{$this->resetCode} |
|
return false; |
|
})(); |
|
|
|
{$this->submitData()} |
|
EJS; |
|
|
|
} |
|
|
|
|
|
/** |
|
* @param Make $make |
|
* @return mixed |
|
* @throws \ReflectionException |
|
*/ |
|
protected function listScene(Make $make) |
|
{ |
|
return Sc::reflex()->getInstance(ListJs::class, ['make' => $make])->load(); |
|
} |
|
|
|
/** |
|
* 提交数据的js |
|
* @return string |
|
*/ |
|
protected function submitData() |
|
{ |
|
return <<<JSA |
|
$('#close').click(function () { |
|
parent.layer.closeAll(); |
|
}); |
|
|
|
form.on('submit(formDemo)', function (data) { |
|
let load = custom.loading(); |
|
$.ajax({ |
|
type: 'post' |
|
, headers: { |
|
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') |
|
} |
|
, data: data.field |
|
, success: function (res) { |
|
layer.close(load); |
|
if (res.code === 200) { |
|
parent.layer.closeAll(); |
|
window.parent.layNotice.success('{:lang("success")}'); |
|
window.parent.table.reload('test'); |
|
} else { |
|
layNotice.warning(res.msg); |
|
} |
|
}, |
|
error: function (err) { |
|
layer.close(load); |
|
} |
|
}); |
|
return false; |
|
}) |
|
JSA; |
|
} |
|
|
|
/** |
|
* 提交数据的js |
|
* @return string |
|
*/ |
|
protected function submitDataS() |
|
{ |
|
return <<<JSA |
|
|
|
form.on('submit(formDemo)', function (data) { |
|
layer.confirm('确认此次操作?', {icon:3}, function (index) { |
|
let load = custom.loading(); |
|
$.ajax({ |
|
type: 'post' |
|
, headers: { |
|
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') |
|
} |
|
, data: data.field |
|
, success: function (res) { |
|
layer.close(load); |
|
if (res.code === 200) { |
|
parent.layer.closeAll(); |
|
window.parent.layNotice.success('成功'); |
|
window.parent.table.reload('test'); |
|
} else { |
|
layNotice.warning(res.msg); |
|
} |
|
}, |
|
error: function (err) { |
|
layer.close(load); |
|
} |
|
}); |
|
}); |
|
return false; |
|
}) |
|
JSA; |
|
} |
|
} |
|
|
|
|