bind.vue 10 KB

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