addStudentMsg.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <template>
  2. <view class="container">
  3. <!-- 姓名区域 -->
  4. <view class="box">
  5. <view class="box_key">
  6. <text class="text">*</text>
  7. 孩子姓名
  8. </view>
  9. <view class="box_value">
  10. <input class="input" type="text" placeholder="请输入孩子姓名" v-model="name" />
  11. </view>
  12. </view>
  13. <!-- 性别区域 -->
  14. <view class="box">
  15. <view class="box_key">
  16. <text class="text">*</text>
  17. 性别
  18. </view>
  19. <view class="box_value">
  20. <view class="men" @click="handlegender(1)">
  21. <radio color="#0061FF" style="transform: scale(0.7)" :checked="gender == 1" />
  22. </view>
  23. <view class="women" @click="handlegender(2)">
  24. <radio color="#0061FF" style="transform: scale(0.7)" :checked="gender == 2" />
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 人脸图片区域 -->
  29. <view class="box2">
  30. <view class="box_key">
  31. <text class="text">*</text>
  32. 人脸图片
  33. </view>
  34. <view v-if="!headImage" class="box_upload" @click="handleUpload">
  35. <uni-icons type="plusempty" size="30" color="#666666"></uni-icons>
  36. 上传照片
  37. </view>
  38. <view v-else class="box_upload">
  39. <image :src="headImage" mode="aspectFill" style="width: 160rpx; height: 160rpx" @click="clickImg(headImage)"></image>
  40. <!-- 图片删除图标 -->
  41. <view class="delete" @click="deleteImg">
  42. <uni-icons type="close" size="20"></uni-icons>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="tips">注:支持.jpg .png,五官清晰无遮挡,大小不超过2M</view>
  47. <!-- 时间组区域 -->
  48. <view class="box">
  49. <view class="box_key">
  50. <text class="text">*</text>
  51. 时间组
  52. </view>
  53. <picker @change="bindPickerChange" :value="currentIndex" :range="timeGroups" range-key="name">
  54. <view class="box_value" :class="{ unactive: !currentIndex }">
  55. {{ currentIndex ? timeGroups[currentIndex].name : '请选择' }}
  56. <image class="value_img" src="/static/images/bottom2.png" mode="aspectFill"></image>
  57. </view>
  58. </picker>
  59. </view>
  60. <!-- 家属列表区域 -->
  61. <view v-for="(item, index) in list" :key="index">
  62. <!-- 家属区域 -->
  63. <view class="box">
  64. <view class="box_key">
  65. <text class="text">*</text>
  66. 家属{{ index + 1 }}
  67. </view>
  68. <view class="box_value">
  69. <input v-model="item.name" class="input" type="text" placeholder="请输入称呼或职业" />
  70. <view class="value_icon">
  71. <uni-icons v-if="index == 0" type="plus" size="25" color="#0061FF" @click="handleAdd"></uni-icons>
  72. <uni-icons v-else type="close" size="25" color="#0061FF" @click="handleDelete(index)"></uni-icons>
  73. </view>
  74. </view>
  75. </view>
  76. <!-- 手机号码区域 -->
  77. <view class="box">
  78. <view class="box_key">
  79. <text class="text">*</text>
  80. 手机号码
  81. </view>
  82. <view class="box_value">
  83. <input v-model="item.phone" class="input" type="text" placeholder="请输入手机号码" />
  84. </view>
  85. </view>
  86. <!-- 家属与本人的关系区域 -->
  87. <view class="box">
  88. <view class="box_key">
  89. <text class="text">*</text>
  90. 家属与本人的关系
  91. </view>
  92. <view class="box_value">
  93. <input v-model="item.ship" class="input" type="text" placeholder="请输入家属与本人的关系" />
  94. </view>
  95. </view>
  96. </view>
  97. <!-- 确认按钮区域 -->
  98. <view class="btn" @click="handleConfirm">确认</view>
  99. <!-- 用于图片压缩的canvas画布 -->
  100. <canvas
  101. :style="{
  102. width: cw + 'px',
  103. height: cw + 'px',
  104. position: 'absolute',
  105. zIndex: -1,
  106. left: '-10000rpx',
  107. top: '-10000rpx'
  108. }"
  109. canvas-id="zipCanvas"
  110. ></canvas>
  111. <!--画布结束-->
  112. </view>
  113. </template>
  114. <script setup>
  115. import { ref } from 'vue'
  116. import { onLoad } from '@dcloudio/uni-app'
  117. import { myRequest } from '@/utils/api.js'
  118. import { decryptDes } from '@/utils/des.js'
  119. import getLessLimitSizeImage from '@/utils/imageCompress.js'
  120. // 姓名
  121. const name = ref()
  122. // 性别
  123. const gender = ref(1)
  124. // 照片
  125. const headImage = ref('')
  126. // 时间组当前激活索引
  127. const currentIndex = ref('')
  128. // 时间组数据
  129. const timeGroups = ref([])
  130. // 家属列表
  131. const list = ref([
  132. {
  133. name: '',
  134. phone: '',
  135. ship: '',
  136. departmentId: uni.getStorageSync('departmentId_list')
  137. }
  138. ])
  139. //画板边长默认是屏幕宽度,正方形画布
  140. const cw = uni.getSystemInfoSync().windowWidth
  141. onLoad(() => {
  142. // 获取时间组列表数据
  143. getTimeGroups()
  144. })
  145. // 获取时间组列表数据
  146. const getTimeGroups = async () => {
  147. const res = await myRequest({
  148. url: '/wanzai/api/smartUser/timeGroups'
  149. })
  150. // console.log(res)
  151. const result = JSON.parse(decryptDes(res.data))
  152. // console.log(result)
  153. timeGroups.value = result
  154. }
  155. // 切换性别回调
  156. const handlegender = (v) => {
  157. gender.value = v
  158. }
  159. // 切换时间组回调
  160. const bindPickerChange = (e) => {
  161. currentIndex.value = e.detail.value
  162. }
  163. // 上传照片回调
  164. const handleUpload = () => {
  165. uni.chooseMedia({
  166. count: 1,
  167. mediaType: ['image'],
  168. sourceType: ['album'],
  169. sizeType: ['compressed'],
  170. success: (res) => {
  171. // console.log(res.tempFiles)
  172. res.tempFiles.forEach((ele) => {
  173. if (ele.fileType === 'image' && ele.size > 1024 * 1024 * 2) {
  174. //这里的id和页面中写的html代码的canvas的id要一致
  175. let canvasId = 'zipCanvas'
  176. //原图的路径
  177. let imagePath = ele.tempFilePath
  178. //大小限制1024kb
  179. let limitSize = 1024 * 2
  180. //初始绘画区域是画布自身的宽度也就是屏幕宽度
  181. let drawWidth = uni.getSystemInfoSync().windowWidth
  182. // 图片过大压缩
  183. getLessLimitSizeImage(canvasId, imagePath, limitSize, drawWidth, (resPath) => {
  184. handleUploadMini({ tempFilePath: resPath, fileType: ele.fileType })
  185. })
  186. } else {
  187. handleUploadMini(ele)
  188. }
  189. })
  190. }
  191. })
  192. }
  193. const handleUploadMini = (ele) => {
  194. uni.showLoading({
  195. title: '上传中,请稍后',
  196. mask: true
  197. })
  198. uni.uploadFile({
  199. url: `https://www.campussmartlife.com/smartApi/wanzai/api/upload/toCos`,
  200. filePath: ele.tempFilePath,
  201. name: 'files',
  202. header: {
  203. token: uni.getStorageSync('token') || '',
  204. user_head: uni.getStorageSync('userhead') || ''
  205. },
  206. success: (uploadFileRes) => {
  207. // console.log(uploadFileRes)
  208. let imgUrl = JSON.parse(uploadFileRes.data).data
  209. const result = JSON.parse(decryptDes(imgUrl))
  210. console.log(result)
  211. headImage.value = result
  212. },
  213. fail: () => {
  214. uni.showToast({
  215. title: '上传失败',
  216. icon: 'error'
  217. })
  218. },
  219. complete: () => {
  220. uni.hideLoading()
  221. }
  222. })
  223. }
  224. // 家属添加图标回调
  225. const handleAdd = () => {
  226. list.value.push({
  227. name: '',
  228. phone: '',
  229. ship: '',
  230. departmentId: uni.getStorageSync('departmentId_list')
  231. })
  232. }
  233. // 家属删除图标回调
  234. const handleDelete = (index) => {
  235. // console.log(index)
  236. uni.showModal({
  237. title: '提示',
  238. content: '确定删除该家属吗?',
  239. success: (res) => {
  240. if (res.confirm) {
  241. list.value.splice(index, 1)
  242. }
  243. }
  244. })
  245. }
  246. // 确认按钮回调
  247. const handleConfirm = () => {
  248. const flag = verify()
  249. if (flag) {
  250. uni.showModal({
  251. title: '提示',
  252. content: '确定添加该学生吗?',
  253. success: (res) => {
  254. if (res.confirm) {
  255. handleConfirmReq()
  256. }
  257. }
  258. })
  259. }
  260. }
  261. // 验证提交数据是否合法
  262. const verify = () => {
  263. if (!name.value) {
  264. uni.showToast({
  265. title: '请输入姓名',
  266. icon: 'none'
  267. })
  268. return false
  269. }
  270. let reg_name = /^[\u4e00-\u9fa5]{2,4}$/
  271. if (!reg_name.test(name.value)) {
  272. uni.showToast({
  273. title: '姓名格式错误',
  274. icon: 'none'
  275. })
  276. return false
  277. }
  278. if (!headImage.value) {
  279. uni.showToast({
  280. title: '请上传图片',
  281. icon: 'none'
  282. })
  283. return false
  284. }
  285. if (!currentIndex.value) {
  286. uni.showToast({
  287. title: '请选择时间组',
  288. icon: 'none'
  289. })
  290. return false
  291. }
  292. let res = list.value.some((ele) => !ele.name || !ele.phone || !ele.ship)
  293. if (res) {
  294. uni.showToast({
  295. title: '家属数据不能为空',
  296. icon: 'none'
  297. })
  298. return false
  299. }
  300. let res2 = list.value.some((ele) => !reg_name.test(ele.name))
  301. if (res2) {
  302. uni.showToast({
  303. title: '家属姓名格式错误',
  304. icon: 'none'
  305. })
  306. return false
  307. }
  308. let reg_phone = /^1[3-9]\d{9}$/
  309. let res3 = list.value.some((ele) => !reg_phone.test(ele.phone))
  310. if (res3) {
  311. uni.showToast({
  312. title: '家属手机号码格式错误',
  313. icon: 'none'
  314. })
  315. return false
  316. }
  317. return true
  318. }
  319. // 确认请求
  320. const handleConfirmReq = async () => {
  321. let data = {
  322. name: name.value,
  323. sexId: gender.value,
  324. headImage: headImage.value,
  325. departmentId: uni.getStorageSync('departmentId'),
  326. gradeId: uni.getStorageSync('grade_student'),
  327. classId: uni.getStorageSync('classes_student'),
  328. timeGroupId: timeGroups.value[currentIndex.value].id,
  329. list: list.value
  330. }
  331. // console.log(data)
  332. const res = await myRequest({
  333. url: '/wanzai/api/smartEnrollmentUser/insertTeacherSmartEnrollmentUser',
  334. method: 'post',
  335. data
  336. })
  337. // console.log(res)
  338. if (res.code == 200) {
  339. uni.showToast({
  340. title: '添加成功',
  341. icon: 'success'
  342. })
  343. setTimeout(() => {
  344. uni.reLaunch({
  345. url: '/pages/studentManage/studentManage'
  346. })
  347. }, 1500)
  348. }
  349. }
  350. // 点击图片回调
  351. const clickImg = (url) => {
  352. uni.previewImage({
  353. urls: [url],
  354. current: 1
  355. })
  356. }
  357. // 删除图片回调
  358. const deleteImg = () => {
  359. uni.showModal({
  360. title: '提示',
  361. content: '确定删除该图片吗?',
  362. success: (res) => {
  363. if (res.confirm) {
  364. headImage.value = ''
  365. }
  366. }
  367. })
  368. }
  369. </script>
  370. <style lang="scss" scoped>
  371. .container {
  372. box-sizing: border-box;
  373. padding: 10rpx 0 30rpx 15rpx;
  374. min-height: 100vh;
  375. font-size: 28rpx;
  376. .box {
  377. display: flex;
  378. justify-content: space-between;
  379. align-items: center;
  380. height: 96rpx;
  381. border-bottom: 2rpx solid #e6e6e6;
  382. .box_key {
  383. display: flex;
  384. align-items: center;
  385. font-weight: bold;
  386. .text {
  387. color: #d43030;
  388. }
  389. }
  390. .box_value {
  391. display: flex;
  392. align-items: center;
  393. justify-content: flex-end;
  394. padding-right: 20rpx;
  395. width: 320rpx;
  396. height: 96rpx;
  397. .input {
  398. height: 100%;
  399. text-align: end;
  400. }
  401. .men,
  402. .women {
  403. display: flex;
  404. align-items: center;
  405. }
  406. .women {
  407. margin-left: 20rpx;
  408. }
  409. .value_icon {
  410. margin-left: 15rpx;
  411. }
  412. .value_img {
  413. margin-left: 20rpx;
  414. width: 27rpx;
  415. height: 16rpx;
  416. }
  417. }
  418. .unactive {
  419. color: #6a6a6a;
  420. }
  421. }
  422. .box2 {
  423. display: flex;
  424. margin-top: 40rpx;
  425. .box_key {
  426. display: flex;
  427. font-weight: bold;
  428. .text {
  429. color: #d43030;
  430. }
  431. }
  432. .box_upload {
  433. position: relative;
  434. display: flex;
  435. flex-direction: column;
  436. justify-content: center;
  437. align-items: center;
  438. margin-left: 30rpx;
  439. width: 160rpx;
  440. height: 160rpx;
  441. font-size: 24rpx;
  442. color: #666666;
  443. border-radius: 6rpx;
  444. background-color: #f2f2f2;
  445. .delete {
  446. position: absolute;
  447. right: 10rpx;
  448. top: 10rpx;
  449. }
  450. }
  451. }
  452. .tips {
  453. margin-top: 20rpx;
  454. margin-bottom: 5rpx;
  455. font-size: 24rpx;
  456. color: #a6a6a6;
  457. }
  458. .btn {
  459. display: flex;
  460. justify-content: center;
  461. align-items: center;
  462. margin-top: 100rpx;
  463. width: 710rpx;
  464. height: 100rpx;
  465. font-size: 32rpx;
  466. color: #fff;
  467. border-radius: 8rpx;
  468. background-color: #0061ff;
  469. }
  470. }
  471. </style>