setOrderTime.uvue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <!-- pages/setOrderTime/setOrderTime.uvue -->
  2. <template>
  3. <view class="container">
  4. <!-- 头部:全部可接单 -->
  5. <view class="header-box" @click="allTime">
  6. <text class="header-text">
  7. {{ allIndex == 2 ? '取消全选' : '全部可接单' }}
  8. </text>
  9. </view>
  10. <!-- 滚动列表区域 -->
  11. <!-- uni-app x 中 scroll-view 需要明确高度或使用 flex 填充 -->
  12. <scroll-view class="time-list" scroll-y="true">
  13. <view class="time-grid">
  14. <view v-for="item in timeArr" :key="item.start_time"
  15. :class="item.isSelect ? 'time-card selected' : 'time-card normal'" @click.stop="chooseTime(item)">
  16. <text :class="item.isSelect ? 'time-text selected' : 'time-text normal'">
  17. {{ item.start_time }}
  18. </text>
  19. <text :class="item.isSelect ? 'time-sub selected' : 'time-sub normal'">
  20. {{ item.isSelect ? '可接单' : '休息' }}
  21. </text>
  22. </view>
  23. </view>
  24. </scroll-view>
  25. <!-- 底部保存按钮 -->
  26. <view class="footer-box" @click="handleSave">
  27. <text class="footer-text">
  28. 保存设置
  29. </text>
  30. </view>
  31. </view>
  32. </template>
  33. <script lang="uts" setup>
  34. import { ref } from 'vue';
  35. import { editWorkTimeSetting } from '@/utils/api/workbenches.uts'
  36. // 状态定义
  37. const allIndex = ref<1 | 2>(1); // 1: 普通模式, 2: 全选模式
  38. // 1. 定义时间项类型(必须最先定义)
  39. type TimeItem = {
  40. start_time : string;
  41. end_time : string;
  42. isSelect : boolean;
  43. }
  44. // 2. 定义响应式数组
  45. const timeArr = ref<TimeItem[]>([]);
  46. // 3. 先定义初始化函数
  47. function initTimeArr() {
  48. const arr : TimeItem[] = [];
  49. for (let i = 0; i < 48; i++) {
  50. const startHour = Math.floor(i / 2);
  51. const startMin = i % 2 === 0 ? '00' : '30';
  52. let endHour = Math.floor((i + 1) / 2);
  53. let endMin = (i + 1) % 2 === 0 ? '00' : '30';
  54. // 处理23:30 -> 24:00
  55. if (endHour >= 24) {
  56. endHour = 24;
  57. endMin = '00';
  58. }
  59. arr.push({
  60. start_time: `${startHour < 10 ? '0' : ''}${startHour}:${startMin}`,
  61. end_time: `${endHour < 10 ? '0' : ''}${endHour}:${endMin}`,
  62. isSelect: false
  63. });
  64. }
  65. timeArr.value = arr;
  66. }
  67. // 4. 定义点击函数
  68. function chooseTime(item : TimeItem) {
  69. item.isSelect = !item.isSelect;
  70. console.log('选中时间:', item.start_time, item.isSelect);
  71. }
  72. // 5. 最后调用函数(UTS 核心:先定义,后调用)
  73. initTimeArr();
  74. // onShow(() => {
  75. // allIndex.value = 1;
  76. // // getCoachTime();
  77. // // adjustSafeArea();
  78. // });
  79. // // 获取技师时间设置
  80. // const getCoachTime = async () => {
  81. // // 重置状态
  82. // timeArr.value.forEach(item => item.isSelect = false);
  83. // allIndex.value = 1;
  84. // try {
  85. // const res = await workbenchesInfoApi.getWorkTimeSetting();
  86. // const timeRanges = res.data?.time_ranges || [];
  87. // // 判断是否全部可接单
  88. // if (timeRanges.length === 1 && timeRanges[0].start_time === '00:00' && timeRanges[0].end_time === '24:00') {
  89. // allTime();
  90. // return;
  91. // }
  92. // // 匹配时间段
  93. // timeArr.value.forEach(item => {
  94. // const itemTime = item.start_time;
  95. // for (const range of timeRanges) {
  96. // // 比较逻辑:start <= item < end
  97. // if (compareTime(itemTime, range.start_time) >= 0 &&
  98. // compareTime(itemTime, range.end_time) < 0
  99. // ) {
  100. // item.isSelect = true;
  101. // break;
  102. // }
  103. // }
  104. // });
  105. // } catch (error) {
  106. // console.error('获取时间设置失败', error);
  107. // uni.showToast({ title: '加载失败', icon: 'none' });
  108. // }
  109. // };
  110. // 时间比较工具函数
  111. // function compareTime(t1, t2) {
  112. // if (!t1 || !t2) return 0;
  113. // const [h1, m1] = t1.split(':').map(Number);
  114. // const [h2, m2] = t2.split(':').map(Number);
  115. // if (h1 !== h2) return h1 > h2 ? 1 : -1;
  116. // if (m1 !== m2) return m1 > m2 ? 1 : -1;
  117. // return 0;
  118. // }
  119. // 点击单个时间
  120. // 全部可接单/取消全选
  121. const allTime = () => {
  122. if (allIndex.value === 2) {
  123. // 当前是全选状态,点击则取消全选
  124. for (let i = 0; i < timeArr.value.length; i++) {
  125. timeArr.value[i].isSelect = false;
  126. }
  127. allIndex.value = 1;
  128. } else {
  129. // 当前是普通状态,点击则全选
  130. for (let i = 0; i < timeArr.value.length; i++) {
  131. timeArr.value[i].isSelect = true;
  132. }
  133. allIndex.value = 2;
  134. }
  135. };
  136. const handleSave = async () => {
  137. // console.log(timeArr.value, 'timeArr')
  138. // 1. 声明选中时间数组(类型固定)
  139. const kjTime : TimeItem[] = []
  140. // 2. 遍历获取选中的时间段
  141. for (let i = 0; i < timeArr.value.length; i++) {
  142. const item : TimeItem = timeArr.value[i]
  143. if (item.isSelect) {
  144. kjTime.push(item)
  145. }
  146. }
  147. // 3. 校验:未选择时间段
  148. if (kjTime.length === 0) {
  149. uni.showToast({
  150. title: '请至少选择一个可接单时间段',
  151. icon: 'none',
  152. duration: 2000
  153. })
  154. return
  155. }
  156. try {
  157. const scheduleData = {
  158. // time_ranges: kjTime.filter((range : TimeItem) => range.isSelect).map((range : TimeItem) => ({
  159. // start_time: range.start_time,
  160. // end_time: range.end_time
  161. // }))
  162. time_ranges: [{ start_time: "04:00", end_time: "04:30" }, { start_time: "04:30", end_time: "05:00" }]
  163. }
  164. console.log(scheduleData, 'scheduleData');
  165. // 调用接口保存
  166. const res = await editWorkTimeSetting(scheduleData)
  167. console.log(res);
  168. // if ((res.code as number) === 200) {
  169. // uni.showToast({
  170. // title: '保存成功',
  171. // icon: 'success',
  172. // duration: 2000
  173. // })
  174. // uni.reLaunch({
  175. // url: '/pages/index/console'
  176. // })
  177. // }
  178. } catch (error) {
  179. uni.showToast({
  180. title: '保存失败,请重试',
  181. icon: 'none'
  182. })
  183. console.error('保存报错:', error)
  184. }
  185. }
  186. </script>
  187. <style lang="scss">
  188. /* 根容器:使用 Flex 布局填满屏幕 */
  189. .container {
  190. display: flex;
  191. flex-direction: column;
  192. width: 100%;
  193. height: 100%;
  194. /* 或 100% */
  195. background-color: #FFFFFF;
  196. box-sizing: border-box;
  197. padding-bottom: env(safe-area-inset-bottom);
  198. /* 适配全面屏底部 */
  199. }
  200. /* 顶部占位 */
  201. .top-safe-area {
  202. width: 100%;
  203. height: 20rpx;
  204. /* 根据需要调整 */
  205. flex-shrink: 0;
  206. }
  207. /* 头部盒子 */
  208. .header-box {
  209. width: 184rpx;
  210. height: 66rpx;
  211. border-radius: 16rpx;
  212. background-color: #FFDA59;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. margin: 20rpx 0 20rpx 32rpx;
  217. flex-shrink: 0;
  218. .header-text {
  219. color: #3D444E;
  220. font-size: 28rpx;
  221. font-weight: 700;
  222. letter-spacing: 3rpx;
  223. }
  224. }
  225. /* 滚动列表区域:flex: 1 占据剩余空间 */
  226. .time-list {
  227. flex: 1;
  228. width: 100%;
  229. padding-bottom: 20rpx;
  230. }
  231. /* 网格布局容器 */
  232. .time-grid {
  233. width: 100%;
  234. padding: 0 20rpx 20rpx;
  235. /* 底部 padding 防止被按钮遮挡 */
  236. box-sizing: border-box;
  237. flex-wrap: wrap;
  238. justify-content: space-between;
  239. flex-direction: row;
  240. }
  241. /* 时间卡片通用样式 */
  242. .time-card {
  243. width: 158rpx;
  244. height: 90rpx;
  245. border-radius: 16rpx;
  246. margin-bottom: 22rpx;
  247. align-items: center;
  248. justify-content: center;
  249. box-sizing: border-box;
  250. border-width: 2rpx;
  251. border-style: solid;
  252. /* 选中状态 */
  253. &.selected {
  254. background-color: #FFFBEF;
  255. border-color: #FFDB5F;
  256. .time-text,
  257. .time-sub {
  258. color: #3A3330;
  259. }
  260. }
  261. /* 未选中状态 */
  262. &.normal {
  263. background-color: #F3F3F3;
  264. border-color: transparent;
  265. .time-text,
  266. .time-sub {
  267. color: #9B9B9B;
  268. }
  269. }
  270. }
  271. /* 时间文字 */
  272. .time-text {
  273. font-size: 28rpx;
  274. font-weight: 400;
  275. margin-bottom: 4rpx;
  276. text-align: center;
  277. }
  278. /* 状态文字 */
  279. .time-sub {
  280. font-size: 26rpx;
  281. font-weight: 400;
  282. letter-spacing: 2rpx;
  283. text-align: center;
  284. }
  285. /* 底部保存按钮 */
  286. .footer-box {
  287. /* bottom: 40rpx; */
  288. /* 距离底部距离 */
  289. transform: translateX(-50%);
  290. left: 50%;
  291. width: 78%;
  292. height: 100rpx;
  293. background-color: #FFDA59;
  294. border-radius: 24rpx;
  295. display: flex;
  296. align-items: center;
  297. justify-content: center;
  298. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
  299. .footer-text {
  300. color: #3A3330;
  301. font-size: 34rpx;
  302. font-weight: 700;
  303. letter-spacing: 3rpx;
  304. }
  305. }
  306. </style>