index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <uni-popup ref="popupRef" type="bottom" :safe-area="true">
  3. <view class="calendar-container">
  4. <!-- 头部 -->
  5. <view class="calendar-header">
  6. <view class="header-title">选择预约日期</view>
  7. <view class="header-subtitle">{{ currentMonth }}</view>
  8. </view>
  9. <!-- 星期标题 -->
  10. <view class="week-header">
  11. <view class="week-item" v-for="week in weekDays" :key="week">
  12. {{ week }}
  13. </view>
  14. </view>
  15. <!-- 日历日期 -->
  16. <view class="calendar-body">
  17. <view
  18. class="date-item"
  19. v-for="(date, index) in dateList"
  20. :key="index"
  21. :class="{
  22. empty: !date.day,
  23. available: date.available,
  24. unavailable: !date.available && date.day,
  25. selected: date.selected,
  26. }"
  27. @click="selectDate(date)"
  28. >
  29. <view class="date-day" v-if="date.day">{{ date.day }}</view>
  30. <view class="date-dot" v-if="date.selected"></view>
  31. </view>
  32. </view>
  33. <!-- 无可用日期提示(在日历下方、底部按钮上方) -->
  34. <view v-if="hasNoAvailableDates" class="no-available-tip">
  35. 无可预约的日期
  36. </view>
  37. <!-- 底部按钮 -->
  38. <view class="calendar-footer">
  39. <view class="legend">
  40. <view class="legend-item">
  41. <view class="legend-dot available"></view>
  42. <text>可预约</text>
  43. </view>
  44. <view class="legend-item">
  45. <view class="legend-dot unavailable"></view>
  46. <text>不可预约</text>
  47. </view>
  48. </view>
  49. <view class="button-group">
  50. <button class="btn-cancel" @click="close">取消</button>
  51. <button
  52. class="btn-confirm"
  53. @click="confirm"
  54. :disabled="!selectedDate"
  55. >
  56. 确认
  57. </button>
  58. </view>
  59. </view>
  60. </view>
  61. </uni-popup>
  62. </template>
  63. <script setup>
  64. import { ref, computed, watch } from "vue";
  65. import { quotaByWeight } from "@/api/vault"; // 引入克重查询接口
  66. const hasNoAvailableDates = ref(false);
  67. const props = defineProps({
  68. metalType: {
  69. type: Number,
  70. default: 1,
  71. },
  72. reservedWeight: {
  73. // 新增:接收父组件传递的克重
  74. type: Number,
  75. required: true,
  76. validator: (val) => val > 0,
  77. },
  78. });
  79. const emit = defineEmits(["confirm"]);
  80. const popupRef = ref(null);
  81. const weekDays = ["日", "一", "二", "三", "四", "五", "六"];
  82. const dateList = ref([]);
  83. const availableDates = ref([]); // 存储可预约日期(从接口获取)
  84. const selectedDate = ref(null);
  85. const currentYear = ref(new Date().getFullYear());
  86. const currentMonthNum = ref(new Date().getMonth() + 1);
  87. // 当前月份显示
  88. const currentMonth = computed(() => {
  89. return `${currentYear.value}年${currentMonthNum.value}月`;
  90. });
  91. // 打开弹窗
  92. const open = async () => {
  93. popupRef.value?.open();
  94. await fetchAvailableDates(); // 打开时查询可预约日期
  95. generateCalendar();
  96. };
  97. // 关闭弹窗
  98. const close = () => {
  99. popupRef.value?.close();
  100. selectedDate.value = null;
  101. };
  102. // 获取可预约日期(基于克重和金属类型)
  103. const fetchAvailableDates = async () => {
  104. try {
  105. const res = await quotaByWeight({
  106. metalType: props.metalType,
  107. weight: props.reservedWeight,
  108. });
  109. const availableDateList = res.data
  110. .filter((item) => item.canReserve)
  111. .map((item) => item.date);
  112. availableDates.value = availableDateList;
  113. hasNoAvailableDates.value = availableDateList.length === 0;
  114. } catch (error) {
  115. console.error("获取可预约日期失败:", error);
  116. uni.showToast({ title: "获取日期失败", icon: "none" });
  117. }
  118. };
  119. // 生成日历
  120. const generateCalendar = () => {
  121. const year = currentYear.value;
  122. const month = currentMonthNum.value;
  123. // 获取当月第一天是星期几(0-6)
  124. const firstDay = new Date(year, month - 1, 1).getDay();
  125. // 获取当月天数
  126. const daysInMonth = new Date(year, month, 0).getDate();
  127. const dates = [];
  128. // 填充空白(月初前的空白)
  129. for (let i = 0; i < firstDay; i++) {
  130. dates.push({ day: null, available: false, selected: false });
  131. }
  132. // 填充日期
  133. for (let day = 1; day <= daysInMonth; day++) {
  134. const dateStr = `${year}-${String(month).padStart(2, "0")}-${String(
  135. day
  136. ).padStart(2, "0")}`;
  137. // 判断是否在可预约列表中
  138. const isAvailable = availableDates.value.includes(dateStr);
  139. dates.push({
  140. day,
  141. date: dateStr,
  142. available: isAvailable,
  143. selected: false,
  144. });
  145. }
  146. dateList.value = dates;
  147. };
  148. // 选择日期
  149. const selectDate = (date) => {
  150. if (!date.day || !date.available) {
  151. if (date.day && !date.available) {
  152. uni.showToast({ title: "该日期不可预约", icon: "none" });
  153. }
  154. return;
  155. }
  156. // 取消之前的选择
  157. dateList.value.forEach((item) => {
  158. item.selected = false;
  159. });
  160. // 设置新选择
  161. date.selected = true;
  162. selectedDate.value = date.date;
  163. };
  164. // 确认选择
  165. const confirm = () => {
  166. if (!selectedDate.value) {
  167. uni.showToast({ title: "请选择预约日期", icon: "none" });
  168. return;
  169. }
  170. emit("confirm", selectedDate.value);
  171. close();
  172. };
  173. // 监听金属类型或克重变化,重新获取日期
  174. watch([() => props.metalType, () => props.reservedWeight], async () => {
  175. if (props.reservedWeight > 0) {
  176. // 确保克重有效
  177. await fetchAvailableDates();
  178. generateCalendar();
  179. }
  180. });
  181. defineExpose({
  182. open,
  183. close,
  184. });
  185. </script>
  186. <style lang="scss" scoped>
  187. .calendar-container {
  188. background: #fff;
  189. border-radius: 30rpx 30rpx 0 0;
  190. padding: 40rpx 30rpx 60rpx;
  191. }
  192. .calendar-header {
  193. text-align: center;
  194. margin-bottom: 40rpx;
  195. .header-title {
  196. font-size: 36rpx;
  197. font-weight: bold;
  198. color: #333;
  199. margin-bottom: 10rpx;
  200. }
  201. .header-subtitle {
  202. font-size: 28rpx;
  203. color: #999;
  204. }
  205. }
  206. .week-header {
  207. display: flex;
  208. justify-content: space-around;
  209. margin-bottom: 20rpx;
  210. .week-item {
  211. width: 14.28%;
  212. text-align: center;
  213. font-size: 28rpx;
  214. color: #666;
  215. font-weight: 500;
  216. }
  217. }
  218. .calendar-body {
  219. display: flex;
  220. flex-wrap: wrap;
  221. margin-bottom: 40rpx;
  222. .date-item {
  223. width: 14.28%;
  224. height: 100rpx;
  225. display: flex;
  226. flex-direction: column;
  227. align-items: center;
  228. justify-content: center;
  229. position: relative;
  230. margin-bottom: 10rpx;
  231. .date-day {
  232. font-size: 30rpx;
  233. color: #333;
  234. }
  235. .date-dot {
  236. width: 8rpx;
  237. height: 8rpx;
  238. background: #e9c279;
  239. border-radius: 50%;
  240. margin-top: 8rpx;
  241. }
  242. &.empty {
  243. pointer-events: none;
  244. }
  245. &.available {
  246. .date-day {
  247. color: #333;
  248. font-weight: 500;
  249. }
  250. &:active {
  251. background: rgba(233, 194, 121, 0.1);
  252. border-radius: 50%;
  253. }
  254. }
  255. &.unavailable {
  256. .date-day {
  257. color: #ccc;
  258. }
  259. }
  260. &.selected {
  261. .date-day {
  262. background: #e9c279;
  263. color: #fff;
  264. width: 60rpx;
  265. height: 60rpx;
  266. border-radius: 50%;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. font-weight: bold;
  271. }
  272. }
  273. }
  274. }
  275. .calendar-footer {
  276. .legend {
  277. display: flex;
  278. justify-content: center;
  279. gap: 40rpx;
  280. margin-bottom: 30rpx;
  281. .legend-item {
  282. display: flex;
  283. align-items: center;
  284. font-size: 24rpx;
  285. color: #666;
  286. .legend-dot {
  287. width: 30rpx;
  288. height: 30rpx;
  289. border-radius: 50%;
  290. margin-right: 10rpx;
  291. &.available {
  292. background: #333;
  293. }
  294. &.unavailable {
  295. background: #ccc;
  296. }
  297. }
  298. }
  299. }
  300. .button-group {
  301. display: flex;
  302. gap: 20rpx;
  303. button {
  304. flex: 1;
  305. height: 80rpx;
  306. border-radius: 40rpx;
  307. font-size: 30rpx;
  308. border: none;
  309. }
  310. .btn-cancel {
  311. background: #f5f5f5;
  312. color: #666;
  313. }
  314. .btn-confirm {
  315. background: #e9c279;
  316. color: #fff;
  317. }
  318. }
  319. }
  320. .no-available-tip {
  321. color: #ff1e0f; // 红色提示
  322. font-size: 28rpx;
  323. text-align: center;
  324. padding: 15rpx 0; // 上下留白
  325. width: 100%;
  326. }
  327. </style>