lowerCodeHelper

低代码平台助手

目前為 2024-01-10 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name lowerCodeHelper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3.6
  5. // @description 低代码平台助手
  6. // @author Ziker
  7. // @match https://ops.iyunquna.com/63008/*
  8. // @match http://localhost:63342/api/file/*
  9. // @require https://code.jquery.com/jquery-3.4.1.min.js
  10. // @icon https://favicon.qqsuu.cn/work.yqn.com
  11. // @note 2023年12月18日20:26:31 V0.3.4 fix RT 显示BUG
  12. // @note 2024年1月4日11:50:57 支持自定义包名接口打开文件
  13. // @note 2024年1月10日11:00:40 修复子流程,mq,job 跳转不显示
  14. // @grant GM_openInTab
  15. // @grant unsafeWindow
  16. // @grant window.close
  17. // @grant window.focus
  18. // @run-at document-body
  19. // @noframes
  20. // @license AGPL License
  21. // ==/UserScript==
  22.  
  23. window.jq = $.noConflict(true);
  24.  
  25. (function (window) {
  26. window.pageHelper = {
  27. // 等待元素可见
  28. waitElementVisible(visibleTag, index, fun) {
  29. let node = jq(visibleTag)
  30. if (node === null || node[index] === null || node[index] === undefined) {
  31. setTimeout(() => {
  32. pageHelper.waitElementVisible(visibleTag, index, fun)
  33. }, 500)
  34. } else {
  35. fun()
  36. }
  37. },
  38. getCurrentAppIndex() {
  39. const nodes = document.getElementsByClassName("bar-tab")
  40. for (let i = 0; i < nodes.length; i++) {
  41. if (nodes[i].className.indexOf("bar-tab-selected") > 0) {
  42. return i
  43. }
  44. }
  45. return -1
  46. },
  47. getCurrentApiId() {
  48. const nodes = document.getElementsByClassName("tab-content-presentation-components")
  49. if (nodes.length === 0) {
  50. return null
  51. }
  52. let className = nodes[this.getCurrentAppIndex()].className
  53. return className.substring(className.indexOf("theia-tab-") + "theia-tab-".length)
  54. },
  55. sleep(duration) {
  56. return new Promise(resolve => {
  57. setTimeout(resolve, duration)
  58. })
  59. },
  60. showToast(msg, duration) {
  61. // 显示提示
  62. duration = isNaN(duration) ? 3000 : duration
  63. const m = document.createElement('div')
  64. m.innerHTML = msg
  65. m.style.cssText = "display: flex;justify-content: center;align-items: center;width:60%; min-width:180px; " +
  66. "background:#000000; opacity:0.98; height:auto;min-height: 50px;font-size:25px; color:#fff; " +
  67. "line-height:30px; text-align:center; border-radius:4px; position:fixed; top:85%; left:20%; z-index:999999;"
  68. document.body.appendChild(m)
  69. setTimeout(function () {
  70. const d = 0.5
  71. m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in'
  72. m.style.opacity = '0'
  73. setTimeout(function () {
  74. document.body.removeChild(m)
  75. }, d * 1000)
  76. }, duration)
  77. },
  78. // 关闭窗口
  79. closeWindow() {
  80. window.opener = null
  81. window.open('', '_self')
  82. window.close()
  83. },
  84. formatString(str, len, padding) {
  85. const diff = len - str.toString().length
  86. if (diff > 0) {
  87. return padding.repeat(diff) + str
  88. } else {
  89. return str
  90. }
  91. },
  92. initSetting() {
  93. const customSetting = document.createElement("div")
  94. document.body.appendChild(customSetting)
  95. customSetting.innerHTML = `
  96. <div id="copy-setting"
  97. style="display: none;position: absolute;top: 0;right: 0;background-color: #fff3f3;padding: 10px;width: 600px;z-index: 9999">
  98. <p>拷贝动作</p>
  99. <p><label>Copy File Id <input type="radio" name="copyValue" value="1" /></label></p>
  100. <p><label>Rest Api Open File <input type="radio" name="copyValue" value="2" checked/></label>&nbsp;&nbsp;&nbsp;较新版本IDEA需要下载 <a target="_blank" href="https://plugins.jetbrains.com/plugin/19991-ide-remote-control">IDE Remote Control </a> 插件</p>
  101. <p><label>ToolBox Open File <input type="radio" name="copyValue" value="3" /></label>&nbsp;&nbsp;&nbsp;需要下载 <a target="_blank" href="https://www.jetbrains.com/toolbox-app/">Jetbrains ToolBox</a> 工具箱软件</p>
  102. <div id="projectPath" style="visibility: visible"><p><label><input name="path" style="width: 100%" type="text" placeholder="项目路径截止到 src 前 E:/yqnProject/yqn-wms/yqn-wms-rest-provider/"/></label></p></div>
  103. <p style="visibility: hidden"><label>同时打开编排IDE <input type="checkbox" name="openIDE" /></label>&nbsp;&nbsp;&nbsp;</p>
  104. <div style="display: flex;margin: 5px;justify-content: space-between">
  105. <button id="save" lang="zh" type="button" class="ant-btn ant-btn-default perf-tracked yqn-button"><span
  106. style="margin-left: 5px;">save</span></button>
  107. <button id="close" lang="zh" type="button" class="ant-btn ant-btn-default perf-tracked yqn-button"><span
  108. style="margin-left: 5px;">close</span></button>
  109. </div>
  110. </div>
  111. `
  112. let copyValue = localStorage.getItem("copyValue")
  113. copyValue = copyValue === null || copyValue === undefined ? 1 : copyValue
  114. document.querySelector('input[name="copyValue"][value="' + copyValue + '"]').checked = true
  115. document.getElementById("projectPath").style.visibility = copyValue === "2" ? "visible" : "hidden"
  116.  
  117. let path = localStorage.getItem("path")
  118. document.querySelector('input[name="path"]').value = path === null || path === undefined ? null : path
  119.  
  120. let openIDE = JSON.parse(localStorage.getItem("openIDE"))
  121. document.querySelector('input[name="openIDE"]').checked = openIDE === null || openIDE === undefined ? false : openIDE
  122.  
  123. const radios = document.querySelectorAll('input[name="copyValue"]')
  124. for (let i = 0; i < radios.length; i++) {
  125. radios[i].onchange = () => {
  126. const remoteRadio = document.querySelector('input[name="copyValue"]:checked')
  127. document.getElementById("projectPath").style.visibility = remoteRadio.value === "2" ? "visible" : "hidden"
  128. }
  129. }
  130. document.getElementById("save").addEventListener("click", () => {
  131. const remoteRadio = document.querySelector('input[name="copyValue"]:checked').value
  132. const pathInput = document.querySelector('input[name="path"]').value
  133. const openIDEValue = document.querySelector('input[name="openIDE"]').checked
  134. if (remoteRadio === '2' && (pathInput === null || pathInput.length === 0)) {
  135. this.showToast("路径不可为空", 1000)
  136. } else {
  137. localStorage.setItem("copyValue", remoteRadio)
  138. localStorage.setItem("path", pathInput)
  139. localStorage.setItem("openIDE", JSON.stringify(openIDEValue))
  140. this.showToast("保存成功", 1000)
  141. }
  142. })
  143. document.getElementById("close").addEventListener("click", () => {
  144. document.getElementById("copy-setting").style.display = "none"
  145. })
  146. }
  147. }
  148. })(window);
  149.  
  150.  
  151. (function () {
  152. let historyTrace = ''
  153. 'use strict';
  154. let appName = null
  155. let pageNameMap = []
  156. jq(document).ready(function () {
  157. if (window.location.href.indexOf("localhost:63342/api/file/") >= 0) {
  158. window.pageHelper.closeWindow()
  159. return
  160. }
  161. // 监听api tab变动
  162. waitObserve(".tabs-bar", () => {
  163. // 应用接口面板
  164. const appPanelTag = ".tab-content-presentation-components.theia-tab-" + window.pageHelper.getCurrentApiId()
  165. // 监听属性面板变动
  166. waitObserve(appPanelTag + " .p-8", () => {
  167. const p8 = document.querySelector(appPanelTag + " .p-8")
  168. const panel = p8.querySelector(".ant-form.ant-form-vertical.yqn-form")
  169. if (nonNull(panel) && isNull(panel.querySelector(".customScriptInfo"))) {
  170. const div = document.createElement("div")
  171. div.className = "customScriptInfo"
  172. panel.appendChild(div)
  173. // 获取当前 NodeName
  174. const childProcessNode = panel.querySelector("#code")
  175. const codeSpan = p8.querySelector(".dashboard-code span")
  176. if (isNull(childProcessNode) && isNull(codeSpan)) {
  177. return
  178. }
  179. let nodeName = nonNull(childProcessNode) ? childProcessNode.value : codeSpan.textContent
  180. // 获取当前节点信息
  181. getNodeInfo(nodeName, node => {
  182. // 处理信息
  183. const divNode = document.querySelector(appPanelTag + " .customScriptInfo")
  184. // 清空显示
  185. divNode.innerHTML = ""
  186. // 脚本
  187. if (nonNull(node.scriptId)) {
  188. divNode.appendChild(createButton("脚本:" + node.scriptId, () => copyToClipboard(node.scriptId)))
  189. }
  190. // 入参
  191. let id = node.inputScriptId
  192. const inputScriptIds = node.inputScriptIds
  193. if (nonNull(inputScriptIds)) {
  194. id = nonNull(id) ? id : inputScriptIds.example
  195. id = nonNull(id) ? id : inputScriptIds.record
  196. id = nonNull(id) ? id : inputScriptIds.recordList
  197. id = nonNull(id) ? id : inputScriptIds.condition
  198. id = nonNull(id) ? id : inputScriptIds.id
  199. }
  200. let isJava = true
  201. if (isNull(id) && nonNull(node.dslBulidData)) {
  202. id = node.dslBulidData.dslScriptId
  203. isJava = false
  204. }
  205. if (nonNull(id)) {
  206. divNode.appendChild(createButton("入参:" + id, () => copyToClipboard(id, isJava)))
  207. }
  208. // 条件
  209. const executeScriptId = node.executeScriptId
  210. if (nonNull(executeScriptId)) {
  211. divNode.appendChild(createButton("条件:" + executeScriptId, () => copyToClipboard(executeScriptId)))
  212. }
  213. // 校验
  214. const assertScriptId = node.assertScriptId
  215. if (nonNull(assertScriptId)) {
  216. divNode.appendChild(createButton("校验:" + assertScriptId, () => copyToClipboard(assertScriptId)))
  217. }
  218. })
  219. }
  220. })
  221.  
  222. // 监听执行历史面板变动
  223. const executeHistoryBodyTag = appPanelTag + " .test-split-item.test-split-item-right .ant-table-body tbody"
  224. waitObserve(executeHistoryBodyTag, () => {
  225. const lines = document.querySelectorAll(executeHistoryBodyTag + " .ant-table-row.ant-table-row-level-0")
  226. if (nonNull(lines)) {
  227. if (nonNull(lines[0].querySelector(".customer-button-div"))) {
  228. return
  229. }
  230. appendFlagNode(lines[0], "customer-button-div")
  231. getExecuteHistory(content => {
  232. for (let i = 0; i < content.length; i++) {
  233. const tds = lines[i].querySelectorAll("td")
  234. const actionCol = tds[tds.length - 1]
  235. const detailButton = actionCol.querySelectorAll("button")[0]
  236.  
  237. detailButton.style.display = "none"
  238. actionCol.insertBefore(createButton("RT:" + content[i].rt, () => {
  239. detailButton.click()
  240. historyTrace = tds[2].innerText
  241. }), detailButton)
  242. }
  243. })
  244. }
  245. })
  246.  
  247. const currentApiId = window.pageHelper.getCurrentApiId();
  248. if (isNull(pageNameMap[currentApiId])) {
  249. getApiPackageName(name => pageNameMap[currentApiId] = name)
  250. }
  251. })
  252.  
  253. // 监听body变动
  254. waitObserve("body", () => {
  255. // 监听执行日志流程图变动
  256. const processPanelTag = ".node-bpmn #canvas .bjs-container .djs-container .viewport .layer-base"
  257. waitObserve(processPanelTag, () => {
  258. const processPanel = document.querySelector(processPanelTag)
  259. if (isNull(processPanel.querySelector(".customer-rt"))) {
  260. appendFlagNode(processPanel, "customer-rt")
  261. const oldRtNode = processPanel.querySelectorAll(".bpmn-tiny-label");
  262. if (nonNull(oldRtNode)) {
  263. oldRtNode.forEach(v => v.style.display = "none")
  264. }
  265. getByTraceId(apiNodeLogList => {
  266. for (let i = 0; i < apiNodeLogList.length; i++) {
  267. const log = apiNodeLogList[i]
  268. const textNode = processPanel.querySelector("[data-element-id='" + log.nodeId + "'] text")
  269. if (isNull(textNode)) {
  270. continue
  271. }
  272. const tspan = textNode.querySelector("tspan")
  273. if (nonNull(tspan)) {
  274. const rtNode = tspan.cloneNode(true)
  275. textNode.appendChild(rtNode)
  276. rtNode.setAttribute("x", "65")
  277. rtNode.setAttribute("y", "15")
  278. rtNode.innerHTML = window.pageHelper.formatString(log.rt, 4, '&nbsp;&nbsp;')
  279. }
  280. }
  281. })
  282. }
  283. }, false)
  284. })
  285.  
  286. // 工具栏设置按钮
  287. waitObserve(".app-actions", () => {
  288. const buttonLists = document.querySelector(".app-actions")
  289. if (isNull(buttonLists) || nonNull(document.querySelector(".setting-flag"))) {
  290. return
  291. }
  292. appendFlagNode(document.body, "setting-flag")
  293. const settingButton = document.createElement("div")
  294. settingButton.style.marginLeft = '10px'
  295. const button = document.createElement("button")
  296. button.type = "button"
  297. button.id = name
  298. button.className = "ant-btn ant-btn-default perf-tracked yqn-button"
  299. button.onclick = () => {
  300. const settingPanel = document.getElementById("copy-setting")
  301. settingPanel.style.display = settingPanel.style.display === 'block' ? 'none' : "block"
  302. }
  303. const span = document.createElement("span")
  304. span.textContent = "CopySetting"
  305. button.appendChild(span)
  306. settingButton.appendChild(button)
  307. buttonLists.appendChild(settingButton)
  308. })
  309.  
  310. window.pageHelper.initSetting()
  311. getAppProjectName(data => {
  312. appName = data
  313. console.log("项目名称", data)
  314. })
  315. })
  316.  
  317.  
  318. function nonNull(o) {
  319. return o !== null && o !== undefined
  320. }
  321.  
  322. function isNull(o) {
  323. return o === null || o === undefined
  324. }
  325.  
  326.  
  327. // 追加标记节点
  328. function appendFlagNode(node, flag) {
  329. const divFlag = document.createElement("div")
  330. node.appendChild(divFlag)
  331. divFlag.className = flag
  332. divFlag.style.display = "none"
  333. }
  334.  
  335. // 等待出现并监听变化
  336. function waitObserve(visibleTag, fun, attributes = true) {
  337. window.pageHelper.waitElementVisible(visibleTag, 0, () => {
  338. new MutationObserver(function () {
  339. fun()
  340. }).observe(document.querySelector(visibleTag), {
  341. attributes: attributes,
  342. childList: true,
  343. subtree: true,
  344. characterData: true
  345. })
  346. })
  347. }
  348.  
  349. function copyToClipboard(text, isJava = true) {
  350. let copyValue = localStorage.getItem("copyValue")
  351. copyValue = copyValue === null || copyValue === undefined ? '1' : copyValue
  352. if (copyValue === '1') {
  353. let textarea = document.createElement('textarea')
  354. textarea.value = text
  355. document.body.appendChild(textarea)
  356. textarea.select()
  357. document.execCommand('copy')
  358. document.body.removeChild(textarea)
  359. window.pageHelper.showToast("已拷贝 " + text, 1500)
  360. } else if (copyValue === '2') {
  361. let path = localStorage.getItem("path")
  362. path = path === null || path === undefined ? null : (path.endsWith("/") ? path : path + '/')
  363. otherReq("http://127.0.0.1:63342/api/file/" + path + "src/main/java/com/yqn/framework/composer/api/" + pageNameMap[window.pageHelper.getCurrentApiId()] + "/script/Script_" + text + (isJava ? ".java" : ".json"))
  364. window.pageHelper.showToast("已打开文件,如未打开,检查插件是否安装以及path是否正确", 2000)
  365. } else if (copyValue === '3') {
  366. const url = 'jetbrains://idea/navigate/reference?project=' + appName + '&fqn=com.yqn.framework.composer.api.' + pageNameMap[window.pageHelper.getCurrentApiId()] + '.script.Script_' + text;
  367. window.open(url)
  368. window.pageHelper.showToast("已打开文件,如未打开,请确认已安装Jetbrains Toolbox ", 2000)
  369. }
  370. }
  371.  
  372. // 创建按钮
  373. function createButton(name, listener) {
  374. const button = document.createElement("button")
  375. button.type = "button"
  376. button.id = name
  377. button.className = "ant-btn ant-btn-link perf-tracked yqn-button yqn-link-no-padding customer-button"
  378. button.style.marginRight = '5px'
  379. button.onclick = listener
  380. const span = document.createElement("span")
  381. span.textContent = name
  382. button.appendChild(span)
  383. return button
  384. }
  385.  
  386. const addressArr = ['', '/process', '/mq', '/job']
  387.  
  388. function getApiPackageName(fuc, apiMode = 0) {
  389. remoteReq('/api/42080/api' + addressArr[apiMode] + '/details_composer', {
  390. "id": window.pageHelper.getCurrentApiId(),
  391. }, data => {
  392. if (isNull(data)) {
  393. if (apiMode === 0) {
  394. getApiPackageName(fuc, 1)
  395. getApiPackageName(fuc, 2)
  396. getApiPackageName(fuc, 3)
  397. }
  398. return
  399. }
  400. let processDefine = JSON.parse(data.processDefine)
  401. fuc(isNull(processDefine.process.packageName) || processDefine.process.packageName === '' ? 'api_' + processDefine.process.id : processDefine.process.packageName)
  402. })
  403. }
  404.  
  405. // 拿流程信息
  406. function getNodeInfo(nodeName, fuc, apiMode = 0) {
  407. remoteReq('/api/42080/api' + addressArr[apiMode] + '/details_composer', {
  408. "id": window.pageHelper.getCurrentApiId(),
  409. }, data => {
  410. if (isNull(data)) {
  411. if (apiMode === 0) {
  412. getNodeInfo(nodeName, fuc, 1)
  413. getNodeInfo(nodeName, fuc, 2)
  414. getNodeInfo(nodeName, fuc, 3)
  415. }
  416. return
  417. }
  418. let processDefine = JSON.parse(data.processDefine)
  419. for (let i = 0; i < processDefine.nodes.length; i++) {
  420. if (processDefine.nodes[i].code === nodeName) {
  421. fuc(processDefine.nodes[i])
  422. break
  423. }
  424. }
  425. })
  426. }
  427.  
  428. // 拿历史执行数据
  429. function getExecuteHistory(fuc) {
  430. remoteReq('/api/42086/apiLog/list', {}, data => {
  431. if (nonNull(data) && nonNull(data.content)) {
  432. fuc(data.content)
  433. }
  434. })
  435. }
  436.  
  437. // 拿 trace 执行数据
  438. function getByTraceId(fuc, apiMode = 0) {
  439. const apiType = ['api', 'process', 'mq', 'job']
  440. remoteReq('/api/42086/apiLog/getByTraceId', {
  441. "traceIdLike": historyTrace,
  442. "testCaseId": null,
  443. "apiTypeCode": apiMode === 0 ? "api" : apiType[apiMode]
  444. }, data => {
  445. if (nonNull(data) && nonNull(data.apiNodeLogList) && data.apiNodeLogList.length !== 0) {
  446. fuc(data.apiNodeLogList)
  447. } else if (apiMode === 0) {
  448. getByTraceId(fuc, 1)
  449. getByTraceId(fuc, 2)
  450. getByTraceId(fuc, 3)
  451. }
  452. })
  453. }
  454.  
  455. // 获取应用名称
  456. function getAppProjectName(fuc) {
  457. remoteReq('/api/42080/application/getById', {}, data => {
  458. if (nonNull(data)) {
  459. fuc(data.appName)
  460. }
  461. })
  462. }
  463.  
  464.  
  465. function remoteReq(url, model, fuc) {
  466. model.apiId = window.pageHelper.getCurrentApiId()
  467. model.appId = new URLSearchParams(window.location.href.split('?')[1]).get('appId')
  468. model.env = "qa4"
  469. model.page = 1
  470. model.size = 20
  471. jq.ajax({
  472. url: 'https://gw-ops.iyunquna.com' + url,
  473. method: 'POST',
  474. xhrFields: {
  475. withCredentials: true
  476. },
  477. crossDomain: true,
  478. contentType: 'application/json',
  479. data: JSON.stringify({
  480. "header": {
  481. "xSourceAppId": "63008",
  482. "guid": "6f87e073-1da1-4017-b2de-c109abcd6d123",
  483. "lang": "zh",
  484. "timezone": "Asia/Shanghai"
  485. },
  486. "model": model
  487. }),
  488. success: function (response) {
  489. if (response.code === 200) {
  490. fuc(response.data)
  491. } else {
  492. fuc(null)
  493. }
  494. },
  495. error: function (xhr, status, error) {
  496. console.log('Request failed:', error)
  497. fuc(null)
  498. }
  499. })
  500. }
  501.  
  502.  
  503. function otherReq(url) {
  504. jq.ajax(url, {
  505. method: "GET",
  506. xhrFields: {
  507. withCredentials: true
  508. },
  509. crossDomain: true
  510. })
  511. }
  512. })();
  513.