index.vue 5.4 KB

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