setPunchTime.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. },
  90. onLoad(options) {
  91. this.flag = options.flag
  92. if (this.flag == 1) {
  93. uni.setNavigationBarTitle({
  94. title: '添加打卡时间'
  95. });
  96. } else {
  97. uni.setNavigationBarTitle({
  98. title: '编辑打卡时间'
  99. });
  100. this.index = options.index
  101. this.info = JSON.parse(options.info)
  102. // console.log(this.info);
  103. this.list = this.info.periods
  104. this.list.forEach((ele) => {
  105. ele.beginTime = this.format_time(ele.beginTime)
  106. ele.endTime = this.format_time(ele.endTime)
  107. })
  108. this.selectedWeeks = this.info.dayOfWeeks.map((ele) => {
  109. if (ele == 7) {
  110. return ele = 0
  111. } else {
  112. return ele
  113. }
  114. })
  115. }
  116. },
  117. methods: {
  118. // 保存按钮回调
  119. handleSave() {
  120. if (!this.selectedWeeks.length) {
  121. uni.showToast({
  122. title: "请选择需要打卡的星期",
  123. icon: 'none'
  124. })
  125. return
  126. }
  127. if (!this.list.length) {
  128. uni.showToast({
  129. title: "请添加需要打卡的时间段",
  130. icon: 'none'
  131. })
  132. return
  133. }
  134. uni.showModal({
  135. title: '提示',
  136. content: '确定保存吗?',
  137. success: (res) => {
  138. if (res.confirm) {
  139. uni.showToast({
  140. title: "保存成功",
  141. icon: 'success'
  142. })
  143. setTimeout(() => {
  144. let temArr = []
  145. temArr = this.list
  146. temArr.forEach((ele) => {
  147. ele.beginTime = this.formatTime(ele.beginTime)
  148. ele.endTime = this.formatTime(ele.endTime)
  149. })
  150. let temArr_week = []
  151. temArr_week = this.selectedWeeks.map((ele) => {
  152. if (ele == 0) {
  153. return ele = 7
  154. } else {
  155. return ele
  156. }
  157. })
  158. let temList = uni.getStorageSync("ruleTime") || []
  159. // 编辑时间
  160. if (this.flag == 2) {
  161. temList.splice(this.index, 1, {
  162. dayOfWeeks: temArr_week,
  163. periods: temArr,
  164. })
  165. } else {
  166. // 添加时间
  167. temList.push({
  168. dayOfWeeks: temArr_week,
  169. periods: temArr
  170. })
  171. }
  172. uni.setStorageSync("ruleTime", temList)
  173. uni.navigateBack({
  174. delta: 1
  175. })
  176. }, 1500)
  177. }
  178. }
  179. });
  180. },
  181. // 选择星期回调
  182. changeWeek(value) {
  183. // 把0变成7
  184. value = value.map((ele) => {
  185. if (ele == 0) {
  186. return ele = 7
  187. } else {
  188. return ele
  189. }
  190. })
  191. // 从小到大排序
  192. value.sort((a, b) => {
  193. return a - b
  194. })
  195. this.selectedWeeks = value
  196. },
  197. // 选择时间段回调
  198. bindTimeChange(e, val, item) {
  199. if (val == 1) {
  200. item.beginTime = e.detail.value
  201. } else {
  202. item.endTime = e.detail.value
  203. }
  204. },
  205. // 添加时段回调
  206. handleAddTime() {
  207. this.list.push({
  208. beginTime: "00:00",
  209. endTime: "00:00",
  210. })
  211. },
  212. // 点击右侧删除按钮回调
  213. onClick(index) {
  214. uni.showModal({
  215. title: '提示',
  216. content: '确定删除该打卡时间段吗?',
  217. success: (res) => {
  218. if (res.confirm) {
  219. this.list.splice(index, 1)
  220. uni.showToast({
  221. title: "删除成功",
  222. icon: 'success'
  223. })
  224. }
  225. }
  226. });
  227. },
  228. // 格式化时间
  229. formatTime(val) {
  230. let tem = '2021-11-22 ' + val + ':00'
  231. // console.log(tem);
  232. let date = new Date(tem);
  233. let time = date.getTime();
  234. return time
  235. },
  236. // 格式化时间
  237. format_time(timestamp) {
  238. //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  239. var date = new Date(timestamp);
  240. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  241. var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  242. let strDate = h + m;
  243. return strDate;
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .container {
  250. min-width: 100vw;
  251. min-height: 100vh;
  252. background-color: #F2F2F2;
  253. .placeholder{
  254. height: 33rpx;
  255. }
  256. .week_title {
  257. margin-left: 35rpx;
  258. font-size: 34rpx;
  259. font-weight: 500;
  260. }
  261. .week {
  262. margin: 0 auto;
  263. margin-top: 12rpx;
  264. width: 690rpx;
  265. height: 107rpx;
  266. border-radius: 10rpx;
  267. background-color: #fff;
  268. }
  269. .time_list {
  270. display: flex;
  271. align-items: center;
  272. margin: 0 auto;
  273. margin-top: 32rpx;
  274. width: 690rpx;
  275. height: 60rpx;
  276. font-weight: 500;
  277. .title {
  278. flex: 4;
  279. font-size: 34rpx;
  280. }
  281. .add {
  282. flex: 1;
  283. text-align: end;
  284. font-size: 24rpx;
  285. color: #3396FB;
  286. }
  287. }
  288. .list {
  289. margin: 0 auto;
  290. margin-top: 13rpx;
  291. padding-top: 20rpx;
  292. padding-bottom: 20rpx;
  293. width: 690rpx;
  294. border-radius: 10rpx;
  295. background-color: #fff;
  296. .item {
  297. display: flex;
  298. justify-content: space-between;
  299. margin: 0 20rpx 20rpx 20rpx;
  300. width: 650rpx;
  301. height: 60rpx;
  302. .item_box {
  303. width: 291rpx;
  304. height: 60rpx;
  305. border-radius: 8rpx;
  306. border: 1rpx solid #CCCCCC;
  307. .uni-input {
  308. display: flex;
  309. align-items: center;
  310. width: 291rpx;
  311. height: 60rpx;
  312. .input_time {
  313. flex: 2;
  314. margin-left: 34rpx;
  315. font-size: 28rpx;
  316. color: #707070;
  317. }
  318. .input_icon {
  319. flex: 1;
  320. display: flex;
  321. justify-content: center;
  322. align-items: center;
  323. img {
  324. width: 26rpx;
  325. height: 26rpx;
  326. }
  327. }
  328. }
  329. }
  330. }
  331. // .edit {
  332. // margin-left: 20rpx;
  333. // padding-top: 10rpx;
  334. // width: 70rpx;
  335. // height: 60rpx;
  336. // color: #3396FB;
  337. // font-size: 24rpx;
  338. // font-weight: 500;
  339. // }
  340. }
  341. .button {
  342. margin: 0 auto;
  343. margin-top: 50rpx;
  344. width: 690rpx;
  345. height: 80rpx;
  346. line-height: 80rpx;
  347. text-align: center;
  348. color: #fff;
  349. font-size: 32rpx;
  350. border-radius: 16rpx;
  351. background-color: #3396FB;
  352. }
  353. }
  354. // 选择星期区域圆角效果
  355. ::v-deep .weeks-outer {
  356. border-radius: 10rpx;
  357. }
  358. // 解决左滑区域突出问题
  359. ::v-deep .uni-swipe_button-group {
  360. margin-bottom: 20rpx;
  361. }
  362. </style>