setPunchTime.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <template>
  2. <view class="container">
  3. <view class="placeholder"></view>
  4. <!-- 选择星期区域 -->
  5. <view class="week_title">选择星期</view>
  6. <view class="week"><jlk-week :value="selectedWeeks" @change="changeWeek"></jlk-week></view>
  7. <!-- 选择打卡时间段区域 -->
  8. <view class="time_list">
  9. <view class="title">选择打卡时间段</view>
  10. <view class="add" @click="handleAddTime">添加时段</view>
  11. </view>
  12. <view class="list">
  13. <uni-swipe-action>
  14. <!-- 每一个时间段区域 -->
  15. <uni-swipe-action-item :auto-close="true" :right-options="options" @click="onClick(index)" v-for="(item, index) in list" :key="index">
  16. <view class="item">
  17. <view class="item_box">
  18. <picker mode="time" :value="item.beginTime" @change="bindTimeChange($event, 1, item)">
  19. <view class="uni-input">
  20. <view class="input_time">{{ item.beginTime }}</view>
  21. <view class="input_icon"><img src="../../static/imgs/time.png" /></view>
  22. </view>
  23. </picker>
  24. </view>
  25. --
  26. <view class="item_box">
  27. <picker mode="time" :value="item.endTime" @change="bindTimeChange($event, 2, item)">
  28. <view class="uni-input">
  29. <view class="input_time">{{ item.endTime }}</view>
  30. <view class="input_icon"><img src="../../static/imgs/time.png" /></view>
  31. </view>
  32. </picker>
  33. </view>
  34. </view>
  35. </uni-swipe-action-item>
  36. </uni-swipe-action>
  37. </view>
  38. <view class="button" @click="handleSave">保存</view>
  39. </view>
  40. </template>
  41. <script>
  42. import JlkWeek from '@/uni_modules/jlk-week/components/jlk-week/jlk-week.vue'
  43. export default {
  44. components: {
  45. JlkWeek
  46. },
  47. data() {
  48. return {
  49. // 选中的星期
  50. selectedWeeks: [],
  51. // 打卡时间段列表
  52. list: [
  53. {
  54. beginTime: '00:00',
  55. endTime: '00:00'
  56. }
  57. ],
  58. // 编辑打卡时间页面带过来的对象
  59. info: {},
  60. // 左滑配置
  61. options: [
  62. {
  63. text: '删除',
  64. style: {
  65. backgroundColor: '#D43030'
  66. }
  67. }
  68. ],
  69. // 添加时间 编辑时间 标识
  70. flag: null,
  71. // 时间列表索引
  72. index: null,
  73. // 判断时间筛选框是否点击过
  74. clickType: false
  75. }
  76. },
  77. onLoad(options) {
  78. this.flag = options.flag
  79. if (this.flag == 1) {
  80. uni.setNavigationBarTitle({
  81. title: '添加打卡时间'
  82. })
  83. } else {
  84. uni.setNavigationBarTitle({
  85. title: '编辑打卡时间'
  86. })
  87. this.index = options.index
  88. this.info = JSON.parse(options.info)
  89. // console.log(this.info);
  90. this.list = this.info.periods
  91. this.list.forEach(ele => {
  92. ele.beginTime = this.format_time(ele.beginTime)
  93. ele.endTime = this.format_time(ele.endTime)
  94. })
  95. this.selectedWeeks = this.info.dayOfWeeks.map(ele => {
  96. if (ele == 7) {
  97. return (ele = 0)
  98. } else {
  99. return ele
  100. }
  101. })
  102. }
  103. },
  104. methods: {
  105. // 保存按钮回调
  106. handleSave() {
  107. if (!this.selectedWeeks.length) {
  108. uni.showToast({
  109. title: '请选择需要打卡的星期',
  110. icon: 'none'
  111. })
  112. return
  113. }
  114. if (!this.list.length) {
  115. uni.showToast({
  116. title: '请添加需要打卡的时间段',
  117. icon: 'none'
  118. })
  119. return
  120. }
  121. if (!this.clickType && this.flag == 1) {
  122. uni.showToast({
  123. title: '打卡开始时间和结束时间不能相同',
  124. icon: 'none'
  125. })
  126. return
  127. }
  128. uni.showModal({
  129. title: '提示',
  130. content: `确定保存吗`,
  131. success: res => {
  132. if (res.confirm) {
  133. uni.showToast({
  134. title: '保存成功',
  135. icon: 'success'
  136. })
  137. setTimeout(() => {
  138. let temArr = []
  139. temArr = this.list
  140. temArr.forEach(ele => {
  141. ele.beginTime = this.formatTime(ele.beginTime)
  142. ele.endTime = this.formatTime(ele.endTime)
  143. })
  144. let temArr_week = []
  145. temArr_week = this.selectedWeeks.map(ele => {
  146. if (ele == 0) {
  147. return (ele = 7)
  148. } else {
  149. return ele
  150. }
  151. })
  152. let temList = uni.getStorageSync('ruleTime') || []
  153. // 编辑时间
  154. if (this.flag == 2) {
  155. temList.splice(this.index, 1, {
  156. dayOfWeeks: temArr_week,
  157. periods: temArr
  158. })
  159. } else {
  160. // 添加时间
  161. temList.push({
  162. dayOfWeeks: temArr_week,
  163. periods: temArr
  164. })
  165. }
  166. uni.setStorageSync('ruleTime', temList)
  167. uni.navigateBack({
  168. delta: 1
  169. })
  170. }, 1500)
  171. }
  172. }
  173. })
  174. },
  175. // 选择星期回调
  176. changeWeek(value) {
  177. // 把0变成7
  178. value = value.map(ele => {
  179. if (ele == 0) {
  180. return (ele = 7)
  181. } else {
  182. return ele
  183. }
  184. })
  185. // 从小到大排序
  186. value.sort((a, b) => {
  187. return a - b
  188. })
  189. this.selectedWeeks = value
  190. },
  191. // 选择时间段回调
  192. bindTimeChange(e, val, item) {
  193. // console.log(e.detail.value);
  194. this.clickType = true
  195. if (val == 1) {
  196. item.beginTime = e.detail.value
  197. } else {
  198. item.endTime = e.detail.value
  199. }
  200. let timeFlag = this.compareTime(item.beginTime, item.endTime)
  201. if (!timeFlag) {
  202. uni.showModal({
  203. content: '打卡结束时间不能小于或等于开始时间',
  204. showCancel: false
  205. })
  206. }
  207. },
  208. // 如果time2大于time1 返回true 否则 返回false
  209. compareTime(time1, time2) {
  210. if (this.time_to_sec(time2) - this.time_to_sec(time1) > 0) {
  211. return true
  212. }
  213. return false
  214. },
  215. //将时分转为时间戳
  216. time_to_sec(time) {
  217. if (time !== null) {
  218. var s = ''
  219. var hour = time.split(':')[0]
  220. var min = time.split(':')[1]
  221. s = Number(hour * 3600) + Number(min * 60)
  222. return s
  223. }
  224. },
  225. // 添加时段回调
  226. handleAddTime() {
  227. this.list.push({
  228. beginTime: '00:00',
  229. endTime: '00:00'
  230. })
  231. },
  232. // 点击右侧删除按钮回调
  233. onClick(index) {
  234. uni.showModal({
  235. title: '提示',
  236. content: '确定删除该打卡时间段吗?',
  237. success: res => {
  238. if (res.confirm) {
  239. this.list.splice(index, 1)
  240. uni.showToast({
  241. title: '删除成功',
  242. icon: 'success'
  243. })
  244. }
  245. }
  246. })
  247. },
  248. // 格式化时间
  249. formatTime(val) {
  250. let tem = '2023/01/12 ' + val + ':00'
  251. // console.log(tem);
  252. let date = new Date(tem)
  253. // let time = date.getTime();
  254. let time = Date.parse(date)
  255. return time
  256. },
  257. // 格式化时间
  258. format_time(timestamp) {
  259. //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  260. var date = new Date(timestamp)
  261. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  262. var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  263. let strDate = h + m
  264. return strDate
  265. }
  266. }
  267. }
  268. </script>
  269. <style lang="scss" scoped>
  270. .container {
  271. min-width: 100vw;
  272. min-height: 100vh;
  273. background-color: #f2f2f2;
  274. .placeholder {
  275. height: 33rpx;
  276. }
  277. .week_title {
  278. margin-left: 35rpx;
  279. font-size: 34rpx;
  280. font-weight: 500;
  281. }
  282. .week {
  283. margin: 0 auto;
  284. margin-top: 12rpx;
  285. width: 690rpx;
  286. height: 107rpx;
  287. border-radius: 10rpx;
  288. background-color: #fff;
  289. }
  290. .time_list {
  291. display: flex;
  292. align-items: center;
  293. margin: 0 auto;
  294. margin-top: 32rpx;
  295. width: 690rpx;
  296. height: 60rpx;
  297. font-weight: 500;
  298. .title {
  299. flex: 4;
  300. font-size: 34rpx;
  301. }
  302. .add {
  303. flex: 1;
  304. text-align: end;
  305. font-size: 24rpx;
  306. color: #3396fb;
  307. }
  308. }
  309. .list {
  310. margin: 0 auto;
  311. margin-top: 13rpx;
  312. padding-top: 20rpx;
  313. padding-bottom: 20rpx;
  314. width: 690rpx;
  315. border-radius: 10rpx;
  316. background-color: #fff;
  317. .item {
  318. display: flex;
  319. justify-content: space-between;
  320. margin: 0 20rpx 20rpx 20rpx;
  321. width: 650rpx;
  322. height: 60rpx;
  323. .item_box {
  324. width: 291rpx;
  325. height: 60rpx;
  326. border-radius: 8rpx;
  327. border: 1rpx solid #cccccc;
  328. .uni-input {
  329. display: flex;
  330. align-items: center;
  331. width: 291rpx;
  332. height: 60rpx;
  333. .input_time {
  334. flex: 2;
  335. margin-left: 34rpx;
  336. font-size: 28rpx;
  337. color: #707070;
  338. }
  339. .input_icon {
  340. flex: 1;
  341. display: flex;
  342. justify-content: center;
  343. align-items: center;
  344. img {
  345. width: 26rpx;
  346. height: 26rpx;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. // .edit {
  353. // margin-left: 20rpx;
  354. // padding-top: 10rpx;
  355. // width: 70rpx;
  356. // height: 60rpx;
  357. // color: #3396FB;
  358. // font-size: 24rpx;
  359. // font-weight: 500;
  360. // }
  361. }
  362. .button {
  363. margin: 0 auto;
  364. margin-top: 50rpx;
  365. width: 690rpx;
  366. height: 80rpx;
  367. line-height: 80rpx;
  368. text-align: center;
  369. color: #fff;
  370. font-size: 32rpx;
  371. border-radius: 16rpx;
  372. background-color: #3396fb;
  373. }
  374. }
  375. // 选择星期区域圆角效果
  376. ::v-deep .weeks-outer {
  377. border-radius: 10rpx;
  378. }
  379. // 解决左滑区域突出问题
  380. ::v-deep .uni-swipe_button-group {
  381. margin-bottom: 20rpx;
  382. }
  383. </style>