pretty-times.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="content">
  3. <view class="container">
  4. <!-- 日期列表 -->
  5. <scroll-view class="scroll-view_H b-t b-b" scroll-x>
  6. <block v-for="(item,index) in dateArr" :key="index">
  7. <div class="flex-box" @click="selectDateEvent(index,item)">
  8. <view class="date-box" :style="{color:index==dateActive?selectedTabColor:'#333'}">
  9. <text class="fontw">{{item.week}}</text>
  10. <text>{{item.date}}</text>
  11. </view>
  12. </div>
  13. </block>
  14. </scroll-view>
  15. <!-- 时间选项 -->
  16. <view class="time-box">
  17. <block v-for="(item,_index) in timeArr" :key="_index">
  18. <view class="item">
  19. <view class="item-box" :class="{'disable':item.disable,
  20. 'active':isMultiple?item.isActive:_index==timeActive}" :style="{color:isMultiple?item.isActive? selectedItemColor:'#333'
  21. :_index==timeActive?selectedItemColor:'#333'}" @click="selectTimeEvent(_index,item)">
  22. <text>{{item.seltime}}</text>
  23. <text class="all">{{item.disable?disableText:undisableText}}</text>
  24. </view>
  25. </view>
  26. </block>
  27. </view>
  28. </view>
  29. <view class="bottom">
  30. <view class="show-time" v-if="!isMultiple">
  31. 预约时间:{{orderDateTime}}
  32. </view>
  33. <button form-type="submit" @click="handleSubmit" class="submit-btn" :style="'background:'+ stylecolor">确认预约</button>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import {
  39. initData,
  40. initTime,
  41. timeStamp,
  42. currentTime
  43. } from '../utils/date.js'
  44. export default {
  45. name: 'times',
  46. model: {
  47. prop: "showPop",
  48. event: "change"
  49. },
  50. props: {
  51. technicalId: {
  52. type: String,
  53. default: ""
  54. },
  55. buynowinfoid: {
  56. type: String,
  57. default: ""
  58. },
  59. isMultiple: { //是否多选
  60. type: Boolean,
  61. default: false
  62. },
  63. disableText: { //禁用显示的文本
  64. type: String,
  65. default: "已约满"
  66. },
  67. undisableText: { //未禁用显示的文本
  68. type: String,
  69. default: "可预约"
  70. },
  71. timeInterval: { // 时间间隔,小时为单位
  72. type: Number,
  73. default: 1
  74. },
  75. selectedTabColor: { // 日期栏选中的颜色
  76. type: String,
  77. default: "#ff1e02"
  78. },
  79. selectedItemColor: { // 时间选中的颜色
  80. type: String,
  81. default: "#ff1e02"
  82. },
  83. stylecolor:{
  84. type: String,
  85. default: '#ff1e02'
  86. },
  87. beginTime: {
  88. type: String,
  89. default: "09:00:00"
  90. },
  91. endTime: {
  92. type: String,
  93. default: "19:00:00"
  94. },
  95. appointTime: { // 预约的时间
  96. type: Array,
  97. default () {
  98. return []
  99. }
  100. }
  101. },
  102. watch: {
  103. appointTime(val) {
  104. if (val && val.length) {
  105. this.initOnload()
  106. }
  107. }
  108. },
  109. data() {
  110. return {
  111. orderDateTime: '暂无选择', // 选中时间
  112. orderTimeArr: {}, //多选的时间
  113. dateArr: [], //日期数据
  114. timeArr: [], //时间数据
  115. nowDate: "", // 当前日期
  116. dateActive: 0, //选中的日期索引
  117. timeActive: 0, //选中的时间索引
  118. timeQuanBeginIndex: 0, //时间段开始的下标
  119. selectDate: "", //选择的日期
  120. selectTime: "", //选择的时间
  121. timeQuanBegin: "", //时间段开始时间
  122. timeQuanEnd: "", //时间段结束时间
  123. }
  124. },
  125. mounted(props) {
  126. this.selectDate = this.nowDate = currentTime().date
  127. this.initOnload()
  128. },
  129. methods: {
  130. initOnload() {
  131. var _this = this;
  132. _this.dateArr = initData() // 日期栏初始化
  133. //_this.timeArr = initTime(_this.beginTime, _this.endTime, _this.timeInterval) //时间选项初始化
  134. _this.$request.post('Servicetime.index', {
  135. technicalId:_this.technicalId,
  136. buynowinfoid:_this.buynowinfoid,
  137. selectDate:_this.selectDate,
  138. samkey: (new Date()).valueOf()
  139. }).then(res => {
  140. if (res.errno == 0) {
  141. _this.timeArr = res.data;
  142. _this.timeQuanBegin = _this.timeQuanEnd = ""
  143. let isFullTime = true
  144. _this.timeArr.forEach((item, index) => {
  145. //判断是当前这一天,选中时间小于当前时间则禁用
  146. if (_this.selectDate == _this.nowDate && currentTime().time > item.time) {
  147. item.disable = true
  148. }
  149. // 将预约的时间禁用
  150. _this.appointTime.forEach(t => {
  151. let [date, time] = t.split(' ')
  152. if (date == _this.selectDate && item.time == time) {
  153. item.disable = true
  154. }
  155. })
  156. // 判断是否当前日期时间都被预约
  157. if (!item.disable) {
  158. isFullTime = false
  159. }
  160. })
  161. _this.orderDateTime = isFullTime ? "暂无选择" : _this.selectDate
  162. _this.timeActive = -1
  163. for (let i = 0, len = _this.timeArr.length; i < len; i++) {
  164. if (!_this.timeArr[i].disable) {
  165. _this.orderDateTime = `${_this.selectDate} ${_this.timeArr[i].seltime}`
  166. _this.timeActive = i
  167. return
  168. }
  169. }
  170. }
  171. })
  172. },
  173. // 日期选择事件
  174. selectDateEvent(index, item) {
  175. this.dateActive = index
  176. this.selectDate = item.date
  177. this.initOnload()
  178. },
  179. // 时间选择事件
  180. selectTimeEvent(index, item) {
  181. if (item.disable) return
  182. if (this.isMultiple) {
  183. item.isActive = !item.isActive
  184. this.timeArr = this.timeArr.slice()
  185. this.orderTimeArr[this.selectDate] = this.timeArr.reduce((prev, cur) => {
  186. cur.isActive && prev.push(cur.time)
  187. return prev
  188. }, [])
  189. } else {
  190. this.timeActive = index
  191. this.selectTime = item.seltime
  192. this.orderDateTime = `${this.selectDate} ${item.seltime}`
  193. }
  194. },
  195. // 选择时间段
  196. handleSelectQuantum(index, item) {
  197. if (item.disable) return
  198. function clearTime() {
  199. this.timeQuanBeginIndex = index
  200. this.timeQuanBegin = item.time
  201. this.timeQuanEnd = ""
  202. }
  203. if (!this.timeQuanBegin) {
  204. clearTime.call(this)
  205. return
  206. }
  207. if (!this.timeQuanEnd && this.timeQuanBegin) {
  208. let isDisble = false
  209. let start = this.timeQuanBeginIndex
  210. let end = index
  211. start > end && ([start, end] = [end, start])
  212. for (let i = start + 1; i < end; i++) {
  213. if (this.timeArr[i].disable) {
  214. isDisble = true
  215. clearTime.call(this)
  216. return
  217. }
  218. }
  219. if (!isDisble) {
  220. for (let i = start + 1; i < end; i++) {
  221. this.timeArr[i].isInclude = true
  222. }
  223. }
  224. this.timeQuanEnd = item.time
  225. return
  226. }
  227. if (this.timeQuanBegin && this.timeQuanEnd) {
  228. this.timeArr.forEach(t => {
  229. t.isInclude = false
  230. })
  231. clearTime.call(this)
  232. }
  233. },
  234. handleChange() {
  235. this.timeQuanBegin > this.timeQuanEnd && ([this.timeQuanBegin, this.timeQuanEnd] = [this.timeQuanEnd, this
  236. .timeQuanBegin
  237. ])
  238. },
  239. handleSubmit() {
  240. if (this.isMultiple) {
  241. let time = []
  242. for (let date in this.orderTimeArr) {
  243. this.orderTimeArr[date].forEach(time => {
  244. time.push(`${date} ${time}`)
  245. })
  246. }
  247. this.$emit('change', time)
  248. } else {
  249. this.$emit('change', this.orderDateTime)
  250. }
  251. }
  252. }
  253. }
  254. </script>
  255. <style lang="scss" scoped>
  256. @import './pretty-times.scss';
  257. page {
  258. height: 100%;
  259. }
  260. .content {
  261. text-align: center;
  262. height: 100%;
  263. }
  264. /* 两个按钮 */
  265. .bottom {
  266. display: flex;
  267. flex-direction: row;
  268. position: fixed;
  269. bottom: 0;
  270. padding-top: 20rpx;
  271. padding-bottom: env(safe-area-inset-bottom);
  272. top: auto;
  273. left: 0px;
  274. width: 100%;
  275. background-color: #fff;
  276. }
  277. .show-time {
  278. width: 70%;
  279. height: 47px;
  280. color: #505050;
  281. background-color: rgba(255, 255, 255, 1);
  282. font-size: 15px;
  283. line-height: 47px;
  284. text-align: center;
  285. }
  286. .submit-btn {
  287. width: 200rpx;
  288. height: 70rpx;
  289. line-height: 70rpx;
  290. font-size: 28rpx;
  291. border-radius: 50rpx;
  292. color: #ffffff;
  293. align-items: center;
  294. }
  295. .fontw {
  296. font-weight: bold;
  297. }
  298. </style>