eof.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. use think\facade\Db;
  3. use app\admin\service\curd\CurdService;
  4. function getAction($key, $value)
  5. {
  6. if($key=='del' || $key=='multi' || $key=='import' || $key=='download' || $key=='recyclebin'){
  7. $return='return "'.$value.'";';
  8. }else{
  9. $return='return $this->fetch();';
  10. }
  11. $action=<<<EOF
  12. //{$value}
  13. #[Route("GET","{$key}")]
  14. public function {$key}()
  15. {
  16. {$return}
  17. }
  18. EOF;
  19. return $action;
  20. }
  21. function getFields($rows,$table,$form,$isTree,$treeTitle)
  22. {
  23. foreach ($rows as &$value){
  24. $value=is_string($value)?trim($value):$value;
  25. }
  26. $arr=[
  27. 'field'=>$rows['field'],
  28. 'title'=>$rows['title']
  29. ];
  30. if($table){
  31. if($rows['visible']==='none'){
  32. return '';
  33. }
  34. if($rows['visible']===false){
  35. $arr['visible']=false;
  36. }
  37. if(!empty($rows['sortable'])){
  38. $arr['sortable']=true;
  39. }
  40. if($rows['operate']){
  41. $operate=parseJson($rows['operate']);
  42. if($operate!='='){
  43. $arr['operate']=$operate;
  44. }
  45. if(is_array($operate) && isset($operate['value'])){
  46. if(str_starts_with($operate['value'],'[') && str_ends_with($operate['value'],']')){
  47. $arr['operate']['value']=json_decode($operate['value'],true);
  48. }
  49. //通过正则表达式判断$operate['value']是否为整数
  50. if(preg_match('/^\d+$/',$operate['value'])){
  51. $arr['operate']['value']=intval($operate['value']);
  52. }
  53. }
  54. }else{
  55. $arr['operate']=false;
  56. }
  57. }
  58. if($form){
  59. if($rows['visible']==='none'){
  60. return '';
  61. }
  62. if($rows['edit']){
  63. $arr['edit']=parseJson($rows['edit']);
  64. if(is_array($arr['edit']) && isset($arr['edit']['value'])){
  65. if(str_starts_with($arr['edit']['value'],'[') && str_ends_with($arr['edit']['value'],']')){
  66. $arr['edit']['value']=json_decode($arr['edit']['value'],true);
  67. }
  68. //通过正则表达式判断$operate['value']是否为整数
  69. if(is_string($arr['edit']['value']) && preg_match('/^\d+$/',$arr['edit']['value'])){
  70. $arr['edit']['value']=intval($arr['edit']['value']);
  71. }
  72. }
  73. }
  74. if($rows['rules']){
  75. $arr['rules']=parseJson($rows['rules']);
  76. }
  77. }
  78. if($rows['searchList']){
  79. $arr['searchList']=parseJson($rows['searchList']);
  80. }
  81. $json=json_encode($arr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  82. if($table){
  83. $json=substr($json,0,-1).getFormatter($rows,$isTree,$treeTitle).'},';
  84. }
  85. if($form){
  86. $json.=',';
  87. }
  88. $str=<<<EOF
  89. {$json}
  90. EOF;
  91. return removeQuotationMarks($str);
  92. }
  93. //删除key的双引号
  94. function removeQuotationMarks($str)
  95. {
  96. $str=preg_replace('/"(\w+)":/','$1:',$str);
  97. return $str;
  98. }
  99. function getTableslot($rows)
  100. {
  101. if($rows['formatter']!='slot'){
  102. return '';
  103. }
  104. $field=$rows['field'];
  105. $title=$rows['title'];
  106. $str=<<<EOF
  107. <template #formatter="{field,rows}">
  108. <div v-if="field=='{$field}'">
  109. <span>{$title}插槽内容</span>
  110. </div>
  111. </template>
  112. EOF;
  113. return $str;
  114. }
  115. function getFormslot($rows,$istree=false,$treetitle='')
  116. {
  117. $edit=parseJson($rows['edit']);
  118. $isslot=false;
  119. if(is_string($edit) && $edit=='slot'){
  120. $isslot=true;
  121. }
  122. if(is_array($edit) && $edit['form']=='slot'){
  123. $isslot=true;
  124. }
  125. if(!$isslot){
  126. return '';
  127. }
  128. $field=$rows['field'];
  129. $title=$rows['title'];
  130. if($field=='pid' && $istree){
  131. $str=<<<EOF
  132. <template #{$field}="{rows}">
  133. <el-form-item label="{:__('{$title}')}:" prop="{$field}">
  134. <el-select placeholder="{:__('请选择父级')}" v-model="rows.pid" :clearable="true" style="width: 100%">
  135. <el-option key="all" label="无" value="0"></el-option>
  136. {foreach name="parentList" item="vo"}
  137. <el-option key="{\$vo.id}" label="{:str_replace('&amp;','&',\$vo.{$treetitle})}" value="{\$vo.id}"></el-option>
  138. {/foreach}
  139. </el-select>
  140. </el-form-item>
  141. </template>
  142. EOF;
  143. }else{
  144. $str=<<<EOF
  145. <template #{$field}="{rows}">
  146. <el-form-item label="{:__('{$title}')}:" prop="{$field}">
  147. <span>插槽内容</span>
  148. </el-form-item>
  149. </template>
  150. EOF;
  151. }
  152. return $str;
  153. }
  154. function parseJson($str)
  155. {
  156. $str=trim($str);
  157. if(str_starts_with($str,'{') && str_ends_with($str,'}')){
  158. $jsonarr=json_decode($str,true);
  159. //如果是非关联数组
  160. if(array_keys($jsonarr) === range(0, count($jsonarr) - 1)){
  161. $r=[];
  162. for($i=count($jsonarr);$i>0;$i--){
  163. $r[$i-1]=$jsonarr[$i-1];
  164. }
  165. return $r;
  166. }
  167. return $jsonarr;
  168. }
  169. return $str;
  170. }
  171. function getRelationMethods($field,$relation)
  172. {
  173. $table=$relation['table'];
  174. $config = Db::getConfig();
  175. $default=$config['default'];
  176. $prefix=$config['connections'][$default]['prefix'];
  177. $table=str_replace($prefix,'',$table);
  178. $funcname=parse_name($table,1);
  179. $tableModelName=str_replace(' ','',ucwords(str_replace('_',' ',$table)));
  180. $selectfields=implode(',',array_unique([$relation['relationField'],$relation['showField'],$relation['filterField']]));
  181. if($relation['ralationType']=='one'){
  182. $action=<<<EOF
  183. public function {$funcname}()
  184. {
  185. return \$this->hasOne({$tableModelName}::class,'{$relation['relationField']}','{$field}')->field('{$selectfields}');
  186. }
  187. EOF;
  188. return $action;
  189. }
  190. if($relation['ralationType']=='many'){
  191. $action=<<<EOF
  192. public function {$table}()
  193. {
  194. return \$this->hasMany({$tableModelName}::class,'{$relation['relationField']}','{$field}')->field('{$selectfields}');
  195. }
  196. EOF;
  197. return $action;
  198. }
  199. }
  200. function getRecyclebinField($field){
  201. if(empty($field)){
  202. return '';
  203. }
  204. $str='';
  205. foreach ($field as $value){
  206. $str.=<<<EOF
  207. "{$value['field']}"=>"{$value['title']}",
  208. EOF;
  209. }
  210. return $str;
  211. }
  212. function getRecyclebinType($field){
  213. if(empty($field)){
  214. return '';
  215. }
  216. $str='';
  217. foreach ($field as $value){
  218. $str.=<<<EOF
  219. "{$value['field']}"=>"{$value['type']}",
  220. EOF;
  221. }
  222. return $str;
  223. }
  224. function getFormatter($rows,$isTree,$treeTitle)
  225. {
  226. if($rows['visible']==='relation'){
  227. $relation=json_decode($rows['relation'],true);
  228. $table=str_replace(CurdService::$prefix,'',$relation['table']);
  229. $arrfieldtype="''";
  230. if($rows['formatter']=='tags' || $rows['formatter']=='images'){
  231. $arrfieldtype="[]";
  232. }
  233. $str=<<<EOF
  234. ,"formatter":function (data,row){
  235. let {$rows['formatter']}=Yunqi.formatter.{$rows['formatter']};
  236. {$rows['formatter']}.value=row.{$table}?row.{$table}.{$relation['showField']}:{$arrfieldtype};
  237. return {$rows['formatter']};
  238. }
  239. EOF;
  240. return $str;
  241. }else{
  242. if($isTree && $rows['field']==$treeTitle){
  243. $str=<<<EOF
  244. ,"formatter":function(data){
  245. let html=Yunqi.formatter.html;
  246. html.value=data.replace(/&nbsp;/g,'&nbsp;&nbsp;');
  247. return html;
  248. }
  249. EOF;
  250. return $str;
  251. }
  252. if($rows['formatter']=='text'){
  253. return '';
  254. }
  255. $formatter=',"formatter":Yunqi.formatter.'.$rows['formatter'];
  256. return $formatter;
  257. }
  258. }