bind.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <template>
  2. <view class="page">
  3. <!-- #ifdef APP-PLUS || H5 -->
  4. <!-- 状态栏占位 -->
  5. <view class="status_bar"></view>
  6. <view class="" style="margin-top: var(--status-bar-height);">
  7. <uni-nav-bar title="" left-icon="left" backgroundColor="#F35546" color="#fff" @clickLeft="back"></uni-nav-bar>
  8. </view>
  9. <view class="content-box">
  10. <u-form :model="formData" ref="uForm" label-align="right" label-width="50">
  11. <u-form-item label="ID号"><u-input v-model="formData.deviceCode" /></u-form-item>
  12. </u-form>
  13. <view class="form-box">
  14. <u-form :model="formData" ref="uForm" label-align="right" label-width="50">
  15. <u-form-item label="照片">
  16. <view class="pictrue" @click.stop='uploadpic'>
  17. <image :src='newAvatar ? newAvatar : formData.avatar'></image>
  18. <image src='../../static/images/alter.png' class="alter"></image>
  19. </view>
  20. </u-form-item>
  21. <u-form-item label="姓名"><u-input v-model="formData.nickName" placeholder="请输入姓名"/></u-form-item>
  22. <u-form-item label="证件号"><u-input v-model="formData.cardNumber" placeholder="请输入证件号" @blur="cardNumberBlur"/></u-form-item>
  23. <u-form-item label="性别" right-icon="arrow-right"><u-input v-model="formData.sexValue" placeholder="请选择性别" disabled/></u-form-item>
  24. <u-form-item label="生日" right-icon="arrow-right"><u-input v-model="formData.birthday" placeholder="请选择生日" disabled/></u-form-item>
  25. <u-form-item label="手机号"><u-input v-model="formData.tel" @blur="telBlur" placeholder="请输入手机号"/></u-form-item>
  26. </u-form>
  27. </view>
  28. <view class="form-box">
  29. <u-form :model="formData" ref="uForm" label-align="right" label-width="50">
  30. <!-- 问题:u-input在disabled得情况下点击事件失效 -->
  31. <u-form-item label="关系" right-icon="arrow-right" @click.native="goChooseRelation">
  32. <u-input disabled v-model="formData.relationValue" placeholder="请选择关系" input-align="right" />
  33. </u-form-item>
  34. </u-form>
  35. </view>
  36. </view>
  37. <!-- #endif -->
  38. <!-- #ifdef MP -->
  39. <view class="content_box">
  40. <van-field v-model="formData.deviceCode" readonly name="ID号" label="ID号" placeholder="ID号" />
  41. <view class="content">
  42. <van-form @submit="bindEqu" ref="formData" :model="formData" label-width="48" :colon="true">
  43. <van-field class="btn" v-model="formData.photo" name="照片" label="照片" placeholder="照片" readonly
  44. @click.native.stop.prevent="uploadpic" input-align="right">
  45. <template #button>
  46. <image src="/static/img/arrow.png" mode=""></image>
  47. </template>
  48. </van-field>
  49. <van-field v-model="formData.nickName" name="姓名" label="姓名" placeholder="请输入姓名"
  50. :rules="[{ required: true, message: '请填写姓名' }]" @change="formData.nickName = $event.detail" />
  51. <van-field v-model="formData.cardNumber" name="证件号" label="证件号" placeholder="请输入证件号"
  52. :rules="[{ required: true, message: '请填写证件号' }]" @blur="cardNumberBlur"
  53. @change="formData.cardNumber = $event.detail" />
  54. <van-field class="btn" readonly clickable name="picker" :value="formData.sexValue" label="性别"
  55. placeholder="请选择性别">
  56. <template #button>
  57. <image src="/static/img/arrow.png" mode=""></image>
  58. </template>
  59. </van-field>
  60. <van-field class="btn" readonly clickable name="calendar" :value="formData.birthday" label="生日"
  61. placeholder="年/月/日" @click.native.stop.prevent="showCalendarFn">
  62. <template #button>
  63. <image src="/static/image/calendar.png" mode=""></image>
  64. </template>
  65. </van-field>
  66. <van-field v-model="formData.tel" name="手机号" label="手机号" placeholder="请输入手机号"
  67. :rules="[{ required: true, message: '请填写手机号' }]" @blur="telBlur"
  68. @change="formData.tel = $event.detail" />
  69. </van-form>
  70. <van-popup :show="showPicker" position="bottom">
  71. <van-picker show-toolbar :columns="columns" @confirm="pickerConfirm" @cancel="showPicker = false" />
  72. </van-popup>
  73. <van-popup :show="showCalendar" position="bottom">
  74. <van-datetime-picker @cancel="showCalendar=false" @confirm="calendarConfirm" :value="currentDay"
  75. type="date" :min-date="minDate" />
  76. </van-popup>
  77. </view>
  78. <van-field readonly class="btn" v-model="formData.relationValue" label="关系" clearable
  79. placeholder="请选择与设备使用者关系" @click.native.stop.prevent="goChooseRelation">
  80. <template #button>
  81. <image src="/static/img/arrow.png" mode=""></image>
  82. </template>
  83. </van-field>
  84. </view>
  85. <!-- #endif -->
  86. <view class="footer_btn">
  87. <view class="footer" @click="bindEqu('formData')">
  88. 绑定
  89. </view>
  90. </view>
  91. </view>
  92. </template>
  93. <script>
  94. import dayjs from "@/plugin/dayjs/dayjs.min.js";
  95. import * as infoApi from '@/api/sleep/equ.js';
  96. import {
  97. mapGetters
  98. } from "vuex";
  99. export default {
  100. data() {
  101. return {
  102. form: {
  103. name: '',
  104. intro: '',
  105. sex: ''
  106. },
  107. checkboxList: [{
  108. name: '苹果',
  109. checked: false,
  110. disabled: false
  111. },
  112. {
  113. name: '雪梨',
  114. checked: false,
  115. disabled: false
  116. },
  117. {
  118. name: '柠檬',
  119. checked: false,
  120. disabled: false
  121. }
  122. ],
  123. radioList: [{
  124. name: '鲜甜',
  125. disabled: false
  126. },
  127. {
  128. name: '麻辣',
  129. disabled: false
  130. }
  131. ],
  132. radio: '',
  133. switchVal: false,
  134. formData: {
  135. photo: '',
  136. deviceCode: '',
  137. nickName: '',
  138. relationId: null,
  139. relationValue: '',
  140. passWord: '',
  141. sex: null, //1 男 2女
  142. sexValue: '',
  143. birthday: '',
  144. cardNumber: '',
  145. tel: ''
  146. },
  147. username: '',
  148. password: '',
  149. showCalendar: false,
  150. showPicker: false,
  151. columns: ['男', '女'],
  152. currentDay: new Date().getTime(),
  153. minDate: new Date(1900, 0, 1).getTime(),
  154. id: '',
  155. list: [],
  156. newAvatar: '',
  157. }
  158. },
  159. computed: mapGetters(['uid', 'deviceId']),
  160. async onLoad(options) {
  161. console.log(options, 88888);
  162. this.formData.deviceCode = options.deviceCode ? options.deviceCode : ''
  163. if (options.id) {
  164. this.id = options.id
  165. await this.getList()
  166. this.getBindDetailInfo()
  167. }
  168. },
  169. methods: {
  170. back() {
  171. uni.navigateBack()
  172. },
  173. getList() {
  174. infoApi.getDetailList().then(res => {
  175. let obj = {
  176. name: '自定义',
  177. avatar: 'https://employmenter.oss-cn-beijing.aliyuncs.com/%E9%80%89%E6%8B%A9%E5%85%B3%E7%B3%BB_slices/%E7%BC%96%E7%BB%84%2013%402x.png',
  178. id: 99
  179. }
  180. this.list = [...res.data, obj]
  181. })
  182. },
  183. getBindDetailInfo() {
  184. infoApi.getBindDetailInfo({
  185. id: this.id
  186. }).then(res => {
  187. if (res.code === 0) {
  188. this.formData = res.data
  189. this.formData.sexValue = res.data.sex === 1 ? '男' : '女'
  190. this.formData.relationValue = this.list.find(item => item.id === res.data.relationId).name
  191. }
  192. })
  193. },
  194. bindEqu() {
  195. console.log(this.$refs.formData, 555);
  196. // this.$refs.formData.validate().then(() => {
  197. // this.$toast.success('提交成功')
  198. // }).catch(() => {
  199. // this.$toast.fail('提交失败')
  200. // })
  201. const {
  202. photo,
  203. nickName,
  204. cardNumber,
  205. relationId,
  206. passWord,
  207. sex,
  208. birthday,
  209. tel
  210. } = this.formData
  211. // if(photo == '') {
  212. // this.$util.Tips({
  213. // title: '请上传图片',
  214. // icon: 'none'
  215. // })
  216. // }else
  217. if (nickName == '') {
  218. this.$util.Tips({
  219. title: '请填写姓名',
  220. icon: 'none'
  221. })
  222. } else if (cardNumber == '') {
  223. this.$util.Tips({
  224. title: '请填写证件号',
  225. icon: 'none'
  226. })
  227. } else if (tel == '') {
  228. this.$util.Tips({
  229. title: '请填写手机号',
  230. icon: 'none'
  231. })
  232. } else if (relationId == null) {
  233. this.$util.Tips({
  234. title: '请选择与设备使用者关系',
  235. icon: 'none'
  236. })
  237. } else {
  238. let params = {
  239. deviceId: this.deviceId,
  240. ...this.formData,
  241. birthday: this.formData.birthday + ' ' + '00:00:00',
  242. memberId: this.uid
  243. }
  244. console.log(params, 7778);
  245. infoApi.bindEqu(params).then(res => {
  246. this.$util.Tips({
  247. title: '绑定成功',
  248. icon: 'success'
  249. }, () => {
  250. uni.navigateBack({
  251. delta: 2
  252. })
  253. })
  254. })
  255. .catch(err => {
  256. console.log(err);
  257. return this.$util.Tips({
  258. title: err,
  259. // icon: 'error'
  260. });
  261. });
  262. }
  263. },
  264. idCardValidator(rule, value, callback) {
  265. const regex =
  266. /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
  267. if (!value || value === '' || value === undefined) {
  268. callback(new Error('请输入证件号码'));
  269. } else if ((value.length === 15 || value.length === 18) && regex.test(value)) {
  270. callback();
  271. } else {
  272. callback(new Error('请输入正确的证件号码'));
  273. }
  274. },
  275. cardNumberBlur(e) {
  276. let value=''
  277. // #ifdef APP-PLUS
  278. value = e
  279. // #endif
  280. // #ifdef MP
  281. value = e.detail.value
  282. // #endif
  283. console.log(value, 2222)
  284. // 调用身份证号验证方法
  285. this.idCardValidator({}, value, (error) => {
  286. if (error) {
  287. // 这里可以根据验证结果处理逻辑
  288. this.$util.Tips({
  289. title: error.message,
  290. icon: 'none'
  291. })
  292. } else {
  293. console.log('身份证号格式正确');
  294. // 这里可以根据验证结果处理逻辑
  295. const {
  296. sex,
  297. sexValue,
  298. birthday
  299. } = this.parseIDCard(value);
  300. console.log('性别:', sexValue);
  301. console.log('出生年月日:', birthday);
  302. this.$set(this.formData, 'sex', sex)
  303. this.$set(this.formData, 'sexValue', sexValue)
  304. this.$set(this.formData, 'birthday', birthday)
  305. }
  306. });
  307. },
  308. telBlur(e) {
  309. let val=''
  310. // #ifdef APP-PLUS
  311. val = e
  312. // #endif
  313. // #ifdef MP
  314. val = e.detail.value
  315. // #endif
  316. console.log(val, 2222)
  317. if (!(/^1(3|4|5|7|8|9|6)\d{9}$/i.test(val))) {
  318. return this.$util.Tips({
  319. title: '请输入正确的手机号码!'
  320. });
  321. }
  322. },
  323. parseIDCard(idCard) {
  324. let birthday = '';
  325. let sex = '';
  326. let sexValue = '';
  327. if (idCard.length === 15) {
  328. birthday = '19' + idCard.substring(6, 12);
  329. sexValue = parseInt(idCard.substr(14, 1)) % 2 === 0 ? '女' : '男';
  330. } else if (idCard.length === 18) {
  331. birthday = idCard.substring(6, 14);
  332. sexValue = parseInt(idCard.substr(16, 1)) % 2 === 0 ? '女' : '男';
  333. sex = parseInt(idCard.substr(16, 1)) % 2 === 0 ? '2' : '1';
  334. }
  335. // 将出生年月日格式化为年-月-日
  336. const year = birthday.substring(0, 4);
  337. const month = birthday.substring(4, 6);
  338. const day = birthday.substring(6, 8);
  339. const formattedBirthday = year + '-' + month + '-' + day;
  340. console.log('性别:', sexValue);
  341. console.log('出生年月日:', formattedBirthday);
  342. return {
  343. sex: sex,
  344. sexValue: sexValue,
  345. birthday: formattedBirthday
  346. };
  347. },
  348. /**
  349. * 上传文件
  350. */
  351. uploadpic: function() {
  352. this.$util.uploadImageOne({}, res => {
  353. this.newAvatar = res.data;
  354. });
  355. },
  356. getValue(item) {
  357. console.log(item, 112233);
  358. this.formData.relationId = item.id
  359. this.formData.relationValue = item.name
  360. },
  361. goChooseRelation() {
  362. console.log('点击了跳转',666)
  363. uni.navigateTo({
  364. url: '/pages/equ_list/choose'
  365. })
  366. },
  367. showCalendarFn() {
  368. this.showCalendar = true
  369. },
  370. showPickerFn() {
  371. this.showPicker = true
  372. },
  373. pickerConfirm(value) {
  374. console.log(value, 66666);
  375. this.formData.sex = value.detail.index + 1;
  376. this.formData.sexValue = value.detail.value;
  377. this.showPicker = false;
  378. },
  379. calendarConfirm(date) {
  380. this.formData.birthday = this.formatDate(date.detail)
  381. this.showCalendar = false;
  382. },
  383. formatDate: function(date) {
  384. return dayjs(date).format("YYYY-MM-DD");
  385. },
  386. },
  387. }
  388. </script>
  389. <style lang="scss">
  390. ::v-deep .u-form-item {
  391. background-color: #fff;
  392. padding:0 20rpx !important;
  393. border-bottom: 2rpx solid #F5F5F5;
  394. }
  395. ::v-deep .u-form-item__body__right__content__slot{
  396. align-content: flex-end;
  397. }
  398. ::v-deep .u-input{
  399. background-color: #fff !important;
  400. }
  401. .avatar_box {
  402. height: 342rpx;
  403. display: flex;
  404. align-items: center;
  405. justify-content: center;
  406. background-color: #F35546;
  407. image {
  408. width: 210rpx;
  409. height: 210rpx;
  410. }
  411. }
  412. .page {
  413. // padding: 60rpx 30rpx;
  414. }
  415. .content-box {
  416. padding: 60rpx 30rpx;
  417. .form-box {
  418. margin-top: 30rpx;
  419. .pictrue {
  420. width: 96rpx;
  421. height: 96rpx;
  422. position: relative;
  423. image {
  424. width: 100%;
  425. height: 100%;
  426. border-radius: 50%;
  427. }
  428. .alter {
  429. width: 30rpx;
  430. height: 30rpx;
  431. border-radius: 50%;
  432. position: absolute;
  433. bottom: 0;
  434. right: 0;
  435. }
  436. }
  437. // .pictrue image {
  438. // width: 100%;
  439. // height: 100%;
  440. // border-radius: 50%;
  441. // }
  442. }
  443. }
  444. .content_box {
  445. background-color: #F7F7F7;
  446. padding: 60rpx 30rpx;
  447. .btn {
  448. image {
  449. width: 32rpx;
  450. height: 32rpx;
  451. }
  452. }
  453. .content {
  454. /* height: 588rpx; */
  455. background-color: #fff;
  456. margin-top: 32rpx;
  457. // padding: 0 32rpx;
  458. // height: calc(100vh - 342rpx);
  459. margin-bottom: 30rpx;
  460. border-radius: 10rpx;
  461. overflow: hidden;
  462. .btn {
  463. image {
  464. width: 32rpx;
  465. height: 32rpx;
  466. }
  467. }
  468. }
  469. }
  470. .footer_btn {
  471. width: 100%;
  472. height: 98rpx;
  473. line-height: 98rpx;
  474. position: fixed;
  475. bottom: 68rpx;
  476. color: #fff;
  477. font-size: 32rpx;
  478. padding: 32rpx;
  479. box-sizing: border-box;
  480. .footer {
  481. background: #F35546;
  482. border-radius: 9rpx;
  483. text-align: center;
  484. }
  485. }
  486. ::v-deep uni-image {
  487. width: 210rpx;
  488. height: 210rpx;
  489. }
  490. // ::v-deep .van-field__label{
  491. // font-size: 32rpx;
  492. // font-weight: 500;
  493. // color: #000000;
  494. // }
  495. </style>