You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 15KB

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