setPunchTime.vue 9.0 KB

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