leaveForm.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="content">
  3. <u-form :model="form" ref="form" label-width="140">
  4. <u-form-item label="请假类型" prop="type" required>
  5. <u-input v-model="form.type" placeholder="请选择请假类型" :select-open="leaveTypeShow" type="select" @click="leaveTypeShow = true" />
  6. </u-form-item>
  7. <u-form-item :label="form.type === '病假'?'开始时间':'请假时间'" prop="leaveTime" required>
  8. <u-input v-model="form.leaveTime" placeholder="请选择请假时间" :select-open="leaveTimeShow" type="select" @click="leaveTimeShow = true" />
  9. </u-form-item>
  10. <u-form-item label="上传证明" v-if="form.type === '病假'" prop="leavePic" required>
  11. <u-upload :action="action" :header="header" :file-list="fileList" max-count="1" :multiple="false" :show-progress="false" @on-success="handlePicSuccess"></u-upload>
  12. </u-form-item>
  13. <u-form-item label="请假理由" prop="leaveReason" required>
  14. <u-input v-model="form.leaveReason" placeholder="请输入请假理由" type="text" />
  15. </u-form-item>
  16. <u-form-item v-if="form.type === '病假'">
  17. 学员重新上课时,教练进行结束病假
  18. </u-form-item>
  19. </u-form>
  20. <u-picker mode="selector" v-model="leaveTypeShow" :range="selectorObj" range-key="cateName" @confirm="handleGetType"></u-picker>
  21. <u-calendar v-model="leaveTimeShow" mode="date" :active-bg-color="mainColor" btn-type="error" availableText="有课"
  22. :available="availableList" activeText="已请假" :activeList="activeList" @change="setLeaveTime" max-date="2300-12-31"></u-calendar>
  23. <view class="handle-fix-box">
  24. <u-button type="warning" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitForm()">提交</u-button>
  25. </view>
  26. <u-top-tips ref="uTips"></u-top-tips>
  27. </view>
  28. </template>
  29. <script>
  30. import {
  31. mapGetters
  32. } from 'vuex'
  33. const NET = require('@/utils/request')
  34. const API = require('@/config/api')
  35. export default {
  36. computed: {
  37. ...mapGetters([
  38. 'mainColor',
  39. 'customStyle',
  40. ])
  41. },
  42. data() {
  43. return {
  44. action: API.uploadFile,
  45. header: {
  46. Authorization: uni.getStorageSync('token')
  47. },
  48. classId: '',
  49. studentId: '',
  50. form: {
  51. type: '病假',
  52. leaveTime: '',
  53. leavePic: '',
  54. leaveReason: '',
  55. },
  56. rules: {
  57. leaveTime: [{
  58. required: true,
  59. message: '请选择请假时间',
  60. trigger: 'change'
  61. }],
  62. leaveReason: [{
  63. required: true,
  64. message: '请输入请假理由',
  65. trigger: 'change'
  66. }],
  67. leavePic: [{
  68. required: true,
  69. message: '请上传证明照片',
  70. trigger: 'change'
  71. }],
  72. },
  73. leaveTypeShow: false,
  74. selectorObj: [
  75. {
  76. cateName: '病假',
  77. type: 1
  78. },
  79. {
  80. cateName: '事假',
  81. type: 2
  82. }
  83. ],
  84. leaveTimeShow: false,
  85. availableList: [],
  86. activeList: [],
  87. fileList: []
  88. }
  89. },
  90. onLoad(options) {
  91. this.classId = options.id
  92. this.studentId = options.studentId
  93. NET.request(API.getLeaveList, {
  94. id: this.classId
  95. }, 'POST').then(res => {
  96. // type: 0:有课 1:请假
  97. // 有课的日期数据
  98. this.availableList = res.data.filter(site => site.type == 0).map(site => {
  99. return this.formatDatedel0(site.lastDate)
  100. })
  101. // 请假的日期数据
  102. this.activeList = res.data.filter(site => site.type == 1).map(site => {
  103. return this.formatDatedel0(site.lastDate)
  104. })
  105. console.log(this.activeList)
  106. }).catch(error => {
  107. this.$refs.uTips.show({
  108. title: error.message,
  109. type: 'warning',
  110. })
  111. })
  112. },
  113. onReady() {
  114. this.$refs.form.setRules(this.rules);
  115. },
  116. methods: {
  117. // 选择请假类型
  118. handleGetType(args) {
  119. this.form.type = this.selectorObj[args[0]]['cateName']
  120. this.form.leaveTime = ''
  121. this.form.leavePic = ''
  122. this.form.leaveReason = ''
  123. // 移除表单校验结果
  124. this.$nextTick(() => {
  125. this.$refs.form.resetFields()
  126. })
  127. },
  128. // 图片上传成功
  129. handlePicSuccess(args) {
  130. if(args.status == 10000) {
  131. this.form.leavePic = args.data.url
  132. } else {
  133. this.$refs.uTips.show({
  134. title: args.message,
  135. type: 'warning',
  136. })
  137. }
  138. },
  139. // 设置请假日期
  140. setLeaveTime(object) {
  141. // 今天0点的时间戳
  142. let d = new Date()
  143. let year = d.getFullYear()
  144. let month = d.getMonth() + 1
  145. let day = d.getDate()
  146. let dateNow = year+'/'+month +'/'+day
  147. const nowTimeStamp = this.dateFormatTimeStamp(dateNow)
  148. console.log(nowTimeStamp)
  149. // 今天后3天的时间戳
  150. const nextThreeDayTimeStamp = nowTimeStamp + 86400
  151. let dateIos = object.year + '/' + object.month + '/' + object.day
  152. let date = object.year + '-' + object.month + '-' + object.day
  153. // 当天是否可请假
  154. if (this.availableList.filter(site => site == date).length) {
  155. // 是否为病假
  156. if(this.form.type === '事假') {
  157. // 病假需提前3天请
  158. if(this.dateFormatTimeStamp(dateIos) >= nextThreeDayTimeStamp) {
  159. this.form.leaveTime = date
  160. } else {
  161. this.form.leaveTime = ''
  162. this.$refs.uTips.show({
  163. title: '事假需提前1天请',
  164. type: 'error',
  165. })
  166. }
  167. } else {
  168. let dateStr=this.dateFormatTimeStamp(dateIos)
  169. console.log(dateStr)
  170. if(dateStr >= nowTimeStamp) {
  171. this.form.leaveTime = date
  172. } else {
  173. this.form.leaveTime = ''
  174. this.$refs.uTips.show({
  175. title: '不能选择今天之前',
  176. type: 'error',
  177. })
  178. }
  179. }
  180. } else {
  181. this.form.leaveTime = ''
  182. this.$refs.uTips.show({
  183. title: '请选择可用日期',
  184. type: 'error',
  185. })
  186. }
  187. },
  188. // 日期转时间戳
  189. dateFormatTimeStamp(date = null,second = true) {
  190.     if (!date) return parseInt(new Date().getTime() / (second ? 1000 : 1))
  191.     return parseInt(new Date(date).getTime() / (second ? 1000 : 1))
  192. },
  193. formatDate(str) {
  194. // 根据 - 符号拆分
  195. return str
  196. .split("-")
  197. .map((item) => {
  198. // +item 将item字符串转换为数字
  199. // 小于10的时候就补全一个前缀0
  200. if (+item < 10) {
  201. return "0" + +item;
  202. }
  203. // 大于10的时候不用补0
  204. return item;
  205. })
  206. .join("-"); // 最后重组回来
  207. },
  208. formatDatedel0(str) {
  209. // 根据 - 符号拆分
  210. return str
  211. .split("-")
  212. .map((item) => {
  213. // +item 将item字符串转换为数字
  214. // 小于10的时候就补全一个前缀0
  215. if (+item < 10) {
  216. return Number(item);
  217. }
  218. // 大于10的时候不用补0
  219. return item;
  220. })
  221. .join("-"); // 最后重组回来
  222. },
  223. // 提交表单
  224. submitForm() {
  225. this.$refs.form.validate(valid => {
  226. if (valid) {
  227. this.form.leaveTime=this.formatDate(this.form.leaveTime)
  228. NET.request(API.submitLevelForm, {
  229. classId: this.classId,
  230. studentId: this.studentId,
  231. ...{ ...this.form, type: this.form.type == '事假' ? 1 : 2 }
  232. }, 'POST').then(res => {
  233. this.$refs.uTips.show({
  234. title: '请假成功',
  235. type: 'success',
  236. })
  237. setTimeout(() => {
  238. uni.navigateBack()
  239. }, 1000)
  240. }).catch(error => {
  241. this.$refs.uTips.show({
  242. title: error.message,
  243. type: 'warning',
  244. })
  245. })
  246. }
  247. });
  248. },
  249. },
  250. }
  251. </script>
  252. <style>
  253. page {
  254. width: 100%;
  255. height: 100%;
  256. position: relative;
  257. }
  258. </style>
  259. <style lang="scss" scoped>
  260. @import "@/static/css/themes.scss";
  261. .content {
  262. width: 100%;
  263. float: left;
  264. padding: 0 15px 60px 15px;
  265. box-sizing: border-box;
  266. }
  267. </style>