journal.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <view class="ss-p-x-30 ss-p-y-30">
  3. <view class="ss-m-b-30">
  4. <uni-easyinput v-model="date" type="text" disabled />
  5. </view>
  6. <uni-easyinput type="textarea" v-model="journal" clearable maxlength="500" autoHeight placeholder="请输入要记录的内容" />
  7. <view style="text-align: end;" class="ss-m-t-10 color-999">{{ journal?.length || 0 }}/500</view>
  8. <view class="f-horizon-center">
  9. <button
  10. v-if="isEdit"
  11. size="default"
  12. class="delete-button commonBtnStyle"
  13. :disabled="deleteDisabled"
  14. @click="handleDelete"
  15. >删 除</button>
  16. <button
  17. size="default"
  18. :class="{'save-button': isEdit, 'commonBtnStyle': isEdit, 'send-button': !isEdit}"
  19. @click="handleSubmit"
  20. :disabled="disabled"
  21. >保 存</button>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup>
  26. import { ref } from 'vue'
  27. import { onLoad } from '@dcloudio/uni-app'
  28. import { getCalendarRecord, saveCalendarRecord } from '@/api/drawLots.js'
  29. const isEdit = ref(false)
  30. const date = ref(null)
  31. const journal = ref(null)
  32. const disabled = ref(false)
  33. const deleteDisabled = ref(false)
  34. const userInfo = ref(uni.getStorageSync('wechat_user'))
  35. // 获取手账记录
  36. const calendarRecord = ref([])
  37. const getCalendarData = async (month_key) => {
  38. try {
  39. const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
  40. calendarRecord.value = result?.calendar_content || []
  41. } catch {}
  42. }
  43. onLoad((options) => {
  44. date.value = options?.date || null
  45. journal.value = options?.notes || null
  46. isEdit.value = options?.notes ? true : false
  47. getCalendarData(options?.date.slice(0, 7))
  48. })
  49. // 删除
  50. const handleDelete = async () => {
  51. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  52. if (index === -1) return uni.showToast({ title: '删除失败,当前数据不存在', icon: 'none', duration: 2000 })
  53. calendarRecord.value.splice(index, 1)
  54. deleteDisabled.value = true
  55. disabled.value = true
  56. try {
  57. await saveCalendarRecord({ openid: userInfo.value.openid, month_key: date.value.slice(0, 7), calendar_content: calendarRecord.value })
  58. uni.showToast({ title: '删除成功', icon: 'none', duration: 2000 })
  59. setTimeout(() => {
  60. uni.navigateBack({
  61. delta: 1
  62. })
  63. disabled.value = false
  64. deleteDisabled.value = false
  65. }, 1000)
  66. } catch {
  67. deleteDisabled.value = false
  68. disabled.value = false
  69. }
  70. }
  71. // 保存
  72. const handleSubmit = async () => {
  73. if (!journal.value) return uni.showToast({ title: '请输入要记录的内容', icon: 'none', duration: 2000 })
  74. disabled.value = true
  75. let list = []
  76. if (!calendarRecord.value.length) {
  77. list = [{ date: date.value, events: [], notes: journal.value }]
  78. } else {
  79. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  80. if (index > -1) {
  81. calendarRecord.value[index].notes = journal.value
  82. } else {
  83. calendarRecord.value.push({ date: date.value, events: [], notes: journal.value })
  84. }
  85. list = calendarRecord.value
  86. }
  87. const params = {
  88. openid: userInfo.value.openid,
  89. month_key: date.value.slice(0, 7),
  90. calendar_content: list
  91. }
  92. console.log(params, 'params')
  93. try {
  94. await saveCalendarRecord(params)
  95. uni.showToast({ title: '保存成功', icon: 'none', duration: 2000 })
  96. setTimeout(() => {
  97. uni.navigateBack({
  98. delta: 1
  99. })
  100. disabled.value = false
  101. }, 1000)
  102. } catch {
  103. disabled.value = false
  104. }
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. </style>