index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <view class="container">
  3. <view class="flex justify-center " style="padding: 20rpx 0 50rpx 0;">
  4. <text class="text-center text-lg text-bold">请选择预约时间</text>
  5. </view>
  6. <!-- tab栏 -->
  7. <scroll-view class="scroll-view_H b-t b-b" scroll-x>
  8. <block v-for="(item, index) in dateArr" :key="index">
  9. <div class="flex-box" @click="selectDateEvent(index, item)" :style="{ 'box-shadow': index == dateActive ? 'inset 0 -2px 0 0 ' + selectedTabColor : '' }">
  10. <view class="date-box">
  11. <text class="days" :style="{ color: index == dateActive ? selectedTabColor : '#333' }">{{ item.week }}</text>
  12. <text class="date" :style="{ color: index == dateActive ? selectedTabColor : '#333' }">{{ item.date }}</text>
  13. </view>
  14. </div>
  15. </block>
  16. </scroll-view>
  17. <!-- 时间选项 -->
  18. <view class="time-box" style="margin-top: 20rpx;">
  19. <view v-if="mode==0" class="flex justify-center">
  20. <view class="box margin-right-20" @click="changeActive(0)" :class="active==0?'active':'unactive'">
  21. <text class="text-center">{{firstTime}}-{{centerTime}}</text>
  22. </view>
  23. <view class="box margin-left-20" @click="changeActive(1)" :class="active==1?'active':'unactive'">
  24. <text class="text-center">{{centerTime}}-{{lastTime}}</text>
  25. </view>
  26. </view>
  27. <block v-else v-for="(item, _index) in timeArr" :key="_index">
  28. <view class="item">
  29. <view
  30. class="item-box line-blue"
  31. :class="{ disable: item.disable,
  32. active: timeActive.indexOf(_index) >= 0 }"
  33. :style="{
  34. filter: _index < timeActive[1] && _index > timeActive[0] ? 'opacity(0.6)' : ''
  35. }"
  36. @click="selectTimeEvent(_index, item)"
  37. >
  38. <text>{{ item.time }}</text>
  39. <text class="all" v-if="item.disable">{{ disableText }}</text>
  40. <text class="all" v-if="_index == timeActive[0] && !item.disable" style="font-size: 20upx;">开始时间</text>
  41. <text class="all" v-if="_index == timeActive[1] && !item.disable" style="font-size: 20upx;">结束时间</text>
  42. </view>
  43. </view>
  44. </block>
  45. <view v-if="mode==0" class="flex justify-center margin-top-50">
  46. <view class="">
  47. {{dateTime.beginTime}}
  48. <text style="padding: 0 20rpx;">至</text>
  49. {{dateTime.endTime}}
  50. </view>
  51. </view>
  52. </view>
  53. <view @click="confirm" class="cu-btn bg-red-btn round flex footer-fixed" style="margin:10rpx 0;padding: 40rpx;">
  54. 确认
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import { dateData, timeData, timeStamp } from './date.js';
  60. let maxIndex;
  61. export default {
  62. props: {
  63. //0 双项模式 1 多项模式
  64. mode:{
  65. type:Number,
  66. default:0
  67. },
  68. //开始时间选项
  69. startTime: {
  70. type: String,
  71. default: '09:00'
  72. },
  73. //结束时间选项
  74. endTime: {
  75. type: String,
  76. default: '18:00'
  77. },
  78. // 提前预约的时间
  79. advanceTime: {
  80. type: Number,
  81. default: 1
  82. },
  83. // 默认选择的时间段间隔
  84. timeSlot: {
  85. type: Number,
  86. default: 2
  87. },
  88. //时间间隔
  89. timeInterval: {
  90. type: [Number, String],
  91. default: 1 //半小时
  92. },
  93. //选中的tab颜色
  94. selectedTabColor: {
  95. type: String,
  96. default: '#59a5f0'
  97. },
  98. //选中的时间颜色
  99. selectedItemColor: {
  100. type: String,
  101. default: '#59a5f0'
  102. },
  103. //禁用显示的文本
  104. disableText: {
  105. type: String,
  106. default: ''
  107. }
  108. },
  109. data() {
  110. return {
  111. firstTime:'9:00',
  112. centerTime:'13:00',
  113. lastTime:'19:00',
  114. active:0,
  115. dateTime:{},
  116. dateArr: [], //日期数据
  117. timeArr: [], //时间数据
  118. dateActive: 0, //选中的日期索引
  119. timeActive: [], //选中的时间索引
  120. oldTimeActive: [], //保存用户筛选的时间
  121. selectDate: '', //选择的日期数据
  122. selectTime: [], //选择的时间
  123. oldSelectTime: '',
  124. currentTimeStamp: '',
  125. currentTime: '',
  126. tabs: 0
  127. };
  128. },
  129. created() {
  130. //获取日期tab数据
  131. this.dateArr = dateData();
  132. //获取时间数据
  133. this.timeArr = timeData(this.startTime, this.endTime, this.timeInterval);
  134. maxIndex = this.timeArr[this.timeArr.length - 1]['index'];
  135. //当前时间戳
  136. this.currentTimeStamp = Date.now() + this.advanceTime * 3600 * 1000;
  137. this.currentTime = timeStamp(this.currentTimeStamp).hour;
  138. this.timeArr.map(item => {
  139. if (item.time > this.currentTime) {
  140. return (item.disable = 0); //判断当前时间大于时间选项则禁用
  141. } else {
  142. return (item.disable = 1);
  143. }
  144. });
  145. //默认选中的日期
  146. // this.selectDate = `${this.dateArr[0]['date']}(${this.dateArr[0]['week']})`;
  147. this.selectDate=new Date().getFullYear()+'-'+this.dateArr[0]['date']
  148. if (this.active==0) {
  149. let beginTime=this.selectDate+" "+this.firstTime+':00'
  150. let endTime=this.selectDate+" "+this.centerTime+':00'
  151. this.dateTime.beginTime=beginTime
  152. this.dateTime.endTime=endTime
  153. }else{
  154. let beginTime=this.selectDate+" "+this.centerTime+':00'
  155. let endTime=this.selectDate+" "+this.lastTime+':00'
  156. this.dateTime.beginTime=beginTime
  157. this.dateTime.endTime=endTime
  158. }
  159. this.timeArr.some((item, index) => {
  160. // 默认选中的时间段
  161. const endIndex = this.timeSlot / this.timeInterval + index > maxIndex ? maxIndex : this.timeSlot / this.timeInterval + index;
  162. this.selectTime = [this.timeArr[index]['time'], this.timeArr[endIndex]['time']]; //默认选中的时间
  163. this.oldSelectTime = this.selectTime; //存储选中的时间
  164. this.timeActive = [index, endIndex]; //选中的时间索引
  165. this.oldTimeActive = [index, endIndex];
  166. return !item.disable;
  167. });
  168. },
  169. methods: {
  170. confirm(){
  171. if (this.mode!=0) {
  172. if (this.dateTime.beginTime==undefined||this.dateTime.endTime==undefined) {
  173. this.dateTime.beginTime=this.selectDate+" "+this.selectTime[0]+":00"
  174. this.dateTime.endTime=this.selectDate+" "+this.selectTime[1]+":00"
  175. }
  176. this.$emit('getDateTime',this.dateTime)
  177. return
  178. }
  179. if (this.selectDate==''||this.selectDate.length==0) {
  180. let now=new Date().toLocaleDateString().replace(new RegExp('/','g'),'-')
  181. this.selectDate=now
  182. }
  183. this.$emit('getDateTime',this.dateTime)
  184. },
  185. changeActive(active){
  186. this.active=active
  187. this.getModeZeroDateTime()
  188. },
  189. getModeZeroDateTime(){
  190. if (this.active==0) {
  191. let beginTime=this.selectDate+" "+this.firstTime+':00'
  192. let endTime=this.selectDate+" "+this.centerTime+':00'
  193. this.dateTime.beginTime=beginTime
  194. this.dateTime.endTime=endTime
  195. }else{
  196. let beginTime=this.selectDate+" "+this.centerTime+':00'
  197. let endTime=this.selectDate+" "+this.lastTime+':00'
  198. this.dateTime.beginTime=beginTime
  199. this.dateTime.endTime=endTime
  200. }
  201. },
  202. selectDateEvent(index, item) {
  203. this.tabs = 0;
  204. if (this.currentTimeStamp < item.timeStamp) {
  205. const endIndex = this.timeSlot / this.timeInterval;
  206. this.timeActive = [0, endIndex];
  207. this.selectTime = [this.timeArr[0]['time'], this.timeArr[endIndex]['time']];
  208. this.timeArr.map(item => {
  209. return (item.disable = 0);
  210. });
  211. } else {
  212. this.timeActive = this.oldTimeActive;
  213. this.selectTime = this.oldSelectTime;
  214. this.timeArr.map(item => {
  215. if (item.time > this.currentTime) {
  216. return (item.disable = 0);
  217. } else {
  218. return (item.disable = 1);
  219. }
  220. });
  221. }
  222. this.dateActive = index;
  223. // this.selectDate = `${this.dateArr[index]['date']}(${this.dateArr[index]['week']})`;
  224. let tmp=this.dateArr[index]['date']
  225. this.selectDate=new Date().getFullYear()+'-'+this.dateArr[index]['date']
  226. if (this.mode==0) {
  227. this.getModeZeroDateTime()
  228. }else{
  229. let beginTime=new Date().getFullYear()+'-'+tmp+" "+this.selectTime[0]+':00'
  230. let endTime=new Date().getFullYear()+'-'+tmp+" "+this.selectTime[1]+':00'
  231. this.dateTime.beginTime=beginTime
  232. this.dateTime.endTime=endTime
  233. }
  234. // this.$emit('selectTime', `${this.selectDate}${this.selectTime.join('-')}`);
  235. },
  236. selectTimeEvent(index, item) {
  237. if (item.disable || this.timeActive.indexOf(index) > -1) return;
  238. this.tabs++;
  239. if (this.tabs % 2 == 0) {
  240. this.$set(this.timeActive, 1, index);
  241. this.selectTime[1] = this.timeArr[index]['time'];
  242. } else {
  243. this.$set(this.timeActive, 0, index);
  244. this.selectTime[0] = this.timeArr[index]['time'];
  245. }
  246. // 判断用户选择的时间是否大于第一次选择的时间
  247. if (this.timeActive[0] > this.timeActive[1]) {
  248. const tempTime = this.selectTime[0];
  249. const tempIndex = this.timeActive[0];
  250. this.selectTime[0] = this.selectTime[1];
  251. this.selectTime[1] = tempTime;
  252. this.$set(this.timeActive, 0, this.timeActive[1]);
  253. this.$set(this.timeActive, 1, tempIndex);
  254. }
  255. let beginTime=this.selectDate+" "+this.selectTime[0]+':00'
  256. let endTime=this.selectDate+" "+this.selectTime[1]+':00'
  257. this.dateTime.beginTime=beginTime
  258. this.dateTime.endTime=endTime
  259. // this.$emit('selectTime', `${this.selectDate}${this.selectTime.join('-')}`);
  260. }
  261. }
  262. };
  263. </script>
  264. <style lang="scss" scoped>
  265. .box{
  266. margin-top: 20rpx;
  267. font-size: 30rpx;
  268. padding: 50rpx 80rpx;
  269. border-radius: 12rpx;
  270. }
  271. .unactive{
  272. color: #8a8a8a;
  273. background-color: #ecedf0;
  274. }
  275. .active{
  276. border: 1rpx solid #5ca6ef;
  277. background-color: rgba(92, 166, 239,.3);
  278. color: #5ca6ef;
  279. }
  280. .container{
  281. view,text,image{
  282. box-sizing: border-box;
  283. }
  284. scroll-view{
  285. width: 100%;
  286. white-space: nowrap;
  287. height: 104upx;
  288. position: relative;
  289. &::after{
  290. background: #e5e5e5;
  291. content: '';
  292. display:block;
  293. width: 100%;
  294. height: 1px;
  295. position: absolute;
  296. bottom: 0;
  297. left: 0;
  298. transform:scaleY(0.5)
  299. }
  300. .flex-box{
  301. display: inline-block;
  302. height: 100%;
  303. padding:0 30upx;
  304. box-sizing: border-box;
  305. &.active{
  306. .date-box{
  307. .days{
  308. color: #0092D5;
  309. }
  310. .date{
  311. color: #0092D5;
  312. }
  313. }
  314. }
  315. .date-box{
  316. display: flex;
  317. height: 100%;
  318. flex-direction: column;
  319. align-items: center;
  320. justify-content: space-around;
  321. font-size: 30upx;
  322. color: #333333;
  323. .date{
  324. color: #999;
  325. font-size: 24upx;
  326. // margin-top: 10upx;
  327. }
  328. }
  329. }
  330. }
  331. .time-box{
  332. padding:28upx 12upx 26upx;
  333. display: flex;
  334. flex-wrap: wrap;
  335. .item{
  336. width: 25%;
  337. padding: 0 9upx;
  338. margin-bottom: 18upx;
  339. &-box{
  340. width: 100%;
  341. height: 80upx;
  342. background: #F1F3F6;
  343. color: #333;
  344. font-size: 28upx;
  345. border-radius: 10upx;
  346. display: flex;
  347. flex-direction: column;
  348. align-items: center;
  349. justify-content: center;
  350. &.disable{
  351. background: #F1F3F6 !important;
  352. color: #999 !important;
  353. }
  354. &.active{
  355. border: 1rpx solid #5ca6ef;
  356. background-color: rgba(92, 166, 239,.1);
  357. color: #5ca6ef;
  358. font-weight: bold;
  359. }
  360. .all{
  361. font-size: 22upx;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. </style>