ai-code.vue 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  1. <template>
  2. <div class="ai-code">
  3. <div class="bg">
  4. <div class="a"></div>
  5. <div class="b"></div>
  6. </div>
  7. <div class="back" @click="toBack">
  8. <el-icon>
  9. <back />
  10. </el-icon>
  11. 返回
  12. </div>
  13. <div class="panel" :class="[`is-${step.value}`]">
  14. <div class="head">
  15. <p class="title">Cool Ai 极速编码</p>
  16. <p class="tag">让软件开发<span>再</span>快一点</p>
  17. <p class="desc">
  18. {{ desc.text }}
  19. </p>
  20. </div>
  21. <div class="start" v-if="step.value == 'start'">
  22. <el-button class="go btn-primary" @click="step.next">
  23. 快速开始
  24. <el-icon>
  25. <arrow-right-bold />
  26. </el-icon>
  27. </el-button>
  28. <el-button class="doc" @click="toDoc"> 文档 </el-button>
  29. </div>
  30. <div class="enter" v-if="step.value == 'enter'">
  31. <el-input
  32. :ref="setRefs('inputEntity')"
  33. v-model="form.entity"
  34. placeholder="如:学生信息、商品信息"
  35. @keydown.enter="step.next"
  36. />
  37. <el-icon class="icon is-loading" v-if="step.loading">
  38. <loading />
  39. </el-icon>
  40. <cl-svg class="icon" name="enter" v-else @click="step.next" />
  41. </div>
  42. <div
  43. class="form"
  44. :class="{
  45. show: ['form', 'coding'].includes(step.value)
  46. }"
  47. >
  48. <div class="editor">
  49. <div class="topbar">
  50. <div class="dots">
  51. <el-tooltip content="返回">
  52. <span @click="step.prev()"></span>
  53. </el-tooltip>
  54. <span></span>
  55. <span></span>
  56. </div>
  57. </div>
  58. <div class="content">
  59. <div class="row">
  60. <div class="label">
  61. 实体名称
  62. <el-tooltip
  63. placement="top"
  64. content="指某类事物的集合名称,如:学生信息、商品信息"
  65. >
  66. <el-icon>
  67. <question-filled />
  68. </el-icon>
  69. </el-tooltip>
  70. </div>
  71. <el-input v-model="form.entity" maxlength="20" placeholder="请输入" />
  72. </div>
  73. <div class="row module">
  74. <div class="label">
  75. 模块
  76. <el-tooltip
  77. placement="top"
  78. content="前、后端模块的标识,如:user、goods、demo"
  79. >
  80. <el-icon>
  81. <question-filled />
  82. </el-icon>
  83. </el-tooltip>
  84. </div>
  85. <el-input v-model="form.module" maxlength="20" placeholder="请输入" />
  86. <el-popover
  87. :ref="setRefs('modulePopover')"
  88. :teleported="false"
  89. :popper-style="{
  90. padding: '5px',
  91. borderRadius: '6px',
  92. zIndex: 99
  93. }"
  94. placement="left"
  95. >
  96. <template #reference>
  97. <cl-svg class="add" name="arrow-down" />
  98. </template>
  99. <div class="module-list">
  100. <div
  101. class="item"
  102. v-for="(item, index) in module.dirs"
  103. :key="index"
  104. @click="
  105. () => {
  106. form.module = item;
  107. refs.modulePopover?.hide();
  108. }
  109. "
  110. >
  111. {{ item }}
  112. </div>
  113. </div>
  114. </el-popover>
  115. </div>
  116. <div class="row">
  117. <div class="label">
  118. 字段
  119. <el-tooltip
  120. placement="top"
  121. content="实体数据的字段名称,如:ID、姓名、手机号、状态"
  122. >
  123. <el-icon>
  124. <question-filled />
  125. </el-icon>
  126. </el-tooltip>
  127. </div>
  128. <el-input v-model="form.column" maxlength="200" placeholder="请输入" />
  129. </div>
  130. <div class="row" v-if="lang.value == 'Node'">
  131. <div class="label">
  132. 其他你想做的事
  133. <el-tooltip
  134. placement="top"
  135. content="功能的扩展,如:分页查询时姓名、手机号字段设置成可模糊搜索"
  136. >
  137. <el-icon>
  138. <question-filled />
  139. </el-icon>
  140. </el-tooltip>
  141. </div>
  142. <el-input v-model="form.other" maxlength="200" placeholder="请输入" />
  143. </div>
  144. </div>
  145. </div>
  146. <div class="btns">
  147. <el-button class="btn-primary" @click="code.create()">
  148. 生成代码
  149. <cl-svg name="code" />
  150. </el-button>
  151. </div>
  152. <div class="tips">如遇见 “代码缺失”、“请求超时”,请尝试「刷新」吧</div>
  153. </div>
  154. <div class="coding" v-if="step.value == 'coding'">
  155. <div class="editor">
  156. <div class="topbar">
  157. <div class="dots">
  158. <el-tooltip content="返回">
  159. <span @click="step.prev()"></span>
  160. </el-tooltip>
  161. <span></span>
  162. <span></span>
  163. </div>
  164. </div>
  165. <div class="content">
  166. <div class="tabs">
  167. <div
  168. class="item"
  169. v-for="(item, index) in code.list"
  170. :key="index"
  171. :class="{
  172. active: code.active == item.value
  173. }"
  174. @click="
  175. () => {
  176. code.active = item.value;
  177. }
  178. "
  179. >
  180. {{ item.label }}
  181. </div>
  182. <div class="op" v-if="!isEmpty(code.list) && !code.loading">
  183. <el-tooltip content="重新生成" v-if="code.active == 'vue'">
  184. <el-icon @click="code.refresh()">
  185. <refresh />
  186. </el-icon>
  187. </el-tooltip>
  188. <el-tooltip content="复制代码">
  189. <el-icon @click="code.copy()">
  190. <document-copy />
  191. </el-icon>
  192. </el-tooltip>
  193. <el-tooltip content="创建文件">
  194. <el-icon @click="createFile">
  195. <download />
  196. </el-icon>
  197. </el-tooltip>
  198. </div>
  199. </div>
  200. <div class="code">
  201. <cl-editor
  202. name="cl-editor-monaco"
  203. :ref="setRefs('editor')"
  204. v-model="activeCode.content"
  205. height="100%"
  206. :border="false"
  207. :options="{
  208. theme: 'ai-code--dark',
  209. minimap: {
  210. enabled: true
  211. }
  212. }"
  213. :key="activeCode.value"
  214. :language="activeCode.value == 'vue' ? 'html' : lang.tpl"
  215. v-if="activeCode"
  216. />
  217. </div>
  218. <div class="console">
  219. <el-scrollbar :ref="setRefs('console.scrollbar')">
  220. <div class="item" v-for="(item, index) in code.logs" :key="index">
  221. <span class="date"> {{ item.date }} </span>
  222. <span class="text">
  223. {{ item.text }}
  224. </span>
  225. <el-icon
  226. class="is-loading"
  227. v-if="code.loading ? index == code.logs.length - 1 : false"
  228. >
  229. <loading />
  230. </el-icon>
  231. </div>
  232. </el-scrollbar>
  233. </div>
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. <!-- 创建菜单 -->
  239. <cl-form ref="Form" />
  240. </div>
  241. </template>
  242. <script lang="tsx" setup name="helper-ai-code">
  243. import { onMounted, reactive, computed, nextTick } from "vue";
  244. import { useCool, module } from "/@/cool";
  245. import {
  246. Download,
  247. Back,
  248. ArrowRightBold,
  249. Loading,
  250. DocumentCopy,
  251. QuestionFilled,
  252. Refresh
  253. } from "@element-plus/icons-vue";
  254. import { ElMessage, ElMessageBox } from "element-plus";
  255. import { assign, isEmpty } from "lodash-es";
  256. import { useMenu, useAi } from "../hooks";
  257. import { config, isDev } from "/@/config";
  258. import { useForm } from "@cool-vue/crud";
  259. import { sleep, storage } from "/@/cool/utils";
  260. import dayjs from "dayjs";
  261. import type { CodeItem, EpsColumn } from "../types";
  262. import { useClipboard } from "@vueuse/core";
  263. import { ctx } from "virtual:ctx";
  264. const { service, refs, setRefs, router } = useCool();
  265. const menu = useMenu();
  266. const ai = useAi();
  267. const Form = useForm();
  268. const { copy } = useClipboard();
  269. // 表单
  270. const form = reactive({
  271. entity: "",
  272. module: "",
  273. other: "",
  274. column: ""
  275. });
  276. // 执行步骤
  277. const step = reactive({
  278. loading: false,
  279. value: "start",
  280. list: ["start", "enter", "form", "coding"],
  281. async next() {
  282. if (step.loading) {
  283. return false;
  284. }
  285. step.loading = true;
  286. let active = step.value;
  287. const i = step.list.indexOf(active);
  288. if (i < step.list.length - 1) {
  289. active = step.list[i + 1];
  290. }
  291. switch (active) {
  292. case "enter":
  293. setTimeout(() => {
  294. refs.inputEntity.focus();
  295. }, 300);
  296. break;
  297. case "form":
  298. if (!form.entity) {
  299. step.loading = false;
  300. return false;
  301. }
  302. desc.set(["正在做初步分析,请稍等..."]);
  303. await code.getColumns();
  304. break;
  305. }
  306. step.loading = false;
  307. step.value = active;
  308. // 切换文案
  309. desc.change();
  310. },
  311. prev() {
  312. const i = step.list.indexOf(step.value);
  313. if (i > 0) {
  314. step.value = step.list[i - 1];
  315. }
  316. }
  317. });
  318. // 语言
  319. const lang = reactive({
  320. value: "Node",
  321. get() {
  322. lang.value = ctx.serviceLang;
  323. code.active = lang.value.toLocaleLowerCase() + "-entity";
  324. },
  325. get tpl() {
  326. const d = {
  327. Node: "typescript",
  328. Java: "java",
  329. Go: "go",
  330. Python: "python"
  331. };
  332. return d[lang.value];
  333. }
  334. });
  335. // 代码
  336. const code = reactive({
  337. active: "",
  338. // 代码列表
  339. list: [] as CodeItem[],
  340. // 其他数据
  341. data: {
  342. router: "",
  343. prefix: "",
  344. fileName: "",
  345. columns: [] as EpsColumn[],
  346. fieldEq: [],
  347. keyWordLikeFields: [],
  348. api: [
  349. {
  350. path: "/add",
  351. summary: "新增"
  352. },
  353. {
  354. path: "/info",
  355. summary: "单个信息"
  356. },
  357. {
  358. path: "/update",
  359. summary: "修改"
  360. },
  361. {
  362. path: "/delete",
  363. summary: "删除"
  364. },
  365. {
  366. path: "/page",
  367. summary: "分页查询"
  368. },
  369. {
  370. path: "/list",
  371. summary: "列表查询"
  372. }
  373. ]
  374. },
  375. // 日志
  376. logs: [] as any[],
  377. // 生成中
  378. loading: false,
  379. // 请求
  380. req: null as Promise<any> | null,
  381. // 获取字段
  382. async getColumns() {
  383. return ai
  384. .invokeFlow("comm-column", {
  385. name: form.entity,
  386. modules: module.dirs.join("、")
  387. })
  388. .then((res) => {
  389. form.column = res.columns;
  390. form.module = res.module;
  391. });
  392. },
  393. // 清空
  394. clear() {
  395. code.list = [];
  396. code.logs = [];
  397. code.req = null;
  398. code.loading = false;
  399. },
  400. // 提示
  401. tips(val?: string) {
  402. code.logs.push({
  403. date: dayjs().format("HH:mm:ss"),
  404. text: val
  405. });
  406. // 日志滚动
  407. nextTick(() => {
  408. refs["console.scrollbar"]?.wrapRef?.scrollTo({
  409. top: Math.random() + 10000,
  410. behavior: "smooth"
  411. });
  412. });
  413. },
  414. // 生成 Node
  415. async createNode() {
  416. code.tips("Entity 代码生成中");
  417. // entity 代码
  418. const entity = await code.setContent("Entity 实体", "node-entity");
  419. code.tips("Entity 生成成功,开始解析");
  420. // entity 关键数据
  421. const entityData = await ai.invokeFlow("comm-parse-entity", {
  422. entity,
  423. module: form.module
  424. });
  425. code.tips(`Entity 解析成功,${JSON.stringify(entityData)}`);
  426. code.data.router = entityData.path.replace("/admin", "");
  427. code.data.prefix = entityData.path;
  428. code.data.fileName = entityData.fileName;
  429. // 解析字段
  430. code.parseColumn();
  431. code.tips("Service 代码生成中");
  432. // service 代码
  433. const service = await code.setContent("Service 服务", "node-service", {
  434. ...entityData,
  435. entity
  436. });
  437. code.tips("Service 生成成功,开始解析");
  438. // service 关键数据
  439. const serviceData = await ai.invokeFlow("comm-parse-service", {
  440. service
  441. });
  442. code.tips(`Service 解析成功,${JSON.stringify(serviceData)}`);
  443. code.tips("Controller 代码生成中");
  444. // controller 代码
  445. const controller = await code.setContent("Controller 控制器", "node-controller", {
  446. ...serviceData,
  447. ...entityData,
  448. service,
  449. entity
  450. });
  451. code.tips("Controller 生成成功,开始解析");
  452. // controller 关键数据
  453. const controllerData = await ai.invokeFlow("comm-parse-controller", {
  454. entity,
  455. controller
  456. });
  457. code.tips(`Controller 解析成功,${JSON.stringify(controllerData)}`);
  458. code.data.fieldEq = controllerData.fieldEq;
  459. code.data.keyWordLikeFields = controllerData.keyWordLikeFields;
  460. },
  461. // 生成 Java
  462. async createJava() {
  463. code.tips("Entity 代码生成中");
  464. // entity 代码
  465. const entity = await code.setContent("Entity 实体", "java-entity");
  466. code.tips("Entity 生成成功,开始解析");
  467. // entity 关键数据
  468. const entityData = await ai.invokeFlow("comm-parse-entity", {
  469. entity,
  470. module: form.module
  471. });
  472. code.tips(`Entity 解析成功,${JSON.stringify(entityData)}`);
  473. code.data.router = entityData.path.replace("/admin", "");
  474. code.data.prefix = entityData.path;
  475. code.data.fileName = entityData.fileName;
  476. // 解析字段
  477. code.parseColumn();
  478. code.tips("Mapper 代码生成中");
  479. // mapper 代码
  480. await code.setContent("Mapper 映射", "java-mapper", {
  481. ...entityData,
  482. entity
  483. });
  484. code.tips("Mapper 生成成功");
  485. code.tips("Service 代码生成中");
  486. // service 接口类
  487. const _service = await code.setContent("Service 接口类", "java-service", {
  488. ...entityData,
  489. entity
  490. });
  491. // service 实现类
  492. const service = await code.setContent("Service 实现类", "java-service-impl", {
  493. ...entityData,
  494. entity,
  495. service: _service
  496. });
  497. code.tips("Service 生成成功,开始解析");
  498. // service 关键数据
  499. const serviceData = await ai.invokeFlow("comm-parse-service", {
  500. service
  501. });
  502. code.tips(`Service 解析成功,${JSON.stringify(serviceData)}`);
  503. code.tips("Controller 代码生成中");
  504. // controller 代码
  505. const controller = await code.setContent("Controller 控制器", "java-controller", {
  506. ...serviceData,
  507. ...entityData,
  508. service,
  509. entity
  510. });
  511. code.tips("Controller 生成成功,开始解析");
  512. // controller 关键数据
  513. const controllerData = await ai.invokeFlow("comm-parse-controller", {
  514. controller
  515. });
  516. code.tips(`Controller 解析成功,${JSON.stringify(controllerData)}`);
  517. code.data.fieldEq = controllerData.fieldEq;
  518. code.data.keyWordLikeFields = controllerData.keyWordLikeFields;
  519. },
  520. // 生成代码
  521. async create() {
  522. if (!form.entity) {
  523. return ElMessage.warning("请填写实体名称");
  524. }
  525. if (!form.module) {
  526. return ElMessage.warning("请填写模块");
  527. }
  528. if (!form.column) {
  529. return ElMessage.warning("请填写字段");
  530. }
  531. code.loading = true;
  532. // 清空
  533. code.clear();
  534. // 下一步
  535. step.next();
  536. code.tips("AI 开始编码");
  537. await sleep(300);
  538. await code[`create${lang.value}`]();
  539. await code.createVue();
  540. code.tips("编码完成");
  541. code.loading = false;
  542. if (isDev) {
  543. ElMessageBox.confirm("编码完成,是否创建文件?", "提示", {
  544. type: "success",
  545. confirmButtonText: "开始创建",
  546. cancelButtonText: "稍后再看"
  547. })
  548. .then(() => {
  549. createFile();
  550. })
  551. .catch(() => null);
  552. }
  553. },
  554. // 解析字段
  555. async parseColumn() {
  556. const a = ai.invokeFlow("comm-parse-entity-column", {
  557. entity: code.getContent(`${lang.value}-entity`)
  558. });
  559. const b = ai.invokeFlow("comm-parse-column", {
  560. entity: code.getContent(`${lang.value}-entity`)
  561. });
  562. code.req = Promise.all([a, b]).then((res) => {
  563. if (res[0]?.columns) {
  564. code.data.columns = res[0].columns.map((e: EpsColumn) => {
  565. if (res[1]?.columns) {
  566. e.component = res[1].columns[e.propertyName] || "input";
  567. }
  568. return e;
  569. });
  570. code.data.columns.push({
  571. comment: "更新时间",
  572. length: 0,
  573. component: "datetime",
  574. nullable: false,
  575. propertyName: "updateTime",
  576. type: "datetime"
  577. });
  578. code.data.columns.push({
  579. comment: "创建时间",
  580. length: 0,
  581. component: "datetime",
  582. nullable: false,
  583. propertyName: "createTime",
  584. type: "datetime"
  585. });
  586. }
  587. });
  588. },
  589. // 创建vue
  590. async createVue() {
  591. const item = code.add("Vue 页面", "vue");
  592. item.content = "";
  593. assign(code.data, form);
  594. code.tips("Vue 代码开始生成");
  595. if (!code.req) {
  596. code.parseColumn();
  597. }
  598. code.tips("AI 分析中");
  599. await code.req;
  600. // 生成内容
  601. item.content = menu.createVue({
  602. ...code.data,
  603. module: form.module
  604. });
  605. await sleep(300);
  606. // 格式化
  607. refs.editor.formatCode();
  608. code.tips("Vue 生成成功");
  609. },
  610. // 添加 tab
  611. add(label: string, flow: string) {
  612. let item = code.list.find((e) => e.value == flow);
  613. if (!item) {
  614. item = reactive<CodeItem>({
  615. label,
  616. value: flow,
  617. content: "",
  618. _content: ""
  619. });
  620. code.list.push(item);
  621. }
  622. code.active = flow;
  623. return item;
  624. },
  625. // 获取数据
  626. get(value: string) {
  627. return code.list.find((e) => e.value == value)!;
  628. },
  629. // 获取内容
  630. getContent(value: string) {
  631. return code.list.find(
  632. (e) => e.value == value || e.value.toLocaleLowerCase() == value.toLocaleLowerCase()
  633. )?.content;
  634. },
  635. // 设置内容
  636. async setContent(label: string, flow: string, data?: any) {
  637. return new Promise((resolve) => {
  638. const item = code.add(label, flow);
  639. // 是否结束
  640. let isEnd = false;
  641. // 所有内容
  642. let content = "";
  643. ai.invokeFlow(flow, { ...form, ...data }, (res) => {
  644. isEnd = res.isEnd;
  645. if (!res.isEnd) {
  646. content += res.content;
  647. if (content.indexOf(`\`\`\`${lang.tpl}\n`) == 0) {
  648. item._content = content
  649. .replace(new RegExp(`^\\\`\\\`\\\`${lang.tpl}\\n`, "g"), "")
  650. .replace(/```$/, "");
  651. }
  652. }
  653. });
  654. const timer = setInterval(() => {
  655. if (step.value != "coding") {
  656. clearInterval(timer);
  657. return;
  658. }
  659. const v = item._content[item.content.length] || "";
  660. if (isEnd) {
  661. if (!v) {
  662. clearInterval(timer);
  663. resolve(item.content);
  664. return false;
  665. }
  666. }
  667. item.content += v;
  668. // 滚动到底
  669. if (flow == code.active) {
  670. refs.editor?.revealLine(99999);
  671. }
  672. }, 5);
  673. });
  674. },
  675. // 复制
  676. copy() {
  677. copy(code.getContent(code.active)!);
  678. ElMessage.success("复制成功");
  679. // 存本地,方便调试
  680. storage.set("ai-code.list", code.list);
  681. storage.set("ai-code.data", code.data);
  682. storage.set("ai-code.form", form);
  683. },
  684. // 重新生成
  685. async refresh() {
  686. code.loading = true;
  687. code.req = null;
  688. await code.createVue();
  689. code.loading = false;
  690. }
  691. });
  692. const activeCode = computed(() => {
  693. return code.list.find((e) => e.value == code.active);
  694. });
  695. // 滚动文案
  696. const desc = reactive({
  697. list: [] as string[],
  698. text: "",
  699. change() {
  700. switch (step.value) {
  701. case "enter":
  702. desc.list = ["请简要描述您的功能,AI帮你写代码"];
  703. break;
  704. case "form":
  705. desc.list = ["准备就绪,配置预设参数"];
  706. break;
  707. default:
  708. desc.list = [
  709. "COOL为开发者而生",
  710. "只需少量的口语提示就能完成特定的功能,大大节省开发时间"
  711. ];
  712. }
  713. desc.start();
  714. },
  715. set(arr: string[]) {
  716. desc.list = arr;
  717. desc.start();
  718. },
  719. t1: null as any,
  720. t2: null as any,
  721. start() {
  722. desc.stop();
  723. function next(n: number) {
  724. const val = desc.list[n];
  725. if (val) {
  726. const next2 = (n2: number) => {
  727. const v = val[n2];
  728. if (v) {
  729. desc.t2 = setTimeout(() => {
  730. desc.text += v;
  731. next2(n2 + 1);
  732. }, 60);
  733. } else {
  734. desc.t2 = setTimeout(() => {
  735. if (desc.list.length > 1) {
  736. desc.t1 = setInterval(() => {
  737. desc.text = desc.text.slice(0, -1);
  738. if (!desc.text) {
  739. clearInterval(desc.t1);
  740. next(n + 1);
  741. }
  742. }, 50);
  743. }
  744. }, 1500);
  745. }
  746. };
  747. next2(0);
  748. } else {
  749. next(0);
  750. }
  751. }
  752. if (!isEmpty(desc.list)) {
  753. next(0);
  754. }
  755. },
  756. stop() {
  757. if (desc.t1) {
  758. clearInterval(desc.t1);
  759. }
  760. if (desc.t2) {
  761. clearTimeout(desc.t2);
  762. }
  763. desc.text = "";
  764. },
  765. init() {
  766. desc.change();
  767. }
  768. });
  769. // 创建文件
  770. function createFile() {
  771. if (!isDev) {
  772. return ElMessage.error("只有在开发环境下才能创建文件");
  773. }
  774. Form.value?.open({
  775. title: "配置菜单",
  776. width: "800px",
  777. items: [
  778. {
  779. prop: "parentId",
  780. label: "上级节点",
  781. component: {
  782. name: "cl-menu-select",
  783. props: {
  784. type: 1
  785. }
  786. }
  787. },
  788. {
  789. prop: "keepAlive",
  790. value: true,
  791. label: "路由缓存",
  792. component: {
  793. name: "el-radio-group",
  794. options: [
  795. {
  796. label: "开启",
  797. value: true
  798. },
  799. {
  800. label: "关闭",
  801. value: false
  802. }
  803. ]
  804. }
  805. },
  806. {
  807. prop: "icon",
  808. label: "菜单图标",
  809. component: {
  810. name: "cl-menu-icon"
  811. }
  812. },
  813. {
  814. prop: "orderNum",
  815. label: "排序号",
  816. component: {
  817. name: "el-input-number",
  818. props: {
  819. placeholder: "请填写排序号",
  820. min: 0,
  821. max: 99,
  822. "controls-position": "right"
  823. }
  824. }
  825. }
  826. ],
  827. op: {
  828. saveButtonText: "开始创建"
  829. },
  830. on: {
  831. submit(data, { close }) {
  832. code.tips("创建 Vue 文件中,过程可能会发生页面及服务重启");
  833. // 添加菜单、权限
  834. menu.create({
  835. code: code.getContent("vue"),
  836. ...code.data,
  837. ...data,
  838. name: form.entity
  839. })
  840. .then((create) => {
  841. const files = {};
  842. // 文件内容
  843. code.list.forEach((e) => {
  844. const i = e.value.indexOf("-");
  845. let k = e.value;
  846. if (i >= 0) {
  847. k = k.substring(i + 1, k.length);
  848. files[k] = code.getContent(e.value);
  849. }
  850. });
  851. // 创建后端文件
  852. service.base.sys.menu.create({
  853. ...form,
  854. ...code.data,
  855. ...files
  856. });
  857. // 每3s检测服务状态
  858. const timer = setInterval(() => {
  859. code.tips("检测后端服务是否启动");
  860. service
  861. .request({
  862. url: "/admin/base/open/eps"
  863. })
  864. .then((res) => {
  865. if (res && !isEmpty(res)) {
  866. code.tips("文件创建成功");
  867. ElMessage.success("文件创建成功");
  868. clearInterval(timer);
  869. create();
  870. }
  871. });
  872. }, 3000);
  873. })
  874. .catch(() => null);
  875. close();
  876. }
  877. }
  878. });
  879. }
  880. // 文档
  881. function toDoc() {
  882. window.open("https://cool-js.com/");
  883. }
  884. // 返回
  885. function toBack() {
  886. ElMessageBox.confirm(`确定要返回 ${config.app.name} 吗?`, "提示", {
  887. type: "warning"
  888. })
  889. .then(() => {
  890. router.back();
  891. })
  892. .catch(() => {});
  893. }
  894. onMounted(() => {
  895. lang.get();
  896. desc.init();
  897. // 测试
  898. if (step.value == "coding") {
  899. code.list = storage.get("ai-code.list") || [];
  900. if (storage.get("ai-code.data")) {
  901. code.data = storage.get("ai-code.data");
  902. }
  903. if (storage.get("ai-code.form")) {
  904. assign(form, storage.get("ai-code.form"));
  905. }
  906. }
  907. });
  908. </script>
  909. <style lang="scss" scoped>
  910. $color: #41d1ff;
  911. .ai-code {
  912. display: flex;
  913. flex-direction: column;
  914. justify-content: center;
  915. align-items: center;
  916. position: relative;
  917. height: 100vh;
  918. overflow: hidden;
  919. .bg {
  920. position: absolute;
  921. left: 0;
  922. top: 0;
  923. height: 100%;
  924. width: 100%;
  925. background-color: #090c13;
  926. display: flex;
  927. justify-content: center;
  928. .a {
  929. background-color: $color;
  930. transform: rotate(20deg);
  931. right: -10px;
  932. }
  933. .b {
  934. background-color: #4165d7;
  935. transform: rotate(-20deg);
  936. right: 10px;
  937. }
  938. .a,
  939. .b {
  940. height: 300px;
  941. width: 420px;
  942. position: relative;
  943. opacity: 0.4;
  944. border-radius: 100%;
  945. filter: blur(60px);
  946. top: 30vh;
  947. animation: fb 5s ease-in-out infinite;
  948. }
  949. @keyframes fb {
  950. 0% {
  951. filter: blur(60px);
  952. }
  953. 40% {
  954. filter: blur(150px);
  955. }
  956. 80% {
  957. filter: blur(60px);
  958. }
  959. 100% {
  960. filter: blur(60px);
  961. }
  962. }
  963. }
  964. .back {
  965. display: flex;
  966. align-items: center;
  967. justify-content: center;
  968. position: fixed;
  969. left: 20px;
  970. top: 20px;
  971. color: #fff;
  972. border: 1px solid rgba(255, 255, 255, 0.8);
  973. border-radius: 30px;
  974. padding: 6px 13px 6px 10px;
  975. cursor: pointer;
  976. transition: all 0.2s;
  977. font-size: 12px;
  978. z-index: 9;
  979. .el-icon {
  980. font-size: 16px;
  981. margin-right: 8px;
  982. }
  983. &:hover {
  984. background-color: rgba(255, 255, 255, 0.1);
  985. }
  986. }
  987. .panel {
  988. display: flex;
  989. flex-direction: column;
  990. justify-content: center;
  991. align-items: center;
  992. position: relative;
  993. height: 100%;
  994. width: 1040px;
  995. max-width: 100%;
  996. .editor {
  997. background-color: #080e14;
  998. width: 100%;
  999. .topbar {
  1000. display: flex;
  1001. align-items: center;
  1002. height: 36px;
  1003. padding: 0 12px;
  1004. .dots {
  1005. display: flex;
  1006. span {
  1007. display: inline-block;
  1008. height: 12px;
  1009. width: 12px;
  1010. border-radius: 12px;
  1011. background-color: #2f3447;
  1012. margin-right: 8px;
  1013. &:first-child {
  1014. cursor: pointer;
  1015. &:hover {
  1016. background-color: var(--el-color-danger);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. }
  1022. .content {
  1023. background-color: #0f151e;
  1024. }
  1025. }
  1026. .btn-primary {
  1027. border: 0;
  1028. background-size: 300% 100%;
  1029. background-image: linear-gradient(-60deg, $color, rgba($color, 0.5), $color);
  1030. background-position: 100% 0px;
  1031. box-shadow: 0 0 10px 1px rgba(255, 255, 255, 0.2);
  1032. border-radius: 8px;
  1033. letter-spacing: 1px;
  1034. color: #111;
  1035. transition: all 0.3s ease;
  1036. .el-icon {
  1037. transition: transform 0.1s;
  1038. }
  1039. &:hover {
  1040. background-position: 0% 0px;
  1041. .el-icon {
  1042. transform: translateX(5px);
  1043. }
  1044. }
  1045. }
  1046. .head {
  1047. padding: 50px 0;
  1048. text-align: center;
  1049. color: #fff;
  1050. line-height: 1;
  1051. letter-spacing: 2px;
  1052. user-select: none;
  1053. transition: all 0.2s ease 0.1s;
  1054. .title {
  1055. display: inline-block;
  1056. font-size: 40px;
  1057. background-clip: text;
  1058. font-weight: bold;
  1059. text-shadow: 0 5px 10px #333;
  1060. transition: all 0.3s;
  1061. transition-delay: 0.2s;
  1062. }
  1063. .tag {
  1064. margin-top: 30px;
  1065. font-size: 18px;
  1066. color: #eee;
  1067. span {
  1068. color: $color;
  1069. padding: 0 2px;
  1070. }
  1071. }
  1072. .desc {
  1073. display: flex;
  1074. align-items: center;
  1075. justify-content: center;
  1076. height: 35px;
  1077. padding: 0 1px;
  1078. color: #fff;
  1079. font-size: 22px;
  1080. margin-top: 50px;
  1081. &::after {
  1082. content: "";
  1083. display: inline-block;
  1084. margin-left: 4px;
  1085. height: 22px;
  1086. width: 3px;
  1087. background-color: #fff;
  1088. border-radius: 3px;
  1089. animation: shan 1s ease infinite;
  1090. }
  1091. @keyframes shan {
  1092. 0% {
  1093. opacity: 0;
  1094. }
  1095. 50% {
  1096. opacity: 1;
  1097. }
  1098. 100% {
  1099. opacity: 0;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. .start {
  1105. height: 50px;
  1106. text-align: center;
  1107. margin: 0 auto;
  1108. flex-shrink: 0;
  1109. .el-button {
  1110. height: 40px;
  1111. background-color: #fff;
  1112. border-radius: 8px;
  1113. }
  1114. .go {
  1115. width: 140px;
  1116. .el-icon {
  1117. margin-left: 5px;
  1118. color: #444;
  1119. }
  1120. }
  1121. .doc {
  1122. background-color: transparent;
  1123. width: 100px;
  1124. color: #fff;
  1125. border-width: 2px;
  1126. border-color: rgba(255, 255, 255, 0.7);
  1127. }
  1128. }
  1129. .enter {
  1130. display: flex;
  1131. align-items: center;
  1132. margin: 0 auto;
  1133. position: relative;
  1134. animation: enter 0.3s forwards;
  1135. width: 10px;
  1136. :deep(.el-input__wrapper) {
  1137. background-color: rgba(0, 0, 0, 0.3);
  1138. padding: 10px 20px;
  1139. border-radius: 12px;
  1140. box-shadow: 0 0 10px 1px #4165d719;
  1141. .el-input__inner {
  1142. color: #fff;
  1143. font-size: 16px;
  1144. text-align: center;
  1145. letter-spacing: 2px;
  1146. }
  1147. }
  1148. .icon {
  1149. position: absolute;
  1150. right: 18px;
  1151. color: #ccc;
  1152. font-size: 18px;
  1153. cursor: pointer;
  1154. &:hover {
  1155. color: #fff;
  1156. }
  1157. }
  1158. }
  1159. @keyframes enter {
  1160. from {
  1161. width: 10px;
  1162. }
  1163. to {
  1164. width: 320px;
  1165. }
  1166. }
  1167. .form {
  1168. transform: translateY(50vh);
  1169. width: calc(100% - 40px);
  1170. transition: all 0.3s ease;
  1171. margin: 0 auto;
  1172. .editor {
  1173. border-radius: 8px;
  1174. .content {
  1175. color: #fff;
  1176. box-sizing: border-box;
  1177. border-radius: 0 0 8px 8px;
  1178. padding: 5px 0 10px 0;
  1179. .row {
  1180. font-size: 12px;
  1181. margin-bottom: 10px;
  1182. &:last-child {
  1183. margin-bottom: 0;
  1184. }
  1185. .label {
  1186. display: flex;
  1187. align-items: center;
  1188. padding: 5px 15px;
  1189. font-size: 12px;
  1190. margin-bottom: 5px;
  1191. color: #999;
  1192. .el-icon {
  1193. margin-left: 4px;
  1194. cursor: pointer;
  1195. }
  1196. }
  1197. :deep(.el-input__wrapper) {
  1198. background-color: #2f344722;
  1199. box-shadow: none;
  1200. padding: 0 15px;
  1201. .el-input__inner {
  1202. color: #fff;
  1203. }
  1204. .el-icon {
  1205. margin-left: 2px;
  1206. }
  1207. }
  1208. &.module {
  1209. position: relative;
  1210. :deep(.el-input__wrapper) {
  1211. padding-left: 35px;
  1212. }
  1213. .add {
  1214. cursor: pointer;
  1215. margin-right: 8px;
  1216. position: absolute;
  1217. left: 15px;
  1218. top: 40px;
  1219. }
  1220. }
  1221. }
  1222. }
  1223. }
  1224. .btns {
  1225. display: flex;
  1226. justify-content: center;
  1227. .el-button {
  1228. height: 50px;
  1229. width: 200px;
  1230. font-size: 16px;
  1231. }
  1232. .cl-svg {
  1233. font-size: 18px;
  1234. margin-left: 5px;
  1235. color: #333;
  1236. }
  1237. }
  1238. .tips {
  1239. color: #eee;
  1240. text-align: center;
  1241. font-size: 14px;
  1242. user-select: none;
  1243. margin-top: 30px;
  1244. }
  1245. .module-list {
  1246. .item {
  1247. border-radius: 6px;
  1248. display: flex;
  1249. align-items: center;
  1250. height: 30px;
  1251. padding: 0 10px;
  1252. cursor: pointer;
  1253. border-radius: 6px;
  1254. font-size: 12px;
  1255. &:hover {
  1256. background-color: var(--el-fill-color-light);
  1257. }
  1258. }
  1259. }
  1260. &.show {
  1261. transform: translateY(0);
  1262. .btns {
  1263. margin-top: 60px;
  1264. }
  1265. }
  1266. }
  1267. .coding {
  1268. position: fixed;
  1269. bottom: 0;
  1270. left: 0;
  1271. height: 100vh;
  1272. width: 100%;
  1273. animation: coding 0.3s forwards;
  1274. box-sizing: border-box;
  1275. opacity: 0;
  1276. z-index: 10;
  1277. .editor {
  1278. height: 100%;
  1279. border-radius: 10px 10px 0 0;
  1280. }
  1281. .content {
  1282. height: calc(100% - 36px);
  1283. background-color: #080e14;
  1284. .tabs {
  1285. display: flex;
  1286. height: 40px;
  1287. .item {
  1288. display: flex;
  1289. align-items: center;
  1290. justify-content: center;
  1291. padding: 0 15px;
  1292. font-size: 12px;
  1293. cursor: pointer;
  1294. color: var(--el-color-info);
  1295. &.active {
  1296. background-color: #0f151e;
  1297. color: #fff;
  1298. }
  1299. &:hover {
  1300. color: #eee;
  1301. }
  1302. }
  1303. .op {
  1304. display: flex;
  1305. align-items: center;
  1306. margin-left: auto;
  1307. margin-right: 5px;
  1308. .el-icon {
  1309. height: 30px;
  1310. width: 30px;
  1311. color: #fff;
  1312. font-size: 15px;
  1313. cursor: pointer;
  1314. border-radius: 5px;
  1315. &:hover {
  1316. background-color: #0f151e;
  1317. }
  1318. }
  1319. }
  1320. }
  1321. .code {
  1322. height: calc(100% - 190px);
  1323. }
  1324. .console {
  1325. height: 150px;
  1326. padding: 5px 0;
  1327. box-sizing: border-box;
  1328. border-top: 1px solid #2f3447;
  1329. .item {
  1330. font-size: 12px;
  1331. padding: 5px 10px;
  1332. color: #fff;
  1333. .date {
  1334. margin-right: 5px;
  1335. color: #ccc;
  1336. }
  1337. .el-icon {
  1338. margin: 0 5px;
  1339. font-size: 14px;
  1340. position: relative;
  1341. top: 3px;
  1342. }
  1343. }
  1344. }
  1345. }
  1346. }
  1347. @keyframes coding {
  1348. from {
  1349. opacity: 0;
  1350. transform: translateY(10vh);
  1351. }
  1352. to {
  1353. opacity: 1;
  1354. transform: translateY(0);
  1355. }
  1356. }
  1357. }
  1358. }
  1359. </style>