Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var plugin = requirePlugin("myPlugin");
  4. //var plugin = require("../../plugins/index.js");
  5. var standardTool = require("../../utils/standard")
  6. var deviceInfo_address = ""
  7. var deviceInfo = ""
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. standardExp:"",
  14. standardTextExp:"",
  15. userInfo: {},
  16. deviceID_wxml: '',
  17. array: ['100', '101', '102', '103', '104', '105', '106', '107', '108', '109',
  18. '110', '111', '112', '113', '114', '115', '116', '117', '118', '119',
  19. '120', '121', '122', '123', '124', '125', '126', '127', '128', '129',
  20. '130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
  21. '140', '141', '142', '143', '144', '145', '146', '147', '148', '149',
  22. '150', '151', '152', '153', '154', '155', '156', '157', '158', '159',
  23. '160', '161', '162', '163', '164', '165', '166', '167', '168', '169',
  24. '170', '171', '172', '173', '174', '175', '176', '177', '178', '179',
  25. '180', '181', '182', '183', '184', '185', '186', '187', '188', '189',
  26. '190', '191', '192', '193', '194', '195', '196', '197', '198', '199',
  27. '200', '201', '202', '203', '204', '205', '206', '207', '208', '209',
  28. '210', '211', '212', '213', '214', '215', '216', '217', '218', '219'],
  29. isUnitShow: false,
  30. isSexShow: false,
  31. sex: '男',
  32. man_image: '../Image/radio.png',
  33. women_image: '../Image/radio-1.png',
  34. briday: '1990-05-10',
  35. endTime: '2020-09-01',
  36. height: '160',
  37. index: 62, // 必须写下来用来指向身高数组的下指标
  38. unit: '%',
  39. UD_unit:"kg",
  40. kcal: "kcal",
  41. kg_unit: '../Image/radio-1.png',
  42. jin_unit: '../Image/radio.png',
  43. edition: 'V1.0',
  44. hasUserInfo: false,
  45. wanShow: false,
  46. linksToPrompt: "提示",
  47. },
  48. bindViewTap: function () {
  49. wx.openSetting({
  50. success: (res) => {
  51. }
  52. })
  53. },
  54. onShow: function () {
  55. // 从存储中拿出连接的设备ID
  56. var that = this;
  57. var fatData = plugin.getBodyDataByInet(27, 170, 1, 60, 500);//年龄 身高 性别 重量 阻抗
  58. console.log("fatData" , fatData);
  59. var scaleFatData = plugin.getBodyScaleDataByInet(170, 60, 1, fatData.bfr, fatData.rom, fatData.pp);//身高 体重 性别 体脂率 肌肉率 蛋白率
  60. console.log("scaleFatData", scaleFatData);
  61. //console.log("height:", obtainHeight("170"));
  62. //判断蓝牙是否开启
  63. that.longnv();
  64. deviceInfo = wx.getStorageSync('deviceInfo');
  65. deviceInfo_address = wx.getStorageSync('devMacAddress')
  66. if (deviceInfo != undefined && deviceInfo != null && deviceInfo != "" && deviceInfo != "1") {
  67. } else {
  68. that.setData({
  69. linksToPrompt: '未绑定设备',
  70. });
  71. }
  72. },
  73. /**
  74. * 生命周期函数--监听页面加载
  75. */
  76. onLoad: function (options) {
  77. var that = this
  78. //判断蓝牙是否开启
  79. that.longnv();
  80. if (wx.getStorageSync('userInfo').avatarUrl == null || wx.getStorageSync('userInfo').nickName == null) {
  81. that.setData({
  82. wanShow: !that.data.wanShow
  83. })
  84. } else {
  85. this.setData({
  86. userInfo: wx.getStorageSync('userInfo')
  87. })
  88. }
  89. // 拿到当前日期(如:2017-06-06)
  90. var time2 = util.forNowTime(new Date());
  91. that.setData({
  92. endTime: time2,
  93. })
  94. // 拿到生日
  95. wx.getStorage({
  96. key: 'UserBriday',
  97. success: function (res) {
  98. that.briday = res.data;
  99. that.theSaveBriday(that.briday);
  100. }, fail: function (res) {
  101. wx.setStorageSync('UserBriday', "1990-05-10")
  102. }
  103. })
  104. // 拿到身高
  105. wx.getStorage({
  106. key: 'UserHight',
  107. success: function (res) {
  108. that.height = res.data;
  109. that.theSaveHeight(that.height);
  110. }, fail: function (res) {
  111. wx.setStorageSync('UserHight', "160")
  112. }
  113. })
  114. // 拿到性别
  115. wx.getStorage({
  116. key: 'UserSex',
  117. success: function (res) {
  118. that.sex = res.data;
  119. that.theSavedSex(that.sex);
  120. },
  121. })
  122. // 拿到单位
  123. wx.getStorage({
  124. key: 'UserUnit',
  125. success: function (res) {
  126. that.unit = res.data;
  127. that.theSavedUnit(that.unit);
  128. },
  129. })
  130. },
  131. // 拿到生日--重新刷UI
  132. theSaveBriday: function (bridayString) {
  133. this.setData({
  134. briday: bridayString,
  135. });
  136. },
  137. // 拿到身高--刷新UI
  138. theSaveHeight: function (heightString) {
  139. this.setData({
  140. index: heightString - 100,
  141. })
  142. },
  143. // 拿到性别--重新刷UI
  144. theSavedSex: function (sexString) {
  145. if (sexString == "男") {
  146. this.setData({
  147. sex: "男",
  148. man_image: '../Image/radio-1.png',
  149. women_image: '../Image/radio.png',
  150. })
  151. } else {
  152. this.setData({
  153. sex: "女",
  154. women_image: '../Image/radio-1.png',
  155. man_image: '../Image/radio.png',
  156. })
  157. }
  158. wx.setStorage({
  159. key: 'UserSex',
  160. data: sexString,
  161. })
  162. },
  163. ///////// ****** 调用点击方法 ******* ////////////////
  164. //** 性别 **//
  165. sexClick: function (res) {
  166. this.setData({
  167. isSexShow: !this.data.isSexShow
  168. })
  169. },
  170. manClick: function () {
  171. this.setData({
  172. sex: '男',
  173. man_image: '../Image/radio-1.png',
  174. women_image: '../Image/radio.png',
  175. });
  176. wx.setStorage({
  177. key: 'UserSex',
  178. data: this.data.sex,
  179. })
  180. },
  181. womanClick: function () {
  182. this.setData({
  183. sex: '女',
  184. women_image: '../Image/radio-1.png',
  185. man_image: '../Image/radio.png',
  186. });
  187. wx.setStorage({
  188. key: 'UserSex',
  189. data: this.data.sex,
  190. })
  191. },
  192. //** 生日 **//
  193. bindDateChange: function (res) {
  194. this.setData({
  195. briday: res.detail.value
  196. });
  197. wx.setStorage({
  198. key: 'UserBriday',
  199. data: this.data.briday,
  200. })
  201. },
  202. //** 身高 **//
  203. bindHightChange: function (res) {
  204. var numberValue = parseInt(res.detail.value)+100;
  205. this.setData({
  206. index: res.detail.value
  207. });
  208. wx.setStorage({
  209. key: 'UserHight',
  210. data: numberValue,
  211. })
  212. },
  213. //** 解除绑定 **/
  214. dismissDevice: function (res) {
  215. wx.navigateTo({
  216. url: '../BluetoothLink/BluetoothLink'
  217. })
  218. },
  219. /** 搜索蓝牙设备 **/
  220. linkBleButton: function () {
  221. wx.navigateTo({
  222. url: '../BluetoothLink/BluetoothLink'
  223. })
  224. },
  225. /** 自动连接蓝牙设备 **/
  226. longnv: function () {
  227. var that = this;
  228. if (deviceInfo != null && deviceInfo != "1") {
  229. if (wx.getStorageSync('deviceInfo').name == "el-deviceinfo") {
  230. wx.openBluetoothAdapter({
  231. success: function (res) {
  232. deviceInfo_address = wx.getStorageSync('devMacAddress')
  233. that.setData({
  234. linksToPrompt: "已绑定广播称:" + wx.getStorageSync('devMacAddress')+",请上秤",
  235. });
  236. // 保持监测
  237. // 蓝牙已开启 开始
  238. that.openBluetoothAdapter();
  239. }, fail: function (res) {
  240. // console.log("开启模块失败!");
  241. that.setData({
  242. linksToPrompt: '请开启手机蓝牙',
  243. });
  244. //重新搜索
  245. setTimeout(function () {
  246. that.longnv();
  247. }, 2000)
  248. },
  249. })
  250. } else {
  251. // 已绑定连接秤 重新连接
  252. wx.showLoading({
  253. title: '重新连接中....',
  254. icon: 'none',
  255. duration: 2000,
  256. })
  257. wx.openBluetoothAdapter({
  258. success: function (res) {
  259. wx.createBLEConnection({
  260. deviceId: wx.getStorageSync('deviceInfo').macAddress,
  261. success: function (res) {
  262. console.log('连接成功');
  263. console.log(res);
  264. wx.hideLoading()
  265. deviceInfo_address = wx.getStorageSync('devMacAddress')
  266. that.setData({
  267. linksToPrompt: '已连接' + wx.getStorageSync('devMacAddress') + ',请上秤',
  268. });
  269. that.link_device();
  270. },
  271. fail: function (res) {
  272. console.log('连接秤连接失败');
  273. console.log(res);
  274. wx.hideLoading()
  275. console.log(res);
  276. wx.showToast({
  277. title: '自动连接失败,手动连接',
  278. icon: 'none',
  279. duration: 2000,
  280. })
  281. }
  282. })
  283. },
  284. fail: function () {
  285. wx.showToast({
  286. title: '请开启手机蓝牙',
  287. icon: 'none',
  288. duration: 2000,
  289. })
  290. that.setData({
  291. linksToPrompt: '请开启手机蓝牙',
  292. });
  293. }
  294. })
  295. }
  296. } else {
  297. that.setData({
  298. linksToPrompt: '未绑定设备',
  299. });
  300. }
  301. },
  302. /** 监听蓝牙 广播开始 **/
  303. openBluetoothAdapter: function () {
  304. var that = this;
  305. wx.openBluetoothAdapter({
  306. success: (res) => {
  307. console.log('openBluetoothAdapter success', res)
  308. that.startBluetoothDevicesDiscovery()
  309. },
  310. fail: (res) => {
  311. if (res.errCode === 10001) {
  312. wx.onBluetoothAdapterStateChange(function (res) {
  313. // console.log('onBluetoothAdapterStateChange', res)
  314. if (res.available) {
  315. this.startBluetoothDevicesDiscovery()
  316. }
  317. })
  318. }
  319. }
  320. })
  321. },
  322. /** 监听广播 **/
  323. startBluetoothDevicesDiscovery() {
  324. wx.startBluetoothDevicesDiscovery({
  325. allowDuplicatesKey: true,
  326. interval: 0,
  327. success: (res) => {
  328. console.log('startBluetoothDevicesDiscovery success', res)
  329. this.onBluetoothDeviceFound()
  330. },
  331. fail: (res) => {
  332. console.log('开始搜索失败', res)
  333. }
  334. })
  335. },
  336. /** 接收广播数据,解析数据 **/
  337. onBluetoothDeviceFound() {
  338. wx.onBluetoothDeviceFound((res) => {
  339. res.devices.forEach(device => {
  340. if (device.advertisServiceUUIDs == null || device.advertisServiceUUIDs.length == 0) { //判断是否为15设备
  341. if (deviceInfo != "1" && deviceInfo != null && deviceInfo.macAddress.length != 9) { //判断是否已经绑定设备
  342. var networkData = plugin.getBbroadcastData(device.advertisData);//获取广播称基础数据
  343. if (networkData != null && networkData.address == deviceInfo_address) {//判断基础数据是否为空,广播数据的mac地址是否等于绑定设备mac地址
  344. var sex = obtainSex(this.data.sex);//获取用户性别
  345. var age = obtainAge(this.data.briday);//获取用户年龄、
  346. var heigjtObt = obtainHeight();
  347. console.log("sex:" + sex + ";age:" + age + ";height:" + heigjtObt);
  348. if (networkData.cmdType == 3) {//称重数据稳定,阻抗测量结束
  349. if (networkData.adc > 0) { //有阻抗时
  350. var fatData = plugin.getBodyDataByInet(age, heigjtObt ,sex,networkData.weight,networkData.adc);//年龄,身高,性别,重量,阻抗
  351. this.setData({
  352. weightsum: networkData.weight.toFixed(1), ///<重量 +
  353. adc: networkData.adc, ///<阻抗
  354. BMI: fatData.bmi, ///<BMI
  355. fatRate: fatData.bfr, ///<体脂率
  356. muscle: fatData.rom, ///<肌肉率
  357. moisture: fatData.moi, ///<水份
  358. boneMass: fatData.bm, ///<骨量
  359. BMR: fatData.bmr, ///<基础代谢率
  360. visceralFat: fatData.uvi, ///<内脏脂肪
  361. subcutaneousFat: fatData.sfr, ///<皮下脂肪
  362. proteinRate: fatData.pp, ///<蛋白率
  363. physicalAge: fatData.pAge, ///<生理年龄
  364. temp: networkData.temp, ////温度
  365. linksToPrompt: "测量完成",
  366. unit: "%"
  367. });
  368. var scaleFatData = plugin.getBodyScaleDataByInet(heigjtObt, networkData.weight, sex, fatData.bfr, fatData.rom, fatData.pp);//身高 体重 性别 体脂率 肌肉率 蛋白率
  369. var weightStandard = standardTool.getWeightDisplay(heigjtObt,networkData.weight) //获取体重判定标准等级
  370. var bmiStandard = standardTool.getBMIDisplay(fatData.bmi) //获取bmi判定标准等级
  371. //其余各项标准可以在utils/standard.js文件查看所需函数和参数。
  372. this.setData({
  373. standardExp:bmiStandard.standard,
  374. standardTextExp:bmiStandard.text,
  375. standardWeight: scaleFatData.standardWeight, //标准体重
  376. controlWeight: scaleFatData.controlWeight, //体重控制量
  377. fat: scaleFatData.fat, //脂肪量
  378. removeFatWeight: scaleFatData.removeFatWeight, //去脂肪体重
  379. muscleMass: scaleFatData.muscleMass, //肌肉量
  380. protein: scaleFatData.protein //蛋白量
  381. });
  382. } else {//无阻抗时
  383. this.restartData();
  384. this.setData({
  385. weightsum: networkData.weight.toFixed(1), ///<重量
  386. adc: networkData.adc, ///<阻抗
  387. temp: networkData.temp, ////温度
  388. linksToPrompt: "阻抗测量失败",
  389. })
  390. }
  391. } else if (networkData.cmdType == 1) {//称重数据未稳定时
  392. this.setData({
  393. weightsum: networkData.weight.toFixed(1),
  394. linksToPrompt: "称量中...",
  395. fatdata: "称量中"
  396. })
  397. } else if (networkData.cmdType == 2) {//阻抗测量中
  398. this.restartData();
  399. this.setData({
  400. linksToPrompt: "数据分析中,请不要下秤...",
  401. weightsum: networkData.weight,
  402. adc: networkData.adc, ///<阻抗
  403. temp: networkData.temp, ////温度
  404. })
  405. }
  406. }
  407. }
  408. }
  409. })
  410. })
  411. },
  412. restartData(){//设置默认值
  413. if (this.data.BMI != null && this.data.BMI != ""){
  414. this.setData({
  415. BMI:"", ///<BMI
  416. fatRate: "", ///<体脂率
  417. muscle: "", ///<肌肉率
  418. moisture: "", ///<水份
  419. boneMass: "", ///<骨量
  420. BMR: "", ///<基础代谢率
  421. visceralFat: "", ///<内脏脂肪
  422. subcutaneousFat: "", ///<皮下脂肪
  423. proteinRate: "", ///<蛋白率
  424. physicalAge: "", ///<生理年龄
  425. unit: "%",
  426. standardWeight: "", //标准体重
  427. controlWeight: "", //体重控制量
  428. fat: "", //脂肪量
  429. removeFatWeight: "", //去脂肪体重
  430. muscleMass: "", //肌肉量
  431. protein: "" //蛋白量
  432. });
  433. }
  434. }
  435. })
  436. function obtainSex(sexTemp) {
  437. if (sexTemp == "男") {
  438. return 1;
  439. } else {
  440. return 2;
  441. }
  442. }
  443. /// 获取年龄
  444. function obtainAge(bridayTemp) {
  445. if (bridayTemp == "") {
  446. bridayTemp = "1990-05-10";
  447. }
  448. var date1 = new Date(bridayTemp)
  449. var date2 = new Date()
  450. var s1 = date1.getTime(), s2 = date2.getTime();
  451. var total = (s2 - s1) / 1000;
  452. var day = parseInt(total / (24 * 60 * 60));//计算整数天数
  453. var age = parseInt(day / 365) + 1;
  454. return age;
  455. }
  456. /// 获取身高
  457. function obtainHeight() {
  458. var heightTemp = wx.getStorageSync('UserHight');
  459. if (heightTemp == null || heightTemp == "") {
  460. heightTemp = 160;
  461. }else{
  462. return heightTemp;
  463. }
  464. }