index.vue 10 KB

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