lowerCodeHelper

低代码平台助手

当前为 2023-12-20 提交的版本,查看 最新版本

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