indexCopy.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <layout-page>
  3. <view style="position: relative;">
  4. <!-- 日历 -->
  5. <Calendar
  6. :date="date"
  7. class="uni-calendar--hook"
  8. :selected="selected"
  9. :lunar="true"
  10. :range="false"
  11. :showMonth="true"
  12. @change="handleChange"
  13. @monthSwitch="handleMonthSwitch"
  14. />
  15. <!-- <button
  16. size="default"
  17. class="send-button"
  18. @click="handleClear"
  19. >
  20. 清除缓存
  21. </button> -->
  22. <!-- 手账 -->
  23. <view class="journal" v-if="calendarRecord && calendarRecord.length > 0">
  24. <view class="title">手账</view>
  25. <uni-card v-for="(val, index) in calendarRecord.filter(item => item.notes)" :key="index" @tap="handleUpdateJournal(val)">
  26. <uni-section :title="val.date" type="line"></uni-section>
  27. <view class="description ss-m-t-15">{{ val.notes }}</view>
  28. </uni-card>
  29. </view>
  30. <!-- 添加手账 -->
  31. <view class="add-btn" @tap="handleAddJournal">
  32. <uni-icons type="plusempty" size="30" color="#fff"></uni-icons>
  33. </view>
  34. <!-- 黄历弹窗 -->
  35. <uni-popup ref="popupRef" type="dialog">
  36. <view class="popupContent">
  37. <view class="content-box">
  38. <view class="text-center ss-p-y-30">
  39. <view class="yangli">{{ calendarInfo.yangli }}</view>
  40. <view class="yinli">{{ calendarInfo.yinli }}</view>
  41. <view class="d-flex align-center justify-center">
  42. <span style="color: #7f7f7f;">幸运色</span>
  43. <span v-if="calendarInfo.color === '白'" class="ss-m-l-20" style="color: #7f7f7f;">白色</span>
  44. <view
  45. v-else
  46. class="luckyColor ss-m-l-20"
  47. :style="{ 'background-color': `${colorDict[calendarInfo.color] || '#2979ff'}` }"
  48. ></view>
  49. </view>
  50. </view>
  51. <view class="center-box ss-p-y-30 ss-p-x-30">
  52. <view class="d-flex">
  53. <view class="yi d-flex align-center justify-center">宜</view>
  54. <view class="font-size-15" style="flex: 1; color: #c6b393;">{{ calendarInfo.yi }}</view>
  55. </view>
  56. <view class="d-flex ss-m-t-30">
  57. <view class="ji d-flex align-center justify-center">忌</view>
  58. <view class="font-size-15" style="flex: 1; color: #7f7f7f;">{{ calendarInfo.ji }}</view>
  59. </view>
  60. </view>
  61. <view class="bottom-box">
  62. <view class="bottom-box-item d-flex flex-column align-center justify-center">
  63. <view class="padding">
  64. <view class="label">五行</view>
  65. <view class="value">{{ calendarInfo.wuxing }}</view>
  66. </view>
  67. <view class="border"></view>
  68. <view class="padding">
  69. <view class="label">吉神</view>
  70. <view class="value">{{ calendarInfo.jishen }}</view>
  71. </view>
  72. </view>
  73. <view class="bottom-box-item d-flex align-center justify-center">
  74. <view class="padding">
  75. <view class="label">彭祖</view>
  76. <view class="value">{{ calendarInfo.baiji }}</view>
  77. </view>
  78. </view>
  79. <view class="bottom-box-item d-flex flex-column align-center justify-center">
  80. <view class="padding">
  81. <view class="label">冲煞</view>
  82. <view class="value">{{ calendarInfo.chongsha }}</view>
  83. </view>
  84. <view class="border"></view>
  85. <view class="padding">
  86. <view class="label">凶神</view>
  87. <view class="value">{{ calendarInfo.xiongshen }}</view>
  88. </view>
  89. </view>
  90. </view>
  91. </view>
  92. </view>
  93. <view class="text-center ss-m-t-50">
  94. <uni-icons type="close" size="40" color="#fff" @click="handleClose"></uni-icons>
  95. </view>
  96. </uni-popup>
  97. </view>
  98. </layout-page>
  99. </template>
  100. <script setup>
  101. import { onLoad, onShow } from '@dcloudio/uni-app'
  102. import { ref } from 'vue'
  103. import Calendar from '@/components/uni-calendar/components/uni-calendar/uni-calendar.vue'
  104. import { getDrawLots } from '@/api/drawLots.js'
  105. import layoutPage from '@/layout'
  106. import { showAuthModal } from '@/hooks/useModal'
  107. import { getCalendarRecord, saveCalendarRecord } from '@/api/drawLots.js'
  108. const colorDict = {
  109. '白': '#FAFAFA',
  110. '黑': '#212121',
  111. '绿': '#43A047',
  112. '红': '#F44336',
  113. '黄': '#FDD835'
  114. }
  115. function getDate(date, AddDayCount = 0) {
  116. if (!date) {
  117. date = new Date()
  118. }
  119. if (typeof date !== 'object') {
  120. date = date.replace(/-/g, '/')
  121. }
  122. const dd = new Date(date)
  123. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  124. const y = dd.getFullYear()
  125. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  126. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  127. return {
  128. fullDate: y + '-' + m + '-' + d,
  129. year: y,
  130. month: m,
  131. date: d,
  132. day: dd.getDay()
  133. }
  134. }
  135. const date = ref(getDate(new Date()).fullDate)
  136. // 设置日期选中内容
  137. const selected = ref([])
  138. // 登录信息
  139. const userInfo = ref(uni.getStorageSync('wechat_user') || {})
  140. onLoad(() => {
  141. if (!userInfo.value || !userInfo.value?.openid) return showAuthModal()
  142. })
  143. onShow(() => {
  144. // 刷新一次本地登录信息,防止 onLoad 时未拿到最新登录态
  145. const latestUser = uni.getStorageSync('wechat_user')
  146. if (latestUser?.openid && latestUser?.openid !== userInfo.value?.openid) {
  147. userInfo.value = latestUser
  148. }
  149. if (userInfo.value?.openid) getCalendarData(date.value.slice(0, 7))
  150. })
  151. // 监听登录成功事件,立刻刷新数据
  152. uni.$off && uni.$off('auth:login')
  153. uni.$on && uni.$on('auth:login', (user) => {
  154. userInfo.value = user || uni.getStorageSync('wechat_user')
  155. if (userInfo.value?.openid) {
  156. getCalendarData(date.value.slice(0, 7))
  157. }
  158. })
  159. // const handleClear = () => {
  160. // selected.value = []
  161. // calendarRecord.value = []
  162. // userInfo.value = {}
  163. // uni.clearStorageSync()
  164. // showAuthModal()
  165. // }
  166. // 点击日期查看黄历信息
  167. const popupRef = ref()
  168. const calendarInfo = ref({})
  169. const handleChange = async (e) => {
  170. date.value = e.fulldate
  171. if (e.isBackToday) return // 点击左上角“今日”按钮不弹窗
  172. const index = calendarRecord.value.findIndex(val => val.date === e.fulldate)
  173. // 有存储过的黄历信息直接使用
  174. if (calendarRecord.value[index]?.almanac) {
  175. calendarInfo.value = calendarRecord.value[index].almanac
  176. popupRef.value.open()
  177. return
  178. }
  179. try {
  180. const { result } = await getDrawLots({ date: e.fulldate })
  181. if (!result) return uni.showToast({ title: '黄历信息获取失败', icon: 'none' })
  182. calendarInfo.value = result || {}
  183. // 将黄历信息存储到手账中
  184. if (calendarRecord.value?.length && userInfo.value?.openid) {
  185. const month_key = date.value.slice(0, 7)
  186. if (index > -1) {
  187. calendarRecord.value[index].almanac = result
  188. } else calendarRecord.value.push({ date: e.fulldate, notes: '', events: [], almanac: result })
  189. await saveCalendarRecord({
  190. openid: userInfo.value.openid,
  191. month_key,
  192. calendar_content: calendarRecord.value
  193. })
  194. getCalendarData(month_key)
  195. }
  196. popupRef.value.open()
  197. } catch (error) {
  198. console.log(error, '错误信息')
  199. }
  200. }
  201. // 获取手账记录
  202. const calendarRecord = ref([])
  203. const getCalendarData = async (month_key) => {
  204. try {
  205. const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
  206. calendarRecord.value = result?.calendar_content || []
  207. selected.value = calendarRecord.value?.length ? calendarRecord.value.map((item) => {
  208. return { date: item.date, color: item?.almanac?.color ? colorDict[item?.almanac?.color] : '#f8bbd0' }
  209. }) : []
  210. } catch {}
  211. }
  212. // 切换月份
  213. const handleMonthSwitch = (e) => {
  214. console.log(e, '切换月份')
  215. getCalendarData(`${e.year}-${e.month < 10 ? '0' + e.month : e.month}`)
  216. }
  217. const handleClose = () => {
  218. popupRef.value.close()
  219. calendarInfo.value = {}
  220. }
  221. // 添加手账
  222. const handleAddJournal = () => {
  223. let url = `/pages/drawLots/journal?date=${date.value}`
  224. const index = calendarRecord.value.findIndex((item) => item.date === date.value)
  225. if (index > -1 && calendarRecord.value[index].notes) {
  226. // uni.showToast({ title: '手账已存在,可进行编辑', icon: 'none' })
  227. url += `&notes=${calendarRecord.value[index].notes}`
  228. }
  229. uni.navigateTo({
  230. url
  231. })
  232. }
  233. // 更新手账
  234. const handleUpdateJournal = (val) => {
  235. uni.navigateTo({
  236. url: `/pages/drawLots/journal?date=${val.date}&notes=${val.notes}`
  237. })
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. $commonColor: #c2a08c;
  242. $size: 25px;
  243. :deep {
  244. .uni-card {
  245. border-radius: 10px !important;
  246. }
  247. }
  248. .journal {
  249. margin-top: 30rpx;
  250. padding-bottom: 100px;
  251. .title {
  252. color: #999;
  253. margin: 0 30px;
  254. }
  255. .description {
  256. display: -webkit-box;
  257. -webkit-box-orient: vertical;
  258. -webkit-line-clamp: 2;
  259. overflow: hidden;
  260. }
  261. }
  262. .add-btn {
  263. position: fixed;
  264. right: 20px;
  265. bottom: 50px;
  266. width: 60px;
  267. height: 60px;
  268. border-radius: 50%;
  269. background-color: #2979ff;
  270. display: flex;
  271. align-items: center;
  272. justify-content: center;
  273. }
  274. .popupContent {
  275. position: relative;
  276. background-color: #fff;
  277. width: 70vw;
  278. padding: 30rpx;
  279. .content-box {
  280. height: 100%;
  281. border: 1px solid $commonColor;
  282. border-radius: 6px;
  283. }
  284. .yangli {
  285. font-weight: bold;
  286. color: $commonColor;
  287. font-size: 30px;
  288. }
  289. .yinli {
  290. color: #7f7f7f;
  291. margin: 10px 0;
  292. }
  293. .center-box {
  294. border-top: 1px solid $commonColor;
  295. border-bottom: 1px solid $commonColor;
  296. .yi {
  297. width: $size;
  298. height: $size;
  299. background-color: #c39c87;
  300. border-radius: 50%;
  301. color: #fff;
  302. margin-right: 10px;
  303. font-size: 14px;
  304. }
  305. .ji {
  306. width: $size;
  307. height: $size;
  308. background-color: #bfbfbf;
  309. border-radius: 50%;
  310. color: #fff;
  311. margin-right: 10px;
  312. font-size: 14px;
  313. }
  314. }
  315. .bottom-box {
  316. display: flex;
  317. &-item {
  318. width: 33.3%;
  319. text-align: center;
  320. border-right: 1px solid $commonColor;
  321. &:nth-child(3n) {
  322. border-right: none;
  323. }
  324. .label {
  325. font-size: 17px;
  326. }
  327. .value {
  328. color: #7f7f7f;
  329. font-size: 14px;
  330. margin-top: 10px;
  331. }
  332. .padding {
  333. padding: 10px;
  334. }
  335. .border {
  336. border-top: 1px solid #c2a08c;
  337. height: 1px;
  338. width: 100%;
  339. }
  340. }
  341. }
  342. .luckyColor {
  343. width: 50rpx;
  344. height: 50rpx;
  345. border-radius: 50%;
  346. }
  347. }
  348. </style>