Curd.php 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  1. <?php
  2. namespace plugin\curd\library;
  3. use laytp\traits\Error;
  4. use plugin\curd\model\curd\Field;
  5. use plugin\curd\model\curd\Table;
  6. use plugin\curd\model\Migrations;
  7. use think\facade\Config;
  8. class Curd
  9. {
  10. use Error;
  11. protected
  12. $tableId, //要生成的表id
  13. $needCreateTable, //是否要执行生成数据表结构的命令
  14. $database, //要生成的表名链接数据库的标识
  15. $tableName, //要生成的表名
  16. $tableComment, //要生成的表注释
  17. $engine, //要生成的表存储引擎
  18. $collation, //要生成的表字符集
  19. $isHidePk, //是否隐藏主键列.2=不隐藏,1=隐藏
  20. $isCreateNumber, //是否生成序号列.2=不生成,1=生成
  21. $fields, //字段列表
  22. $midName, //中间名称,比如表名为lt_test_a_b那么这里的midName就是/test/a/B,拼接控制器和模型文件的路径和namespace都需要用到
  23. $controllerModelClassName, //控制器和模型的类名
  24. $migrationClassName, //要生成的数据迁移文件类名
  25. $migrationFileName, //要生成的数据迁移文件名
  26. $controllerFileName, //要生成的控制器文件名
  27. $validateAddFileName, //要生成的添加验证器文件名
  28. $validateEditFileName, //要生成的编辑验证器文件名
  29. $modelFileName, //要生成的模型文件名
  30. $jsFileName, //要生成的js文件名
  31. $recycleJsFileName, //要生成的回收站的js文件名
  32. $htmlIndexFileName, //要生成的首页html文件名
  33. $htmlAddFileName, //要生成的添加html文件名
  34. $htmlEditFileName, //要生成的编辑html文件名
  35. $htmlDetailFileName, //要生成的查看html文件名
  36. $htmlRecycleFileName, //要生成的回收站html文件名
  37. $migrationParam, //生成数据迁移文件,模板需要用到的参数数组
  38. $controllerParam, //生成controller文件,模板需要用到的参数数组
  39. $validateAddParam, //生成添加验证器文件,模板需要用到的参数数组
  40. $validateEditParam, //生成编辑验证器文件,模板需要用到的参数数组
  41. $modelParam, //生成model文件,模板需要用到的参数数组
  42. $jsParam, //生成js文件,模板需要用到的参数数组
  43. $recycleJsParam, //生成回收站js文件,模板需要用到的参数数组
  44. $htmlIndexParam, //生成index.html,模板需要用到的参数数组
  45. $htmlAddParam, //生成add.html,模板需要用到的参数数组
  46. $htmlEditParam, //生成edit.html,模板需要用到的参数数组
  47. $htmlDetailParam, //生成detail.html,模板需要用到的参数数组,暂时屏蔽
  48. $htmlRecycleParam, //生成recycle.html,模板需要用到的参数数组
  49. $recycleCols, //回收站需要的js字段列表
  50. $hasSoftDel //是否拥有软删除
  51. ;
  52. public function __construct($tableId, $needCreateTable)
  53. {
  54. $this->tableId = $tableId;
  55. $this->needCreateTable = $needCreateTable;
  56. }
  57. public function execute()
  58. {
  59. $table = Table::find($this->tableId);
  60. if (!$table) {
  61. $this->setError('table_id参数错误');
  62. return false;
  63. }
  64. if($table->table == 'lt_admin_user'){
  65. $this->setError('lt_admin_user表暂不支持生成');
  66. return false;
  67. }
  68. //这里来删除lt_migrations表对当前表的记录
  69. $this->database = $table->database;
  70. $this->tableName = $table->table;
  71. $this->tableComment = $table->comment;
  72. $this->engine = $table->engine;
  73. $this->collation = $table->collation;
  74. $this->isHidePk = $table->is_hide_pk;
  75. $this->isCreateNumber = $table->is_create_number;
  76. $this->fields = Field::where('table_id', '=', $this->tableId)->order(['show_sort' => 'desc', 'id' => 'asc'])->select();
  77. if (!$this->fields) {
  78. $this->setError($this->tableName . '还没有字段,请先添加字段');
  79. return false;
  80. }
  81. $this->hasSoftDel = false;
  82. //是否拥有软删除功能
  83. foreach ($this->fields as $k => $v) {
  84. if ($v->field == 'delete_time') {
  85. $this->hasSoftDel = true;
  86. }
  87. }
  88. $this->setParam();
  89. if(!$this->createMigration()){
  90. return false;
  91. }
  92. $this->create();
  93. return true;
  94. }
  95. /**
  96. * 设置参数,待生成
  97. */
  98. public function setParam()
  99. {
  100. $this->setMidFileName();
  101. $this->setMigrationFileName();
  102. $this->setFileName();
  103. $this->setMigrationParam();
  104. $this->setControllerParam();
  105. $this->setModelParam();
  106. $this->setJsParam();
  107. $this->setHtmlParam();
  108. $this->setValidateParam();
  109. }
  110. /**
  111. * 生成静态文件、控制器和模型文件
  112. */
  113. public function create()
  114. {
  115. // $this->createMigration();
  116. $this->createController();
  117. $this->createModel();
  118. $this->createJs();
  119. $this->createHtml();
  120. $this->createValidate();
  121. }
  122. /**
  123. * 设置当前生成的类路径名称
  124. */
  125. protected function setMidFileName()
  126. {
  127. $arrTable = explode('_', $this->tableName);
  128. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  129. $this->controllerModelClassName = $basename;
  130. array_shift($arrTable);
  131. array_pop($arrTable);
  132. if (count($arrTable)) {
  133. $strTable = implode('/', $arrTable) . '/' . $basename;
  134. } else {
  135. $strTable = $basename;
  136. }
  137. $this->midName = $strTable;
  138. }
  139. /**
  140. * 根据表名获取中间名
  141. * @param $tableName
  142. * @return string
  143. */
  144. protected function getMidName($tableName)
  145. {
  146. $arrTable = explode('_', $tableName);
  147. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  148. $this->controllerModelClassName = $basename;
  149. array_shift($arrTable);
  150. array_pop($arrTable);
  151. if (count($arrTable)) {
  152. $strTable = implode('/', $arrTable) . '/' . $basename;
  153. } else {
  154. $strTable = $basename;
  155. }
  156. return $strTable;
  157. }
  158. /**
  159. * 根据表名获取类路径名称
  160. * @param $table
  161. * @return string
  162. */
  163. protected function getNameByTable($table)
  164. {
  165. $arrTable = explode('_', $table);
  166. $basename = ucfirst($arrTable[count($arrTable) - 1]);
  167. array_shift($arrTable);//del_db_prefix
  168. array_pop($arrTable);
  169. if (count($arrTable)) {
  170. $strTable = implode('\\', $arrTable) . '\\' . $basename;
  171. } else {
  172. $strTable = $basename;
  173. }
  174. return $strTable;
  175. }
  176. //设置需要生成的数据迁移文件名
  177. protected function setMigrationFileName()
  178. {
  179. if ($this->needCreateTable) {
  180. $arrTable = explode('_', $this->tableName);
  181. array_shift($arrTable);
  182. foreach ($arrTable as $k => $name) {
  183. $arrTable[$k] = ucfirst($name);
  184. }
  185. $this->migrationClassName = implode('', $arrTable);
  186. $migrations = new Migrations();
  187. $migration = $migrations->where('migration_name', '=', ucfirst($this->migrationClassName))->find();
  188. if ($migration) {
  189. $this->migrationFileName = app()->getRootPath() . 'database' . DS . 'migrations' . DS . $migration->version . '_' . lcfirst($migration->migration_name) . '.php';
  190. $migration->delete();
  191. } else {
  192. $this->migrationFileName = app()->getRootPath() . 'database' . DS . 'migrations' . DS . date('YmdHis') . '_' . lcfirst(implode('', $arrTable)) . '.php';
  193. }
  194. }
  195. }
  196. protected function filterMidName($midName)
  197. {
  198. if (substr($midName, 0, 6) === 'admin/') {
  199. return substr($midName, 6);
  200. }
  201. return $midName;
  202. }
  203. //设置所有需要生成的文件名
  204. protected function setFileName()
  205. {
  206. $this->controllerFileName = app()->getAppPath() . 'controller' . DS . 'admin' . DS . $this->filterMidName($this->midName) . '.php';
  207. $this->validateAddFileName = app()->getAppPath() . 'validate' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'Add.php';
  208. $this->validateEditFileName = app()->getAppPath() . 'validate' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'Edit.php';
  209. $this->modelFileName = app()->getAppPath() . 'model' . DS . $this->midName . '.php';
  210. $this->jsFileName = app()->getRootPath() . 'public' . DS . 'static' . DS . 'admin' . DS . 'js' . DS . $this->filterMidName(strtolower($this->midName)) . '.js';
  211. $this->recycleJsFileName = app()->getRootPath() . 'public' . DS . 'static' . DS . 'admin' . DS . 'js' . DS . $this->filterMidName(strtolower($this->midName)) . 'Recycle.js';
  212. $this->htmlIndexFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'index.html';
  213. $this->htmlAddFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'add.html';
  214. $this->htmlEditFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'edit.html';
  215. $this->htmlRecycleFileName = app()->getRootPath() . 'public' . DS . 'admin' . DS . $this->filterMidName(strtolower($this->midName)) . DS . 'recycle.html';
  216. }
  217. //设置生成migration需要的参数
  218. protected function setMigrationParam()
  219. {
  220. $tplName = 'migration' . DS . 'base';
  221. $data['className'] = $this->migrationClassName;
  222. $data['tableName'] = str_replace(Config::get("database.connections." . Config::get("database.default") . ".prefix"), '', $this->tableName);
  223. $data['engine'] = $this->engine;
  224. $data['tableComment'] = $this->tableComment;
  225. $data['collation'] = $this->collation;
  226. $fields = '';
  227. foreach ($this->fields as $field) {
  228. $fieldData['field'] = $field->field;
  229. $fieldData['dataType'] = $field->data_type;
  230. // $fieldData['limit'] = $field->limit;
  231. $fieldData['null'] = ($field->is_empty == 2) ? 0 : 1;
  232. if (in_array($field->data_type, ['integer', 'biginteger', 'boolean', 'decimal', 'float'])) {
  233. $field->default = intval($field->default);
  234. }
  235. $fieldData['default'] = (!in_array($field->data_type, ['text', 'datetime', 'timestamp'])) ? ' \'default\' => \'' . $field->default . '\',' : ' ';
  236. if ($field->form_type == 'radio' || $field->form_type == 'select') {
  237. $comment = '';
  238. foreach ($field->addition['text'] as $k => $t) {
  239. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  240. }
  241. if (is_string($field->addition['default'])) {
  242. $comment .= '默认:' . $field->addition['default'];
  243. }
  244. $fieldData['comment'] = $field->comment . '.' . $comment;
  245. } elseif ($field->form_type == 'checkbox') {
  246. $comment = '';
  247. foreach ($field->addition['text'] as $k => $t) {
  248. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  249. }
  250. $fieldData['comment'] = $field->comment . '.' . $comment . '默认:' . implode(',', $field->addition['default']);
  251. } elseif ($field->form_type == 'switch') {
  252. $comment = $field->addition['close_value'] . '=' . $field->addition['close_text'] . ',' .
  253. $field->addition['open_value'] . '=' . $field->addition['open_text'] .
  254. ',默认:' . (($field->addition['default_status'] === 'close') ? $field->addition['close_value'] : $field->addition['open_value']);
  255. $fieldData['comment'] = $field->comment . '.' . $comment;
  256. } elseif ($field->form_type == 'xm_select' && $field->addition['data_from_type'] == 'data') {
  257. $comment = '';
  258. foreach ($field->addition['text'] as $k => $t) {
  259. $comment .= $field->addition['value'][$k] . '=' . $t . ',';
  260. }
  261. if ($field->addition['default']) {
  262. $comment .= '默认:' . implode(',', $field->addition['default']);
  263. }
  264. $fieldData['comment'] = $field->comment . '.' . $comment;
  265. } else {
  266. $fieldData['comment'] = $field->comment;
  267. }
  268. if (in_array($field->data_type, ["float", "decimal"])) {
  269. $fieldData['limitPrecisionScale'] = "'precision' => {$field->precision}, 'scale' => {$field->scale}, ";
  270. } elseif (in_array($field->data_type, ["text", "datetime", 'timestamp'])) {
  271. $fieldData['limitPrecisionScale'] = '';
  272. } else {
  273. $fieldData['limitPrecisionScale'] = "'limit' => {$field->limit}, ";
  274. }
  275. $fields .= $this->getReplacedTpl('migration' . DS . 'field', $fieldData) . "\n\t\t\t";
  276. }
  277. $data['fields'] = $fields;
  278. $this->migrationParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->migrationFileName];
  279. }
  280. //生成数据迁移文件并执行数据库迁移命令,生成数据表
  281. protected function createMigration()
  282. {
  283. if ($this->needCreateTable) {
  284. $this->writeToFile($this->migrationParam['tplName'], $this->migrationParam['data'], $this->migrationParam['fileName']);
  285. // 执行php think migrate:run命令是基于数据库迁移文件生成数据表
  286. // 而最新的数据库迁移文件刚使用php程序生成,马上调用数据库迁移命令会读取不到最新刚生成的数据库迁移文件
  287. // 使用clearstatcache函数没什么作用,所以此处停顿1秒
  288. sleep(1);
  289. // 2>&1 是为了将标准错误输出存入output,如果不加,当有错误时output不会有错误输出,仅在windows下经过了测试
  290. exec('php ' . app()->getRootPath() . '\think migrate:run 2>&1', $output, $resCode);
  291. if($resCode){
  292. $filter = array_filter($output);
  293. $this->setError(array_pop($filter));
  294. unlink($this->migrationParam['fileName']);
  295. return false;
  296. }
  297. }
  298. return true;
  299. }
  300. /**
  301. * 设置生成controller需要的参数
  302. * [
  303. * 'tplName'=>模板名,
  304. * 'data' => '执行替换模板的key=>value数组',
  305. * 'fileName' => '要生成的文件名'
  306. * ]
  307. */
  308. protected function setControllerParam()
  309. {
  310. $tplName = 'controller' . DS . 'base';
  311. $data['controllerNamespace'] = str_replace('/', '\\', dirname('app/controller/admin/' . $this->filterMidName($this->midName)));
  312. $data['tableComment'] = $this->tableComment;
  313. $data['modelName'] = strtolower(str_replace('/', '_', $this->midName));
  314. $data['modelClassName'] = $this->controllerModelClassName;
  315. $data['modelNamespace'] = str_replace('/', '\\', dirname('app/model/' . $this->midName));
  316. $data['controllerClassName'] = $this->controllerModelClassName;
  317. $data['indexFunction'] = $this->setIndexFunctionController();
  318. $data['infoFunction'] = $this->setInfoFunctionController();
  319. $data['recycleFunction'] = $this->setRecycleFunctionController();
  320. $data['addFunction'] = $this->setAddFunctionController();
  321. $data['editFunction'] = $this->setEditFunctionController();
  322. $data['tableEditFunction'] = $this->setTableEditFunctionController();
  323. $data['useAdminUserServiceFacade'] = '';
  324. $data['uploadDomainPackage'] = '';
  325. $data['useValidate'] = '';
  326. foreach ($this->fields as $k => $v) {
  327. if ($v['form_type'] === 'admin_user_id') {
  328. $data['useAdminUserServiceFacade'] = "\nuse app\service\admin\UserServiceFacade;";
  329. }
  330. if ($v['form_type'] == 'upload' || $v['form_type'] == 'editor') {
  331. $data['uploadDomainPackage'] = "\nuse laytp\library\UploadDomain;";
  332. }
  333. if ($v['form_type'] == 'password') {
  334. $data['useValidate'] = "\nuse " . str_replace('/', '\\', 'app/validate/admin/' . strtolower(str_replace('/', '_', $this->filterMidName($this->midName))) . '/Add;');
  335. $data['useValidate'] .= "\nuse " . str_replace('/', '\\', 'app/validate/admin/' . strtolower(str_replace('/', '_', $this->filterMidName($this->midName))) . '/Edit;');
  336. if($v['addition']['save_type'] == 2){
  337. $data['useValidate'] .= "\nuse laytp\\library\\Str;";
  338. }
  339. }
  340. }
  341. $this->controllerParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->controllerFileName];
  342. }
  343. //在index方法里,如果有关联模型,在查询时需要查询出关联模型数据
  344. protected function setIndexFunctionController()
  345. {
  346. $relationIndexFunctionLt = 'controller' . DS . 'index';
  347. $data = [];
  348. $data['withRelation'] = "";
  349. $with = [];
  350. foreach ($this->fields as $k => $v) {
  351. if ($v['relation']['table']) {
  352. $with[] = $v['relation']['fun_name'];
  353. }
  354. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'single') {
  355. $with[] = $v['field'] . '_file';
  356. }
  357. }
  358. if ($with) {
  359. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  360. }
  361. $indexFunctionController = $this->getReplacedTpl($relationIndexFunctionLt, $data);
  362. return $indexFunctionController;
  363. }
  364. //在info方法里,如果有上传组件,在查询的时候需要把文件相关的数据查询出来
  365. protected function setInfoFunctionController()
  366. {
  367. $infoFunctionLt = 'controller' . DS . 'info';
  368. $data = [];
  369. $data['withRelation'] = "";
  370. $with = [];
  371. foreach ($this->fields as $k => $v) {
  372. if ($v['form_type'] === 'upload' && $v['addition']['multi'] === 'single') {
  373. $with[] = $v['field'] . '_file';
  374. }
  375. }
  376. if ($with) {
  377. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  378. }
  379. $infoFunctionController = $this->getReplacedTpl($infoFunctionLt, $data);
  380. return $infoFunctionController;
  381. }
  382. //在index方法里,如果有关联模型,在查询时需要查询出关联模型数据
  383. protected function setRecycleFunctionController()
  384. {
  385. $recycleFunctionController = '';
  386. $with = [];
  387. foreach ($this->fields as $k => $v) {
  388. if ($v['relation']['table']) {
  389. $with[] = $v['relation']['fun_name'];
  390. }
  391. }
  392. if ($with) {
  393. $relationIndexFunctionLt = 'controller' . DS . 'recycle';
  394. $data['withRelation'] = "->with(['" . implode("','", $with) . "'])";
  395. $recycleFunctionController = $this->getReplacedTpl($relationIndexFunctionLt, $data);
  396. }
  397. return $recycleFunctionController;
  398. }
  399. //富文本编辑器字段,在添加和编辑的控制器方法里面,所有链接要去掉前缀
  400. protected function setAddFunctionController()
  401. {
  402. $validate = '';
  403. $arrUploadDomainFields = [];
  404. $adminUserId = '';
  405. $arrUnsetFields = [];
  406. foreach ($this->fields as $field) {
  407. if ($field->form_type == 'editor') {
  408. $arrUploadDomainFields[] = "\$post['{$field->field}'] = UploadDomain::delUploadDomain(\$post['{$field->field}'], '{$field->addition['upload_type']}');";
  409. }
  410. if ($field->form_type == 'admin_user_id') {
  411. $adminUserId = "\$post['{$field->field}'] = UserServiceFacade::getUser()->id;";
  412. }
  413. if ($field->field == 'password') {
  414. $validate = "\$validate = new Add();\n\t\tif(!\$validate->check(\$post)){\n\t\t\treturn \$this->error(\$validate->getError());\n\t\t}";
  415. if($field->addition['save_type'] == 2){
  416. if($field->is_empty == 2){
  417. $arrUnsetFields[] = "if(!\$post['" . $field->field . "']){\n\t\t\tunset(\$post['" . $field->field . "']);\n\t\t}else{\n\t\t\tStr::createPassword(\$post['" . $field->field . "']);\n\t\t}";
  418. }else{
  419. $arrUnsetFields[] = "if(isset(\$post['" . $field->field . "']) && \$post['" . $field->field . "']) \$post['" . $field->field . "'] = Str::createPassword(\$post['" . $field->field . "']);";
  420. }
  421. }
  422. }
  423. }
  424. if ($arrUploadDomainFields || $adminUserId || $validate || $arrUnsetFields) {
  425. $data['validate'] = $validate ? "\n\t\t" . $validate : '';
  426. $data['adminUserId'] = $adminUserId ? "\n\t\t" . $adminUserId : '';
  427. $data['uploadDomainFields'] = $arrUploadDomainFields ? "\n\t\t" . implode("\n\t\t", $arrUploadDomainFields) : '';
  428. $data['unsetFields'] = $arrUnsetFields ? "\n\t\t" . implode("\n\t\t", $arrUnsetFields) : '';
  429. $addFunctionLt = 'controller' . DS . 'add';
  430. $addFunctionController = $this->getReplacedTpl($addFunctionLt, $data);
  431. return $addFunctionController;
  432. } else {
  433. return '';
  434. }
  435. }
  436. //富文本编辑器字段,在添加和编辑的控制器方法里面,所有链接要去掉前缀
  437. protected function setEditFunctionController()
  438. {
  439. $validate = '';
  440. $arrUploadDomainFields = [];
  441. $adminUserId = '';
  442. $arrUnsetFields = [];
  443. foreach ($this->fields as $k => $v) {
  444. if ($v['form_type'] == 'editor') {
  445. $arrUploadDomainFields[] = "\$post['{$v['field']}'] = UploadDomain::delUploadDomain(\$post['{$v['field']}'], '{$v['addition']['upload_type']}');";
  446. }
  447. if ($v['form_type'] == 'admin_user_id') {
  448. $adminUserId = "\$post['{$v['field']}'] = UserServiceFacade::getUser()->id;";
  449. }
  450. if ($v['field'] == 'password') {
  451. $validate = "\$validate = new Edit();\r\n\t\tif(!\$validate->check(\$post)){\r\n\t\t\treturn \$this->error(\$validate->getError());\r\n\t\t}";
  452. if($v['addition']['save_type'] == 2){
  453. $arrUnsetFields[] = "if(!\$post['" . $v['field'] . "']){\n\t\t\tunset(\$post['" . $v['field'] . "']);\n\t\t}else{\n\t\t\t\$post['" . $v['field'] . "'] = Str::createPassword(\$post['" . $v['field'] . "']);\n\t\t}";
  454. }else{
  455. $arrUnsetFields[] = 'if(!$post[\'' . $v['field'] . '\']) unset($post[\'' . $v['field'] . '\']);';
  456. }
  457. }
  458. }
  459. if ($arrUploadDomainFields || $adminUserId || $validate || $arrUnsetFields) {
  460. $data['validate'] = $validate ? "\n\t\t" . $validate : '';
  461. $data['adminUserId'] = $adminUserId ? "\n\t\t" . $adminUserId : '';
  462. $data['uploadDomainFields'] = $arrUploadDomainFields ? "\n\t\t" . implode("\n\t\t", $arrUploadDomainFields) : '';
  463. $data['unsetFields'] = $arrUnsetFields ? "\n\t\t" . implode("\n\t\t", $arrUnsetFields) : '';
  464. $editFunctionLt = 'controller' . DS . 'edit';
  465. $editFunctionController = $this->getReplacedTpl($editFunctionLt, $data);
  466. return $editFunctionController;
  467. } else {
  468. return '';
  469. }
  470. }
  471. //设置表格编辑方法
  472. protected function setTableEditFunctionController()
  473. {
  474. $functionContent = [];
  475. foreach ($this->fields as $k => $v) {
  476. if (($v['form_type'] == 'input' && isset($v['addition']['open_table_edit']) && $v['addition']['open_table_edit'] == 1) || $v['form_type'] == 'switch') {
  477. $data['field'] = $v['field'];
  478. $data['comment'] = $v['comment'];
  479. $data['funcName'] = ucfirst($this->underlineToCamel($v['field']));
  480. $tableEditFunctionLt = 'controller' . DS . 'tableEdit';
  481. $functionContent[] = $this->getReplacedTpl($tableEditFunctionLt, $data);
  482. }
  483. }
  484. $tableEditFunctionController = implode("\n\t\t", $functionContent);
  485. return $tableEditFunctionController;
  486. }
  487. //生成controller层
  488. protected function createController()
  489. {
  490. $this->controllerParam['data']['hasSoftDel'] = "\n\tprotected \$hasSoftDel=0;//是否拥有软删除功能";
  491. //是否拥有软删除功能
  492. foreach ($this->fields as $k => $v) {
  493. if ($v['field'] == 'delete_time') {
  494. $this->controllerParam['data']['hasSoftDel'] = "\n\tprotected \$hasSoftDel=1;//是否拥有软删除功能";
  495. break;
  496. }
  497. }
  498. $this->writeToFile($this->controllerParam['tplName'], $this->controllerParam['data'], $this->controllerParam['fileName']);
  499. }
  500. /**
  501. * 设置生成model需要的参数
  502. * [
  503. * 'tplName'=>模板名,
  504. * 'data' => '执行替换模板的key=>value数组',
  505. * 'fileName' => '要生成的文件名'
  506. * ]
  507. */
  508. protected function setModelParam()
  509. {
  510. $tplName = 'model' . DS . 'base';
  511. $arrTableName = explode('_', $this->tableName);
  512. $tablePrefix = $arrTableName['0'] . '_';
  513. $data['tableName'] = '';
  514. if ($tablePrefix != Config::get('database.connections.' . Config::get('database.default') . '.prefix')) {
  515. $data['tableName'] = 'protected $table = \'' . $this->tableName . '\';';
  516. }
  517. $data['tableComment'] = $this->tableComment;
  518. $data['modelName'] = strtolower(str_replace('/', '_', $this->midName));
  519. $data['modelClassName'] = $this->controllerModelClassName;
  520. $data['modelNamespace'] = str_replace('/', '\\', dirname('app/model/' . $this->midName));
  521. $data['relationModel'] = $this->setRelationModel();
  522. $data['autoTimeFormat'] = $this->autoTimeFormat();
  523. $data['getAttrFun'] = $this->getAttrFun();
  524. $this->modelParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->modelFileName];
  525. }
  526. protected function setRelationModel()
  527. {
  528. $relationModelFunctionLt = 'model' . DS . 'relation_model_function';
  529. $arrayRelationModel = [];
  530. $relationModel = '';
  531. foreach ($this->fields as $k => $item) {
  532. if ($item['relation']['table']) {
  533. $data['relationFunctionName'] = $this->underlineToCamel($item['relation']['fun_name']);
  534. $data['relationType'] = $item['relation']['type'];
  535. $data['relationModelName'] = 'app\model\\' . $this->getNameByTable($item['relation']['table']);
  536. $data['foreignKey'] = $item['field'];
  537. $data['localKey'] = $item['relation']['field'];
  538. $arrayRelationModel[] = $this->getReplacedTpl($relationModelFunctionLt, $data);
  539. }
  540. if ($item['form_type'] === 'upload' && $item['addition']['multi'] === 'single') {
  541. $data['relationFunctionName'] = $this->underlineToCamel($item['field']) . 'File';
  542. $data['relationType'] = 'belongsTo';
  543. $data['relationModelName'] = 'app\model\Files';
  544. $data['foreignKey'] = $item['field'];
  545. $data['localKey'] = 'id';
  546. $arrayRelationModel[] = $this->getReplacedTpl($relationModelFunctionLt, $data);
  547. }
  548. }
  549. if ($arrayRelationModel) {
  550. $relationModel = implode("\n\n\t", $arrayRelationModel);
  551. }
  552. return $relationModel;
  553. }
  554. //时间选择器,int类型,自动类型转换
  555. protected function autoTimeFormat()
  556. {
  557. $timeSet = [];
  558. foreach ($this->fields as $k => $v) {
  559. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'integer') {
  560. if ($v['addition']['date_type'] == 'datetime') {
  561. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m-d H:i:s\',';
  562. } else if ($v['addition']['date_type'] == 'month') {
  563. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m\',';
  564. } else if ($v['addition']['date_type'] == 'date') {
  565. $timeSet[$v['field']] = "\n\t\t" . '\'' . $v['field'] . '\' => \'timestamp:Y-m-d\',';
  566. }
  567. }
  568. }
  569. if ($timeSet) {
  570. return 'protected $type = [' . implode('', $timeSet) . "\n\t];";
  571. } else {
  572. return '';
  573. }
  574. }
  575. protected function getAttrFun()
  576. {
  577. $fun = [];
  578. foreach ($this->fields as $k => $v) {
  579. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'integer') {
  580. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'IntAttr($value, $data)' . "\n\t" .
  581. '{' . "\n\t\t" .
  582. 'return isset($data[\'' . $v['field'] . '\']) ? $data[\'' . $v['field'] . '\'] : 0;' . "\n\t" .
  583. '}';
  584. }
  585. if ($v['form_type'] == 'laydate' && $v['data_type'] == 'datetime') {
  586. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'IntAttr($value, $data)' . "\n\t" .
  587. '{' . "\n\t\t" .
  588. 'return isset($data[\'' . $v['field'] . '\']) ? strtotime($data[\'' . $v['field'] . '\']) : 0;' . "\n\t" .
  589. '}';
  590. }
  591. if ($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi') {
  592. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'FileAttr($value, $data)' . "\n\t" .
  593. '{' . "\n\t\t" .
  594. 'return (isset($data[\'' . $v['field'] . '\']) && $data[\'' . $v['field'] . '\']) ? UploadDomain::multiJoin($data[\'' . $v['field'] . '\']) : \'\';' . "\n\t" .
  595. '}';
  596. }
  597. if ($v['form_type'] == 'editor') {
  598. if ($v['addition']['upload_type'] != 'local') {
  599. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'Attr($value, $data)' . "\n\t" .
  600. '{' . "\n\t\t" .
  601. 'return $value ? UploadDomain::addUploadDomain($value, \'' . $v['addition']['upload_type'] . '\') : \'\';' . "\n\t" .
  602. '}';
  603. } else {
  604. $fun[] = 'public function get' . ucfirst($this->underlineToCamel($v['field'])) . 'Attr($value, $data)' . "\n\t" .
  605. '{' . "\n\t\t" .
  606. 'return $value ? UploadDomain::addUploadDomain($value) : \'\';' . "\n\t" .
  607. '}';
  608. }
  609. }
  610. }
  611. if ($fun) {
  612. return implode("\n\n\t", $fun);
  613. } else {
  614. return '';
  615. }
  616. }
  617. protected function createModel()
  618. {
  619. //是否拥有软删除功能
  620. $this->modelParam['data']['softDelPackage'] = "";
  621. $this->modelParam['data']['useSoftDel'] = "";
  622. $this->modelParam['data']['defaultSoftDelete'] = "";
  623. $this->modelParam['data']['uploadDomainPackage'] = "";
  624. $this->modelParam['data']['append'] = "";
  625. $append = [];
  626. foreach ($this->fields as $k => $v) {
  627. if ($v['field'] == 'delete_time') {
  628. $this->modelParam['data']['softDelPackage'] = "\nuse think\model\concern\SoftDelete;";
  629. $this->modelParam['data']['useSoftDel'] = "\n\tuse SoftDelete;";
  630. if ($v['data_type'] === 'datetime' || $v['data_type'] === 'timestamp') {
  631. $this->modelParam['data']['defaultSoftDelete'] = "";
  632. } else if ($v['data_type'] === 'integer') {
  633. $this->modelParam['data']['defaultSoftDelete'] = "\n\tprotected \$defaultSoftDelete=0;";
  634. }
  635. }
  636. if (($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi') || $v['form_type'] == 'editor') {
  637. $this->modelParam['data']['uploadDomainPackage'] = "\nuse laytp\library\UploadDomain;";
  638. }
  639. if (($v['form_type'] == 'upload' && $v['addition']['multi'] == 'multi')) {
  640. $append[] = '\'' . $v['field'] . '_file\'';
  641. }
  642. if ($v['form_type'] == 'laydate') {
  643. $append[] = '\'' . $v['field'] . '_int\'';
  644. }
  645. }
  646. if ($append) {
  647. $this->modelParam['data']['append'] = implode(',', $append);
  648. }
  649. $this->writeToFile($this->modelParam['tplName'], $this->modelParam['data'], $this->modelParam['fileName']);
  650. }
  651. protected function setJsParam()
  652. {
  653. $apiPrefix = $this->compatibleApiRoute();
  654. $tplName = 'js' . DS . 'index';
  655. $cols = "{type:'checkbox',fixed:'left'}\n\t\t\t\t";
  656. $recycleCols = "{type:'checkbox',fixed:'left'}\n\t\t\t\t";
  657. $hasFirstCols = true;//是否已经有了正常的第一行数据
  658. //是否隐藏主键列
  659. if ($this->isHidePk != 1) {
  660. $hasFirstCols = true;
  661. $temp = ",{field:'id',title:'ID',align:'center',width:80,fixed:'left'}";
  662. $cols .= $temp;
  663. $recycleCols .= $temp;
  664. } else {
  665. $temp = "//,{field:'id',title:'ID',align:'center',width:80}";
  666. $cols .= $temp;
  667. $recycleCols .= $temp;
  668. }
  669. //是否生成序号列
  670. if ($this->isCreateNumber == 1) {
  671. if ($hasFirstCols) {
  672. $temp = "\n\t\t\t\t,{field:'layui_number',title:'序号',align:'center',width:80,type:'numbers'}";
  673. $cols .= $temp;
  674. $recycleCols .= $temp;
  675. } else {
  676. $temp = "\t\t\t\t{field:'layui_number',title:'序号',align:'center',width:80,type:'numbers'}";
  677. $cols .= $temp;
  678. $recycleCols .= $temp;
  679. }
  680. if (!$hasFirstCols) $hasFirstCols = true;
  681. }
  682. foreach ($this->fields as $k => $v) {
  683. // if ($v['table_show'] == 2) continue;
  684. if (!$hasFirstCols) {
  685. $hasFirstCols = true;
  686. $temp = "\t\t\t\t{field:'{$v['field']}',title:'{$v['comment']}'";
  687. $recycleTemp = "\t\t\t\t{field:'{$v['field']}',title:'{$v['comment']}'";
  688. } else {
  689. $temp = "\n\t\t\t\t,{field:'{$v['field']}',title:'{$v['comment']}'";
  690. $recycleTemp = "\n\t\t\t\t,{field:'{$v['field']}',title:'{$v['comment']}'";
  691. }
  692. if ($v['cell_width']) {
  693. $temp .= ",width:{$v['cell_width']}";
  694. $recycleTemp .= ",width:{$v['cell_width']}";
  695. }
  696. $temp .= ",align:'center'";
  697. $recycleTemp .= ",align:'center'";
  698. //表头排序
  699. if ($v['is_thead_sort'] == 1) {
  700. $temp .= ",sort:true";
  701. $recycleTemp .= ",sort:true";
  702. }
  703. //关联模型js渲染
  704. if ($v['relation']['table'] && $v['relation']['show_field']) {
  705. if ($v['form_type'] === 'xm_select') {
  706. if (isset($v['addition']['single_multi_type']) && $v['addition']['single_multi_type'] === 'single') {
  707. $templet = "{{# if(d." . $v['relation']['fun_name'] . "){ }}{{d." . $v['relation']['fun_name'] . "." . $v['relation']['show_field'] . "}}{{# }else{ }}-{{# } }}";
  708. $temp .= ",templet:'<div>" . $templet . "</div>'";
  709. $recycleTemp .= ",templet:'<div>" . $templet . "</div>'";
  710. }
  711. } else {
  712. $templet = "{{# if(d." . $v['relation']['fun_name'] . "){ }}{{d." . $v['relation']['fun_name'] . "." . $v['relation']['show_field'] . "}}{{# }else{ }}-{{# } }}";
  713. $temp .= ",templet:'<div>" . $templet . "</div>'";
  714. $recycleTemp .= ",templet:'<div>" . $templet . "</div>'";
  715. }
  716. }
  717. //单行输入框,开启了表格编辑,需要渲染成editInput模板,没有开启的话要转义展示,避免XSS
  718. if ($v['form_type'] == 'input') {
  719. if (isset($v['addition']['open_table_edit']) && $v['addition']['open_table_edit'] == 1) {
  720. $editInputUrl = $apiPrefix . '/set' . ucfirst($this->underlineToCamel($v['field']));
  721. $temp .= ",templet:function(d){
  722. return laytpForm.tableForm.editInput('{$v['field']}',d,'{$editInputUrl}');
  723. }";
  724. $recycleTemp .= ",templet:function(d){
  725. return laytpForm.tableForm.recycleEditInput('{$v['field']}',d,'{$editInputUrl}');
  726. }";
  727. } else {
  728. // $temp .= ",templet:function(d){
  729. // return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  730. // }";
  731. // $recycleTemp .= ",templet:function(d){
  732. // return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  733. // }";
  734. }
  735. }
  736. //文本域要转义展示,避免XSS
  737. // if ($v['form_type'] == 'textarea') {
  738. // $temp .= ",templet:function(d){
  739. // return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  740. // }";
  741. // $recycleTemp .= ",templet:function(d){
  742. // return layui.laytpl('{{=d.{$v['field']}}}').render({{$v['field']}:d.{$v['field']}});
  743. // }";
  744. // }
  745. //渲染单选按钮,单选按钮渲染成status模板
  746. if ($v['form_type'] == 'radio') {
  747. $items = $v['addition'];
  748. $jsonArr['value'] = $items['value'];
  749. $jsonArr['text'] = $items['text'];
  750. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  751. $temp .= ",templet:function(d){
  752. return laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});
  753. }";
  754. $recycleTemp .= ",templet:function(d){
  755. return laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});
  756. }";
  757. }
  758. //渲染开关按钮,开关按钮渲染成开关
  759. if ($v['form_type'] == 'switch') {
  760. $items = $v['addition'];
  761. $temp .= ",templet:function(d){
  762. return laytpForm.tableForm.switch(\"{$v['field']}\", d, {
  763. \"open\": {\"value\": {$items['open_value']}, \"text\": \"{$items['open_text']}\"},
  764. \"close\": {\"value\": {$items['close_value']}, \"text\": \"{$items['close_text']}\"}
  765. });
  766. }";
  767. $recycleTemp .= ",templet:function(d){
  768. return laytpForm.tableForm.recycleSwitch(\"{$v['field']}\", d, {
  769. \"open\": {\"value\": {$items['open_value']}, \"text\": \"{$items['open_text']}\"},
  770. \"close\": {\"value\": {$items['close_value']}, \"text\": \"{$items['close_text']}\"}
  771. });
  772. }";
  773. }
  774. //渲染下拉框,下拉框渲染成status模板
  775. if ($v['form_type'] == 'select') {
  776. $jsonObj = json_encode($v['addition'], JSON_UNESCAPED_UNICODE);
  777. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  778. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  779. }
  780. //复选框渲染成flag的模板
  781. if ($v['form_type'] == 'checkbox') {
  782. $jsonObj = json_encode($v['addition'], JSON_UNESCAPED_UNICODE);
  783. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  784. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  785. }
  786. //xmSelect,数据来源=data,单选,渲染成status
  787. if ($v['form_type'] == 'xm_select' && $v['addition']['data_from_type'] == 'data' && $v['addition']['single_multi_type'] == 'single') {
  788. $jsonArr['value'] = $v['addition']['value'];
  789. $jsonArr['text'] = $v['addition']['text'];
  790. $jsonArr['default'] = $v['addition']['default'];
  791. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  792. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  793. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.status('{$v['field']}',d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  794. }
  795. //xmSelect,数据来源=data,多选,渲染成flag
  796. if ($v['form_type'] == 'xm_select' && $v['addition']['data_from_type'] == 'data' && $v['addition']['single_multi_type'] == 'multi') {
  797. $jsonArr['value'] = $v['addition']['value'];
  798. $jsonArr['text'] = $v['addition']['text'];
  799. $jsonArr['default'] = $v['addition']['default'];
  800. $jsonObj = json_encode($jsonArr, JSON_UNESCAPED_UNICODE);
  801. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  802. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.flag(d.{$v['field']},{$jsonObj});\n\t\t\t\t}";
  803. }
  804. //image模板
  805. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'image') {
  806. if ($v['addition']['multi'] == 'single') {
  807. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.images(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  808. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.images(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  809. } else {
  810. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.images(d.{$v['field']}_file.path);\n\t\t\t\t}";
  811. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.images(d.{$v['field']}_file.path);\n\t\t\t\t}";
  812. }
  813. }
  814. //video模板
  815. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'video') {
  816. if ($v['addition']['multi'] == 'single') {
  817. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.video(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  818. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.video(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  819. } else {
  820. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.video(d.{$v['field']}_file.path);\n\t\t\t\t}";
  821. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.video(d.{$v['field']}_file.path);\n\t\t\t\t}";
  822. }
  823. }
  824. //audio模板
  825. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'audio') {
  826. if ($v['addition']['multi'] == 'single') {
  827. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.audio(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  828. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.audio(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  829. } else {
  830. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.audio(d.{$v['field']}_file.path);\n\t\t\t\t}";
  831. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.audio(d.{$v['field']}_file.path);\n\t\t\t\t}";
  832. }
  833. }
  834. //file模板
  835. if ($v['form_type'] == 'upload' && $v['addition']['accept'] == 'file') {
  836. if ($v['addition']['multi'] == 'single') {
  837. $temp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.file(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  838. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn d.{$v['field']}_file ? laytp.tableFormatter.file(d.{$v['field']}_file.path) : \"\";\n\t\t\t\t}";
  839. } else {
  840. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.file(d.{$v['field']}_file.path);\n\t\t\t\t}";
  841. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.file(d.{$v['field']}_file.path);\n\t\t\t\t}";
  842. }
  843. }
  844. //colorPicker模板
  845. if ($v['form_type'] == 'color_picker') {
  846. $temp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.colorPicker(d.{$v['field']});\n\t\t\t\t}";
  847. $recycleTemp .= ",templet:function(d){\n\t\t\t\t\treturn laytp.tableFormatter.colorPicker(d.{$v['field']});\n\t\t\t\t}";
  848. }
  849. $temp .= "}";
  850. $recycleTemp .= "}";
  851. if ($v['table_show'] == 1) $cols .= $temp;
  852. $recycleCols .= $recycleTemp;
  853. }
  854. $data['cols'] = $cols;
  855. $data['cellMinWidth'] = 120;
  856. $data['apiPrefix'] = $this->compatibleApiRoute();
  857. $data['htmlPrefix'] = $this->compatibleHtmlPath();
  858. $this->jsParam = ['tplName' => $tplName, 'data' => $data, 'fileName' => $this->jsFileName];
  859. $data['recycleCols'] = $recycleCols;
  860. $this->recycleJsParam = ['tplName' => 'js' . DS . 'recycle', 'data' => $data, 'fileName' => $this->recycleJsFileName];
  861. }
  862. public function compatibleApiRoute($midName = false)
  863. {
  864. if (!$midName) {
  865. $midName = strtolower($this->midName);
  866. } else {
  867. $midName = strtolower($midName);
  868. }
  869. if ($midName === "user") {
  870. return "/admin.api.user";
  871. }
  872. if (substr($midName, 0, 5) . '.' != 'admin.') {
  873. return '/admin.' . str_replace('/', '.', $midName);
  874. } else {
  875. return '/' . str_replace('/', '.', $midName);
  876. }
  877. }
  878. public function compatibleHtmlPath()
  879. {
  880. $midName = strtolower($this->midName);
  881. if (substr($midName, 0, 5) != 'admin/') {
  882. return '/admin/' . strtolower($midName);
  883. } else {
  884. return '/' . strtolower($midName);
  885. }
  886. }
  887. /**
  888. * 获取一个数组,根据传入的参数,这个参数string的格式类似于:0=游泳,1=下棋,2=游戏,3=乒乓球,4=羽毛,5=跑步,6=爬山,7=美食,default=0;1;2
  889. * @param $string
  890. * @return array
  891. */
  892. public function getArrayByString($string)
  893. {
  894. $items = explode(",", $string);
  895. $radio_items = [];//待选项数组
  896. foreach ($items as $k => $v) {
  897. $temp = explode('=', $v);
  898. if ($temp[0] != 'default') {
  899. $radio_items[$temp[0]] = $temp[1];
  900. }
  901. }
  902. return $radio_items;
  903. }
  904. //生成js文件
  905. protected function createJs()
  906. {
  907. $this->writeToFile($this->jsParam['tplName'], $this->jsParam['data'], $this->jsParam['fileName']);
  908. if ($this->hasSoftDel) {
  909. $this->writeToFile($this->recycleJsParam['tplName'], $this->recycleJsParam['data'], $this->recycleJsParam['fileName']);
  910. }
  911. }
  912. protected function setHtmlParam()
  913. {
  914. $indexTplName = 'html' . DS . 'index';
  915. $indexData['jsFileName'] = $this->filterMidName(strtolower($this->midName)) . '.js';
  916. $indexSearchForm = [];
  917. $recycleSearchForm = [];
  918. $linkageSelectSearchHtml = [];
  919. $recycleLinkageSelectSearchHtml = [];
  920. $unSearchType = ['password', 'upload', 'color_picker'];
  921. foreach ($this->fields as $k => $v) {
  922. if (!in_array($v['form_type'], $unSearchType)) {
  923. if (in_array($v['form_type'], ['linkage_select', 'linkage_xm_select'])) {
  924. if ($v['search_show'] == 1) {
  925. $linkageSelectSearchHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getSearchFormContent($v);
  926. }
  927. $recycleLinkageSelectSearchHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getSearchFormContent($v);
  928. } else {
  929. $searchItemContent = $this->getSearchFormContent($v);
  930. if ($v['search_show'] == 1) {
  931. $indexSearchForm[] = $this->getSearchFormItem($v, $searchItemContent);
  932. }
  933. $recycleSearchForm[] = $this->getSearchFormItem($v, $searchItemContent);
  934. }
  935. }
  936. }
  937. if (count($linkageSelectSearchHtml)) {
  938. foreach ($linkageSelectSearchHtml as $formType => $groupList) {
  939. foreach ($groupList as $groupName => $item) {
  940. $indexSearchForm[] = $this->getLinkageSelectSearchFormGroup($groupName, join("\n\t\t\t\t", $item));
  941. }
  942. }
  943. }
  944. if (count($recycleLinkageSelectSearchHtml)) {
  945. foreach ($recycleLinkageSelectSearchHtml as $formType => $groupList) {
  946. foreach ($groupList as $groupName => $item) {
  947. $recycleSearchForm[] = $this->getLinkageSelectSearchFormGroup($groupName, join("\n\t\t\t\t", $item));
  948. }
  949. }
  950. }
  951. $indexData['searchItemId'] = ($this->isHidePk == 1) ? '' : '<div class="layui-form-item layui-inline">
  952. <label class="layui-form-label" title="ID">ID</label>
  953. <div class="layui-input-inline">
  954. <input autocomplete="off" type="text" id="id" name="search_param[id][value]" id="id" placeholder="请输入ID" class="layui-input">
  955. <input type="hidden" name="search_param[id][condition]" value="=">
  956. </div>
  957. </div>';
  958. $indexData['searchForm'] = implode("\n\n\t\t\t", $indexSearchForm);
  959. $indexData['apiPrefix'] = $this->compatibleApiRoute();
  960. $indexData['htmlPrefix'] = $this->compatibleHtmlPath();
  961. $this->htmlIndexParam = ['tplName' => $indexTplName, 'data' => $indexData, 'fileName' => $this->htmlIndexFileName];
  962. $addData = [];
  963. $editData = [];
  964. // $detailData = [];
  965. $recycleData = [];
  966. $linkageSelectAddHtml = [];
  967. $linkageSelectEditHtml = [];
  968. foreach ($this->fields as $k => $v) {
  969. if (in_array($v['form_type'], ['linkage_select', 'linkage_xm_select'])) {
  970. if ($v['add_show'] == 1) {
  971. $linkageSelectAddHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getFormItem($v, 'add');
  972. }
  973. if ($v['edit_show'] == 1) {
  974. $linkageSelectEditHtml[$v['form_type']][$v['addition']['group_name']][] = $this->getFormItem($v, 'edit');
  975. }
  976. } else if (in_array($v['form_type'], ['admin_user_id'])) {
  977. // $addData[] = $this->getFormItem($v, 'add');
  978. // $editData[] = $this->getFormItem($v, 'edit');
  979. } else {
  980. if ($v['add_show'] == 1) {
  981. $addItemContent = $this->getFormItem($v, 'add');
  982. $addData[] = $this->getFormGroup($v['comment'], $addItemContent, $v['is_empty']);
  983. if ($v['form_type'] == 'password' && $v['addition']['re_password']) {
  984. $reV = $v->toArray();
  985. $reV['field'] = 're_' . $reV['field'];
  986. $addItemContent = $this->getFormItem($reV, 'add');
  987. $addData[] = $this->getFormGroup('重复' . $reV['comment'], $addItemContent, $reV['is_empty']);
  988. }
  989. }
  990. if ($v['edit_show'] == 1) {
  991. $editItemContent = $this->getFormItem($v, 'edit');
  992. $editData[] = $this->getFormGroup($v['comment'], $editItemContent, $v['is_empty']);
  993. if ($v['form_type'] == 'password' && $v['addition']['re_password']) {
  994. $reV = $v->toArray();
  995. $reV['field'] = 're_' . $reV['field'];
  996. $addItemContent = $this->getFormItem($reV, 'edit');
  997. $editData[] = $this->getFormGroup('重复' . $reV['comment'], $addItemContent, $reV['is_empty']);
  998. }
  999. }
  1000. }
  1001. // $detailData[] = $this->getDetailItem($v, $this->getDetailContent($v));
  1002. }
  1003. if (count($linkageSelectAddHtml)) {
  1004. foreach ($linkageSelectAddHtml as $formType => $groupList) {
  1005. foreach ($groupList as $groupName => $item) {
  1006. $addData[] = $this->getFormGroup($groupName, join("\n\t\t\t\t\t\t", $item), 1);
  1007. }
  1008. }
  1009. }
  1010. if (count($linkageSelectEditHtml)) {
  1011. foreach ($linkageSelectEditHtml as $formType => $groupList) {
  1012. foreach ($groupList as $groupName => $item) {
  1013. $editData[] = $this->getFormGroup($groupName, join("\n\t\t\t\t\t\t", $item), 1);
  1014. }
  1015. }
  1016. }
  1017. $addTplName = 'html' . DS . 'add';
  1018. $addForm['formContent'] = implode("\n\n", $addData);
  1019. $addForm['action'] = $this->compatibleApiRoute() . '/add';
  1020. $this->htmlAddParam = ['tplName' => $addTplName, 'data' => $addForm, 'fileName' => $this->htmlAddFileName];
  1021. $editTplName = 'html' . DS . 'edit';
  1022. $editForm['formContent'] = implode("\n\n", $editData);
  1023. $editForm['action'] = $this->compatibleApiRoute() . '/edit';
  1024. $editForm['infoAction'] = $this->compatibleApiRoute() . '/info';
  1025. $this->htmlEditParam = ['tplName' => $editTplName, 'data' => $editForm, 'fileName' => $this->htmlEditFileName];
  1026. // $detailTplName = 'html' . DS . 'detail';
  1027. // $detailHtmlData['tableContent'] = implode("\n", $detailData);
  1028. // $this->htmlDetailParam = ['tplName' => $detailTplName, 'data' => $detailHtmlData, 'fileName' => $this->htmlDetailFileName];
  1029. $recycleTplName = 'html' . DS . 'recycle';
  1030. $recycleData['searchForm'] = implode("\n\n\t\t", $recycleSearchForm);
  1031. $recycleData['jsFileName'] = strtolower($this->midName) . 'Recycle.js';
  1032. $recycleData['apiPrefix'] = $this->compatibleApiRoute();
  1033. $recycleData['htmlPrefix'] = $this->compatibleHtmlPath();
  1034. $this->htmlRecycleParam = ['tplName' => $recycleTplName, 'data' => $recycleData, 'fileName' => $this->htmlRecycleFileName];
  1035. }
  1036. protected function getDetailItem($info, $content)
  1037. {
  1038. return <<<EOD
  1039. <tr>
  1040. <td style="table-layout: fixed;word-break: break-all;">{$info['comment']}</td>
  1041. <td style="table-layout: fixed;word-break: break-all;">{$content}</td>
  1042. </tr>
  1043. EOD;
  1044. }
  1045. protected function getDetailContent($info)
  1046. {
  1047. //关联模型
  1048. if ($info['relation']['table'] && $info['relation']['show_field']) {
  1049. return "{{d." . $info['relation']['fun_name'] . "." . $info['relation']['show_field'] . "}}";
  1050. } else if ($info['form_type'] === 'radio') {
  1051. $additionArr = [];
  1052. foreach ($info['addition']['value'] as $k => $item) {
  1053. $additionArr[$item] = $info['addition']['text'][$k];
  1054. }
  1055. $additionJson = json_encode($additionArr, JSON_UNESCAPED_UNICODE);
  1056. return "\n\t\t\t\t{{# let " . $info['field'] . "Json = " . $additionJson . " }}\n\t\t\t\t<span class='layui-icon layui-icon-circle-dot' style='color:#009688;font-size:14px;'>{{" . $info['field'] . "Json[d." . $info['field'] . "]}}</span>\n\t\t\t";
  1057. } else if ($info['form_type'] === 'switch') {
  1058. $additionArr = [
  1059. $info['addition']['close_value'] => $info['addition']['close_text'],
  1060. $info['addition']['open_value'] => $info['addition']['open_text'],
  1061. ];
  1062. $additionJson = json_encode($additionArr, JSON_UNESCAPED_UNICODE);
  1063. return "\n\t\t\t\t{{# let " . $info['field'] . "Json = " . $additionJson . " }}\n\t\t\t\t<span class='layui-icon layui-icon-circle-dot' style='color:#FF5722;font-size:14px;'>{{" . $info['field'] . "Json[d." . $info['field'] . "]}}</span>\n\t\t\t";
  1064. } else if ($info['form_type'] === 'checkbox') {
  1065. $custom = ["#FF5722", "#009688", "#FFB800", "#2F4056", "#1E9FFF", "#393D49", "#999999", "#0b1bf8", "#7a0bf8", "#f00bf8", "#5FB878", "#1E9FFF", "#2F4056"];
  1066. $additionArr = [];
  1067. foreach ($info['addition']['value'] as $k => $item) {
  1068. $additionArr[$item] = [
  1069. 'text' => $info['addition']['text'][$k],
  1070. 'color' => $custom[$k],
  1071. ];
  1072. }
  1073. $additionJson = json_encode($additionArr, JSON_UNESCAPED_UNICODE);
  1074. $additionJson = substr($additionJson, 0, -1) . ' ' . substr($additionJson, -1, 1);
  1075. return "\n\t\t\t\t{{# let " . $info['field'] . "Json = " . $additionJson . " }}\n\t\t\t\t" .
  1076. "{{# let " . $info['field'] . "Arr = d." . $info['field'] . ".split(\",\"), " . $info['field'] . "K}}\n\t\t\t\t" .
  1077. "{{# for(" . $info['field'] . "K in " . $info['field'] . "Arr){ }}\n\t\t\t\t\t"
  1078. . "<span class=\"layui-btn layui-btn-xs\" style=\"background-color: {{" . $info['field'] . "Json[" . $info['field'] . "Arr[" . $info['field'] . "K]]['color']}}\">{{" . $info['field'] . "Json[" . $info['field'] . "Arr[" . $info['field'] . "K]]['text']}}</span>\n\t\t\t\t" .
  1079. "{{# } }}\n\t\t\t";
  1080. } else if ($info['form_type'] === 'select') {
  1081. $additionArr = [];
  1082. foreach ($info['addition']['value'] as $k => $item) {
  1083. $additionArr[$item] = $info['addition']['text'][$k];
  1084. }
  1085. $additionJson = json_encode($additionArr, JSON_UNESCAPED_UNICODE);
  1086. return "\n\t\t\t\t{{# let " . $info['field'] . "Json = " . $additionJson . " }}\n\t\t\t\t<span class='layui-icon layui-icon-circle-dot' style='color:#1E9FFF;font-size:14px;'>{{" . $info['field'] . "Json[d." . $info['field'] . "]}}</span>\n\t\t\t";
  1087. } else if ($info['form_type'] === 'color_picker') {
  1088. return "<span class='layui-badge' style='background-color:{{d." . $info['field'] . "}};font-size:14px;'>&nbsp;&nbsp;</span>\n\t\t\t";
  1089. } else if ($info['form_type'] === 'upload') {
  1090. if ($info['addition']['accept'] === 'image') {
  1091. if ($info['addition']['multi'] === 'single') {
  1092. return "\n\t\t\t\t<a target=\"_blank\" href=\"{{d." . $info['field'] . "}}\"><img id=\"" . $info['field'] . "\" style=\"width:30px;height:30px;\" /></a>" .
  1093. "\n\t\t\t\t<script>" .
  1094. "\n\t\t\t\t\tvar " . $info['field'] . "T" . $info['table_id'] . " = \"{{d." . $info['field'] . "}}\";" .
  1095. "\n\t\t\t\t\tdocument.getElementById(\"" . $info['field'] . "\").src = " . $info['field'] . "T" . $info['table_id'] . ";" .
  1096. "\n\t\t\t\t</script>\n\t\t\t";
  1097. }
  1098. if ($info['addition']['multi'] === 'multi') {
  1099. return "\n\t\t\t\t{{# var " . $info['field'] . "Arr = d." . $info['field'] . ".split(\", \"), " . $info['field'] . "K}}" .
  1100. "\n\t\t\t\t{{# for(" . $info['field'] . "K in " . $info['field'] . "Arr){ }}" .
  1101. "\n\t\t\t\t\t<a target=\"_blank\" href=\"{{" . $info['field'] . "Arr[" . $info['field'] . "K]}}\"><img id=\"" . $info['field'] . "{{" . $info['field'] . "K}}\" style=\"width:30px;height:30px;\" /></a>" .
  1102. "\n\t\t\t\t\t<script>" .
  1103. "\n\t\t\t\t\t\tvar " . $info['field'] . "{{" . $info['field'] . "K}} = \"{{" . $info['field'] . "Arr[" . $info['field'] . "K]}}\";" .
  1104. "\n\t\t\t\t\t\tdocument.getElementById(\"" . $info['field'] . "{{" . $info['field'] . "K}}\").src = " . $info['field'] . "{{" . $info['field'] . "K}};" .
  1105. "\n\t\t\t\t\t</script>\n\t\t\t\t" .
  1106. "{{# } }}\n\t\t\t";
  1107. }
  1108. } else if ($info['addition']['accept'] === 'video') {
  1109. if ($info['addition']['multi'] === 'single') {
  1110. return "\n\t\t\t\t<a href=\"javascript:void(0);\" onclick=\"layui.laytp.tableFormatter.showVideo('{{d." . $info['field'] . "}}')\" class=\"layui-link\">视频</a>\n\t\t\t";
  1111. }
  1112. if ($info['addition']['multi'] === 'multi') {
  1113. return "\n\t\t\t\t{{# var " . $info['field'] . "Arr = d." . $info['field'] . ".split(\", \"), " . $info['field'] . "K}}\n\t\t\t\t" .
  1114. "{{# for(" . $info['field'] . "K in " . $info['field'] . "Arr){ }}\n\t\t\t\t\t"
  1115. . "<a href=\"javascript:void(0);\" onclick=\"layui.laytp.tableFormatter.showVideo('{{" . $info['field'] . "Arr[" . $info['field'] . "K]}}')\" class=\"layui-link\">视频{{parseInt(" . $info['field'] . "K) + 1}}</a>\n\t\t\t\t" .
  1116. "{{# } }}\n\t\t\t";
  1117. }
  1118. } else if ($info['addition']['accept'] === 'audio') {
  1119. if ($info['addition']['multi'] === 'single') {
  1120. return "\n\t\t\t\t<audio src=\"{{d." . $info['field'] . "}}\" width=\"200px\" height=\"30px\" controls=\"controls\" preload=\"none\"></audio>\n\t\t\t";
  1121. }
  1122. if ($info['addition']['multi'] === 'multi') {
  1123. return "\n\t\t\t\t{{# var " . $info['field'] . "Arr = d." . $info['field'] . ".split(\", \"), " . $info['field'] . "K}}" .
  1124. "\n\t\t\t\t{{# for(" . $info['field'] . "K in " . $info['field'] . "Arr){ }}" .
  1125. "\n\t\t\t\t\t<audio id=\"" . $info['field'] . "{{" . $info['field'] . "K}}\" width=\"200px\" height=\"30px\" controls=\"controls\" preload=\"none\"></audio>" .
  1126. "\n\t\t\t\t\t<script>" .
  1127. "\n\t\t\t\t\t\tvar " . $info['field'] . "{{" . $info['field'] . "K}} = \"{{" . $info['field'] . "Arr[" . $info['field'] . "K]}}\";" .
  1128. "\n\t\t\t\t\t\tdocument.getElementById(\"" . $info['field'] . "{{" . $info['field'] . "K}}\").src = " . $info['field'] . "{{" . $info['field'] . "K}};" .
  1129. "\n\t\t\t\t\t</script>" .
  1130. "\n\t\t\t\t{{# } }}\n\t\t\t";
  1131. }
  1132. } else if ($info['addition']['accept'] === 'file') {
  1133. if ($info['addition']['multi'] === 'single') {
  1134. return "\n\t\t\t\t<a target=\"_blank\" href=\"{{d." . $info['field'] . "}}\" class=\"layui-link\">文件</a>\n\t\t\t";
  1135. }
  1136. if ($info['addition']['multi'] === 'multi') {
  1137. return "\n\t\t\t\t{{# var " . $info['field'] . "Arr = d." . $info['field'] . ".split(\", \"), " . $info['field'] . "K}}\n\t\t\t\t" .
  1138. "{{# for(" . $info['field'] . "K in " . $info['field'] . "Arr){ }}\n\t\t\t\t\t" .
  1139. "<a target=\"_blank\" href=\"{{d." . $info['field'] . "}}\" class=\"layui-link\">文件{{parseInt(" . $info['field'] . "K) + 1}}</a>\n\t\t\t\t" .
  1140. "{{# } }}\n\t\t\t";
  1141. }
  1142. }
  1143. } else if ($info['form_type'] === 'editor') {
  1144. if ($info['addition']['type'] === 'ueditor') {
  1145. return "\n\t\t\t\t<iframe src=\"/admin/showUEditor.html?val={{window.btoa(unescape(encodeURIComponent(d.ueditor)))}}\" class=\"editor\" data-type=\"ueditor\" data-id=\"ueditor\" style=\"width:100%;height:560px;border: 0\"></iframe>\n\t\t\t";
  1146. }
  1147. }
  1148. return "{{d." . $info['field'] . "}}";
  1149. }
  1150. //生成html模板文件
  1151. protected function createHtml()
  1152. {
  1153. $this->writeToFile($this->htmlIndexParam['tplName'], $this->htmlIndexParam['data'], $this->htmlIndexParam['fileName']);
  1154. $this->writeToFile($this->htmlAddParam['tplName'], $this->htmlAddParam['data'], $this->htmlAddParam['fileName']);
  1155. $this->writeToFile($this->htmlEditParam['tplName'], $this->htmlEditParam['data'], $this->htmlEditParam['fileName']);
  1156. // $this->writeToFile($this->htmlDetailParam['tplName'], $this->htmlDetailParam['data'], $this->htmlDetailParam['fileName']);
  1157. //是否拥有软删除功能
  1158. foreach ($this->fields as $k => $v) {
  1159. if ($v->field == 'delete_time') {
  1160. $this->writeToFile($this->htmlRecycleParam['tplName'], $this->htmlRecycleParam['data'], $this->htmlRecycleParam['fileName']);
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. protected function getFormItem($info, $type = 'add')
  1166. {
  1167. $func = 'get' . ucfirst($this->underlineToCamel($info['form_type'])) . 'Html';
  1168. return $this->$func($info, $type);
  1169. }
  1170. /**
  1171. * 获取表单分组数据
  1172. * @param string $field
  1173. * @param string $content
  1174. * @param integer $isEmpty
  1175. * @return string
  1176. */
  1177. protected function getFormGroup($field, $content, $isEmpty)
  1178. {
  1179. if ($isEmpty != 1) {
  1180. $requiredHtml = " <text title=\"必填项\" style=\"color:red;\">*</text> ";
  1181. } else {
  1182. $requiredHtml = "";
  1183. }
  1184. return <<<EOD
  1185. \t\t\t\t<div class="layui-form-item">
  1186. \t\t\t\t<label class="layui-form-label" title="{$field}">{$requiredHtml}{$field}</label>
  1187. \t\t\t\t<div class="layui-input-block">
  1188. \t\t\t{$content}
  1189. \t\t\t\t</div>
  1190. \t\t\t\t</div>
  1191. EOD;
  1192. }
  1193. protected function getSearchFormContent($info)
  1194. {
  1195. $func = 'getSearch' . ucfirst($this->underlineToCamel($info['form_type'])) . 'Html';
  1196. return $this->$func($info);
  1197. }
  1198. protected function getSearchFormItem($info, $content)
  1199. {
  1200. $fieldComment = $info['comment'];
  1201. return <<<EOD
  1202. <div class="layui-form-item layui-inline">
  1203. <label class="layui-form-label" title="{$fieldComment}">{$fieldComment}</label>
  1204. <div class="layui-input-inline">
  1205. {$content}
  1206. </div>
  1207. </div>
  1208. EOD;
  1209. }
  1210. protected function getLinkageSelectSearchFormGroup($field, $content)
  1211. {
  1212. return <<<EOD
  1213. <div class="layui-form-item layui-inline">
  1214. <label class="layui-form-label" title="{$field}">{$field}</label>
  1215. {$content}
  1216. </div>
  1217. EOD;
  1218. }
  1219. protected function getLinkageXmSelectSearchFormGroup($field, $content)
  1220. {
  1221. return <<<EOD
  1222. <div class="layui-form-item layui-inline">
  1223. <label class="layui-form-label" title="{$field}">{$field}</label>
  1224. {$content}
  1225. </div>
  1226. EOD;
  1227. }
  1228. /**
  1229. * 接下来是html的各种控件模板内容生成,比如input、select、upload等等
  1230. */
  1231. /**
  1232. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  1233. * @param $info
  1234. * @param $type string 类型,add或者edit
  1235. * @return string
  1236. */
  1237. protected function getInputHtml($info, $type)
  1238. {
  1239. $name = 'html' . DS . $type . DS . 'input';
  1240. $data['field'] = $info['field'];
  1241. $data['comment'] = $info['comment'];
  1242. if ($info['is_empty'] == 2) {
  1243. $data['verify'] = $info['addition']['verify'] ? 'required|' . $info['addition']['verify'] : 'required';
  1244. } else {
  1245. $data['verify'] = $info['addition']['verify'];
  1246. }
  1247. $data['defaultValue'] = ($info['default'] === "") ? "" : " value=\"" . $info['default'] . "\"";
  1248. return $this->getReplacedTpl($name, $data);
  1249. }
  1250. /**
  1251. * 获取input需要生成的html,在生成搜索表单时用到
  1252. * @param $info
  1253. * @return string
  1254. */
  1255. protected function getSearchInputHtml($info)
  1256. {
  1257. $name = 'html' . DS . 'search' . DS . 'input';
  1258. $data['field'] = $info['field'];
  1259. $data['comment'] = $info['comment'];
  1260. return $this->getReplacedTpl($name, $data);
  1261. }
  1262. /**
  1263. * 获取password需要生成的html,在生成add和edit表单的时候可以用到
  1264. * @param $info
  1265. * @param $type string 类型,add或者edit
  1266. * @return string
  1267. */
  1268. protected function getPasswordHtml($info, $type)
  1269. {
  1270. $name = 'html' . DS . $type . DS . 'password';
  1271. $data['field'] = $info['field'];
  1272. $data['comment'] = $info['comment'];
  1273. if ($type == 'edit') {
  1274. $data['comment'] .= ',留空表示不修改';
  1275. }
  1276. if ($info['is_empty'] == 2) {
  1277. $data['verify'] = 'required';
  1278. } else {
  1279. $data['verify'] = '';
  1280. }
  1281. if ($info['addition']['input_type'] == '1') {
  1282. $data['input_type'] = 'text';
  1283. } else {
  1284. $data['input_type'] = 'password';
  1285. }
  1286. $data['defaultValue'] = ($info['default'] === "") ? "" : " value=\"" . $info['default'] . "\"";
  1287. return $this->getReplacedTpl($name, $data);
  1288. }
  1289. /**
  1290. * admin_id类型模板
  1291. * @param $info
  1292. * @param $type
  1293. * @return string
  1294. */
  1295. protected function getAdminUserIdHtml($info, $type)
  1296. {
  1297. $name = 'html' . DS . $type . DS . 'admin_id';
  1298. $data['field'] = $info['field'];
  1299. $data['comment'] = $info['comment'];
  1300. return $this->getReplacedTpl($name, $data);
  1301. }
  1302. /**
  1303. * admin_id类型模板
  1304. * @param $info
  1305. * @return string
  1306. */
  1307. protected function getSearchAdminUserIdHtml($info)
  1308. {
  1309. $name = 'html' . DS . 'search' . DS . 'admin_id';
  1310. $data['field'] = $info['field'];
  1311. $data['comment'] = $info['comment'];
  1312. return $this->getReplacedTpl($name, $data);
  1313. }
  1314. /**
  1315. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  1316. * @param $info
  1317. * @param $type string 类型,add或者edit
  1318. * @return string
  1319. */
  1320. protected function getTextareaHtml($info, $type)
  1321. {
  1322. $name = 'html' . DS . $type . DS . 'textarea';
  1323. $data['field'] = $info['field'];
  1324. $data['comment'] = $info['comment'];
  1325. $data['verify'] = $info['is_empty'] == 1 ? '' : 'required';
  1326. return $this->getReplacedTpl($name, $data);
  1327. }
  1328. /**
  1329. * 获取input需要生成的html,在生成搜索表单时用到
  1330. * @param $info
  1331. * @return string
  1332. */
  1333. protected function getSearchTextareaHtml($info)
  1334. {
  1335. $name = 'html' . DS . 'search' . DS . 'textarea';
  1336. $data['field'] = $info['field'];
  1337. $data['comment'] = $info['comment'];
  1338. return $this->getReplacedTpl($name, $data);
  1339. }
  1340. /**
  1341. * 获取input需要生成的html,在生成add和edit表单的时候可以用到
  1342. * @param $info
  1343. * @param $type string 类型,add或者edit
  1344. * @return string
  1345. */
  1346. protected function getRadioHtml($info, $type)
  1347. {
  1348. $items = $info['addition'];
  1349. $name = 'html' . DS . $type . DS . 'radio';
  1350. $radioHtml = '';
  1351. foreach ($items['value'] as $k => $v) {
  1352. $tempData['field'] = $info['field'];
  1353. $tempData['value'] = $items['value'][$k];
  1354. $tempData['title'] = $items['text'][$k];
  1355. $tempData['checkedStatus'] = ($items['value'][$k] == $items['default']) ? 'checked="checked"' : '';
  1356. $radioHtml .= $this->getReplacedTpl($name, $tempData) . "\n\t\t\t\t\t\t";
  1357. }
  1358. $radioHtml = rtrim($radioHtml, "\n\t\t\t");
  1359. // $this->set_model_array_const($info['field_name'], $model_array_const);
  1360. return $radioHtml;
  1361. }
  1362. protected function getSearchRadioHtml($info)
  1363. {
  1364. $name = 'html' . DS . 'search' . DS . 'radio';
  1365. $data['field'] = $info['field'];
  1366. $items = $info['addition'];
  1367. $options = '';
  1368. foreach ($items['value'] as $k => $v) {
  1369. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['value'][$k] . '">' . $items['text'][$k] . '</option>' . "\n";
  1370. }
  1371. $options = "\t\t\t\t" . '<option value=""></option>' . "\n" . rtrim($options, "\n");
  1372. $data['options'] = $options;
  1373. $data['comment'] = $info['comment'];
  1374. return $this->getReplacedTpl($name, $data);
  1375. }
  1376. protected function getSwitchHtml($info, $type)
  1377. {
  1378. $items = $info['addition'];
  1379. $name = 'html' . DS . $type . DS . 'switch';
  1380. $data['field'] = $info['field'];
  1381. $data['close_value'] = $items['close_value'];
  1382. $data['open_value'] = $items['open_value'];
  1383. $data['checked_status'] = ($items['default_status'] == 'open') ? 'checked="checked"' : '';
  1384. $data['lay_text'] = $items['open_text'] . '|' . $items['close_text'];
  1385. return $this->getReplacedTpl($name, $data);
  1386. }
  1387. protected function getSearchSwitchHtml($info)
  1388. {
  1389. $name = 'html' . DS . 'search' . DS . 'radio';
  1390. $data['field'] = $info['field'];
  1391. $items = $info['addition'];
  1392. $options = '';
  1393. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['close_value'] . '">' . $items['close_text'] . '</option>' . "\n";
  1394. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['open_value'] . '">' . $items['open_text'] . '</option>' . "\n";
  1395. $data['options'] = $options;
  1396. $data['comment'] = $info['comment'];
  1397. return $this->getReplacedTpl($name, $data);
  1398. }
  1399. protected function getSearchCheckboxHtml($info)
  1400. {
  1401. $name = 'html' . DS . 'search' . DS . 'checkbox';
  1402. $data['field'] = $info['field'];
  1403. $items = $info['addition'];
  1404. $data['max'] = count($items);
  1405. $optionItems = [];
  1406. // $model_array_const = [];
  1407. foreach ($items['value'] as $k => $v) {
  1408. $optionItems[] = ['value' => $items['value'][$k], 'name' => $items['text'][$k]];
  1409. // $model_array_const[(string)$temp[0]] = $temp[1];
  1410. }
  1411. // $data['source'] = str_replace("\"", "'", json_encode($optionItems, JSON_UNESCAPED_UNICODE));
  1412. $data['source'] = json_encode($optionItems, JSON_UNESCAPED_UNICODE);
  1413. // $this->set_controller_array_const($info['field_name'], $model_array_const);
  1414. return $this->getReplacedTpl($name, $data);
  1415. }
  1416. protected function getCheckboxHtml($info, $type)
  1417. {
  1418. $name = 'html' . DS . $type . DS . 'checkbox';
  1419. $data['field'] = $info['field'];
  1420. $items = $info['addition'];
  1421. $optionItems = [];
  1422. $checkboxHtml = '';
  1423. foreach ($items['value'] as $k => $v) {
  1424. $optionItems[] = ['value' => $items['value'][$k], 'text' => $items['text'][$k]];
  1425. }
  1426. $defaultValueArr = isset($items['default']) ? $items['default'] : [];
  1427. foreach ($optionItems as $k => $v) {
  1428. if ($k == 0 && $type == 'edit') {
  1429. $checkboxHtml .= "{{# var " . $info['field'] . "ValueArr = d." . $info['field'] . ".toString().split(','); }}\n\t\t\t\t\t\t";
  1430. }
  1431. if ($type == 'add') {
  1432. $data['checked'] = in_array($v['value'], $defaultValueArr) ? 'checked="checked"' : '';
  1433. }
  1434. $data['value'] = $v['value'];
  1435. $data['text'] = $v['text'];
  1436. $checkboxHtml .= $this->getReplacedTpl($name, $data) . "\n\t\t\t\t\t\t";
  1437. }
  1438. $checkboxHtml = rtrim($checkboxHtml, "\n\t\t\t\t\t\t");
  1439. // $this->set_model_array_const($info['field_name'], $model_array_const);
  1440. return $checkboxHtml;
  1441. }
  1442. protected function getSearchSelectHtml($info)
  1443. {
  1444. $name = 'html' . DS . 'search' . DS . 'radio';
  1445. $data['field'] = $info['field'];
  1446. $items = $info['addition'];
  1447. $options = '';
  1448. foreach ($items['value'] as $k => $v) {
  1449. $options .= "\t\t\t\t\t\t" . '<option value="' . $items['value'][$k] . '">' . $items['text'][$k] . '</option>' . "\n";
  1450. }
  1451. $options = "\t\t\t\t" . '<option value=""></option>' . "\n" . rtrim($options, "\n");
  1452. $data['options'] = $options;
  1453. $data['comment'] = $info['comment'];
  1454. return $this->getReplacedTpl($name, $data);
  1455. }
  1456. /**
  1457. * 获取select需要生成的html,在生成add和edit表单的时候可以用到
  1458. * @param $info
  1459. * @param $type string 类型,add或者edit
  1460. * @return string
  1461. */
  1462. protected function getSelectHtml($info, $type)
  1463. {
  1464. $name = 'html' . DS . $type . DS . 'select';
  1465. $items = $info['addition'];
  1466. $options = "\t\t\t\t\t\t\t" . '<option value="">请选择' . $info['comment'] . '</option>' . "\n";
  1467. foreach ($items['value'] as $k => $v) {
  1468. if ($type == 'add') {
  1469. if ($items['value'][$k] === $items['default']) {
  1470. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '" selected="selected">' . $items['text'][$k] . '</option>' . "\n";
  1471. } else {
  1472. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '">' . $items['text'][$k] . '</option>' . "\n";
  1473. }
  1474. } else {
  1475. $options .= "\t\t\t\t\t\t\t" . '<option value="' . $v . '" {{# if(d.' . $info['field'] . '.toString() == \'' . $v . '\'){ }}selected="selected"{{# } }}>' . $items['text'][$k] . '</option>' . "\n";
  1476. }
  1477. }
  1478. $data['options'] = $options;
  1479. $data['field'] = $info['field'];
  1480. $data['comment'] = $info['comment'];
  1481. $data['verify'] = $info['is_empty'] == 1 ? '' : 'required';
  1482. return $this->getReplacedTpl($name, $data);
  1483. }
  1484. protected function getSearchXmSelectHtml($info)
  1485. {
  1486. $name = 'html' . DS . 'search' . DS . 'xm_select';
  1487. $data['field'] = $info['field'];
  1488. if ($info['addition']['data_from_type'] === "data") {
  1489. $data['sourceType'] = "data";
  1490. $source = [];
  1491. foreach ($info['addition']['value'] as $k => $v) {
  1492. $source[] = ['name' => $info['addition']['text'][$k], 'value' => $info['addition']['value'][$k]];
  1493. }
  1494. // $data['source'] = str_replace('"', '\'', json_encode($source, JSON_UNESCAPED_UNICODE));
  1495. $data['source'] = json_encode($source, JSON_UNESCAPED_UNICODE);
  1496. $data['sourceTree'] = "false";
  1497. $data['paging'] = "false";
  1498. $data['textField'] = "\n\t\t\t\t\t\t" . 'data-textField=name"';
  1499. $data['subTextField'] = "";
  1500. $data['iconField'] = "";
  1501. $data['valueField'] = "value";
  1502. } else {
  1503. $data['sourceType'] = "route";
  1504. $tableName = $info['addition']['table_id'];
  1505. $midName = str_replace('/', '.', $this->getMidName($tableName));
  1506. $data['source'] = $this->compatibleApiRoute($midName) . '/index';
  1507. $createType = Table::where('table', '=', $tableName)->value('create_type');
  1508. if (!$createType) {
  1509. $this->setError('生成失败。' . $tableName . '数据表未使用生成程序生成');
  1510. return false;
  1511. }
  1512. $tableIsTree = ($createType == 1) ? 0 : 1;//取到数据表是否为无限极分类模型
  1513. if ($tableIsTree) {
  1514. $data['sourceTree'] = "true";
  1515. $data['paging'] = "false";
  1516. } else {
  1517. $data['sourceTree'] = "false";
  1518. $data['paging'] = "true";
  1519. }
  1520. $data['textField'] = $info['addition']['title_field'] ? "\n\t\t\t\t\t\t" . 'data-textField="' . $info['addition']['title_field'] . '"' : '';
  1521. $data['subTextField'] = $info['addition']['sub_title_field'] ? "\n\t\t\t\t\t\t" . 'data-subTextField="' . $info['addition']['sub_title_field'] . '"' : '';
  1522. $data['iconField'] = $info['addition']['icon_field'] ? "\n\t\t\t\t\t\t" . 'data-iconField="' . $info['addition']['icon_field'] . '"' : '';
  1523. $data['valueField'] = "id";
  1524. }
  1525. $data['comment'] = $info['comment'] ? "\n\t\t\t\t\t\t" . 'data-placeholder="请选择' . $info['comment'] . '"' : '';
  1526. $data['direction'] = $info['addition']['direction'] ? "\n\t\t\t\t\t\t" . 'data-direction="' . $info['addition']['direction'] . '"' : '';
  1527. return $this->getReplacedTpl($name, $data);
  1528. }
  1529. protected function getXmSelectHtml($info, $type)
  1530. {
  1531. $name = 'html' . DS . $type . DS . 'xm_select';
  1532. $data['field'] = $info['field'];
  1533. if ($info['addition']['data_from_type'] === "data") {
  1534. $data['sourceType'] = "data";
  1535. $source = [];
  1536. foreach ($info['addition']['value'] as $k => $v) {
  1537. $source[] = ['name' => $info['addition']['text'][$k], 'value' => $info['addition']['value'][$k]];
  1538. }
  1539. $data['source'] = str_replace('"', '\'', json_encode($source, JSON_UNESCAPED_UNICODE));
  1540. $data['sourceTree'] = "false";
  1541. $data['paging'] = "false";
  1542. $data['textField'] = "\n\t\t\t\t\t\t" . 'data-textField=name"';
  1543. $data['subTextField'] = "";
  1544. $data['iconField'] = "";
  1545. $data['valueField'] = "value";
  1546. } else {
  1547. $data['sourceType'] = "route";
  1548. $tableName = $info['addition']['table_id'];
  1549. $midName = str_replace('/', '.', $this->getMidName($tableName));
  1550. $data['source'] = $this->compatibleApiRoute($midName) . '/index';
  1551. $createType = Table::where('table', '=', $tableName)->value('create_type');
  1552. $tableIsTree = ($createType == 1) ? 0 : 1;//取到数据表是否为无限极分类模型
  1553. if ($tableIsTree) {
  1554. $data['sourceTree'] = "true";
  1555. $data['paging'] = "false";
  1556. } else {
  1557. $data['sourceTree'] = "false";
  1558. $data['paging'] = "true";
  1559. }
  1560. $data['textField'] = $info['addition']['title_field'] ? "\n\t\t\t\t\t\t\t" . 'data-textField="' . $info['addition']['title_field'] . '"' : '';
  1561. $data['subTextField'] = $info['addition']['sub_title_field'] ? "\n\t\t\t\t\t\t\t" . 'data-subTextField="' . $info['addition']['sub_title_field'] . '"' : '';
  1562. $data['iconField'] = $info['addition']['icon_field'] ? "\n\t\t\t\t\t\t\t" . 'data-iconField="' . $info['addition']['icon_field'] . '"' : '';
  1563. $data['valueField'] = "\n\t\t\t\tdata-valueField=\"id\"";
  1564. }
  1565. $data['layVerify'] = ($info['is_empty'] == 1) ? "" : "\n\t\t\t\t\t\t\t" . 'data-layVerify="required"';
  1566. $data['comment'] = $info['comment'] ? "\n\t\t\t\t\t\t\t" . 'data-placeholder="请选择' . $info['comment'] . '"' : '';
  1567. $data['direction'] = $info['addition']['direction'] ? "\n\t\t\t\t\t\t\t" . 'data-direction="' . $info['addition']['direction'] . '"' : '';
  1568. $data['radio'] = ($info['addition']['single_multi_type'] === 'single') ? "true" : "false";
  1569. $data['max'] = $info['addition']['max'] ? 'data-max="' . $info['addition']['max'] . '"' : '';
  1570. return $this->getReplacedTpl($name, $data);
  1571. }
  1572. public function getSearchLinkageSelectHtml($info)
  1573. {
  1574. $name = 'html' . DS . 'search' . DS . 'linkage_select';
  1575. $data['field'] = $info['field'];
  1576. $data['comment'] = $info['comment'];
  1577. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1578. $data['url'] = $this->compatibleApiRoute($midName) . '/index';
  1579. $data['leftField'] = $info['addition']['left_field'];
  1580. $data['rightField'] = $info['addition']['right_field'];
  1581. $data['showField'] = $info['addition']['show_field'];
  1582. $data['searchField'] = $info['addition']['search_field'];
  1583. $data['searchCondition'] = $info['addition']['search_condition'];
  1584. $data['searchVal'] = $info['addition']['search_val'];
  1585. return $this->getReplacedTpl($name, $data);
  1586. }
  1587. public function getLinkageSelectHtml($info, $type)
  1588. {
  1589. $name = 'html' . DS . $type . DS . 'linkage_select';
  1590. $data['field'] = $info['field'];
  1591. $data['verify'] = ($info['is_empty'] == 1) ? "" : "required";
  1592. $data['comment'] = $info['comment'];
  1593. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1594. $data['url'] = $this->compatibleApiRoute($midName) . '/index';
  1595. $data['leftField'] = $info['addition']['left_field'];
  1596. $data['rightField'] = $info['addition']['right_field'];
  1597. $data['showField'] = $info['addition']['show_field'];
  1598. $data['searchField'] = $info['addition']['search_field'];
  1599. $data['searchCondition'] = $info['addition']['search_condition'];
  1600. $data['searchVal'] = $info['addition']['search_val'];
  1601. return $this->getReplacedTpl($name, $data);
  1602. }
  1603. public function getSearchLaydateHtml($info)
  1604. {
  1605. $name = 'html' . DS . 'search' . DS . 'laydate';
  1606. $data['field'] = $info['field'];
  1607. $data['comment'] = $info['comment'];
  1608. $data['laydateType'] = $info['addition']['date_type'];
  1609. if ($info['data_type'] == 'integer') {
  1610. if (in_array($info['addition']['date_type'], ['datetime', 'month', 'date'])) {
  1611. $data['searchType'] = 'BETWEEN_STRTOTIME';
  1612. } else {
  1613. $data['searchType'] = 'BETWEEN';
  1614. }
  1615. } else {
  1616. $data['searchType'] = 'BETWEEN';
  1617. }
  1618. return $this->getReplacedTpl($name, $data);
  1619. }
  1620. public function getLaydateHtml($info, $type)
  1621. {
  1622. $name = 'html' . DS . $type . DS . 'laydate';
  1623. $data['field'] = $info['field'];
  1624. $data['comment'] = $info['comment'];
  1625. $data['laydateType'] = $info['addition']['date_type'];
  1626. $data['verify'] = ($info['is_empty'] == 1) ? "" : "required";
  1627. return $this->getReplacedTpl($name, $data);
  1628. }
  1629. public function getSearchColorPickerHtml()
  1630. {
  1631. return "";
  1632. }
  1633. public function getColorPickerHtml($info, $type)
  1634. {
  1635. $name = 'html' . DS . $type . DS . 'color_picker';
  1636. $data['field'] = $info['field'];
  1637. $data['comment'] = $info['comment'];
  1638. $data['color'] = $info['addition']['color'];
  1639. $data['predefine'] = isset($info['addition']['predefine']) ? "true" : "false";
  1640. $data['colors'] = isset($info['addition']['colors']) ?
  1641. json_encode($info['addition']['colors'], JSON_UNESCAPED_UNICODE) : '';
  1642. $data['alpha'] = (isset($info['addition']['alpha']) && $info['addition']['alpha']) ? "true" : "false";
  1643. $data['format'] = $info['addition']['format'];
  1644. return $this->getReplacedTpl($name, $data);
  1645. }
  1646. public function getSearchUploadHtml()
  1647. {
  1648. return "";
  1649. }
  1650. public function getUploadHtml($info, $type)
  1651. {
  1652. $name = 'html' . DS . $type . DS . 'upload';
  1653. $data['field'] = $info['field'];
  1654. $data['comment'] = $info['comment'];
  1655. $data['accept'] = $info['addition']['accept'];
  1656. $data['width'] = ($info['addition']['accept'] == 'image' && $info['addition']['width']) ? "\n\t\t\t\t\t\t\t\tdata-width=\"" . $info['addition']['width'] . "\"" : "";
  1657. $data['height'] = ($info['addition']['accept'] == 'image' && $info['addition']['height']) ? "\n\t\t\t\t\t\t\t\tdata-height=\"" . $info['addition']['height'] . "\"" : "";
  1658. $data['multi'] = ($info['addition']['multi'] == 'single') ? "false" : "true";
  1659. $data['max'] = intval($info['addition']['max']) ? "\n\t\t\t\t\t\t\t\tdata-max=\"" . intval($info['addition']['max']) . "\"" : "";
  1660. $data['dir'] = $info['addition']['dir'] ? "\n\t\t\t\t\t\t\t\tdata-dir=\"" . $info['addition']['dir'] . "\"" : "";
  1661. $data['url'] = $info['addition']['url'] ? "\n\t\t\t\t\t\t\t\tdata-url=\"" . $info['addition']['url'] . "\"" : "";
  1662. $data['mime'] = $info['addition']['mime'] ? "\n\t\t\t\t\t\t\t\tdata-mime=\"" . $info['addition']['mime'] . "\"" : "";
  1663. $data['size'] = $info['addition']['size'] ? "\n\t\t\t\t\t\t\t\tdata-size=\"" . $info['addition']['size'] . "\"" : "";
  1664. $data['verify'] = ($info['is_empty'] == 1) ? "" : "\n\t\t\t\t\t\t\t\tdata-layVerify=\"required\"";
  1665. $data['type'] = ($info['addition']['upload_type'] !== "local") ? "\n\t\t\t\t\t\t\tdata-type=\"" . $info['addition']['upload_type'] . "\"" : "";
  1666. $data['viaServer'] = ($info['addition']['upload_type'] !== "local" && $info['addition']['via_server'] === "unVia") ? "\n\t\t\t\t\t\t\tdata-viaServer=\"unVia\"" : "";
  1667. if ($info['addition']['multi'] == 'single') {
  1668. $data['uploadedInfo'] = "data-uploaded=\"{{# if(d." . $info['field'] . "_file){ }}{{=d." . $info['field'] . "_file.path}}{{# } }}\"
  1669. data-uploadedId=\"{{=d." . $info['field'] . "}}\"
  1670. data-uploadedFilename=\"{{# if(d." . $info['field'] . "_file){ }}{{=d." . $info['field'] . "_file.name}}{{# } }}\"";
  1671. } else {
  1672. $data['uploadedInfo'] = "data-uploaded=\"{{=d." . $info['field'] . "_file.path}}\"
  1673. data-uploadedId=\"{{=d." . $info['field'] . "}}\"
  1674. data-uploadedFilename=\"{{=d." . $info['field'] . "_file.filename}}\"";
  1675. }
  1676. return $this->getReplacedTpl($name, $data);
  1677. }
  1678. public function getSearchEditorHtml($info)
  1679. {
  1680. $name = 'html' . DS . 'search' . DS . 'editor' . DS . $info['addition']['type'];
  1681. $data['field'] = $info['field'];
  1682. $data['comment'] = $info['comment'];
  1683. return $this->getReplacedTpl($name, $data);
  1684. }
  1685. public function getEditorHtml($info, $type)
  1686. {
  1687. $name = 'html' . DS . $type . DS . 'editor' . DS . $info['addition']['type'];
  1688. $referer = request()->header('referer');
  1689. $refDomain = '';
  1690. if ($referer) {
  1691. $refPath = parse_url($referer);
  1692. $refDomain = $refPath['scheme'] . '://' . $refPath['host'];
  1693. }
  1694. $domain = request()->domain();
  1695. if ($refDomain && $domain && $refDomain != $domain) {
  1696. $data['iframeSrc'] = '/' . $info['addition']['type'] . '.html';
  1697. } else {
  1698. $data['iframeSrc'] = '/admin/' . $info['addition']['type'] . '.html';
  1699. }
  1700. if ($info['addition']['upload_type'] != 'local') {
  1701. if ($type == 'add') {
  1702. $data['iframeSrc'] .= '?upload_type=' . $info['addition']['upload_type'];
  1703. } else {
  1704. $data['iframeSrc'] .= '?id=' . $info['field'] . '&upload_type=' . $info['addition']['upload_type'];
  1705. }
  1706. } else {
  1707. if ($type == 'edit') {
  1708. $data['iframeSrc'] .= '?id=' . $info['field'];
  1709. }
  1710. }
  1711. $data['field'] = $info['field'];
  1712. $data['comment'] = $info['comment'];
  1713. return $this->getReplacedTpl($name, $data);
  1714. }
  1715. public function getSearchLinkageXmSelectHtml($info)
  1716. {
  1717. $name = 'html' . DS . 'search' . DS . 'linkage_xm_select';
  1718. $data['field'] = $info['field'];
  1719. $data['comment'] = $info['comment'];
  1720. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1721. $data['route'] = $this->compatibleApiRoute($midName) . '/index';
  1722. $data['leftId'] = $info['addition']['left_id'];
  1723. $data['rightId'] = $info['addition']['right_id'];
  1724. $data['textField'] = $info['addition']['text_field'];
  1725. $data['subTextField'] = $info['addition']['sub_text_field'];
  1726. $param = [];
  1727. foreach ($info['addition']['param']['field'] as $k => $field) {
  1728. if ($field) {
  1729. $param[$field] = ['condition' => $info['addition']['param']['condition'][$k], 'value' => $info['addition']['param']['value'][$k]];
  1730. }
  1731. }
  1732. $data['param'] = $param ? json_encode($param, JSON_UNESCAPED_UNICODE) : "";
  1733. $data['paging'] = $info['addition']['paging'] ? "true" : "false";
  1734. $data['search'] = $info['addition']['search'] ? "true" : "false";
  1735. $searchParam = [];
  1736. foreach ($info['addition']['search_param']['field'] as $k => $field) {
  1737. if ($field) {
  1738. $searchParam[$field] = ['condition' => $info['addition']['search_param']['condition'][$k]];
  1739. }
  1740. }
  1741. $data['searchParam'] = $searchParam ? json_encode($searchParam, JSON_UNESCAPED_UNICODE) : "";
  1742. $leftOnchangeParam = [];
  1743. foreach ($info['addition']['left_onchange_param']['field'] as $k => $field) {
  1744. if ($field) {
  1745. $leftOnchangeParam[$field] = ['condition' => $info['addition']['left_onchange_param']['condition'][$k]];
  1746. }
  1747. }
  1748. $data['leftOnchangeParam'] = $leftOnchangeParam ? json_encode($leftOnchangeParam, JSON_UNESCAPED_UNICODE) : "";
  1749. $data['width'] = (isset($info['addition']['width']) && $info['addition']['width']) ? ' style="width:' . $info['addition']['width'] . 'px"' : "";
  1750. return $this->getReplacedTpl($name, $data);
  1751. }
  1752. public function getLinkageXmSelectHtml($info, $type)
  1753. {
  1754. $name = 'html' . DS . $type . DS . 'linkage_xm_select';
  1755. $data['field'] = $info['field'];
  1756. $data['comment'] = $info['comment'];
  1757. $data['leftId'] = $info['addition']['left_id'];
  1758. $data['rightId'] = $info['addition']['right_id'];
  1759. $midName = str_replace('/', '.', $this->getMidName($info['addition']['table_id']));
  1760. $data['route'] = $this->compatibleApiRoute($midName) . '/index';
  1761. $data['textField'] = $info['addition']['text_field'];
  1762. $data['subTextField'] = $info['addition']['sub_text_field'];
  1763. $param = [];
  1764. foreach ($info['addition']['param']['field'] as $k => $field) {
  1765. if ($field) {
  1766. $param[$field] = ['condition' => $info['addition']['param']['condition'][$k], 'value' => $info['addition']['param']['value'][$k]];
  1767. }
  1768. }
  1769. $data['param'] = $param ? json_encode($param, JSON_UNESCAPED_UNICODE) : "";
  1770. $data['paging'] = $info['addition']['paging'] ? "true" : "false";
  1771. $data['search'] = $info['addition']['search'] ? "true" : "false";
  1772. $searchParam = [];
  1773. foreach ($info['addition']['search_param']['field'] as $k => $field) {
  1774. if ($field) {
  1775. $searchParam[$field] = ['condition' => $info['addition']['search_param']['condition'][$k]];
  1776. }
  1777. }
  1778. $data['searchParam'] = $searchParam ? json_encode($searchParam, JSON_UNESCAPED_UNICODE) : "";
  1779. $leftOnchangeParam = [];
  1780. foreach ($info['addition']['left_onchange_param']['field'] as $k => $field) {
  1781. if ($field) {
  1782. $leftOnchangeParam[$field] = ['condition' => $info['addition']['search_param']['condition'][$k]];
  1783. }
  1784. }
  1785. $data['leftOnchangeParam'] = $leftOnchangeParam ? json_encode($leftOnchangeParam, JSON_UNESCAPED_UNICODE) : "";
  1786. $data['verify'] = ($info['is_empty'] == 1) ? "" : "required";
  1787. $data['width'] = (isset($info['addition']['width']) && $info['addition']['width']) ? ' style="width:' . $info['addition']['width'] . 'px"' : "";
  1788. return $this->getReplacedTpl($name, $data);
  1789. }
  1790. /**
  1791. * 设置生成验证器需要的参数
  1792. * [
  1793. * 'tplName'=>模板名,
  1794. * 'data' => '执行替换模板的key=>value数组',
  1795. * 'fileName' => '要生成的文件名'
  1796. * ]
  1797. */
  1798. protected function setValidateParam()
  1799. {
  1800. $addRule = [];
  1801. $editRule = [];
  1802. $addMessage = [];
  1803. $editMessage = [];
  1804. foreach ($this->fields as $field) {
  1805. if ($field['form_type'] == 'password') {
  1806. $addFieldRule = [];
  1807. $editFieldRule = [];
  1808. if ($field['is_empty'] == 2) {
  1809. $addFieldRule[] = 'require';
  1810. $addMessage[] = '\'' . $field['field'] . '.require\' => \'' . $field['comment'] . '不能为空\',';
  1811. $editFieldRule[] = 'require';
  1812. $editMessage[] = '\'' . $field['field'] . '.require\' => \'' . $field['comment'] . '不能为空\',';
  1813. }
  1814. if ($field['addition']['re_password'] == 1) {
  1815. $addFieldRule[] = "confirm:re_" . $field['field'];
  1816. $addMessage[] = '\'' . $field['field'] . '.confirm\' => \'两次' . $field['comment'] . '输入不相同\',';
  1817. $editFieldRule[] = "confirm:re_" . $field['field'];
  1818. $editMessage[] = '\'' . $field['field'] . '.confirm\' => \'两次' . $field['comment'] . '输入不相同\',';
  1819. }
  1820. if ($field['addition']['password_min_length']) {
  1821. $addFieldRule[] = "min:" . $field['addition']['password_min_length'];
  1822. $addMessage[] = '\'' . $field['field'] . '.min\' => \'' . $field['comment'] . '长度不能低于' . $field['addition']['password_min_length'] . '个字符\',';
  1823. $editFieldRule[] = "min:" . $field['addition']['password_min_length'];
  1824. $editMessage[] = '\'' . $field['field'] . '.min\' => \'' . $field['comment'] . '长度不能低于' . $field['addition']['password_min_length'] . '个字符\',';
  1825. }
  1826. if ($field['addition']['password_max_length']) {
  1827. $addFieldRule[] = "max:" . $field['addition']['password_max_length'];
  1828. $addMessage[] = '\'' . $field['field'] . '.max\' => \'' . $field['comment'] . '长度不能高于' . $field['addition']['password_max_length'] . '个字符\',';
  1829. $editFieldRule[] = "max:" . $field['addition']['password_max_length'];
  1830. $editMessage[] = '\'' . $field['field'] . '.max\' => \'' . $field['comment'] . '长度不能高于' . $field['addition']['password_max_length'] . '个字符\',';
  1831. }
  1832. $addFieldRule && $addRule[] = '\'' . $field['field'] . '\' => \'' . implode('|', $addFieldRule) . '\',';
  1833. $editFieldRule && $editRule[] = '\'' . $field['field'] . '\' => \'' . implode('|', $editFieldRule) . '\',';
  1834. }
  1835. }
  1836. $addTplName = 'validate' . DS . 'add';
  1837. $addData = [];
  1838. $addData['validateNamespace'] = str_replace('/', '\\', 'app/validate/admin/' . $this->filterMidName(strtolower($this->midName)));
  1839. $addData['rule'] = implode(',', $addRule);
  1840. $addData['message'] = implode("\r\n\t\t", $addMessage);
  1841. $this->validateAddParam = ['tplName' => $addTplName, 'data' => $addData, 'fileName' => $this->validateAddFileName];
  1842. $editTplName = 'validate' . DS . 'edit';
  1843. $editData = [];
  1844. $editData['validateNamespace'] = str_replace('/', '\\', 'app/validate/admin/' . $this->filterMidName(strtolower($this->midName)));
  1845. $editData['rule'] = implode(',', $editRule);
  1846. $editData['message'] = implode("\r\n\t\t", $editMessage);
  1847. $this->validateEditParam = ['tplName' => $editTplName, 'data' => $editData, 'fileName' => $this->validateEditFileName];
  1848. }
  1849. // 生成验证器
  1850. protected function createValidate()
  1851. {
  1852. $this->writeToFile($this->validateAddParam['tplName'], $this->validateAddParam['data'], $this->validateAddParam['fileName']);
  1853. $this->writeToFile($this->validateEditParam['tplName'], $this->validateEditParam['data'], $this->validateEditParam['fileName']);
  1854. }
  1855. /**
  1856. * 写入到文件
  1857. * @param string $name 模板文件名
  1858. * @param array $data key=>value形式的替换数组
  1859. * @param string $pathname 生成的绝对文件名
  1860. * @return mixed
  1861. */
  1862. protected function writeToFile($name, $data, $pathname)
  1863. {
  1864. foreach ($data as $index => &$datum) {
  1865. $datum = is_array($datum) ? '' : $datum;
  1866. }
  1867. unset($datum);
  1868. $content = $this->getReplacedTpl($name, $data);
  1869. if (!is_dir(dirname($pathname))) {
  1870. mkdir(dirname($pathname), 0777, true);
  1871. }
  1872. return file_put_contents($pathname, $content);
  1873. }
  1874. /**
  1875. * 获取替换后的数据
  1876. * @param string $name 模板文件名
  1877. * @param array $data key=>value形式的替换数组
  1878. * @return string
  1879. */
  1880. protected function getReplacedTpl($name, $data = [])
  1881. {
  1882. foreach ($data as $index => &$datum) {
  1883. $datum = is_array($datum) ? '' : $datum;
  1884. }
  1885. unset($datum);
  1886. $search = $replace = [];
  1887. foreach ($data as $k => $v) {
  1888. $search[] = "{%{$k}%}";
  1889. $replace[] = $v;
  1890. }
  1891. $tplTrueName = $this->getTplTrueName($name);
  1892. $tpl = file_get_contents($tplTrueName);
  1893. $content = str_replace($search, $replace, $tpl);
  1894. return $content;
  1895. }
  1896. /**
  1897. * 获取模板文件真实路径
  1898. * @param string $name 举例: $name = 'controller' . DS . 'edit';
  1899. * @return string
  1900. */
  1901. protected function getTplTrueName($name)
  1902. {
  1903. return app()->getRootPath() . DS . 'plugin' . DS . 'curd' . DS . 'library' . DS . 'curd_template' . DS . $name . '.lt';
  1904. }
  1905. /**
  1906. * 下划线转驼峰
  1907. * @param $str
  1908. * @return string|string[]|null
  1909. */
  1910. protected function underlineToCamel($str)
  1911. {
  1912. $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
  1913. return strtoupper($matches[2]);
  1914. }, $str);
  1915. return $str;
  1916. }
  1917. }