BMI.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="margin-xl">
  3. <uv-gap height="10" ></uv-gap>
  4. <view class="flex justify-between">
  5. <view>
  6. <p class="head-text">年龄</p>
  7. <uv-gap height="10" ></uv-gap>
  8. <view style="width: 50vw">
  9. <input type="number" v-model="BMI.age" @input="validateInput('age')" maxlength="3"
  10. class="input-text" placeholder="请输入年龄"
  11. placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;"
  12. />
  13. </view>
  14. </view>
  15. <view>
  16. <p style="text-align: center;" class="head-text">历史</p>
  17. <uv-gap height="5"></uv-gap>
  18. <image @click="$util.navigateTo('/pages/BMI/BMI_history/BMI_history')" class="image_bmi button-all" src="@/static/img/history_bmi.png"></image>
  19. </view>
  20. </view>
  21. <uv-gap height="20" ></uv-gap>
  22. <view class="head-text">
  23. <p>身高(厘米)</p>
  24. </view>
  25. <uv-gap height="10" ></uv-gap>
  26. <view>
  27. <input type="number" v-model="BMI.height" @input="validateInput('height')" maxlength="3"
  28. class="input-text" placeholder="请输入身高"
  29. placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;"
  30. />
  31. </view>
  32. <uv-gap height="20" ></uv-gap>
  33. <view class="head-text">
  34. <p>体重(公斤)</p>
  35. </view>
  36. <uv-gap height="10" ></uv-gap>
  37. <view>
  38. <input type="number" v-model="BMI.weight" @input="validateInput('weight')" maxlength="3"
  39. class="input-text" placeholder="请输入体重"
  40. placeholder-style="color: #cccccc; font-size: 24px; font-weight: 100;"
  41. />
  42. </view>
  43. <uv-gap height="30" ></uv-gap>
  44. <view class="tip-text">
  45. <p class="title">BMI指数:</p>
  46. <uv-gap height="5" ></uv-gap>
  47. <p>简称体质指数,是通过体重公斤除以身高米数平方得出的数字。是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。</p>
  48. </view>
  49. <uv-button @click="to_BMI_output()" customStyle="position: fixed; bottom: 40px;width: calc(100vw - 50px);" shape="circle" size="large" color="#1678ff">开始计算</uv-button>
  50. </view>
  51. </template>
  52. <script>
  53. export default {
  54. onLoad() {
  55. let user = uni.getStorageSync("user");
  56. if(user){
  57. this.BMI.age = user.age;
  58. this.BMI.gender = user.gender;
  59. }
  60. },
  61. data() {
  62. return {
  63. BMI: {
  64. age: null,
  65. gender: 0,
  66. height: null,
  67. weight: null
  68. }
  69. }
  70. },
  71. methods: {
  72. to_BMI_output(){
  73. if(!this.BMI.age) uni.showToast({duration:1500,icon:'error',title: '请输入年龄 !'});
  74. else if(this.BMI.age < 14) uni.showToast({duration:1500,icon:'none',title: '14岁以下不提供此服务!'});
  75. else if(!this.BMI.height) uni.showToast({duration:1500,icon:'error',title: '请输入身高 !'});
  76. else if(!this.BMI.weight) uni.showToast({duration:1500,icon:'error',title: '请输入体重 !'});
  77. else{
  78. // 将对象参数转为JSON字符串,并使用encodeURIComponent编码
  79. const BMI = encodeURIComponent(JSON.stringify(this.BMI));
  80. // 跳转页面并传递对象参数
  81. uni.navigateTo({
  82. url: `/pages/BMI/BMI_output/BMI_output?BMI=${BMI}`
  83. });
  84. }
  85. },
  86. validateInput(fieldName) {
  87. const value = this.BMI[fieldName];
  88. // 如果输入的是负数或者不是数字,则清空输入框
  89. if (value < 0 || isNaN(value)) {
  90. this.BMI[fieldName] = null;
  91. uni.showToast({duration:1500,icon:'error',title: '请输入正数 !'});
  92. }
  93. }
  94. }
  95. }
  96. </script>
  97. <style>
  98. page{
  99. background-color: #ffffff;
  100. }
  101. .head-text{
  102. color: #8c92ad;
  103. font-size: 14px;
  104. }
  105. .input-text {
  106. /* 去掉默认边框 */
  107. border: none;
  108. /* 添加底部下划线 */
  109. border-bottom: 1px solid #cccccc; /* 下划线颜色和粗细 */
  110. /* 其他样式 */
  111. width: 100%;
  112. height: 7vh;
  113. padding-bottom: 5px;
  114. font-size: 24px;
  115. font-weight: 1000;
  116. color: #000;
  117. outline: none; /* 移除聚焦时的默认边框 */
  118. }
  119. .tip-text{
  120. color: #666666;
  121. font-size: 14px;
  122. }
  123. .tip-text .title{
  124. font-weight: 1000;
  125. }
  126. .image_bmi{
  127. width: 60px;
  128. height: 60px;
  129. }
  130. </style>