fortune.vue 9.7 KB

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