BMI_output.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <view class="margin-xl">
  3. <view class="flex justify-between">
  4. <view>
  5. <p class="BMI">{{bmi}}</p>
  6. <uv-gap height="5"></uv-gap>
  7. <p class="wight-head">身高体重指数</p>
  8. </view>
  9. <view>
  10. <image @click="$util.navigateTo('/pages/BMI/BMI_history/BMI_history')" class="image_bmi button-all" src="@/static/img/history_bmi.png"></image>
  11. <uv-gap height="5"></uv-gap>
  12. <p style="text-align: center;" class="wight-head">历史</p>
  13. </view>
  14. </view>
  15. <uv-gap height="40"></uv-gap>
  16. <div class="bmi-labels">
  17. <span class="weight-tip" :class="{ 'active1 active': bmi < 18.5 }">偏瘦</span>
  18. <span class="weight-tip" :class="{ 'active2 active': bmi >= 18.5 && bmi < 24.0 }">正常</span>
  19. <span class="weight-tip" :class="{ 'active3 active': bmi >= 24.0 && bmi < 28.0 }">偏重</span>
  20. <span class="weight-tip" :class="{ 'active4 active': bmi >= 28.0 }">肥胖</span>
  21. </div>
  22. <uv-gap height="15"></uv-gap>
  23. <div class="bmi-bar"></div>
  24. <uv-gap height="15"></uv-gap>
  25. <div class="bmi-values">
  26. <span></span>
  27. <span>18.5</span>
  28. <span>24.0</span>
  29. <span>28.0</span>
  30. <span></span>
  31. </div>
  32. <uv-gap height="25"></uv-gap>
  33. <uv-divider></uv-divider>
  34. <uv-gap height="25"></uv-gap>
  35. <p class="sug-head">数据分析</p>
  36. <uv-gap height="25"></uv-gap>
  37. <view class="flex justify-between">
  38. <p class="sug-title">身高(厘米)</p>
  39. <p class="sug-res">{{height}}cm</p>
  40. </view>
  41. <uv-gap height="25"></uv-gap>
  42. <view class="flex justify-between">
  43. <p class="sug-title">建议体重(公斤)</p>
  44. <p class="sug-res">{{min_weight}} ~ {{max_weight}}kg</p>
  45. </view>
  46. <uv-button @click="save()" 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(options) {
  52. // 获取传递的对象参数,使用decodeURIComponent解码,并转为对象
  53. if ('BMI' in options) {
  54. let BMI = JSON.parse(decodeURIComponent(options.BMI));
  55. this.BMI = BMI;
  56. let bmi = this.calculateBMI(BMI.age, BMI.gender, BMI.weight, BMI.height);
  57. this.bmi = bmi.bmi;
  58. this.height = parseFloat(BMI.height).toFixed(1);
  59. let sug_weight = this.calculateSuggestedWeightRange(BMI.height);
  60. console.log(bmi);
  61. console.log(sug_weight);
  62. this.min_weight = sug_weight.minWeight;
  63. this.max_weight = sug_weight.maxWeight;
  64. }
  65. else{
  66. uni.navigateTo({url: '/pages/BMI/BMI'});
  67. }
  68. },
  69. data() {
  70. return {
  71. bmi: null,
  72. height: null,
  73. min_weight: null,
  74. max_weight: null,
  75. saved: false
  76. }
  77. },
  78. methods: {
  79. save(){
  80. if(this.saved){
  81. uni.showToast({icon:'none',title: '已保存,请勿重复操作',duration:1500});
  82. return ;
  83. }
  84. this.saved = true;
  85. let BMI = this.BMI;
  86. const timestamp = Date.now();
  87. BMI['bmi'] = this.bmi;
  88. console.log('BMI: ',BMI);
  89. let bmi_history = uni.getStorageSync('bmi_history');
  90. if(bmi_history){
  91. bmi_history[timestamp.toString()] = BMI;
  92. uni.setStorageSync('bmi_history',bmi_history);
  93. }
  94. else{
  95. bmi_history = {};
  96. bmi_history[timestamp.toString()] = BMI;
  97. uni.setStorageSync('bmi_history',bmi_history);
  98. }
  99. uni.showToast({icon:'success',title: '保存成功',duration:1500});
  100. },
  101. calculateBMI(age, gender, weight, height) {
  102. // 将身高从米转换为米,将体重从千克转换为千克
  103. height = height/100;
  104. // const bmi = weight / (height * height);
  105. const bmi = parseFloat((weight / (height * height)).toFixed(1));
  106. console.log(bmi);
  107. let bmiCategory;
  108. if (bmi < 18.5) {
  109. bmiCategory = "体重过轻";
  110. } else if (bmi >= 18.5 && bmi <= 24) {
  111. bmiCategory = "正常体重";
  112. } else if (bmi > 24 && bmi < 29.9) {
  113. bmiCategory = "超重";
  114. } else {
  115. bmiCategory = "肥胖";
  116. }
  117. let additionalInfo = "";
  118. if (gender === 0) {
  119. additionalInfo = `男性,${age}岁,BMI值为${bmi.toFixed(2)},属于${bmiCategory}。`;
  120. } else if (gender === 1) {
  121. additionalInfo = `女性,${age}岁,BMI值为${bmi.toFixed(2)},属于${bmiCategory}。`;
  122. } else {
  123. additionalInfo = `性别未知,BMI值为${bmi.toFixed(2)},属于${bmiCategory}。`;
  124. }
  125. return {
  126. bmi: bmi.toFixed(1),
  127. category: bmiCategory,
  128. info: additionalInfo
  129. };
  130. },
  131. calculateSuggestedWeightRange(height) {
  132. height = height/100;
  133. const minBMI = 18.5;
  134. const maxBMI = 24;
  135. // const minWeight = minBMI * (height * height);
  136. // const maxWeight = maxBMI * (height * height);
  137. const minWeight = ((minBMI * (height * height))).toFixed(1);
  138. const maxWeight = ((maxBMI * (height * height))).toFixed(1);
  139. return {
  140. minWeight: minWeight,
  141. maxWeight: maxWeight
  142. }
  143. },
  144. }
  145. }
  146. </script>
  147. <style>
  148. page{
  149. background-color: #ffffff;
  150. }
  151. .BMI{
  152. font-size: 60px;
  153. font-weight: 1000;
  154. }
  155. .wight-head{
  156. color: #666666;
  157. font-size: 14px;
  158. letter-spacing: 1px;
  159. }
  160. .bmi-bar {
  161. width: 100%;
  162. height: 10px;
  163. background: linear-gradient(to right, #57bff6, #13d080, #ffb951, #ff7433);
  164. border-radius: 15px;
  165. }
  166. .bmi-labels {
  167. display: flex;
  168. justify-content: space-between;
  169. align-items: center;
  170. width: 100%;
  171. font-size: 13px;
  172. color: #999999;
  173. }
  174. .bmi-labels span {
  175. flex: 1;
  176. text-align: center;
  177. margin: 0 5vw;
  178. height: 25px;
  179. line-height: 25px;
  180. }
  181. .bmi-labels .active {
  182. color: white;
  183. border-radius: 8px;
  184. position: relative;
  185. --active-color: #13d080; /* 默认颜色 */
  186. background-color: var(--active-color);
  187. }
  188. .bmi-labels .active::before {
  189. content: "";
  190. position: absolute;
  191. top: 100%; /* 三角形位于元素下方 */
  192. left: 50%; /* 水平居中 */
  193. width: 0;
  194. height: 0;
  195. border-left: 5px solid transparent;
  196. border-right: 5px solid transparent;
  197. border-top: 7px solid var(--active-color); /* 使用变量 */
  198. transform: translateX(-50%); /* 确保三角形的中心点位于 .active 元素的中心 */
  199. }
  200. .bmi-labels .active1 {
  201. --active-color: #57bff6; /* 自定义颜色 */
  202. }
  203. .bmi-labels .active2 {
  204. --active-color: #13d080; /* 自定义颜色 */
  205. }
  206. .bmi-labels .active3 {
  207. --active-color: #ffb951; /* 自定义颜色 */
  208. }
  209. .bmi-labels .active4 {
  210. --active-color: #ff7433; /* 自定义颜色 */
  211. }
  212. .bmi-values {
  213. display: flex;
  214. justify-content: space-between;
  215. align-items: center;
  216. width: 100%;
  217. bottom: -30px;
  218. font-size: 14px;
  219. color: #b1b1b1;
  220. }
  221. .sug-head {
  222. font-size: 14px;
  223. color: #8d93ad;
  224. }
  225. .sug-title{
  226. font-size: 18px;
  227. color: #000;
  228. font-weight: 700;
  229. }
  230. .sug-res{
  231. font-size: 16px;
  232. color: #999999;
  233. font-weight: 500;
  234. }
  235. .image_bmi{
  236. width: 60px;
  237. height: 60px;
  238. }
  239. </style>