MyCalendarForm.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view>
  3. <cu-custom :backUrl="'/pages/calendar/MyCalendar'" :isBack="true" bgColor="bg-gradual-blue" >
  4. <block slot="content">日程内容</block>
  5. </cu-custom>
  6. <u--form :model="inputForm" labelWidth="100px" class="u-form" labelPosition="left" :rules="rules" ref="inputForm">
  7. <u-form-item label="日程内容" borderBottom prop="title" :required="true">
  8. <u--textarea v-model="inputForm.title" placeholder="请输入日程内容" border="none" style="border: 1px solid #ccc;"></u--textarea>
  9. </u-form-item>
  10. <u-form-item style="margin-top: 20px; text-align: center;" v-if="this.method === 'edit'">
  11. <el-button type="danger" @click="del" style="width: 100%;">
  12. <span style="margin: 0 auto;">删除</span>
  13. </el-button>
  14. </u-form-item>
  15. <u-form-item style="margin-top: 20px; text-align: center;">
  16. <el-button type="primary" @click="saveForm" style="width: 100%;">
  17. <span style="margin: 0 auto;">提交</span>
  18. </el-button>
  19. </u-form-item>
  20. </u--form>
  21. </view>
  22. </template>
  23. <script>
  24. import myCalendarService from "@/api/calendar/myCalendarService";
  25. import {mapState, mapMutations, mapActions} from 'vuex'
  26. export default {
  27. data() {
  28. return {
  29. title: "",
  30. method: "",
  31. visible: false,
  32. loading: false,
  33. inputForm: {
  34. id: "",
  35. title: "",
  36. startDate: "",
  37. endDate: "",
  38. userId: '',
  39. },
  40. rules: {
  41. 'title': [
  42. {
  43. required: true,
  44. message: '日程内容不能为空',
  45. trigger: ['blur', 'change']
  46. }],
  47. }
  48. };
  49. },
  50. computed: mapState({
  51. userInfo: (state) => state.user.userInfo,
  52. avatar: (state) => state.user.avatar
  53. }),
  54. onLoad(options) {
  55. // 从 URL 参数中获取传递过来的index
  56. this.method = options.method;
  57. this.inputForm.id = options.id;
  58. this.inputForm.startDate = options.startDate;
  59. this.inputForm.endDate = options.endDate;
  60. this.init(this.method,this.inputForm.id,this.inputForm.startDate,this.inputForm.endDate)
  61. },
  62. methods: {
  63. init(method, id, startDate, endDate) {
  64. this.method = method;
  65. this.inputForm.id = id;
  66. if (method === "add") {
  67. this.title = `新建日程`;
  68. } else if (method === "edit") {
  69. this.title = "修改日程";
  70. } else if (method === "view") {
  71. this.title = "查看日程";
  72. }
  73. this.visible = true;
  74. this.loading = false;
  75. this.$nextTick(() => {
  76. this.$refs.inputForm.resetFields();
  77. if (method === "edit" || method === "view") {
  78. // 修改或者查看
  79. this.loading = true;
  80. myCalendarService
  81. .queryById(this.inputForm.id)
  82. .then((data) => {
  83. this.inputForm = this.recover(this.inputForm, data);
  84. this.loading = false;
  85. });
  86. } else {
  87. this.inputForm.startDate = this.formatStartDate(startDate)
  88. this.inputForm.endDate = this.formatStartDate(endDate)
  89. }
  90. });
  91. },
  92. del() {
  93. uni.showLoading()
  94. myCalendarService
  95. .delete(this.inputForm.id)
  96. .then((data) => {
  97. // 接口调用成功,隐藏加载动画
  98. uni.hideLoading();
  99. uni.showToast({
  100. title: data,
  101. icon: "none",
  102. duration:2000
  103. })
  104. uni.navigateTo({
  105. url: '/pages/calendar/MyCalendar' // DialogPage 对应的页面路径
  106. });
  107. })
  108. .catch(() => {
  109. this.loading = false;
  110. });
  111. },
  112. // 表单提交
  113. saveForm() {
  114. this.inputForm.userId = this.userInfo.id
  115. this.$refs.inputForm.validate().then(res => {
  116. uni.showLoading()
  117. myCalendarService
  118. .save(this.inputForm)
  119. .then((data) => {
  120. // 接口调用成功,隐藏加载动画
  121. uni.hideLoading();
  122. uni.showToast({
  123. title: data,
  124. icon: "none",
  125. duration:2000
  126. })
  127. uni.navigateTo({
  128. url: '/pages/calendar/MyCalendar' // DialogPage 对应的页面路径
  129. });
  130. })
  131. .catch(() => {
  132. this.loading = false;
  133. });
  134. }).catch((e)=>{
  135. })
  136. },
  137. formatStartDate(date) {
  138. const formattedDate = new Date(date);
  139. const year = formattedDate.getFullYear();
  140. const month = String(formattedDate.getMonth() + 1).padStart(2, '0');
  141. const day = String(formattedDate.getDate()).padStart(2, '0');
  142. const hours = String(formattedDate.getHours()).padStart(2, '0');
  143. const minutes = String(formattedDate.getMinutes()).padStart(2, '0');
  144. const seconds = String(formattedDate.getSeconds()).padStart(2, '0');
  145. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  146. },
  147. },
  148. };
  149. </script>