GitLab Viewer Publish and Deploy Project

GitLab Viewer Publish and Deploy Project!

目前为 2023-05-31 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitLab Viewer Publish and Deploy Project
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.06
  5. // @description GitLab Viewer Publish and Deploy Project!
  6. // @author Sean
  7. // @match http://192.168.0.200/fe3project/*
  8. // @match http://192.168.0.200/frontend_pc/project/*
  9. // @icon http://192.168.0.200/assets/favicon-7901bd695fb93edb07975966062049829afb56cf11511236e61bcf425070e36e.png
  10. // @require https://cdn.bootcdn.net/ajax/libs/vue/2.7.14/vue.min.js
  11. // @require https://unpkg.com/element-ui/lib/index.js
  12. // @require https://cdn.bootcdn.net/ajax/libs/jszip/3.7.1/jszip.min.js
  13. // @resource myFontFile https://element.eleme.io/2.11/static/element-icons.535877f.woff
  14. // @grant GM_getResourceURL
  15. // @grant GM_addStyle
  16. // @grant GM_getResourceText
  17. // @resource ElementCSS https://unpkg.com/element-ui/lib/theme-chalk/index.css
  18. // @grant GM_xmlhttpRequest
  19. // @license MIT
  20. // @run-at document-end
  21. // ==/UserScript==
  22.  
  23. (function() {
  24. 'use strict';
  25.  
  26. const fontUrl = 'https://element.eleme.io/2.11/static/element-icons.535877f.woff';
  27.  
  28. // 添加样式规则,将字体应用到指定元素上
  29. GM_addStyle(`
  30. @font-face {
  31. font-family: element-icons;
  32. src: url(${fontUrl}) format("woff");
  33. }
  34. `);
  35.  
  36. GM_addStyle(GM_getResourceText('ElementCSS'));
  37.  
  38. let epointCss = ".epoint-tool {position: fixed; bottom: 0%; right:10px; transform: translateY(-50%);}";
  39. epointCss += ".el-row { padding: 3px 0;} .el-dialog__body .el-tree{min-height: 420px; max-height: 500px;overflow: auto;}";
  40. epointCss += ".deploy-body {height: 306px;}"
  41. epointCss += ".deploy-body .el-loading-spinner {margin-top: -50px;} .el-button--primary:focus {outline: 0 !important;}";
  42. // 添加注入样式
  43. let extraStyleElement = document.createElement("style");
  44. extraStyleElement.innerHTML = epointCss;
  45. document.head.appendChild(extraStyleElement);
  46.  
  47. const MyComponent = {
  48. template: `<div class="epoint-wrap">
  49. <div class="epoint-tool">
  50. <el-row><el-button type="primary" icon="el-icon-search" round @click="viewProject">查看</el-button></el-row>
  51. <el-row>
  52. <el-tooltip content="一键部署到170服务器" placement="top" effect="light">
  53. <el-button type="primary" icon="el-icon-s-unfold" round @click="doDeploy">部署</el-button>
  54. </el-tooltip>
  55. </el-row>
  56. <el-row>
  57. <el-tooltip content="一键发布到项目案例库" placement="top" effect="light">
  58. <el-button type="primary" icon="el-icon-upload" round @click="publish">发布</el-button>
  59. </el-tooltip>
  60. </el-row>
  61. </div>
  62. <el-dialog
  63. title="目录结构"
  64. width="900px"
  65. :append-to-body="true"
  66. :visible.sync="dialogVisible"
  67. :before-close="handleClose">
  68. <el-tree
  69. class="filter-tree"
  70. :data="data"
  71. :props="defaultProps"
  72. node-key="id"
  73. default-expand-all
  74. @node-click="handleNodeClick"
  75. v-loading="loadingTree"
  76. ref="tree">
  77. </el-tree>
  78. </el-dialog>
  79. <el-dialog
  80. title="部署"
  81. width="420px"
  82. :visible.sync="depDialogVisible">
  83. <div class="deploy-body"
  84. v-loading="loading"
  85. element-loading-text="正在打包部署至 170 服务器,请耐心等待"
  86. element-loading-spinner="el-icon-loading">
  87. </div>
  88. </el-dialog>
  89. <el-dialog title="部署提示" width="640px" :visible.sync="dialogDeployVisible">
  90. <el-alert
  91. title="此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?"
  92. type="warning" :closable="false" style="margin-bottom: 20px;">
  93. </el-alert>
  94. <el-form :model="form" ref="form" :rules="rules">
  95. <el-form-item label="框架类型" :label-width="formLabelWidth" prop="frame">
  96. <el-select v-model="form.frame" placeholder="请框架类型">
  97. <el-option v-for="item in framework" :key="item.value" :label="item.label" :value="item.value"></el-option>
  98. </el-select>
  99. </el-form-item>
  100. </el-form>
  101. <div slot="footer" class="dialog-footer">
  102. <el-button @click="dialogDeployVisible = false">取 消</el-button>
  103. <el-button type="primary" @click="doDeploy">确 定</el-button>
  104. </div>
  105. </el-dialog>
  106. </div>`,
  107. data() {
  108. return {
  109. dialogVisible: false, // 查看目录结构弹窗
  110. data: [], // 目录结构树的数据结构
  111. defaultProps: {
  112. children: 'children',
  113. label: 'label'
  114. },
  115. loadingTree: false,
  116. depDialogVisible: false, // 部署中弹窗
  117. loading: false,
  118. projectLibUrl: 'http://192.168.201.159:9999/webapp/pages/default/onlinecase.html', // 项目案例库地址
  119. projectIsDeployed: false, // 项目是否部署过
  120. projectFtpUrl: '', // ftp路径
  121. projectEntryUrl: 'http://192.168.219.170/showcase/', // 项目案例库发布表单的入口页面
  122. supportDeploy: true, // 项目是否支持部署
  123. dialogDeployVisible: false,
  124. formLabelWidth: '120px',
  125. form: {
  126. frame: ''
  127. },
  128. framework: [
  129. {label: '重构模板', value: 1},
  130. {label: 'f9x1.0', value: 2},
  131. {label: 'f9x2.0', value: 3},
  132. {label: 'f950', value: 4},
  133. {label: 'f950sp1', value: 5},
  134. {label: 'f950sp2', value: 6},
  135. {label: 'f950sp3', value: 7},
  136. {label: 'f940', value: 8},
  137. {label: 'f942', value: 9},
  138. {label: 'f941', value: 10},
  139. {label: 'f934', value: 11},
  140. {label: 'f933', value: 12},
  141. {label: 'f932', value: 13},
  142. {label: 'f9211', value: 14},
  143. {label: '骨架', value: 15},
  144. {label: 'Vue', value: 16},
  145. {label: 'React', value: 17}
  146. ],
  147. rules: {
  148. frame: [
  149. { required: true, message: '请选择框架类型', trigger: 'change' }
  150. ]
  151. },
  152. clickNodeEntry: null
  153. };
  154. },
  155. methods: {
  156. handleClose(done) {
  157. done();
  158. /*
  159. this.$confirm('确认关闭?')
  160. .then(_ => {
  161. done();
  162. })
  163. .catch(_ => {});*/
  164. },
  165. handleNodeClick(data) {
  166. console.log(data);
  167. if(data.type === 'folder') {
  168. return false;
  169. }
  170. var self = this;
  171. var entry = data.entry;
  172. self.clickNodeEntry = entry;
  173. if(self.projectFtpUrl) {
  174. window.open('http://192.168.219.170' + self.projectFtpUrl + data.entry);
  175. self.clickNodeEntry = null;
  176. } else {
  177. if(this.supportDeploy === false) {
  178. this.$message({
  179. type: 'error',
  180. message: '暂只支持查看 重构模板 和 F9x2.0 的项目'
  181. });
  182. self.clickNodeEntry = null;
  183. return false;
  184. }
  185.  
  186. this.$confirm('资源未部署,部署至 170 服务器后可查看,是否部署?')
  187. .then(_ => {
  188.  
  189. this.dialogDeployVisible = true;
  190. /*
  191. this.depDialogVisible = true;
  192. this.loading = true;
  193. // 部署
  194. this.getDeployInfo({ type: '1' }, (data)=> {
  195. this.loading = false;
  196. this.depDialogVisible = false;
  197.  
  198. this.data = data.custom.detail;
  199. this.projectFtpUrl = data.custom.ftpUrl;
  200.  
  201. this.$alert('部署成功!', '提示', {
  202. confirmButtonText: '确定',
  203. callback: action => {
  204. window.open('http://192.168.219.170' + self.projectFtpUrl + entry)
  205. }
  206. });
  207.  
  208. });*/
  209. })
  210. .catch(_ => {});
  211. }
  212. },
  213. // 部署
  214. doDeploy () {
  215. if(!this.isDownLoadPage()) {
  216. return;
  217. }
  218. if(!this.dialogDeployVisible) {
  219. this.dialogDeployVisible = true;
  220. return;
  221. }
  222.  
  223. let self = this;
  224.  
  225. this.$refs['form'].validate((valid) => {
  226. if (valid) {
  227. this.dialogDeployVisible = false;
  228. this.depDialogVisible = true;
  229. this.loading = true;
  230. // 部署
  231. this.getDeployInfo({ type: '1', frame: this.form.frame }, (data)=> {
  232. this.loading = false;
  233. this.depDialogVisible = false;
  234.  
  235. if(!data.custom.text){
  236.  
  237. this.data = data.custom.detail;
  238. this.projectFtpUrl = data.custom.ftpUrl;
  239. this.supportDeploy = data.custom.supportDeploy;
  240. // 从部署按钮直接过来的
  241. if(!this.clickNodeEntry) {
  242. this.$message({
  243. type: 'success',
  244. message: '部署成功!'
  245. });
  246. // 打开查看弹窗
  247. this.viewProject();
  248. } else {// 从点击目录结构过来的,可以调整点击的树节点页面
  249. this.$alert('部署成功!', '提示', {
  250. confirmButtonText: '确定',
  251. callback: action => {
  252. window.open('http://192.168.219.170' + self.projectFtpUrl + self.clickNodeEntry);
  253. self.clickNodeEntry = null;
  254. }
  255. });
  256. }
  257.  
  258. } else {
  259. this.$message({
  260. type: 'error',
  261. message: data.custom.text
  262. });
  263. }
  264. });
  265. } else {
  266. console.log('error submit!!');
  267. return false;
  268. }
  269. });
  270.  
  271. /*
  272. this.$confirm('此操作将把 GitLab 资源打包部署至 170 服务器,已部署过的项目会进行覆盖部署,是否继续?', '提示', {
  273. confirmButtonText: '确定',
  274. cancelButtonText: '取消',
  275. type: 'warning'
  276. }).then(() => {
  277. }).catch(() => {
  278. this.$message({
  279. type: 'info',
  280. message: '已取消部署'
  281. });
  282. });*/
  283. },
  284. // 发布项目案例库
  285. publish () {
  286. this.$confirm('此操作将把项目发布至 <a href="'+ this.projectLibUrl +'" target="_blank">项目案例库</a>,已发布过的项目案例库会有重复项,是否继续?', '提示', {
  287. confirmButtonText: '确定',
  288. cancelButtonText: '取消',
  289. dangerouslyUseHTMLString: true,
  290. type: 'warning'
  291. }).then(() => {
  292. const themes = document.querySelectorAll('.badge-secondary');
  293. let keys = [];
  294.  
  295. for(let i = 0, l = themes.length; i < l; i++) {
  296. if(themes[0].innerText == '智能设备' && i > 0) {
  297. keys.push(themes[i].innerText);
  298. } else if (themes[0].innerText !== '智能设备' && i > 1) {
  299. keys.push(themes[i].innerText);
  300. }
  301. }
  302. // 存在更多主题的情况
  303. const moreKeyEl = document.querySelector('.gl-w-full .text-nowrap');
  304. if(moreKeyEl) {
  305. const moreKeyElContent = moreKeyEl.getAttribute('data-content');
  306. const regex = />([^<]+)</g;
  307. const matches = moreKeyElContent.match(regex);
  308. const results = matches.filter(function(match) {
  309. return match.length > 3;
  310. });
  311. const moreKeyData = results.map(function(match) {
  312. return match.substring(2, match.length - 2);
  313. });
  314.  
  315. if(moreKeyData && moreKeyData.length) {
  316. keys = keys.concat(moreKeyData);
  317. }
  318. }
  319.  
  320. // 组织项目案例库所需参数
  321. const projectName = document.querySelector('.home-panel-title').innerText;
  322. const projectBU = themes[0] ? themes[0].innerText : null;
  323. const projectKeys = keys.join(' ');
  324. const entryUrl = this.projectEntryUrl;
  325. let projectType = themes[1] ? themes[1].innerText : null;
  326.  
  327. if(themes[0]) {
  328. if(themes[0].innerText == '智能设备') {
  329. projectType = themes[1] ? themes[0].innerText : null;
  330. } else {
  331. projectType = themes[1] ? themes[1].innerText : null;
  332. }
  333. }
  334.  
  335. const destUrl = this.projectLibUrl + '?projectName=' + projectName + '&projectBU=' + projectBU + '&projectType=' + projectType + '&projectKeys=' + projectKeys + '&entryUrl=' + entryUrl + '&git=' + window.location.href;
  336.  
  337. this.$message({
  338. type: 'success',
  339. message: destUrl
  340. });
  341.  
  342. window.open(destUrl);
  343.  
  344. }).catch(() => {
  345. this.$message({
  346. type: 'info',
  347. message: '已取消发布'
  348. });
  349. });
  350. },
  351. // 查看项目
  352. viewProject () {
  353. if(!this.isDownLoadPage()) {
  354. return;
  355. }
  356. this.dialogVisible = true;
  357. this.loadingTree = true;
  358. let self = this;
  359.  
  360. // 发送ajax请求,查看是否进行过部署
  361. this.getDeployInfo((data)=> {
  362. // 有部署信息,直接赋值,
  363. if(true) {
  364. self.projectIsDeployed = true;
  365. self.data = data.custom.detail;
  366. self.projectFtpUrl = data.custom.ftpUrl;
  367. self.supportDeploy = data.custom.supportDeploy;
  368. self.loadingTree = false;
  369.  
  370.  
  371. } else {
  372. // 无部署信息,仅查看文件目录
  373. getZipResource((data)=> {
  374. self.data = data;
  375. self.loadingTree = false;
  376. });
  377. }
  378. });
  379.  
  380. },
  381. // 项目部署信息
  382. getDeployInfo(params, callback) {
  383. const projectId = document.body.getAttribute('data-project-id');
  384. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  385.  
  386. const sourceUrl = downloadBtn.getAttribute('href');
  387. const downloadUrl = window.location.origin + sourceUrl;
  388. const files = document.body.getAttribute('data-find-file').split('/');
  389. const name = document.body.getAttribute('data-project') + '-' + files[files.length - 1];
  390. const author = document.querySelector('.current-user .gl-font-weight-bold').innerText.trim();
  391. const projectName = document.querySelector('.sidebar-context-title').innerText.trim();
  392. const deployManOA = document.querySelector('.current-user>a').getAttribute('data-user');
  393. const projectGitUrl = 'http://192.168.0.200' + document.body.getAttribute('data-find-file').split('/-/')[0];
  394.  
  395. if(typeof params == 'function') {
  396. callback = params;
  397. params = null;
  398. }
  399.  
  400. if(projectId && projectId.length && sourceUrl) {
  401. fetch('http://192.168.219.170:3008/api/getDeployInfo', {
  402. method: 'POST',
  403. // 允许跨域请求
  404. mode: 'cors',
  405. headers: {
  406. 'Accept': 'application/json',
  407. 'Content-Type': 'application/json'
  408. },
  409. body: JSON.stringify({//post请求参数
  410. params: {
  411. "type": (params && params.type !== undefined) ? params.type : '0',// 0 代表查看, 1代表部署
  412. "name": name, // 项目路径英文名
  413. "deployMan": author, // 部署人姓名
  414. "deployManOA": deployManOA, // 部署人账号
  415. "projectName": projectName, // 项目名称
  416. "downloadUrl": downloadUrl, // 下载地址
  417. "projectId": projectId, // 主键,gitlab上的项目id
  418. "projectGitUrl": projectGitUrl,
  419. "frame": (params && params.frame) ? params.frame : undefined
  420. }
  421. })
  422. })
  423. .then(response => response.text())
  424. .then((result) => {
  425. var data = JSON.parse(result);
  426. callback && callback(data);
  427. })
  428. .catch(error => {
  429. this.depDialogVisible = false;
  430. this.dialogVisible = false;
  431. this.$message({
  432. type: 'error',
  433. message: '系统故障,请联系管理员。'
  434. });
  435. console.error(error);
  436. });
  437. } else {
  438. this.$message({
  439. type: 'error',
  440. message: '本页不支持查看和部署,请至仓库页。'
  441. });
  442. console.error('部署信息请求参数error');
  443. }
  444. },
  445. // 设置入口
  446. setProjectEntry(){
  447. const firstNode = findFirstFileNode(this.data);
  448. if(firstNode) {
  449. this.projectEntryUrl = 'http://192.168.219.170' + this.projectFtpUrl + firstNode.entry;
  450. }
  451. },
  452. isDownLoadPage() {
  453. const downloadBtn = document.querySelector('.gl-button.btn-sm.btn-confirm');
  454. const sourceUrl = downloadBtn && downloadBtn.getAttribute('href');
  455.  
  456. if(downloadBtn && sourceUrl) {
  457. return true;
  458. } else {
  459. this.$message({
  460. type: 'error',
  461. message: '本页面不支持查看和部署,请移至仓库页。'
  462. });
  463. return false;
  464. }
  465. }
  466. },
  467. mounted() {
  468.  
  469. }
  470. };
  471.  
  472. const placeholder = document.createElement('div');
  473.  
  474. // 创建 Vue 实例并挂载到页面
  475. const vueInstance = new Vue({
  476. el: placeholder,
  477. components: {
  478. MyComponent
  479. },
  480. methods: {
  481. },
  482. template: `<my-component></my-component>`
  483. });
  484.  
  485. // 等待页面加载完成
  486. window.addEventListener('load', function() {
  487. // 将占位元素追加到 body 元素中
  488. document.body.appendChild(vueInstance.$el);
  489. });
  490.  
  491. // 将文件条目组织成嵌套结构
  492. function organizeFileEntries(fileEntries) {
  493. const root = {
  494. label: document.querySelector('.home-panel-title').innerText || document.getElementById('project_name_edit').value,
  495. type: 'folder',
  496. children: []
  497. };
  498.  
  499. // 创建嵌套结构
  500. fileEntries.forEach(entry => {
  501. const pathSegments = entry.name.split('/');
  502. let currentFolder = root;
  503.  
  504. // 遍历路径中的每个部分,创建相应的文件夹节点
  505. for (let i = 0; i < pathSegments.length - 1; i++) {
  506. const folderName = pathSegments[i];
  507. let folder = currentFolder.children.find(child => child.label === folderName);
  508.  
  509. if(isExcludeFolder(entry.name)) {
  510. continue;
  511. }
  512.  
  513. if (!folder) {
  514. folder = {
  515. label: folderName,
  516. type: 'folder',
  517. children: []
  518. };
  519. currentFolder.children.push(folder);
  520. }
  521.  
  522. currentFolder = folder;
  523. }
  524.  
  525. // 创建文件节点并添加到相应的文件夹中
  526. const fileName = pathSegments[pathSegments.length - 1];
  527.  
  528. if(fileName && fileName.length && isIncludeFile(fileName) && !isExcludeFolder(entry.name)) {
  529. const fileNode = {
  530. label: fileName,
  531. type: 'file',
  532. entry: entry.name
  533. };
  534. currentFolder.children.push(fileNode);
  535. }
  536. });
  537.  
  538. return [root];
  539. }
  540. // 是否排除的文件夹
  541. function isExcludeFolder(entry) {
  542. if(entry.indexOf('css') > -1 ||
  543. entry.indexOf('scss') > -1 ||
  544. entry.indexOf('js') > -1 ||
  545. entry.indexOf('images') > -1 ||
  546. entry.indexOf('fui') > -1 ||
  547. entry.indexOf('lib') > -1 ||
  548. entry.indexOf('test') > -1 ||
  549. entry.indexOf('font') > -1 ||
  550. entry.indexOf('frame/fui') > -1) {
  551. return true;
  552. } else {
  553. return false;
  554. }
  555. }
  556. // 是否包含的文件
  557. function isIncludeFile(fileName) {
  558. if(fileName.indexOf('.html') > -1) {
  559. return true;
  560. } else {
  561. return false;
  562. }
  563. }
  564.  
  565. function getZipResource(callback) {
  566. const downloadUrl = window.location.origin + document.querySelector('.gl-button.btn-sm.btn-confirm').getAttribute('href');
  567.  
  568. fetch(downloadUrl)
  569. .then(response => response.arrayBuffer())
  570. .then(data => {
  571. // 将 ZIP 文件的二进制数据传递给 JSZip 进行解析
  572. return JSZip.loadAsync(data);
  573. })
  574. .then(zip => {
  575. // 获取 ZIP 文件中的所有条目(文件和目录)
  576. const zipEntries = Object.values(zip.files);
  577. const treeData = organizeFileEntries(zipEntries)
  578.  
  579. callback && callback(treeData);
  580.  
  581. })
  582. .catch(error => {
  583. console.error(error);
  584. });
  585. }
  586.  
  587. // 树结构第一个节点数据
  588. function findFirstFileNode(tree) {
  589. // 遍历树的节点
  590. for (let i = 0; i < tree.length; i++) {
  591. const node = tree[i];
  592.  
  593. // 如果节点的类型为 file,则返回该节点
  594. if (node.type === 'file') {
  595. return node;
  596. }
  597.  
  598. // 如果节点有子节点,则递归调用该函数查找子节点中的第一个 file 节点
  599. if (node.children && node.children.length > 0) {
  600. const fileNode = findFirstFileNode(node.children);
  601. if (fileNode) {
  602. return fileNode;
  603. }
  604. }
  605. }
  606.  
  607. // 如果没有找到 file 节点,则返回 null
  608. return null;
  609. }
  610. })();