addModal.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. @register="register"
  5. :title="title"
  6. :width="1000"
  7. @visible-change="handleVisibleChange"
  8. @cancel="resetFields"
  9. @ok="handleSubmit"
  10. >
  11. <div class="pt-2px pr-3px">
  12. <BasicForm @register="registerForm" :model="model" >
  13. <template #text="{ model, field }">
  14. {{ model[field] }}
  15. </template>
  16. </BasicForm>
  17. </div>
  18. </BasicModal>
  19. </template>
  20. <script lang="ts">
  21. import { defineComponent, ref, nextTick, onMounted, reactive, h } from 'vue';
  22. import { BasicModal, useModalInner } from '/@/components/Modal';
  23. import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  24. import { useMessage } from '/@/hooks/web/useMessage';
  25. import { uploadApi } from '/@/api/product/index';
  26. import { newAddNews, NewsDetail, newUpdateNews } from '/@/api/operate'
  27. import { useI18n } from '/@/hooks/web/useI18n';
  28. import { Tinymce } from '/@/components/Tinymce/index';
  29. const { t } = useI18n();
  30. export default defineComponent({
  31. components: { BasicModal, BasicForm },
  32. props: {
  33. userData: { type: Object },
  34. },
  35. emits: ['update', 'register'],
  36. setup(props, { emit }) {
  37. const modelRef = ref({});
  38. const title = ref('新增新闻');
  39. const fileFlow = reactive({
  40. coverImageUrl:''
  41. })
  42. const { createMessage } = useMessage();
  43. const schemas: FormSchema[] = [
  44. {
  45. field: 'id',
  46. component: 'Input',
  47. show:false,
  48. label: 'id',
  49. required: false,
  50. },{
  51. field: 'title',
  52. component: 'Input',
  53. required: true,
  54. label: '新闻标题',
  55. colProps: {
  56. span: 22,
  57. },
  58. },{
  59. field: 'source',
  60. component: 'Input',
  61. label: '新闻来源',
  62. required: true,
  63. colProps: {
  64. span: 22,
  65. },
  66. },
  67. {
  68. field: 'coverImageUrl',
  69. component: 'Upload',
  70. label: '封面',
  71. required: true,
  72. rules: [{ required: true, message: t('common.uploadMessge') }],
  73. // helpMessage: t('routes.corporation.uploadHelp'),
  74. itemProps: {
  75. validateTrigger: 'onBlur',
  76. },
  77. componentProps: {
  78. api: uploadApi,
  79. maxNumber: 1,
  80. maxSize: 10,
  81. accept: ['jpeg','jpg','png'],
  82. afterFetch: function (data) {
  83. console.log('uploadApi',data)
  84. Reflect.set(data, 'url', data.data);
  85. fileFlow.coverImageUrl = data.data
  86. return data;
  87. },
  88. },
  89. colProps: {
  90. span: 22,
  91. },
  92. },
  93. {
  94. field: 'newType',
  95. label: '类型',
  96. component: 'RadioButtonGroup',
  97. required: true,
  98. defaultValue: 1,
  99. componentProps: {
  100. onChange: NewTypeChange,
  101. options: [
  102. { label: '链接', value: 1 },
  103. { label: '图文', value: 2 },
  104. ],
  105. },
  106. },{
  107. field: 'newsUrl',
  108. component: 'Input',
  109. label: '新闻链接',
  110. required: true,
  111. colProps: {
  112. span: 22,
  113. },
  114. },
  115. {
  116. field: 'content',
  117. label: '新闻内容',
  118. component: 'Input',
  119. required: true,
  120. ifShow:false,
  121. colProps: {
  122. span: 22,
  123. },
  124. render: ({ model, field }) => {
  125. return h(Tinymce, {
  126. value: model[field],
  127. maxlength: 200,
  128. onChange: (value: string) => {
  129. model[field] = value;
  130. },
  131. showImageUpload: true,
  132. });
  133. },
  134. },
  135. ];
  136. const [registerForm, { validate, resetFields, setFieldsValue, updateSchema }] = useForm({
  137. labelWidth: 120,
  138. schemas,
  139. showActionButtonGroup: false,
  140. actionColOptions: {
  141. span: 24,
  142. },
  143. });
  144. onMounted(() => {});
  145. let addListFunc = () => {};
  146. const [register, { closeModal }] = useModalInner((data) => {
  147. data && onDataReceive(data);
  148. });
  149. async function onDataReceive(data) {
  150. modelRef.value = data
  151. resetFields();
  152. if(data.newType){
  153. NewTypeChange(data.newType)
  154. }
  155. if(data.id){
  156. title.value = '编辑新闻'
  157. const detail = await NewsDetail({id:data.id})
  158. setFieldsValue({
  159. ...detail,
  160. coverImageUrl:detail.coverImageUrl?[detail.coverImageUrl]:''
  161. });
  162. }else{
  163. title.value = '新增新闻'
  164. setFieldsValue(data);
  165. }
  166. }
  167. function NewTypeChange(val){
  168. console.log('NewTypeChange',val)
  169. updateSchema([
  170. { field: 'content', ifShow:val == 2,},
  171. { field: 'newsUrl', ifShow:val != 2,},
  172. ])
  173. }
  174. const handleSubmit = async () => {
  175. const params = await validate();
  176. let apiUrl = title.value == '新增新闻'?newAddNews:newUpdateNews
  177. const apiData = {
  178. ...params as any,
  179. coverImageUrl:params.coverImageUrl && params.coverImageUrl[0],
  180. content:params.newType == 1?params.newsUrl:params.content
  181. }
  182. try {
  183. await apiUrl(apiData);
  184. closeModal();
  185. resetFields();
  186. createMessage.success(t('common.optSuccess'));
  187. emit('update');
  188. } catch (error) {
  189. console.log('not passing', error);
  190. }
  191. };
  192. function handleVisibleChange(v) {
  193. v && props.userData && nextTick(() => onDataReceive(props.userData));
  194. }
  195. return {
  196. register,
  197. schemas,
  198. registerForm,
  199. model: modelRef,
  200. fileFlow,
  201. NewTypeChange,
  202. handleVisibleChange,
  203. handleSubmit,
  204. addListFunc,
  205. resetFields,
  206. title,
  207. t,
  208. };
  209. },
  210. });
  211. </script>