index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <layout-page>
  3. <Calendar
  4. :date="date"
  5. class="uni-calendar--hook"
  6. :selected="selected"
  7. :lunar="true"
  8. :range="false"
  9. :showMonth="true"
  10. @change="handleChange"
  11. @monthSwitch="handleMonthSwitch"
  12. />
  13. </layout-page>
  14. </template>
  15. <script setup>
  16. import { onLoad, onShow } from '@dcloudio/uni-app'
  17. import { ref } from 'vue'
  18. import Calendar from '@/components/uni-calendar/components/uni-calendar/uni-calendar.vue'
  19. import layoutPage from '@/layout'
  20. import { showAuthModal } from '@/hooks/useModal'
  21. import { getCalendarRecord } from '@/api/drawLots.js'
  22. const colorDict = {
  23. '白': '#FAFAFA',
  24. '黑': '#212121',
  25. '绿': '#43A047',
  26. '红': '#F44336',
  27. '黄': '#FDD835'
  28. }
  29. function getDate(date, AddDayCount = 0) {
  30. if (!date) {
  31. date = new Date()
  32. }
  33. if (typeof date !== 'object') {
  34. date = date.replace(/-/g, '/')
  35. }
  36. const dd = new Date(date)
  37. dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
  38. const y = dd.getFullYear()
  39. const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期,不足10补0
  40. const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
  41. return {
  42. fullDate: y + '-' + m + '-' + d,
  43. year: y,
  44. month: m,
  45. date: d,
  46. day: dd.getDay()
  47. }
  48. }
  49. const date = ref(getDate(new Date()).fullDate)
  50. // 设置日期选中内容
  51. const selected = ref([])
  52. // 登录信息
  53. const userInfo = ref(uni.getStorageSync('wechat_user') || {})
  54. onLoad(() => {
  55. if (!userInfo.value || !userInfo.value?.openid) return showAuthModal()
  56. })
  57. onShow(() => {
  58. // 刷新一次本地登录信息,防止 onLoad 时未拿到最新登录态
  59. const latestUser = uni.getStorageSync('wechat_user')
  60. if (latestUser?.openid && latestUser?.openid !== userInfo.value?.openid) {
  61. userInfo.value = latestUser
  62. }
  63. if (userInfo.value?.openid) getCalendarData(date.value.slice(0, 7))
  64. })
  65. // 监听登录成功事件,立刻刷新数据
  66. uni.$off && uni.$off('auth:login')
  67. uni.$on && uni.$on('auth:login', (user) => {
  68. userInfo.value = user || uni.getStorageSync('wechat_user')
  69. if (userInfo.value?.openid) {
  70. getCalendarData(date.value.slice(0, 7))
  71. }
  72. })
  73. // 点击日期查看黄历信息
  74. const handleChange = async (e) => {
  75. date.value = e.fulldate
  76. if (e.isBackToday) return // 点击左上角“今日”按钮不弹窗
  77. uni.navigateTo({
  78. url: `/pages/drawLots/fortune?date=${e.fulldate}`
  79. })
  80. }
  81. // 获取手账记录
  82. const calendarRecord = ref([])
  83. const getCalendarData = async (month_key) => {
  84. try {
  85. const { result } = await getCalendarRecord({ month_key, openid: userInfo.value.openid })
  86. calendarRecord.value = result?.calendar_content || []
  87. selected.value = calendarRecord.value?.length ? calendarRecord.value.map((item) => {
  88. return {
  89. date: item.date,
  90. color: item?.almanac?.color ? colorDict[item?.almanac?.color] : '',
  91. hasJournal: item?.notes ? true : false
  92. }
  93. }) : []
  94. } catch {}
  95. }
  96. // 切换月份
  97. const handleMonthSwitch = (e) => {
  98. console.log(e, '切换月份')
  99. getCalendarData(`${e.year}-${e.month < 10 ? '0' + e.month : e.month}`)
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. $commonColor: #c2a08c;
  104. $size: 25px;
  105. :deep {
  106. .uni-card {
  107. border-radius: 10px !important;
  108. }
  109. }
  110. .journal {
  111. margin-top: 30rpx;
  112. padding-bottom: 100px;
  113. .title {
  114. color: #999;
  115. margin: 0 30px;
  116. }
  117. .description {
  118. display: -webkit-box;
  119. -webkit-box-orient: vertical;
  120. -webkit-line-clamp: 2;
  121. overflow: hidden;
  122. }
  123. }
  124. .add-btn {
  125. position: fixed;
  126. right: 20px;
  127. bottom: 50px;
  128. width: 60px;
  129. height: 60px;
  130. border-radius: 50%;
  131. background-color: #2979ff;
  132. display: flex;
  133. align-items: center;
  134. justify-content: center;
  135. }
  136. .popupContent {
  137. position: relative;
  138. background-color: #fff;
  139. width: 70vw;
  140. padding: 30rpx;
  141. .content-box {
  142. height: 100%;
  143. border: 1px solid $commonColor;
  144. border-radius: 6px;
  145. }
  146. .yangli {
  147. font-weight: bold;
  148. color: $commonColor;
  149. font-size: 30px;
  150. }
  151. .yinli {
  152. color: #7f7f7f;
  153. margin: 10px 0;
  154. }
  155. .center-box {
  156. border-top: 1px solid $commonColor;
  157. border-bottom: 1px solid $commonColor;
  158. .yi {
  159. width: $size;
  160. height: $size;
  161. background-color: #c39c87;
  162. border-radius: 50%;
  163. color: #fff;
  164. margin-right: 10px;
  165. font-size: 14px;
  166. }
  167. .ji {
  168. width: $size;
  169. height: $size;
  170. background-color: #bfbfbf;
  171. border-radius: 50%;
  172. color: #fff;
  173. margin-right: 10px;
  174. font-size: 14px;
  175. }
  176. }
  177. .bottom-box {
  178. display: flex;
  179. &-item {
  180. width: 33.3%;
  181. text-align: center;
  182. border-right: 1px solid $commonColor;
  183. &:nth-child(3n) {
  184. border-right: none;
  185. }
  186. .label {
  187. font-size: 17px;
  188. }
  189. .value {
  190. color: #7f7f7f;
  191. font-size: 14px;
  192. margin-top: 10px;
  193. }
  194. .padding {
  195. padding: 10px;
  196. }
  197. .border {
  198. border-top: 1px solid #c2a08c;
  199. height: 1px;
  200. width: 100%;
  201. }
  202. }
  203. }
  204. .luckyColor {
  205. width: 50rpx;
  206. height: 50rpx;
  207. border-radius: 50%;
  208. }
  209. }
  210. </style>