fortune.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="ss-p-x-30 ss-p-y-30">
  3. <!-- 黄历信息 -->
  4. <view class="popupContent">
  5. <view class="content-box">
  6. <view class="text-center ss-p-y-30">
  7. <view class="yangli">{{ almanac.yangli }}</view>
  8. <view class="yinli">{{ almanac.yinli }}</view>
  9. <view class="d-flex align-center justify-center">
  10. <span style="color: #7f7f7f;">幸运色</span>
  11. <span v-if="almanac.color === '白'" class="ss-m-l-20" style="color: #7f7f7f;">白色</span>
  12. <view
  13. v-else
  14. class="luckyColor ss-m-l-20"
  15. :style="{ 'background-color': `${colorDict[almanac.color] || '#2979ff'}` }"
  16. ></view>
  17. </view>
  18. </view>
  19. <view class="center-box ss-p-y-30 ss-p-x-30">
  20. <view class="d-flex">
  21. <view class="yi d-flex align-center justify-center">宜</view>
  22. <view class="font-size-15" style="flex: 1; color: #c6b393;">{{ almanac.yi }}</view>
  23. </view>
  24. <view class="d-flex ss-m-t-30">
  25. <view class="ji d-flex align-center justify-center">忌</view>
  26. <view class="font-size-15" style="flex: 1; color: #7f7f7f;">{{ almanac.ji }}</view>
  27. </view>
  28. </view>
  29. <view class="bottom-box">
  30. <view class="bottom-box-item d-flex flex-column align-center justify-center">
  31. <view class="padding">
  32. <view class="label">五行</view>
  33. <view class="value">{{ almanac.wuxing }}</view>
  34. </view>
  35. <view class="border"></view>
  36. <view class="padding">
  37. <view class="label">吉神</view>
  38. <view class="value">{{ almanac.jishen }}</view>
  39. </view>
  40. </view>
  41. <view class="bottom-box-item d-flex align-center justify-center">
  42. <view class="padding">
  43. <view class="label">彭祖</view>
  44. <view class="value">{{ almanac.baiji }}</view>
  45. </view>
  46. </view>
  47. <view class="bottom-box-item d-flex flex-column align-center justify-center">
  48. <view class="padding">
  49. <view class="label">冲煞</view>
  50. <view class="value">{{ almanac.chongsha }}</view>
  51. </view>
  52. <view class="border"></view>
  53. <view class="padding">
  54. <view class="label">凶神</view>
  55. <view class="value">{{ almanac.xiongshen }}</view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. <view class="journal">
  62. <uni-section title="手账" type="line"></uni-section>
  63. <uni-card v-if="journal && journal?.notes && !isEdit" @click="handleUpdateJournal">
  64. <view class="ss-m-t-15">{{ journal.notes }}</view>
  65. </uni-card>
  66. <view v-if="isEdit" class="ss-m-t-30 journal-content">
  67. <uni-easyinput
  68. type="textarea"
  69. v-model="notes"
  70. clearable
  71. maxlength="500"
  72. autoHeight
  73. placeholder="请输入要记录的内容"
  74. />
  75. <view v-if="notes?.length" style="text-align: end;" class="ss-m-t-10 color-999">{{ notes?.length || 0 }}/500</view>
  76. <view class="f-horizon-center" v-if="notes?.length">
  77. <button class="commonBtnStyle cancel-button" size="default" @click="handleCancel">取 消</button>
  78. <button
  79. size="default"
  80. :class="{'save-button': notes, 'commonBtnStyle': notes, 'send-button': !notes}"
  81. @click="handleSubmit"
  82. :disabled="disabled"
  83. >保 存</button>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script setup>
  90. import { ref } from 'vue'
  91. import { onLoad, onUnload } from '@dcloudio/uni-app'
  92. import { getDrawLots, getCalendarRecord, saveCalendarRecord } from '@/api/drawLots.js'
  93. const colorDict = {
  94. '白': '#FAFAFA',
  95. '黑': '#212121',
  96. '绿': '#43A047',
  97. '红': '#F44336',
  98. '黄': '#FDD835'
  99. }
  100. const isEdit = ref(false)
  101. const date = ref(null)
  102. const notes = ref(null) // 手账内容
  103. const journal = ref({}) // 当前日期手账记录
  104. const disabled = ref(false)
  105. const deleteDisabled = ref(false)
  106. const userInfo = ref(uni.getStorageSync('wechat_user'))
  107. // 获取黄历信息
  108. const almanac = ref({})
  109. const getCalendarInfo = async () => {
  110. try {
  111. const { result } = await getDrawLots({ date: date.value })
  112. if (!result) return uni.showToast({ title: '黄历信息获取失败', icon: 'none' })
  113. almanac.value = result || {}
  114. // 没有编辑手账时,只查看黄历的需要在返回上一页时保存黄历信息
  115. await handleSaveAlmanac()
  116. } catch (error) {
  117. console.log(error, '黄历-错误信息')
  118. }
  119. }
  120. // 获取手账记录
  121. const calendarRecord = ref([])
  122. const getCalendarData = async (month_key) => {
  123. try {
  124. const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
  125. calendarRecord.value = result?.calendar_content || []
  126. // 当前日期手账记录
  127. journal.value = calendarRecord.value.find(item => item.date === date.value) || {}
  128. // 在手账记录中有黄历信息的则直接展示,没有则从接口获取
  129. if (journal.value?.almanac) almanac.value = journal.value.almanac
  130. else getCalendarInfo()
  131. // 没有手账记录的时候,默认为编辑状态展示输入框
  132. if (!journal.value?.notes) {
  133. isEdit.value = true
  134. }
  135. } catch {}
  136. }
  137. onLoad((options) => {
  138. date.value = options?.date || null
  139. // 获取手账记录
  140. getCalendarData(options?.date.slice(0, 7))
  141. })
  142. const handleSaveAlmanac = async () => {
  143. if (journal.value.almanac) return
  144. const index = calendarRecord.value.findIndex(item => item.date === date.value)
  145. if (index === -1) {
  146. calendarRecord.value.push({
  147. date: date.value,
  148. almanac: almanac.value,
  149. notes: '',
  150. events: []
  151. })
  152. } else calendarRecord.value[index].almanac = almanac.value
  153. await saveCalendarRecord({
  154. openid: userInfo.value.openid,
  155. month_key: date.value.slice(0, 7),
  156. calendar_content: calendarRecord.value
  157. })
  158. }
  159. onUnload(async () => {
  160. console.log('onUnload')
  161. // 没有编辑手账时,只查看黄历的需要在返回上一页时保存黄历信息
  162. // await handleSaveAlmanac()
  163. })
  164. const handleUpdateJournal = () => {
  165. isEdit.value = true
  166. notes.value = journal.value?.notes || ''
  167. }
  168. const handleCancel = () => {
  169. isEdit.value = !journal.value?.notes ? true : false
  170. notes.value = null
  171. }
  172. // 删除
  173. const handleDelete = async () => {
  174. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  175. if (index === -1) return uni.showToast({ title: '删除失败,当前数据不存在', icon: 'none', duration: 2000 })
  176. calendarRecord.value.splice(index, 1)
  177. deleteDisabled.value = true
  178. disabled.value = true
  179. try {
  180. await saveCalendarRecord({ openid: userInfo.value.openid, month_key: date.value.slice(0, 7), calendar_content: calendarRecord.value })
  181. uni.showToast({ title: '删除成功', icon: 'none', duration: 2000 })
  182. setTimeout(() => {
  183. uni.navigateBack({
  184. delta: 1
  185. })
  186. disabled.value = false
  187. deleteDisabled.value = false
  188. }, 1000)
  189. } catch {
  190. deleteDisabled.value = false
  191. disabled.value = false
  192. }
  193. }
  194. // 保存
  195. const handleSubmit = async () => {
  196. if (!notes.value) return uni.showToast({ title: '请输入要记录的内容', icon: 'none', duration: 2000 })
  197. disabled.value = true
  198. let list = []
  199. if (!calendarRecord.value.length) {
  200. // 新增手账
  201. list = [{ date: date.value, events: [], notes: notes.value, almanac: almanac.value }]
  202. } else {
  203. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  204. if (index > -1) {
  205. calendarRecord.value[index].notes = notes.value
  206. // 没有黄历信息的,保存黄历信息
  207. if (!calendarRecord.value[index].almanac) calendarRecord.value[index].almanac = almanac.value
  208. } else {
  209. calendarRecord.value.push({ date: date.value, events: [], notes: notes.value, almanac: almanac.value })
  210. }
  211. list = calendarRecord.value
  212. }
  213. const params = {
  214. openid: userInfo.value.openid,
  215. month_key: date.value.slice(0, 7),
  216. calendar_content: list
  217. }
  218. console.log(params, 'params')
  219. try {
  220. await saveCalendarRecord(params)
  221. uni.showToast({ title: '保存成功', icon: 'none', duration: 2000 })
  222. disabled.value = false
  223. isEdit.value = false
  224. notes.value = null
  225. getCalendarData(date.value.slice(0, 7))
  226. } catch {
  227. disabled.value = false
  228. }
  229. }
  230. </script>
  231. <style lang="scss" scoped>
  232. $commonColor: #c2a08c;
  233. $size: 25px;
  234. :deep {
  235. .uni-card {
  236. margin: 30rpx 0 !important;
  237. }
  238. }
  239. .journal {
  240. margin-top: 30rpx;
  241. padding-bottom: 100px;
  242. .title {
  243. color: #999;
  244. margin: 0 30px;
  245. }
  246. .description {
  247. display: -webkit-box;
  248. -webkit-box-orient: vertical;
  249. -webkit-line-clamp: 2;
  250. overflow: hidden;
  251. }
  252. &-content {
  253. background-color: #f1f1f1;
  254. padding: 30rpx;
  255. border-radius: 8px;
  256. }
  257. }
  258. .popupContent {
  259. position: relative;
  260. background-color: #fff;
  261. width: 80vw;
  262. margin: 0 auto;
  263. .content-box {
  264. height: 100%;
  265. border: 1px solid $commonColor;
  266. border-radius: 6px;
  267. }
  268. .yangli {
  269. font-weight: bold;
  270. color: $commonColor;
  271. font-size: 30px;
  272. }
  273. .yinli {
  274. color: #7f7f7f;
  275. margin: 10px 0;
  276. }
  277. .center-box {
  278. border-top: 1px solid $commonColor;
  279. border-bottom: 1px solid $commonColor;
  280. .yi {
  281. width: $size;
  282. height: $size;
  283. background-color: #c39c87;
  284. border-radius: 50%;
  285. color: #fff;
  286. margin-right: 10px;
  287. font-size: 14px;
  288. }
  289. .ji {
  290. width: $size;
  291. height: $size;
  292. background-color: #bfbfbf;
  293. border-radius: 50%;
  294. color: #fff;
  295. margin-right: 10px;
  296. font-size: 14px;
  297. }
  298. }
  299. .bottom-box {
  300. display: flex;
  301. &-item {
  302. width: 33.3%;
  303. text-align: center;
  304. border-right: 1px solid $commonColor;
  305. &:nth-child(3n) {
  306. border-right: none;
  307. }
  308. .label {
  309. font-size: 17px;
  310. }
  311. .value {
  312. color: #7f7f7f;
  313. font-size: 14px;
  314. margin-top: 10px;
  315. }
  316. .padding {
  317. padding: 10px;
  318. }
  319. .border {
  320. border-top: 1px solid #c2a08c;
  321. height: 1px;
  322. width: 100%;
  323. }
  324. }
  325. }
  326. .luckyColor {
  327. width: 50rpx;
  328. height: 50rpx;
  329. border-radius: 50%;
  330. }
  331. }
  332. </style>