index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <div class="h-100 position-relative">
  3. <div class="dashboard-card-wrap">
  4. <div class="dashboard-card-header">
  5. <div class="dashboard-card-header-left">
  6. {{ form.fd.name || $t('dashboard.text_6') }}<a-icon class="ml-2" type="loading" v-if="loading" />
  7. <span v-if="isResDeny" class="ml-2"><a-icon class="warning-color mr-1" type="warning" />{{ $t('common.permission.403') }}</span>
  8. </div>
  9. <div class="dashboard-card-header-right">
  10. <slot name="actions" :handle-edit="handleEdit" />
  11. <router-link v-if="!edit" to="/notice" class="ml-2">
  12. <icon type="arrow-right" style="font-size:18px" />
  13. </router-link>
  14. </div>
  15. </div>
  16. <div class="dashboard-card-body flex-column mini-text justify-content-center">
  17. <template v-if="data && data[0]">
  18. <div class="flex-fill position-relative">
  19. <div class="dashboard-fco-wrap">{{ data[0]['content'] }}</div>
  20. </div>
  21. <div class="d-flex flex-shrink-0 flex-grow-0 mt-1">
  22. <div class="text-color-help flex-fill">{{ data[0]['author'] }} · {{ $moment(data[0]['updated_at']).fromNow() }}</div>
  23. <div class="flex-shrink-0 flex-grow-0" @click.stop.prevent="doLike" style="cursor: pointer;">{{ readmarkTotal }}<a-icon class="ml-1" type="like" :style="{ color: likeColor }" :theme="liked ? 'filled' : 'outlined'" /></div>
  24. </div>
  25. </template>
  26. </div>
  27. </div>
  28. <base-drawer :visible.sync="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit">
  29. <a-form
  30. hideRequiredMark
  31. :form="form.fc"
  32. v-bind="formItemLayout">
  33. <a-form-item :label="$t('dashboard.text_6')">
  34. <a-input v-decorator="decorators.name" />
  35. </a-form-item>
  36. </a-form>
  37. </base-drawer>
  38. </div>
  39. </template>
  40. <script>
  41. import { mapGetters } from 'vuex'
  42. import BaseDrawer from '@Dashboard/components/BaseDrawer'
  43. // import { load } from '@Dashboard/utils/cache'
  44. import { getRequestT } from '@/utils/utils'
  45. import { hasPermission } from '@/utils/auth'
  46. export default {
  47. name: 'Notify',
  48. components: {
  49. BaseDrawer,
  50. },
  51. props: {
  52. options: {
  53. type: Object,
  54. required: true,
  55. },
  56. params: Object,
  57. edit: Boolean,
  58. dataRangeParams: {
  59. type: Object,
  60. },
  61. },
  62. data () {
  63. const initialNameValue = (this.params && this.params.name) || this.$t('dashboard.text_19')
  64. return {
  65. data: [],
  66. readmarkData: {},
  67. visible: false,
  68. loading: false,
  69. form: {
  70. fc: this.$form.createForm(this),
  71. fd: {
  72. name: initialNameValue,
  73. },
  74. },
  75. decorators: {
  76. name: [
  77. 'name',
  78. {
  79. initialValue: initialNameValue,
  80. rules: [
  81. { required: true, message: this.$t('dashboard.text_8') },
  82. ],
  83. },
  84. ],
  85. },
  86. formItemLayout: {
  87. wrapperCol: {
  88. span: 18,
  89. },
  90. labelCol: {
  91. span: 6,
  92. },
  93. },
  94. }
  95. },
  96. computed: {
  97. ...mapGetters(['userInfo', 'scope', 'isAdminMode', 'themeColor', 'permission']),
  98. readmarkTotal () {
  99. return this.readmarkData.total || 0
  100. },
  101. liked () {
  102. const data = this.readmarkData.data || []
  103. return data.some(item => item.user_id === this.userInfo.id)
  104. },
  105. likeColor () {
  106. return this.themeColor
  107. },
  108. isResDeny () {
  109. return !hasPermission({ key: 'notices_list', permissionData: this.permission })
  110. },
  111. },
  112. watch: {
  113. 'form.fd' (val) {
  114. for (const key in this.decorators) {
  115. let config = this.decorators[key][1] || {}
  116. config = {
  117. ...config,
  118. initialValue: val[key],
  119. }
  120. this.decorators[key][1] = config
  121. }
  122. },
  123. data (val) {
  124. if (val && val.length) {
  125. this.fetchReadmarks()
  126. }
  127. },
  128. 'dataRangeParams.scope': {
  129. handler (val) {
  130. this.fetchNotices()
  131. },
  132. immediate: true,
  133. },
  134. 'dataRangeParams.domain': {
  135. handler (val) {
  136. this.fetchNotices()
  137. },
  138. immediate: true,
  139. },
  140. 'dataRangeParams.project': {
  141. handler (val) {
  142. this.fetchNotices()
  143. },
  144. immediate: true,
  145. },
  146. },
  147. destroyed () {
  148. this.rm = null
  149. },
  150. created () {
  151. this.rm = new this.$Manager('readmarks', 'v1')
  152. this.noticesManager = new this.$Manager('notices', 'v1')
  153. this.fetchNotices()
  154. this.$emit('update', this.options.i, {
  155. name: this.form.fd.name,
  156. })
  157. },
  158. methods: {
  159. refresh () {
  160. return this.fetchNotices()
  161. },
  162. async fetchNotices () {
  163. if (this.isResDeny) return
  164. this.loading = true
  165. try {
  166. // const data = await load({
  167. // res: 'notices',
  168. // apiVersion: 'v1',
  169. // action: 'list',
  170. // actionArgs: {
  171. // params: {
  172. // scope: this.$store.getters.scope,
  173. // $t: getRequestT(),
  174. // },
  175. // },
  176. // resPath: 'data.data',
  177. // })
  178. const params = {
  179. scope: this.$store.getters.scope,
  180. $t: getRequestT(),
  181. }
  182. if (this.isAdminMode) {
  183. if (this.dataRangeParams?.scope === 'system') {
  184. params.visible_scope = 'system'
  185. }
  186. if (this.dataRangeParams?.scope === 'domain' && this.dataRangeParams?.domain) {
  187. params.scope = 'domain'
  188. params.domain_id = this.dataRangeParams?.domain
  189. }
  190. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  191. params.scope = 'project'
  192. params.project_id = this.dataRangeParams?.project
  193. }
  194. }
  195. if (this.isDomainMode) {
  196. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  197. params.scope = 'project'
  198. params.project_id = this.dataRangeParams?.project
  199. }
  200. }
  201. const response = await this.noticesManager.list({
  202. params,
  203. })
  204. this.data = response.data.data || []
  205. } finally {
  206. this.loading = false
  207. }
  208. },
  209. async fetchReadmarks () {
  210. if (this.isResDeny) return
  211. try {
  212. const response = await this.rm.list({
  213. params: {
  214. notice_id: this.data[0].id,
  215. $t: getRequestT(),
  216. },
  217. })
  218. this.readmarkData = response.data
  219. } catch (error) {
  220. throw error
  221. }
  222. },
  223. async doLike () {
  224. if (this.liked) {
  225. return
  226. }
  227. try {
  228. await this.rm.create({
  229. data: {
  230. notice_id: this.data[0].id,
  231. scope: this.scope,
  232. },
  233. })
  234. this.fetchReadmarks()
  235. } catch (error) {
  236. throw error
  237. }
  238. },
  239. handleEdit () {
  240. this.visible = true
  241. },
  242. async handleSubmit () {
  243. try {
  244. const values = await this.form.fc.validateFields()
  245. this.form.fd = values
  246. this.$emit('update', this.options.i, values)
  247. this.visible = false
  248. } catch (error) {
  249. throw error
  250. }
  251. },
  252. },
  253. }
  254. </script>