index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div ref="link">
  3. <!-- <el-link
  4. v-if="showType === 'link'&&show"
  5. :underline="underline"
  6. :icon="icon"
  7. :disabled="disabled"
  8. :type="type"
  9. @click.native="click"
  10. >{{ showText }}</el-link>
  11. -->
  12. <div v-if="showType === 'link'&&iframeShow" style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)">
  13. <iframe
  14. v-if="showType === 'link'&&iframeShow"
  15. :height="height-50"
  16. width="100%"
  17. :src="iframeUrl"
  18. frameborder="0"
  19. scrolling="no"
  20. />
  21. </div>
  22. <el-button
  23. v-else-if="showType === 'button'&&show"
  24. :icon="icon"
  25. :disabled="disabled"
  26. :type="type"
  27. @click.native="click"
  28. >{{ showText }}</el-button>
  29. <!-- 弹窗模式 -->
  30. <custom-dialog
  31. ref="userDialog"
  32. :visible="openDialog"
  33. :data="formData"
  34. :components="customComponents"
  35. @close="visible => openDialog = visible"
  36. />
  37. </div>
  38. </template>
  39. <script>
  40. import request from '@/utils/request'
  41. import { getScriptValue } from '@/api/platform/form/common.js'
  42. import customDialog from './dialog'
  43. export default {
  44. name: 'ibps-hyperlink',
  45. components: {
  46. customDialog
  47. },
  48. props: {
  49. previewEntrance: {
  50. type: Boolean,
  51. default: false
  52. },
  53. showType: { // 是按钮还是文本
  54. type: String,
  55. default: 'link',
  56. validator: function(value) {
  57. return ['link', 'button'].indexOf(value) !== -1
  58. }
  59. },
  60. formData: {
  61. type: Object
  62. },
  63. text: {
  64. type: String
  65. },
  66. textJavascript: {
  67. type: String
  68. },
  69. linkType: { // 打开链接的类型
  70. type: String,
  71. default: 'fixed',
  72. validator: function(value) { // fixed 固定值; dynamic groovy脚本; javascript js 脚本
  73. return ['fixed', 'dynamic', 'javascript'].indexOf(value) !== -1
  74. }
  75. },
  76. textType: { // 文本展示的类型
  77. type: String,
  78. default: 'fixed',
  79. validator: function(value) { // fixed 固定值; dynamic groovy脚本; javascript js 脚本
  80. return ['fixed', 'dynamic', 'javascript'].indexOf(value) !== -1
  81. }
  82. },
  83. link: {
  84. type: String
  85. },
  86. type: {
  87. type: String,
  88. default: 'default'
  89. },
  90. underline: {
  91. type: Boolean,
  92. default: false
  93. },
  94. disabled: {
  95. type: Boolean,
  96. default: false
  97. },
  98. // href: {
  99. // type: String
  100. // },
  101. icon: {
  102. type: String
  103. },
  104. isNewTab: { // 新开窗口还是弹框显示
  105. type: Boolean,
  106. default: true
  107. },
  108. beforeClick: { // 方法参数( linkData, text, cb) cb 是回调函数
  109. type: Function
  110. },
  111. afterClick: {
  112. type: Function
  113. }
  114. },
  115. data() {
  116. return {
  117. linkData: '',
  118. showText: this.text || '暂无数据',
  119. openType: '',
  120. openDialog: false,
  121. show: true,
  122. iframeUrl:'',
  123. iframeShow:false,
  124. height:document.documentElement.clientHeight,
  125. customComponents: {}
  126. }
  127. },
  128. watch: {
  129. textType: {
  130. handler: function(val) {
  131. if (!this.previewEntrance) return
  132. // this.showText = ''
  133. this.setVal(val, 'text', this.textJavascript)
  134. },
  135. immediate: true
  136. },
  137. linkType: {
  138. handler: function(val) {
  139. if (!this.previewEntrance) return
  140. setTimeout(()=>{
  141. this.setVal(val, 'link', this.link);
  142. }, 100);
  143. },
  144. immediate: true
  145. }
  146. },
  147. methods: {
  148. /**
  149. * val 属性的值
  150. * prop [link]
  151. */
  152. setVal(val, prop, script, motion) {
  153. if (val === 'fixed') {
  154. this.showText = this.text || ''
  155. } else if (val === 'dynamic') {
  156. this.runGroovy(prop, script)
  157. } else if (val === 'javascript') {
  158. this.runScript(prop, script, motion)
  159. }
  160. },
  161. runScript(prop, scrip, motion) {
  162. var formData = this.formData
  163. const options = {
  164. formData: formData,
  165. request: request,
  166. router: this.$router,
  167. message: this.$message,
  168. link: this,
  169. reportPath:this.$reportPath,
  170. reportPaths:this.$reportPaths,
  171. }
  172. if (this.$utils.isEmpty(scrip)) return
  173. const promise = new window.Promise((resolve, reject) => {
  174. new Function('options', 'resolve', scrip)(options, resolve)
  175. })
  176. promise.then(result => {
  177. /* 后补 ,直接打开ifrom*/
  178. if(this.showType === 'link'){
  179. this.iframeUrl = result[0].logic.split('url:"')[1].split('"')[0]
  180. this.iframeShow = true
  181. return
  182. }
  183. if (this.$utils.isEmpty(result)) {
  184. this.showText = '暂无数据'
  185. return
  186. }
  187. options.result = result
  188. if (prop === 'text') {
  189. this.showText = result || '暂无数据'
  190. } else {
  191. result.formData = formData
  192. this.linkData = result || ''
  193. if (result.constructor === Object && result.openType && this.showType === 'link' && motion === 'click') {
  194. switch (result.openType) {
  195. case 'dialog':
  196. this.customComponents = result
  197. this.openDialog = true
  198. break
  199. case 'url':
  200. window.open(result.url)
  201. break
  202. case 'tab':
  203. this.$router.push(result.url)
  204. break
  205. default:
  206. break
  207. }
  208. } else if (this.showType === 'button') {
  209. if (result.constructor === Array) {
  210. const promise = new window.Promise((resolve, reject) => {
  211. result.forEach(item => {
  212. if (item.event === 'beforeSubmit' && motion !== 'click') {
  213. new Function('options', 'resolve', item.logic)(options, resolve)
  214. } else if (item.event === 'afterSubmit' && motion === 'click') {
  215. new Function('options', 'resolve', item.logic)(options, resolve)
  216. }
  217. })
  218. })
  219. promise.then(res => {
  220. this.conditionalExecution(res.constructor === Object && res.openType, res, 'openType')
  221. })
  222. } else {
  223. this.$message({
  224. message: '请按规定脚本格式填写脚本',
  225. type: 'warning'
  226. })
  227. }
  228. }
  229. }
  230. })
  231. },
  232. conditionalExecution(condition, res, key) {
  233. if (condition) {
  234. switch (res[key]) {
  235. case 'dialog':
  236. this.customComponents = res
  237. this.openDialog = true
  238. break
  239. case 'url':
  240. window.open(res.url)
  241. break
  242. case 'tab':
  243. this.$router.push(res.url)
  244. break
  245. default:
  246. break
  247. }
  248. }
  249. },
  250. // TODO: 待完善
  251. runGroovy(prop, scrip) {
  252. var formData = this.formData
  253. const str = JSON.parse(JSON.stringify(scrip))
  254. const arr = []
  255. const vars = {}
  256. for (var i in formData.models) {
  257. if (str.indexOf(i) !== -1) {
  258. arr.push(str.substring(str.indexOf(i), str.indexOf(i) + i.length))
  259. }
  260. }
  261. arr.forEach(a => {
  262. vars[a] = formData.models[a]
  263. })
  264. if (this.$utils.isNotEmpty(scrip)) {
  265. getScriptValue({
  266. script: scrip,
  267. vars: JSON.stringify(vars) || ''
  268. }).then(res => {
  269. this[scrip + 'Data'] = res.content
  270. if (prop === 'text') {
  271. this.showText = res.data || '暂无数据'
  272. }
  273. this.$message({
  274. showClose: true,
  275. message: '动态脚本执行成功!',
  276. type: 'success'
  277. })
  278. }).catch(err => {
  279. console.log(err)
  280. })
  281. }
  282. },
  283. click() {
  284. this.setVal(this.linkType, 'link', this.link, 'click')
  285. }
  286. }
  287. }
  288. </script>