bind.vue 13 KB

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