BMI.vue 4.1 KB

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