fortune.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 inputType = ref('text')
  101. const isEdit = ref(false)
  102. const date = ref(null)
  103. const notes = ref(null) // 手账内容
  104. const journal = ref({}) // 当前日期手账记录
  105. const disabled = ref(false)
  106. const deleteDisabled = ref(false)
  107. const userInfo = ref(uni.getStorageSync('wechat_user'))
  108. // 获取黄历信息
  109. const almanac = ref({})
  110. const getCalendarInfo = async () => {
  111. try {
  112. const { result } = await getDrawLots({ date: date.value })
  113. if (!result) return uni.showToast({ title: '黄历信息获取失败', icon: 'none' })
  114. almanac.value = result || {}
  115. // 没有编辑手账时,只查看黄历的需要在返回上一页时保存黄历信息
  116. await handleSaveAlmanac()
  117. } catch (error) {
  118. console.log(error, '黄历-错误信息')
  119. }
  120. }
  121. // 获取手账记录
  122. const calendarRecord = ref([])
  123. const getCalendarData = async (month_key) => {
  124. try {
  125. const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
  126. calendarRecord.value = result?.calendar_content || []
  127. // 当前日期手账记录
  128. journal.value = calendarRecord.value.find(item => item.date === date.value) || {}
  129. // 在手账记录中有黄历信息的则直接展示,没有则从接口获取
  130. if (journal.value?.almanac) almanac.value = journal.value.almanac
  131. else getCalendarInfo()
  132. // 没有手账记录的时候,默认为编辑状态展示输入框
  133. if (!journal.value?.notes) {
  134. isEdit.value = true
  135. inputType.value = 'text'
  136. }
  137. } catch {}
  138. }
  139. onLoad((options) => {
  140. date.value = options?.date || null
  141. // 获取手账记录
  142. getCalendarData(options?.date.slice(0, 7))
  143. })
  144. const handleSaveAlmanac = async () => {
  145. if (journal.value.almanac) return
  146. const index = calendarRecord.value.findIndex(item => item.date === date.value)
  147. if (index === -1) {
  148. calendarRecord.value.push({
  149. date: date.value,
  150. almanac: almanac.value,
  151. notes: '',
  152. events: []
  153. })
  154. } else calendarRecord.value[index].almanac = almanac.value
  155. await saveCalendarRecord({
  156. openid: userInfo.value.openid,
  157. month_key: date.value.slice(0, 7),
  158. calendar_content: calendarRecord.value
  159. })
  160. }
  161. onUnload(async () => {
  162. console.log('onUnload')
  163. // 没有编辑手账时,只查看黄历的需要在返回上一页时保存黄历信息
  164. // await handleSaveAlmanac()
  165. })
  166. const handleUpdateJournal = () => {
  167. isEdit.value = true
  168. inputType.value = 'textarea'
  169. notes.value = journal.value?.notes || ''
  170. }
  171. const handleInput = () => {
  172. if (!notes.value) inputType.value = 'textarea'
  173. }
  174. const handleCancel = () => {
  175. isEdit.value = !journal.value?.notes ? true : false
  176. inputType.value = 'text'
  177. notes.value = null
  178. }
  179. // 删除
  180. const handleDelete = async () => {
  181. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  182. if (index === -1) return uni.showToast({ title: '删除失败,当前数据不存在', icon: 'none', duration: 2000 })
  183. calendarRecord.value.splice(index, 1)
  184. deleteDisabled.value = true
  185. disabled.value = true
  186. try {
  187. await saveCalendarRecord({ openid: userInfo.value.openid, month_key: date.value.slice(0, 7), calendar_content: calendarRecord.value })
  188. uni.showToast({ title: '删除成功', icon: 'none', duration: 2000 })
  189. setTimeout(() => {
  190. uni.navigateBack({
  191. delta: 1
  192. })
  193. disabled.value = false
  194. deleteDisabled.value = false
  195. }, 1000)
  196. } catch {
  197. deleteDisabled.value = false
  198. disabled.value = false
  199. }
  200. }
  201. // 保存
  202. const handleSubmit = async () => {
  203. if (!notes.value) return uni.showToast({ title: '请输入要记录的内容', icon: 'none', duration: 2000 })
  204. disabled.value = true
  205. let list = []
  206. if (!calendarRecord.value.length) {
  207. // 新增手账
  208. list = [{ date: date.value, events: [], notes: notes.value, almanac: almanac.value }]
  209. } else {
  210. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  211. if (index > -1) {
  212. calendarRecord.value[index].notes = notes.value
  213. // 没有黄历信息的,保存黄历信息
  214. if (!calendarRecord.value[index].almanac) calendarRecord.value[index].almanac = almanac.value
  215. } else {
  216. calendarRecord.value.push({ date: date.value, events: [], notes: notes.value, almanac: almanac.value })
  217. }
  218. list = calendarRecord.value
  219. }
  220. const params = {
  221. openid: userInfo.value.openid,
  222. month_key: date.value.slice(0, 7),
  223. calendar_content: list
  224. }
  225. console.log(params, 'params')
  226. try {
  227. await saveCalendarRecord(params)
  228. uni.showToast({ title: '保存成功', icon: 'none', duration: 2000 })
  229. disabled.value = false
  230. handleCancel()
  231. getCalendarData(date.value.slice(0, 7))
  232. } catch {
  233. disabled.value = false
  234. }
  235. }
  236. </script>
  237. <style lang="scss" scoped>
  238. $commonColor: #c2a08c;
  239. $size: 25px;
  240. :deep {
  241. .uni-card {
  242. margin: 30rpx 0 !important;
  243. }
  244. }
  245. .journal {
  246. margin-top: 30rpx;
  247. padding-bottom: 100px;
  248. .title {
  249. color: #999;
  250. margin: 0 30px;
  251. }
  252. .description {
  253. display: -webkit-box;
  254. -webkit-box-orient: vertical;
  255. -webkit-line-clamp: 2;
  256. overflow: hidden;
  257. }
  258. &-content {
  259. background-color: #f1f1f1;
  260. padding: 30rpx;
  261. border-radius: 8px;
  262. }
  263. }
  264. .popupContent {
  265. position: relative;
  266. background-color: #fff;
  267. width: 80vw;
  268. margin: 0 auto;
  269. .content-box {
  270. height: 100%;
  271. border: 1px solid $commonColor;
  272. border-radius: 6px;
  273. }
  274. .yangli {
  275. font-weight: bold;
  276. color: $commonColor;
  277. font-size: 30px;
  278. }
  279. .yinli {
  280. color: #7f7f7f;
  281. margin: 10px 0;
  282. }
  283. .center-box {
  284. border-top: 1px solid $commonColor;
  285. border-bottom: 1px solid $commonColor;
  286. .yi {
  287. width: $size;
  288. height: $size;
  289. background-color: #c39c87;
  290. border-radius: 50%;
  291. color: #fff;
  292. margin-right: 10px;
  293. font-size: 14px;
  294. }
  295. .ji {
  296. width: $size;
  297. height: $size;
  298. background-color: #bfbfbf;
  299. border-radius: 50%;
  300. color: #fff;
  301. margin-right: 10px;
  302. font-size: 14px;
  303. }
  304. }
  305. .bottom-box {
  306. display: flex;
  307. &-item {
  308. width: 33.3%;
  309. text-align: center;
  310. border-right: 1px solid $commonColor;
  311. &:nth-child(3n) {
  312. border-right: none;
  313. }
  314. .label {
  315. font-size: 17px;
  316. }
  317. .value {
  318. color: #7f7f7f;
  319. font-size: 14px;
  320. margin-top: 10px;
  321. }
  322. .padding {
  323. padding: 10px;
  324. }
  325. .border {
  326. border-top: 1px solid #c2a08c;
  327. height: 1px;
  328. width: 100%;
  329. }
  330. }
  331. }
  332. .luckyColor {
  333. width: 50rpx;
  334. height: 50rpx;
  335. border-radius: 50%;
  336. }
  337. }
  338. </style>