6983df1487c67429e1f3143f275f95f6bbc09012.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <span v-if="syncToApp || syncToLocal">
  3. <j-third-app-dropdown v-if="enabledTypes.wechatEnterprise" type="wechatEnterprise" name="企微" v-bind="bindAttrs" v-on="bindEvents"/>
  4. <j-third-app-dropdown v-if="enabledTypes.dingtalk" type="dingtalk" name="钉钉" v-bind="bindAttrs" v-on="bindEvents"/>
  5. </span>
  6. <span v-else>未设置任何同步方向</span>
  7. </template>
  8. <script>
  9. import { getAction } from '@/api/manage'
  10. import { cloneObject } from '@/utils/util'
  11. import JThirdAppDropdown from './JThirdAppDropdown'
  12. const backEndUrl = {
  13. // 获取启用的第三方App
  14. getEnabledType: '/sys/thirdApp/getEnabledType',
  15. // 企业微信
  16. wechatEnterprise: {
  17. user: '/sys/thirdApp/sync/wechatEnterprise/user',
  18. depart: '/sys/thirdApp/sync/wechatEnterprise/depart',
  19. },
  20. // 钉钉
  21. dingtalk: {
  22. user: '/sys/thirdApp/sync/dingtalk/user',
  23. depart: '/sys/thirdApp/sync/dingtalk/depart',
  24. }
  25. }
  26. export default {
  27. name: 'JThirdAppButton',
  28. components: {JThirdAppDropdown},
  29. props: {
  30. // 同步类型,可以是 user、depart
  31. bizType: {
  32. type: String,
  33. required: true,
  34. },
  35. // 是否允许同步到第三方APP
  36. syncToApp: Boolean,
  37. // 是否允许第三方APP同步到本地
  38. syncToLocal: Boolean,
  39. // 选择的行
  40. selectedRowKeys: Array,
  41. },
  42. data() {
  43. return {
  44. enabledTypes: {},
  45. attrs: {
  46. dingtalk: {},
  47. },
  48. }
  49. },
  50. computed: {
  51. bindAttrs() {
  52. return {
  53. syncToApp: this.syncToApp,
  54. syncToLocal: this.syncToLocal
  55. }
  56. },
  57. bindEvents() {
  58. return {
  59. 'to-app': this.onToApp,
  60. 'to-local': this.onToLocal,
  61. }
  62. },
  63. },
  64. created() {
  65. this.loadEnabledTypes()
  66. },
  67. methods: {
  68. handleMenuClick() {
  69. console.log(arguments)
  70. },
  71. onToApp(e) {
  72. this.doSync(e.type, '/toApp')
  73. },
  74. onToLocal(e) {
  75. this.doSync(e.type, '/toLocal')
  76. },
  77. // 获取启用的第三方App
  78. async loadEnabledTypes() {
  79. this.enabledTypes = await loadEnabledTypes()
  80. },
  81. // 开始同步第三方App
  82. doSync(type, direction) {
  83. let urls = backEndUrl[type]
  84. if (!(urls && urls[this.bizType])) {
  85. console.warn('配置出错')
  86. return
  87. }
  88. let url = urls[this.bizType] + direction
  89. let selectedRowKeys = this.selectedRowKeys
  90. let content = '确定要开始同步全部数据吗?可能花费较长时间!'
  91. if (Array.isArray(selectedRowKeys) && selectedRowKeys.length > 0) {
  92. content = `确定要开始同步这 ${selectedRowKeys.length} 项吗?`
  93. } else {
  94. selectedRowKeys = []
  95. }
  96. return new Promise((resolve, reject) => {
  97. let model = this.$confirm({
  98. title: '同步',
  99. content,
  100. onOk: () => {
  101. model.update({
  102. keyboard: false,
  103. okText: '同步中…',
  104. cancelButtonProps: {props: {disabled: true}}
  105. })
  106. return getAction(url, {
  107. ids: selectedRowKeys.join(',')
  108. }).then(res => {
  109. let options = null
  110. if (res.result) {
  111. options = {
  112. width: 600,
  113. title: res.message,
  114. content: (h) => {
  115. let nodes
  116. let successInfo = [
  117. `成功信息如下:`,
  118. this.renderTextarea(h, res.result.successInfo.map((v, i) => `${i + 1}. ${v}`).join('\n')),
  119. ]
  120. if (res.success) {
  121. nodes = [
  122. ...successInfo,
  123. h('br'),
  124. `无失败信息!`,
  125. ]
  126. } else {
  127. nodes = [
  128. `失败信息如下:`,
  129. this.renderTextarea(h, res.result.failInfo.map((v, i) => `${i + 1}. ${v}`).join('\n')),
  130. h('br'),
  131. ...successInfo,
  132. ]
  133. }
  134. return nodes
  135. }
  136. }
  137. }
  138. if (res.success) {
  139. if (options != null) {
  140. this.$success(options)
  141. } else {
  142. this.$message.success(res.message)
  143. }
  144. this.$emit('sync-ok')
  145. } else {
  146. if (options != null) {
  147. this.$warning(options)
  148. } else {
  149. this.$message.warning(res.message)
  150. }
  151. this.$emit('sync-error')
  152. }
  153. }).catch(() => model.destroy()).finally(() => {
  154. resolve()
  155. this.$emit('sync-finally', {
  156. type,
  157. direction,
  158. isToApp: direction === '/toApp',
  159. isToLocal: direction === '/toLocal',
  160. })
  161. })
  162. },
  163. onCancel() {
  164. resolve()
  165. },
  166. })
  167. })
  168. },
  169. renderTextarea(h, value) {
  170. return h('a-textarea', {
  171. props: {
  172. value: value,
  173. readOnly: true,
  174. autosize: {minRows: 5, maxRows: 10},
  175. },
  176. style: {
  177. // 关闭textarea的自动换行,使其可以左右滚动
  178. whiteSpace: 'pre',
  179. overflow: 'auto',
  180. }
  181. })
  182. }
  183. },
  184. }
  185. // 启用了哪些第三方App(在此缓存)
  186. let enabledTypes = null
  187. // 获取启用的第三方App
  188. export async function loadEnabledTypes() {
  189. // 获取缓存
  190. if (enabledTypes != null) {
  191. return cloneObject(enabledTypes)
  192. } else {
  193. let {success, result} = await getAction(backEndUrl.getEnabledType)
  194. if (success) {
  195. // 在此缓存
  196. enabledTypes = cloneObject(result)
  197. return result
  198. } else {
  199. console.warn('getEnabledType查询失败:', res)
  200. }
  201. }
  202. return {}
  203. }
  204. </script>
  205. <style scoped>
  206. </style>