bodyParams.nvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <template>
  2. <view class="page">
  3. <image class="content-bg" src="/static/device/devicebg.png" mode="scaleToFill"></image>
  4. <view class="pageContent">
  5. <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view>
  6. <view class="customHead">
  7. <view class="back-wrap" @click="goBack">
  8. <image src="/static/public/back.png" mode="aspectFill" class="backimg"></image>
  9. </view>
  10. <text class="customTitle">体形参数</text>
  11. <view class="placeholder"></view>
  12. </view>
  13. <text class="tipTxt">请录入体形参数,用于精准定位穴位</text>
  14. <scroll-view class="formArea" scroll-y="true">
  15. <!-- 用户姓名 -->
  16. <view class="formItem">
  17. <text class="formLabel">姓名</text>
  18. <view class="inputWrap">
  19. <input class="formInput" v-model="form.name" placeholder="请输入姓名" placeholder-class="pholder" maxlength="10" />
  20. </view>
  21. </view>
  22. <view class="formItem">
  23. <text class="formLabel">手机号(选填)</text>
  24. <view class="inputWrap">
  25. <input class="formInput" v-model="form.phone" type="number" placeholder="填写后可作为登录账号" placeholder-class="pholder" maxlength="11" />
  26. </view>
  27. </view>
  28. <!-- 性别 -->
  29. <view class="formItem">
  30. <text class="formLabel">性别</text>
  31. <view class="genderRow">
  32. <view class="genderBtn" :class="{'genderActive': form.gender === 1}" @click="form.gender = 1">
  33. <text class="genderTxt" :style="{color: form.gender === 1 ? '#FFFFFF' : '#545F71'}">男</text>
  34. </view>
  35. <view class="genderBtn" :class="{'genderActive': form.gender === 2}" @click="form.gender = 2">
  36. <text class="genderTxt" :style="{color: form.gender === 2 ? '#FFFFFF' : '#545F71'}">女</text>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 年龄 -->
  41. <view class="formItem">
  42. <text class="formLabel">年龄</text>
  43. <view class="inputWrap">
  44. <input class="formInput" v-model="form.age" type="number" placeholder="例如:28" placeholder-class="pholder" maxlength="3" />
  45. <text class="unitTxt">岁</text>
  46. </view>
  47. </view>
  48. <!-- 身长/坐高 -->
  49. <view class="formItem">
  50. <text class="formLabel">身长/坐高</text>
  51. <view class="inputWrap">
  52. <input class="formInput" v-model="form.bodyHeight" type="digit" placeholder="例如:135" placeholder-class="pholder" />
  53. <text class="unitTxt">cm</text>
  54. </view>
  55. <view class="sliderWrap">
  56. <slider :value="Number(form.bodyHeight) || 100" :min="50" :max="250" :step="1"
  57. activeColor="#389588" backgroundColor="#E8E8E8" block-size="20"
  58. @change="onBodyHeightChange" />
  59. <view class="sliderRange">
  60. <text class="rangeTxt">50cm</text>
  61. <text class="rangeTxt">250cm</text>
  62. </view>
  63. </view>
  64. </view>
  65. <!-- 脊柱长度 -->
  66. <view class="formItem">
  67. <text class="formLabel">C7-S4脊柱长度</text>
  68. <view class="inputWrap">
  69. <input class="formInput" v-model="form.spineLength" type="digit" placeholder="例如:45" placeholder-class="pholder" />
  70. <text class="unitTxt">cm</text>
  71. </view>
  72. <view class="sliderWrap">
  73. <slider :value="Number(form.spineLength) || 45" :min="1" :max="80" :step="1"
  74. activeColor="#389588" backgroundColor="#E8E8E8" block-size="20"
  75. @change="onSpineLengthChange" />
  76. <view class="sliderRange">
  77. <text class="rangeTxt">1cm</text>
  78. <text class="rangeTxt">80cm</text>
  79. </view>
  80. </view>
  81. </view>
  82. <!-- 肩宽 -->
  83. <view class="formItem">
  84. <text class="formLabel">肩宽(两肩之间的宽度)</text>
  85. <view class="inputWrap">
  86. <input class="formInput" v-model="form.shoulderWidth" type="digit" placeholder="例如:42" placeholder-class="pholder" />
  87. <text class="unitTxt">cm</text>
  88. </view>
  89. <view class="sliderWrap">
  90. <slider :value="Number(form.shoulderWidth) || 40" :min="20" :max="80" :step="1"
  91. activeColor="#389588" backgroundColor="#E8E8E8" block-size="20"
  92. @change="onShoulderWidthChange" />
  93. <view class="sliderRange">
  94. <text class="rangeTxt">20cm</text>
  95. <text class="rangeTxt">80cm</text>
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 体重 -->
  100. <view class="formItem">
  101. <text class="formLabel">体重</text>
  102. <view class="inputWrap">
  103. <input class="formInput" v-model="form.weight" type="digit" placeholder="例如:65" placeholder-class="pholder" />
  104. <text class="unitTxt">kg</text>
  105. </view>
  106. </view>
  107. <!-- 保存按钮 -->
  108. <view class="saveBtn" @click="onSave">
  109. <text class="saveTxt">保存</text>
  110. </view>
  111. <view class="bottomSpace"></view>
  112. </scroll-view>
  113. </view>
  114. <ay-toast ref="ayToast" />
  115. </view>
  116. </template>
  117. <script>
  118. import ayToast from '@/components/ay-toast/ay-toast.nvue'
  119. import { addGroupMember, updateGroupMember } from '@/utils/api/device'
  120. import { updateProfile } from '@/utils/api/user'
  121. export default {
  122. components: {
  123. ayToast
  124. },
  125. data() {
  126. return {
  127. statusBarHeight: 44,
  128. mode: 'add', // add / edit
  129. userId: '',
  130. form: {
  131. name: '',
  132. phone: '',
  133. gender: 1,
  134. age: '',
  135. bodyHeight: '',
  136. shoulderWidth: '',
  137. spineLength: '',
  138. weight: ''
  139. }
  140. }
  141. },
  142. onLoad(options) {
  143. const sysInfo = uni.getSystemInfoSync()
  144. this.statusBarHeight = sysInfo.statusBarHeight || 44
  145. this.mode = options.mode || 'add'
  146. this.userId = options.userId || ''
  147. if (this.mode === 'edit' && this.userId) {
  148. this.loadUserData()
  149. }
  150. },
  151. methods: {
  152. goBack() {
  153. uni.navigateBack()
  154. },
  155. /** 加载编辑数据 */
  156. loadUserData() {
  157. const list = uni.getStorageSync('user_manage_list') || []
  158. const currentUser = uni.getStorageSync('current_manage_user') || {}
  159. const user = list.find(u => String(u.id) === String(this.userId)) ||
  160. ((String(currentUser.id) === String(this.userId) || String(currentUser.profileId) === String(this.userId)) ? currentUser : null)
  161. if (user) {
  162. this.form.name = user.name || ''
  163. this.form.phone = user.phone || ''
  164. this.form.gender = user.gender || 1
  165. this.form.age = user.age ? String(user.age) : ''
  166. this.form.bodyHeight = user.bodyHeight || user.shoulderHeight ? String(user.bodyHeight || user.shoulderHeight) : ''
  167. this.form.shoulderWidth = user.shoulderWidth ? String(user.shoulderWidth) : ''
  168. this.form.spineLength = user.spineLength ? String(user.spineLength) : ''
  169. this.form.weight = user.weight ? String(user.weight) : ''
  170. }
  171. },
  172. onBodyHeightChange(e) {
  173. this.form.bodyHeight = String(e.detail.value)
  174. },
  175. onShoulderWidthChange(e) {
  176. this.form.shoulderWidth = String(e.detail.value)
  177. },
  178. onSpineLengthChange(e) {
  179. this.form.spineLength = String(e.detail.value)
  180. },
  181. /** 校验并保存 */
  182. async onSave() {
  183. // 校验
  184. if (!this.form.name.trim()) {
  185. this.$refs.ayToast.error('请输入姓名')
  186. return
  187. }
  188. const phone = this.form.phone.trim()
  189. if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
  190. this.$refs.ayToast.error('手机号格式不正确')
  191. return
  192. }
  193. const age = Number(this.form.age)
  194. if (!age || age < 1 || age > 120) {
  195. this.$refs.ayToast.error('年龄范围为1-120岁')
  196. return
  197. }
  198. const height = Number(this.form.bodyHeight)
  199. if (!height || height < 50 || height > 250) {
  200. this.$refs.ayToast.error('身长/坐高范围为50-250cm')
  201. return
  202. }
  203. const width = Number(this.form.shoulderWidth)
  204. if (!width || width < 20 || width > 80) {
  205. this.$refs.ayToast.error('肩宽范围为20-80cm')
  206. return
  207. }
  208. const spineLength = Number(this.form.spineLength)
  209. if (!spineLength || spineLength < 1 || spineLength > 80) {
  210. this.$refs.ayToast.error('脊柱长度范围为1-80cm')
  211. return
  212. }
  213. const weight = this.form.weight ? Number(this.form.weight) : null
  214. if (weight && (weight < 10 || weight > 300)) {
  215. this.$refs.ayToast.error('体重范围为10-300kg')
  216. return
  217. }
  218. let list = uni.getStorageSync('user_manage_list') || []
  219. const currentDevice = uni.getStorageSync('current_device') || {}
  220. const payload = {
  221. name: this.form.name.trim(),
  222. phone,
  223. gender: this.form.gender,
  224. age,
  225. bodyHeight: height,
  226. shoulderWidth: width,
  227. spineLength,
  228. weight
  229. }
  230. if (this.mode === 'add') {
  231. // 再次检查是否超过10个
  232. if (list.length >= 10) {
  233. this.$refs.ayToast.error('最多可添加10个用户')
  234. return
  235. }
  236. if (!currentDevice.groupId) {
  237. this.$refs.ayToast.error('公共设备不支持新增成员')
  238. return
  239. }
  240. const saved = await addGroupMember(currentDevice.groupId, payload)
  241. const newUser = this.normalizeSavedMember(saved)
  242. list.push(newUser)
  243. uni.setStorageSync('user_manage_list', list)
  244. // 如果是第一个用户,自动设为当前用户
  245. if (list.length === 1) {
  246. uni.setStorageSync('current_manage_user_id', newUser.id)
  247. uni.setStorageSync('current_manage_user', newUser)
  248. }
  249. this.$refs.ayToast.success('添加成功')
  250. } else {
  251. // 编辑模式
  252. let index = list.findIndex(u => String(u.id) === String(this.userId))
  253. if (index === -1) {
  254. const currentUser = uni.getStorageSync('current_manage_user') || {}
  255. if (String(currentUser.id) === String(this.userId) || String(currentUser.profileId) === String(this.userId)) {
  256. list = [this.normalizeSavedMember(currentUser)]
  257. index = 0
  258. }
  259. }
  260. if (index === -1) {
  261. this.$refs.ayToast.error('用户信息已过期,请刷新')
  262. return
  263. }
  264. let saved
  265. if (currentDevice.groupId) {
  266. saved = await updateGroupMember(currentDevice.groupId, this.userId, payload)
  267. } else {
  268. saved = await updateProfile(payload)
  269. }
  270. list[index] = this.normalizeSavedMember({ ...list[index], ...(saved || payload) })
  271. uni.setStorageSync('user_manage_list', list)
  272. // 如果编辑的是当前用户,更新当前用户缓存
  273. const curId = uni.getStorageSync('current_manage_user_id')
  274. if (String(curId) === String(this.userId)) {
  275. uni.setStorageSync('current_manage_user', list[index])
  276. }
  277. this.$refs.ayToast.success('保存成功')
  278. }
  279. setTimeout(() => {
  280. uni.navigateBack()
  281. }, 800)
  282. },
  283. normalizeSavedMember(item) {
  284. return {
  285. ...item,
  286. id: String(item.profileId || item.id),
  287. bodyHeight: item.bodyHeight || item.shoulderHeight || '',
  288. profileComplete: !!item.profileComplete
  289. }
  290. }
  291. }
  292. }
  293. </script>
  294. <style>
  295. .page {
  296. flex: 1;
  297. position: relative;
  298. }
  299. .content-bg {
  300. position: absolute;
  301. left: 0;
  302. top: 0;
  303. width: 750rpx;
  304. height: 2000px;
  305. }
  306. .pageContent {
  307. flex: 1;
  308. background-image: linear-gradient(to bottom, rgba(56,149,136,1), rgba(56,149,136,0.5));
  309. }
  310. .status-bar {
  311. background-color: rgba(0,0,0,0);
  312. }
  313. .customHead {
  314. flex-direction: row;
  315. justify-content: space-between;
  316. align-items: center;
  317. height: 88rpx;
  318. padding-left: 30rpx;
  319. padding-right: 30rpx;
  320. }
  321. .back-wrap {
  322. width: 50rpx;
  323. height: 50rpx;
  324. justify-content: center;
  325. align-items: center;
  326. }
  327. .backimg {
  328. width: 50rpx;
  329. height: 50rpx;
  330. }
  331. .customTitle {
  332. font-weight: bold;
  333. font-size: 32rpx;
  334. color: #FFFFFF;
  335. }
  336. .placeholder {
  337. width: 50rpx;
  338. }
  339. .tipTxt {
  340. font-size: 24rpx;
  341. color: rgba(255,255,255,0.7);
  342. margin-left: 38rpx;
  343. margin-top: 10rpx;
  344. margin-bottom: 20rpx;
  345. }
  346. .formArea {
  347. flex: 1;
  348. padding-left: 30rpx;
  349. padding-right: 30rpx;
  350. }
  351. .formItem {
  352. background-color: #FFFFFF;
  353. border-radius: 16rpx;
  354. padding: 28rpx;
  355. margin-bottom: 20rpx;
  356. }
  357. .formLabel {
  358. font-size: 26rpx;
  359. color: #9BA5B7;
  360. margin-bottom: 16rpx;
  361. }
  362. .inputWrap {
  363. flex-direction: row;
  364. align-items: center;
  365. background-color: #F5F7FA;
  366. border-radius: 12rpx;
  367. padding-left: 20rpx;
  368. padding-right: 20rpx;
  369. height: 80rpx;
  370. }
  371. .formInput {
  372. flex: 1;
  373. font-size: 30rpx;
  374. color: #333333;
  375. }
  376. .unitTxt {
  377. font-size: 26rpx;
  378. color: #9BA5B7;
  379. margin-left: 10rpx;
  380. }
  381. .genderRow {
  382. flex-direction: row;
  383. }
  384. .genderBtn {
  385. width: 160rpx;
  386. height: 72rpx;
  387. border-radius: 36rpx;
  388. background-color: #F5F7FA;
  389. justify-content: center;
  390. align-items: center;
  391. margin-right: 24rpx;
  392. }
  393. .genderActive {
  394. background-color: #389588;
  395. }
  396. .genderTxt {
  397. font-size: 28rpx;
  398. }
  399. .sliderWrap {
  400. margin-top: 20rpx;
  401. }
  402. .sliderRange {
  403. flex-direction: row;
  404. justify-content: space-between;
  405. margin-top: 4rpx;
  406. }
  407. .rangeTxt {
  408. font-size: 22rpx;
  409. color: #9BA5B7;
  410. }
  411. .saveBtn {
  412. background-color: #389588;
  413. border-radius: 44rpx;
  414. height: 88rpx;
  415. justify-content: center;
  416. align-items: center;
  417. margin-top: 30rpx;
  418. margin-left: 20rpx;
  419. margin-right: 20rpx;
  420. }
  421. .saveTxt {
  422. font-size: 32rpx;
  423. color: #FFFFFF;
  424. font-weight: bold;
  425. }
  426. .bottomSpace {
  427. height: 60rpx;
  428. }
  429. </style>