setPunchTime.vue 7.9 KB

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