store.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import config from 'common/config.js'
  4. import reqUtil from 'util/util-js/requstUtil.js'
  5. import date from 'util/util-js/date.js'
  6. import cNumber from 'util/util-js/makeNumber.js'
  7. import BLE from 'util/util-js/BLE.js';
  8. //快速打击部分
  9. import o0 from "@/util/util-js/o0.js"
  10. import o0Project from "@/util/util-js/o0Project.js"
  11. import deviceData from "@/util/util-js/devices.js"
  12. Vue.use(Vuex)
  13. const store = new Vuex.Store({
  14. state: {
  15. //引导层
  16. bGuidePages: false,
  17. //是否显示Canvas
  18. bCanvasShow: true,
  19. /**
  20. * 是否需要强制登录
  21. */
  22. forcedLogin: false,
  23. /**
  24. * 蓝牙实时发送数据的页面的名称
  25. */
  26. currentPageName: '',
  27. hasLogin: false,
  28. userInfo: null,
  29. userName: "匿名",
  30. days: 0,
  31. remainingDays: 0,
  32. signature: '',
  33. avatarUrl: "/static/defaultAvatar.png",
  34. // 是否是新用户,
  35. //TODO:登录接口返回的数据来判断
  36. bNewUser: false,
  37. //是否是第一次走新手
  38. bNewGuide: false,
  39. token: '',
  40. gender: 0,
  41. //省市区数据
  42. city: {
  43. label: '北京市-北京市-东城区',
  44. cityCode: "110101",
  45. //picker 选择器的index数组
  46. value: [0, 0, 0]
  47. },
  48. birthday: date.formatTime(new Date()),
  49. //默认值
  50. height: 0,
  51. weight: 0,
  52. //最近在玩的列表
  53. playGames: [],
  54. //最近在玩的排行游戏列表
  55. playRankingGames: [],
  56. //全部的游戏列表
  57. allGames: [],
  58. //关注的游戏列表
  59. favoriteGames: [],
  60. // 排行榜里面显示的游戏
  61. rankSelectedGame: null,
  62. rankInfo: null,
  63. cityName: "某市",
  64. /**
  65. * 默认的运动数据
  66. * date.addDaysFromNewDate(new Date(),1)
  67. */
  68. defaultPlanData: {
  69. startTime: date.formatDate(new Date()),
  70. endTime: date.addDaysFromNewDate(new Date(), 6),
  71. targetWeight: 0, //目标公斤
  72. calorie: 360, //运动消耗的卡路里,默认300 大卡
  73. cumulativeCalorie: 0, //累计增重
  74. sportTime: 40, //运动时间
  75. /**
  76. * 本地记录的消耗卡路里数量
  77. */
  78. localCalorie: 0,
  79. /**
  80. * 显示卡路里,过了12点清空
  81. */
  82. showCalorie: 0,
  83. /**
  84. * 总和
  85. */
  86. allCalorie: 0
  87. },
  88. /**
  89. * 计划的运动数据
  90. */
  91. planData: {
  92. startTime: date.formatDate(new Date()),
  93. endTime: date.formatDate(new Date()), //date.addDaysFromNewDate(new Date(),1),
  94. targetWeight: 0, //目标公斤
  95. cumulativeCalorie: 0, //累计增重
  96. calorie: 300, //运动消耗的卡路里
  97. sportTime: 40, //运动时间
  98. /**
  99. * 本地记录的消耗卡路里数量
  100. */
  101. localCalorie: 0,
  102. /**
  103. * 显示卡路里,过了12点清空
  104. */
  105. showCalorie: 0,
  106. /**
  107. * 总和
  108. */
  109. allCalorie: 0
  110. },
  111. //记录表盘计划刷新的卡路里
  112. oldArcbarProCalorie: 0,
  113. //记录表盘当前的总卡路里
  114. oldArcbarAllCalorie: 0,
  115. //当前运动的世时间
  116. localSportTime: 0,
  117. //当前运动时间,运动时候记录的时间
  118. runingTime: 0,
  119. //蓝牙设备信息列表
  120. //id 不可变,用于保存到本地区分
  121. BLEInfoList: deviceData.getDeviceList(),
  122. //记录的蓝牙设备,保存id, 和对应的蓝牙连接数据
  123. BLEDeviceList: [],
  124. //显示的蓝牙设备,取本地数据时候更新
  125. BLEDeviceShowList: [],
  126. //选择连接的蓝牙设备
  127. BLEConnectDevice: null,
  128. //获取到的服务
  129. BLEGetServices:null,
  130. //当前发送给蓝牙的指令,目前是只有蓝牙手柄
  131. currentInstruction: '',
  132. instructionState: {
  133. // bAcc: false, //是否开启加速计状态
  134. // bGyroscope: false, //是否开启陀螺仪状态
  135. bOpen: false, //是否开启指令
  136. bSteps: false, //是否开启步数
  137. },
  138. //绑定的设备
  139. DeviceBindingList: [],
  140. ConnectBindingDevice: null,
  141. cIndex: -1,
  142. //当前是否连接
  143. bConnection: false,
  144. //最近连接过的设备
  145. finallyUseDevice: null,
  146. //phoneNumber
  147. phoneNumber: '',
  148. openid: '',
  149. appleid: '',
  150. //获取验证码
  151. bCodeDisabled: false,
  152. count: 59,
  153. interval: null,
  154. //conversion列表
  155. oldSwiperList: [],
  156. // 计划任务过期
  157. bPlanExpired: false,
  158. // 制定玩计划任务后
  159. bPlanFinish: false,
  160. //标准体重,男女根据当前年龄计算
  161. standardWeight: 0,
  162. //是否安装了微信
  163. bInstallWechat: false,
  164. bHideWeixin: false,
  165. system: '', // 系统版本
  166. platform: '', // 平台
  167. appName: '',
  168. version: '1.6.3', //软件版本
  169. versionCode: '',
  170. showEndTime: config.endTime, //游戏显示时间字段,后台返回对应此时间之前的数据
  171. globalAcc: null,
  172. globalOri: null,
  173. //当前显示的模式下标
  174. currentModeIndex: 0,
  175. //蓝牙变量操作
  176. /**
  177. * 操作蓝牙设备
  178. */
  179. bOpenBluetooth: false,
  180. /**
  181. * 是否初始化蓝牙适配器
  182. */
  183. bOpenSuccess: false,
  184. /**
  185. * 是否监听蓝牙状态变化
  186. */
  187. bListenAdapterStateChange: false,
  188. /**
  189. * 是否监听寻找新设备事件
  190. */
  191. bListenDeviceFound: false,
  192. // 设备Id
  193. BLEDeviceId: "",
  194. // 服务Id
  195. BLEServiceId: "",
  196. // 特征值id
  197. BLENotifyCharacteristicId: "",
  198. //监听rssi对象
  199. BLERSSIInterval: null,
  200. //蓝牙服务timeout
  201. getBLEDeviceServicesTimeout: null,
  202. //本地游戏地址
  203. // http://192.168.0.112:7456/build/index.html
  204. // http://127.0.0.1:7456/build/index.html
  205. LocationGameUrl: "http://110.43.54.43/t8/",
  206. //快速打击对象
  207. filter: null,
  208. deviceMs: 1
  209. },
  210. mutations: {
  211. /**
  212. * 获取记录的最后一次连接设备
  213. * @param {Object} state
  214. */
  215. getFinalUseDevice(state) {
  216. // 获取本地存储的最后一次使用的设备
  217. //1.获取最近连接的蓝牙设备, 指蓝牙搜索到的obj
  218. const value = uni.getStorageSync('finallyUseDevice');
  219. if (value) {
  220. state.finallyUseDevice = value;
  221. }
  222. console.log('获取的finallyUseDevice', value);
  223. },
  224. /**
  225. * 存储最后一次使用的设备信息,后面登陆时候,如果存在设备最后一次使用信息,则自动连接
  226. * @param {Object} state
  227. * @param {Object} device
  228. */
  229. setFinallUseDevice(state, device) {
  230. uni.setStorage({
  231. key: 'finallyUseDevice',
  232. data: device,
  233. success: (res) => {
  234. state.finallyUseDevice = device;
  235. console.log("保存finallyUseDevice:", res);
  236. }
  237. })
  238. },
  239. /**
  240. * 移除记录的use device
  241. */
  242. clearFinallUseDevice(state) {
  243. uni.removeStorage({
  244. key: 'finallyUseDevice',
  245. success: (res) => {
  246. state.finallyUseDevice = null;
  247. console.log("移除finallyUseDevice:", res);
  248. }
  249. })
  250. },
  251. /**
  252. * 设置运动时间
  253. * @param {Object} state
  254. * @param {Object} time
  255. */
  256. setLocalSportTime(state, time) {
  257. // let _sportTime = Number(time);
  258. state.localSportTime = Number(time);
  259. uni.setStorage({
  260. key: 'localSportTime',
  261. data: state.localSportTime,
  262. success: (res) => {
  263. // uni.showToast({
  264. // title: "保存成功"
  265. // })
  266. }
  267. })
  268. },
  269. /**
  270. * 设置卡路里
  271. * @param {Object} state
  272. * @param {Object} calorie
  273. */
  274. setLocalCalorie(state, calorie) {
  275. state.planData.localCalorie = Number(calorie);
  276. uni.setStorage({
  277. key: 'localCalorie',
  278. data: state.planData.localCalorie,
  279. success: (res) => {
  280. // uni.showToast({
  281. // title: "保存成功"
  282. // })
  283. }
  284. })
  285. },
  286. /**
  287. * 设置表盘显示的卡路里,
  288. * @param {Object} state
  289. * @param {Object} calorie
  290. */
  291. setShowCalorie(state, calorie) {
  292. state.planData.showCalorie = Number(calorie);
  293. uni.setStorage({
  294. key: 'showCalorie',
  295. data: state.planData.showCalorie,
  296. success: (res) => {
  297. // uni.showToast({
  298. // title: "保存成功"
  299. // })
  300. }
  301. })
  302. },
  303. /**
  304. * 增加卡路里
  305. * @param {Object} state
  306. * @param {Object} calorie
  307. */
  308. addlocalCalorie(state, calorie) {
  309. //卡路里不能为负数
  310. calorie = Math.abs(calorie);
  311. //当前需要添加的卡路里
  312. let temNum = Number(calorie);
  313. //本地的卡路里
  314. let temNum2 = Number(state.planData.localCalorie);
  315. // 本地加 + 传入的卡路里
  316. let _numberLocal = temNum.add(temNum2);
  317. //显示的卡路里
  318. let _showCalorie = Number(state.planData.showCalorie);
  319. //显示的卡路里加上 本地卡路里 和添加的卡路里之和要小于或等于 计划的卡路里
  320. let _allCalorie = _numberLocal.add(_showCalorie);
  321. // console.log("计算的最后卡路里_showCalorie:", _showCalorie,_allCalorie);
  322. //如果超过计划的,用计划的卡路里记录
  323. if (_allCalorie > state.planData.calorie) {
  324. let _calorie = Number(state.planData.calorie);
  325. //假如更新时候,localCalorie 值不为0,但是还未更新给服务器,此时,不能设置localCalorie 0
  326. //用传入的值,减去 allcalorie 总和和目标的差
  327. let _value = Number(_allCalorie).sub(_calorie);
  328. let _endValue = Number(_numberLocal).sub(_value);
  329. state.planData.localCalorie = Number(_endValue.toFixed(2));
  330. //如果大于或者等于的话,用计划 于 显示的卡路里 差值
  331. // state.planData.localCalorie = Number(_calorie.sub(_showCalorie).toFixed(2));
  332. // //显示最大值
  333. // state.planData.showCalorie = Number(_calorie.sub(state.planData.localCalorie).toFixed(2));
  334. } else {
  335. //
  336. state.planData.localCalorie = Number(_numberLocal.toFixed(2));
  337. // //输入的卡路里 + 自身显示的卡路里
  338. // state.planData.showCalorie = Number(temNum.add(_showCalorie).toFixed(2));
  339. }
  340. console.log("计算的最后卡路里:", state.planData);
  341. if (state.planData.localCalorie < 0) {
  342. //如果修改计划时候,本地数据计算的结果为负数,先取总和,localCalorie负再重置为0
  343. state.planData.showCalorie = _showCalorie.add(state.planData.localCalorie);
  344. state.planData.allCalorie = state.planData.showCalorie;
  345. state.planData.localCalorie = 0;
  346. } else {
  347. state.planData.allCalorie = _showCalorie.add(state.planData.localCalorie);
  348. }
  349. try {
  350. uni.setStorageSync('localCalorie', state.planData.localCalorie);
  351. } catch (e) {
  352. // error
  353. console.error(e);
  354. }
  355. try {
  356. uni.setStorageSync('showCalorie', state.planData.showCalorie);
  357. } catch (e) {
  358. // error
  359. console.error(e);
  360. }
  361. // uni.setStorage({
  362. // key: 'localCalorie',
  363. // data: state.planData.localCalorie,
  364. // success: (res) => {
  365. // // uni.showToast({
  366. // // title: "保存成功"
  367. // // })
  368. // }
  369. // })
  370. // uni.setStorage({
  371. // key: 'showCalorie',
  372. // data: state.planData.showCalorie,
  373. // success: (res) => {
  374. // // uni.showToast({
  375. // // title: "保存成功"
  376. // // })
  377. // }
  378. // })
  379. },
  380. /**
  381. * 每次登陆同步本地数据到服务器
  382. * 当天的数据更新,只更新一次
  383. * @param {Object} state
  384. */
  385. syncLocalDataToServer(state, callback) {
  386. let self = this;
  387. uni.getStorage({
  388. key: 'syncDate',
  389. success: function(res) {
  390. // console.log(res.data);
  391. let oldTime = res.data;
  392. let nDate = new Date();
  393. let newTime = date.formatDate(nDate);
  394. let days = date.datedifference(newTime, oldTime);
  395. // console.log(days);
  396. // 如果相差大于1天,
  397. if (days != 0) {
  398. // 更新存储日期
  399. uni.setStorageSync("syncDate", newTime);
  400. // 执行更新函数
  401. self.commit("syncRequestEvent", {
  402. success: () => {
  403. //清空显示的本地数据
  404. self.commit("setShowCalorie", 0);
  405. state.planData.allCalorie = state.planData.showCalorie.
  406. add(state.planData.localCalorie);
  407. if (callback)
  408. callback({
  409. newDate: nDate
  410. });
  411. }
  412. });
  413. //清空记录的本地运动时间
  414. // console.log('情况运动时间');
  415. self.commit("setLocalSportTime", 0);
  416. //设置本地数据是0
  417. state.localSportTime = 0;
  418. } else {
  419. if (callback)
  420. callback({
  421. newDate: nDate
  422. });
  423. }
  424. // date.getDays.
  425. },
  426. fail: function(res) {
  427. // console.error(res);
  428. // 如果没有日期,则存储一个新的
  429. let nDate = new Date();
  430. let tempTime = date.formatDate(nDate);
  431. uni.setStorageSync("syncDate", tempTime);
  432. if (callback)
  433. callback({
  434. newDate: nDate
  435. });
  436. }
  437. });
  438. },
  439. // 同步数据的请求
  440. syncRequestEvent(state, data) {
  441. let self = this;
  442. let {
  443. success,
  444. fail
  445. } = data;
  446. //需要卡路里取整存服务器,小数点后面,继续保留。以消除累计误差
  447. let oldLocalCalorie = state.planData.localCalorie;
  448. let _localCalorieInt = parseInt(state.planData.localCalorie);
  449. //小数点后面
  450. let _afterLocalCalorie = Number(oldLocalCalorie).sub(_localCalorieInt);
  451. console.log("*******===:", oldLocalCalorie, _localCalorieInt, _afterLocalCalorie);
  452. if (_localCalorieInt === 0) {
  453. // uni.showToast({
  454. // title: '',
  455. // icon: 'loading',
  456. // mask: true,
  457. // duration: 1000
  458. // })
  459. if (success)
  460. success();
  461. return;
  462. }
  463. uni.showToast({
  464. title: '',
  465. icon: 'loading',
  466. mask: false,
  467. duration: 10000
  468. })
  469. //把卡路里累计到cumulativeCalorie 变量,之后传服务器 += Math.round(state.planData.localCalorie)
  470. state.planData.cumulativeCalorie += _localCalorieInt;
  471. //添加到showCalorie 中
  472. state.planData.showCalorie = Number(state.planData.showCalorie).add(_localCalorieInt);
  473. state.planData.allCalorie = state.planData.showCalorie.add(_afterLocalCalorie);
  474. //不管是否同步成功,清空本地记录的卡路里,前一步已添加到cumulativeCalorie
  475. self.commit("setLocalCalorie", _afterLocalCalorie);
  476. self.commit("setShowCalorie", Number(state.planData.showCalorie));
  477. // 把卡路里记录到服务器
  478. reqUtil.requestData(config.URL.FITNESSPROGRAM, state.planData, 'POST').then(
  479. res => {
  480. // console.warn(res);
  481. if (res.code == 0) {
  482. uni.showToast({
  483. title: '同步成功',
  484. mask: true,
  485. duration: 1000
  486. });
  487. //同步一下服务器的返回值
  488. state.planData = Object.assign(state.planData, res.data);
  489. //暂时默认同步拳击1 的卡路里
  490. reqUtil.requestData(config.URL.UPLOADRANKING, {
  491. gameId: '1', //拳击模式 game id = 1;
  492. score: _localCalorieInt,
  493. cityCode: state.city.cityCode,
  494. }, "POST").then(res => {
  495. console.warn('****上传首页拳击卡路里,后面多个模式,可能需要多个不同的游戏id ***', res);
  496. },
  497. e => {
  498. console.log(e)
  499. });
  500. setTimeout(() => {
  501. uni.hideToast();
  502. }, 1000);
  503. if (success)
  504. success();
  505. } else {
  506. state.planData.cumulativeCalorie = Number(state.planData.cumulativeCalorie).sub(_localCalorieInt);
  507. state.planData.showCalorie = Number(state.planData.showCalorie).sub(_localCalorieInt);
  508. self.commit("setLocalCalorie", oldLocalCalorie);
  509. if (fail)
  510. fail();
  511. }
  512. console.log("减法 ==", Number(state.planData.cumulativeCalorie).sub(_localCalorieInt));
  513. },
  514. e => {
  515. console.log(e);
  516. state.planData.cumulativeCalorie = Number(state.planData.cumulativeCalorie).sub(_localCalorieInt);
  517. state.planData.showCalorie = Number(state.planData.showCalorie).sub(_localCalorieInt);
  518. self.commit("setLocalCalorie", oldLocalCalorie);
  519. uni.hideToast();
  520. if (fail)
  521. fail();
  522. }
  523. );
  524. },
  525. //登录成功后,重置一下状态
  526. resetCountDown(state) {
  527. if (state.interval) {
  528. clearInterval(state.interval);
  529. state.interval = null;
  530. state.count = 59;
  531. state.bCodeDisabled = false;
  532. }
  533. },
  534. //验证码操作
  535. countDown(state, phoneNumber) {
  536. //限制验证码操作
  537. state.bCodeDisabled = true;
  538. state.interval = setInterval(() => {
  539. --state.count;
  540. if (state.count <= 0) {
  541. clearInterval(state.interval);
  542. state.interval = null;
  543. state.count = 59;
  544. state.bCodeDisabled = false;
  545. }
  546. }, 1000);
  547. setTimeout(() => {
  548. clearInterval(state.interval)
  549. state.count = 59;
  550. state.bCodeDisabled = false;
  551. }, 60000);
  552. reqUtil.requestData(config.URL.GETCODE, {
  553. "phoneNumber": phoneNumber
  554. }).then(res => {
  555. console.log('requestData GETCODE =====', res);
  556. uni.showToast({
  557. title: "发送成功",
  558. mask: true,
  559. duration: 1000
  560. })
  561. },
  562. e => {
  563. console.log(e)
  564. });
  565. },
  566. //获取记录的蓝牙列表数据
  567. getBLEDeviceList(state) {
  568. // console.log("getBLEDeviceList=");
  569. uni.getStorage({
  570. key: 'BLEDeviceList',
  571. success: (res) => {
  572. console.log("*****getBLEDeviceList=", res);
  573. let _list = res.data;
  574. state.BLEDeviceList = res.data;
  575. //更新BLEDeviceShowList 数组
  576. state.BLEDeviceShowList = [];
  577. _list.forEach(item => {
  578. //如果记录到本地的id,和信息列表中的id一致,则结合
  579. for (let i = 0; i < state.BLEInfoList.length; i++) {
  580. let eq = state.BLEInfoList[i];
  581. if (item.id == eq.id) {
  582. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  583. break;
  584. }
  585. }
  586. })
  587. }
  588. })
  589. },
  590. //添加蓝牙设备数据
  591. addBLEDevice(state, data) {
  592. let devicelist = [];
  593. if (state.BLEDeviceList && state.BLEDeviceList.length != 0) {
  594. devicelist = state.BLEDeviceList;
  595. }
  596. // console.log(data, devicelist)
  597. let bHas = false;
  598. devicelist.forEach((item, index, selfArr) => {
  599. if (item.id == data.id) {
  600. //如果存在,更新保存的数据,比如 mac或者uuid等
  601. selfArr[index] = item = Object.assign({}, item, data);
  602. bHas = true;
  603. }
  604. })
  605. //把当前添加的的蓝牙设信息备作为最后一次连接覆盖到本地
  606. this.commit('setFinallUseDevice', data);
  607. if (bHas) {
  608. return;
  609. }
  610. //设置蓝牙列表本地信息
  611. devicelist.push(data);
  612. uni.setStorage({
  613. key: 'BLEDeviceList',
  614. data: devicelist,
  615. success: (res) => {
  616. // uni.showToast({
  617. // title: "保存成功"
  618. // })
  619. state.BLEDeviceList = devicelist;
  620. // console.log("devicelist:",devicelist);
  621. //更新BLEDeviceShowList 数组
  622. state.BLEDeviceShowList = [];
  623. devicelist.forEach(item => {
  624. //如果记录到本地的id,和信息列表中的id一致,则结合
  625. for (let i = 0; i < state.BLEInfoList.length; i++) {
  626. let eq = state.BLEInfoList[i];
  627. if (item.id == eq.id) {
  628. // console.log("item:",item);
  629. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  630. break;
  631. }
  632. }
  633. })
  634. // console.log("state.BLEDeviceShowList:",state.BLEDeviceShowList);
  635. }
  636. })
  637. },
  638. //删除蓝牙设备数据
  639. deleteBLEDevice(state, index) {
  640. let devicelist = [];
  641. devicelist = state.BLEDeviceList;
  642. devicelist.splice(index, 1);
  643. uni.setStorage({
  644. key: 'BLEDeviceList',
  645. data: devicelist,
  646. success: (res) => {
  647. uni.showToast({
  648. title: "删除成功"
  649. })
  650. state.BLEDeviceList = devicelist;
  651. //更新BLEDeviceShowList 数组
  652. state.BLEDeviceShowList = [];
  653. devicelist.forEach(item => {
  654. //如果记录到本地的id,和信息列表中的id一致,则结合
  655. for (let i = 0; i < state.BLEInfoList.length; i++) {
  656. let eq = state.BLEInfoList[i];
  657. if (item.id == eq.id) {
  658. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  659. break;
  660. }
  661. }
  662. })
  663. }
  664. })
  665. },
  666. //获取计划表数据
  667. getPlanData(state, callback) {
  668. reqUtil.requestData(config.URL.FITNESSPROGRAMGET).then(res => {
  669. // console.log('获取计划表数据:getPlanData=====', res.data);
  670. if (res.code == 0) {
  671. state.planData = Object.assign(state.planData, res.data);
  672. // 处理本地数据
  673. let templocalCalorie = uni.getStorageSync("localCalorie");
  674. // console.log("templocalCalorie:", templocalCalorie, state.planData);
  675. if (templocalCalorie) {
  676. templocalCalorie = Number(templocalCalorie);
  677. if (templocalCalorie > state.planData.calorie) {
  678. state.planData.localCalorie = state.planData.calorie;
  679. uni.setStorage({
  680. key: "localCalorie",
  681. data: state.planData.localCalorie
  682. });
  683. } else {
  684. state.planData.localCalorie = templocalCalorie;
  685. }
  686. } else {
  687. state.planData.localCalorie = 0;
  688. }
  689. //处理本地的cumulativeCalorie
  690. let tempShowCalorie = uni.getStorageSync("showCalorie");
  691. // console.log("tempShowCalorie:", tempShowCalorie, state.planData);
  692. if (tempShowCalorie) {
  693. tempShowCalorie = Number(tempShowCalorie);
  694. if (tempShowCalorie > state.planData.calorie) {
  695. state.planData.showCalorie = state.planData.calorie;
  696. uni.setStorage({
  697. key: "showCalorie",
  698. data: state.planData.showCalorie
  699. });
  700. } else {
  701. state.planData.showCalorie = tempShowCalorie;
  702. }
  703. } else {
  704. state.planData.showCalorie = 0;
  705. }
  706. state.planData.allCalorie = state.planData.showCalorie.add(state.planData.localCalorie);
  707. // console.log("======",state.planData )
  708. state.days = date.datedifference(res.data.startTime, res.data.endTime) + 1;
  709. let nDate = new Date();
  710. let tempTime = date.formatDate(nDate);
  711. state.remainingDays = date.datedifference(tempTime, res.data.endTime) + 1;
  712. // console.log("state.remainingDays======", state.remainingDays);
  713. }
  714. // showArcbar
  715. if (callback) {
  716. callback();
  717. }
  718. },
  719. e => {
  720. console.log(e)
  721. });
  722. },
  723. /**
  724. * 获取全部游戏
  725. * @param {Object} state
  726. */
  727. getAllGame(state, callback) {
  728. //获得全部游戏列表
  729. if (state.allGames.length != 0) {
  730. if (callback)
  731. callback();
  732. return;
  733. }
  734. reqUtil.requestData(
  735. config.URL.GAMELIST_BY_RANKING_SHOW, {
  736. rankingShow: 1,
  737. page: 1,
  738. size: 100
  739. }
  740. ).then(res => {
  741. // console.log('获取默认的全部游戏 =====', res);
  742. if (res.code == 0) {
  743. state.allGames = [];
  744. state.allGames = state.allGames.concat(res.data);
  745. }
  746. if (callback)
  747. callback();
  748. },
  749. e => {
  750. console.log(e);
  751. if (callback)
  752. callback();
  753. }
  754. );
  755. },
  756. getRankGame(state, data) {
  757. let {
  758. param,
  759. callback
  760. } = data;
  761. reqUtil.requestData(config.URL.GETRANKRANGR, param).then(res => {
  762. console.log('获取默认的游戏数据:GETRANKRANGR=====', res);
  763. if (res.data != null) {
  764. state.rankSelectedGame = res.data.gameInfo;
  765. state.rankInfo = res.data.rankInfo;
  766. //然后获取排名
  767. }
  768. if (callback)
  769. callback(res);
  770. },
  771. e => {
  772. console.log(e)
  773. });
  774. },
  775. /**
  776. * 苹果端登陆
  777. * @param {Object} state
  778. * @param {Object} data
  779. */
  780. appleUserInfoLogin(state, data) {
  781. let self = this;
  782. let {
  783. params,
  784. callback
  785. } = data;
  786. console.log('获取用户数据:USERINFOAPPLEADD params=====', params);
  787. reqUtil.requestData(config.URL.USERINFOAPPLEADD, params, 'POST').then(res => {
  788. console.log('获取用户数据:USERINFOAPPLEADD =====', res);
  789. if (res.code == 0) {
  790. let userInfo = res.data;
  791. self.commit('login', userInfo);
  792. console.log("11~~");
  793. } else if (res.code = 201) {
  794. state.hasLogin = true;
  795. }
  796. console.log("apple callback!!!");
  797. callback();
  798. },
  799. e => {
  800. console.log(e)
  801. });
  802. },
  803. //添加用户信息和登录
  804. addUserAvatarAndLogin(state, data) {
  805. let self = this;
  806. let {
  807. params,
  808. filePath,
  809. callback
  810. } = data;
  811. console.log("addUserAvatarAndLogin 初始化对应数据");
  812. reqUtil.reqUpload(config.URL.USERINFOADDAVATARINFO, params, filePath).then(res => {
  813. console.log('获取用户数据:USERINFOADDAVATARINFO =====', res);
  814. if (res.code == 0) {
  815. let userInfo = res.data;
  816. self.commit('login', userInfo);
  817. } else if (res.code = 201) {
  818. state.hasLogin = true;
  819. }
  820. if (callback) {
  821. callback();
  822. }
  823. },
  824. e => {
  825. console.log(e)
  826. });
  827. },
  828. //app登录或h5
  829. accountLogin(state, callback) {
  830. let self = this;
  831. // console.log("app登录或h5 初始化对应数据")
  832. reqUtil.requestData(config.URL.USERINFOURL).then(res => {
  833. console.log('获取用户数据:USERINFOURL =====', res);
  834. if (res.code == 0) {
  835. let userInfo = res.data.userInfo;
  836. state.phoneNumber = res.data.phoneNumber || '';
  837. state.openid = res.data.openid || '';
  838. self.commit('login', userInfo);
  839. } else if (res.code = 201) {
  840. state.hasLogin = true;
  841. }
  842. if (callback) {
  843. callback();
  844. }
  845. },
  846. e => {
  847. console.log(e)
  848. });
  849. },
  850. //小程序页面登录
  851. login(state, userInfo) {
  852. console.log("登陆的信息==", userInfo);
  853. if (userInfo.cityCode == "" || userInfo.cityCode == null) {
  854. //不存在就设置默认值
  855. userInfo.cityCode = state.city.cityCode;
  856. }
  857. let cityCode = userInfo.cityCode;
  858. // console.log("cicicicitycode:", cityCode);
  859. let provinceData = require('@/components/slambb-picker/city-data/province.js').default;
  860. let cityData = require('@/components/slambb-picker/city-data/city.js').default;
  861. let areaData = require('@/components/slambb-picker/city-data/area.js').default;
  862. // 解析citycode
  863. for (let i = 0; i < provinceData.length; i++) {
  864. if (provinceData[i].value == cityCode.substr(0, 2)) {
  865. let currentCityData = cityData[i];
  866. for (let j = 0; j < currentCityData.length; j++) {
  867. if (currentCityData[j].value == cityCode.substr(0, 4)) {
  868. let areaDataTemp = areaData[i][j];
  869. for (let k = 0; k < areaDataTemp.length; k++) {
  870. if (areaDataTemp[k].value == cityCode) {
  871. state.city.value = [i, j, k];
  872. state.city.label = provinceData[i].label +
  873. '-' +
  874. currentCityData[j].label +
  875. '-' +
  876. areaDataTemp[k].label;
  877. state.cityName = currentCityData[j].label;
  878. break;
  879. }
  880. }
  881. break;
  882. }
  883. }
  884. break;
  885. }
  886. }
  887. state.userInfo = userInfo;
  888. state.userName = userInfo.username || '新用户';
  889. state.avatarUrl = userInfo.avatarUrl || '/static/defaultAvatar.png';
  890. state.gender = userInfo.gender;
  891. state.signature = userInfo.signature || '';
  892. state.birthday = userInfo.birthday;
  893. state.city.cityCode = cityCode.toString();
  894. state.weight = userInfo.weight || state.weight;
  895. state.height = userInfo.height || state.height;
  896. state.hasLogin = true;
  897. //登录时候 获取一下排行榜里面的游戏
  898. // this.commit('getRankGame', {
  899. // param: {
  900. // type: "china",
  901. // cityCode: 12345
  902. // }
  903. // });
  904. },
  905. // 退出登录
  906. // 删除token 操作
  907. delectToken(state, data) {
  908. let self = this;
  909. let {
  910. success
  911. } = data;
  912. reqUtil.requestData(config.URL.USERLOGINOUT, {}).then(res => {
  913. console.log('USERLOGINOUT=====', res);
  914. self.commit("logout");
  915. try {
  916. //清空保存的storage
  917. uni.clearStorageSync();
  918. } catch (e) {
  919. // error
  920. console.error(e);
  921. }
  922. if (state.BLERSSIInterval) {
  923. clearInterval(state.BLERSSIInterval);
  924. state.BLERSSIInterval = null;
  925. }
  926. if (state.BLEConnectDevice) {
  927. self.commit("onCloseBLEConnection", {
  928. getSuccess: () => {}
  929. });
  930. }
  931. if (success) {
  932. success(res);
  933. }
  934. },
  935. e => {
  936. console.log(e)
  937. });
  938. },
  939. logout(state) {
  940. state.userInfo = null;
  941. state.userName = "匿名";
  942. state.avatarUrl = '/static/defaultAvatar.png';
  943. state.days = '0';
  944. state.signature = '';
  945. state.gender = 0;
  946. state.hasLogin = false;
  947. state.phoneNumber = '';
  948. state.openid = '';
  949. state.appleid = '';
  950. //处理设备值
  951. state.cIndex = -1;
  952. //绑定的设备
  953. state.ConnectBindingDevice = null;
  954. //蓝牙设备
  955. state.BLEConnectDevice = null;
  956. //蓝牙服务
  957. state.BLEGetServices = null;
  958. //最近连接过的设备
  959. state.finallyUseDevice = null;
  960. //记录的蓝牙设备,保存id, 和对应的蓝牙连接数据
  961. state.BLEDeviceList = [];
  962. //显示的蓝牙设备,取本地数据时候更新
  963. state.BLEDeviceShowList = [];
  964. //记录表盘计划刷新的卡路里
  965. state.oldArcbarProCalorie = 0;
  966. //记录表盘当前的总卡路里
  967. state.oldArcbarAllCalorie = 0;
  968. },
  969. loginUserInfo(state, data) {
  970. let {
  971. infoRes,
  972. success,
  973. fail
  974. } = data;
  975. console.log('loginUserInfo');
  976. reqUtil.requestData(config.URL.WXGETUSERINFO, {
  977. appid: 'wxd6dfd60729d33d17',
  978. encryptedData: infoRes.encryptedData,
  979. iv: infoRes.iv,
  980. signature: infoRes.signature,
  981. rawData: infoRes.rawData
  982. }).then(res => {
  983. console.log('wxInfoRes=====', res);
  984. let userInfo = res.data;
  985. this.commit('login', userInfo);
  986. if (success) {
  987. success(res);
  988. }
  989. },
  990. e => {
  991. console.log(e)
  992. if (fail) {
  993. success(fail);
  994. }
  995. });
  996. },
  997. //这里统一静默处理login
  998. oauth(state, context) {
  999. let {
  1000. value,
  1001. callback
  1002. } = context;
  1003. uni.login({
  1004. provider: value,
  1005. success: (res) => {
  1006. // console.log('code:', res);
  1007. reqUtil.requestData(config.URL.USERlOGINURL, {
  1008. appid: 'wxd6dfd60729d33d17',
  1009. code: res.code,
  1010. platfrom: value
  1011. }).then(res => {
  1012. console.log('login token=====', res);
  1013. state.token = res.data.token;
  1014. uni.setStorageSync('token', state.token);
  1015. uni.getUserInfo({
  1016. provider: value,
  1017. success: (infoRes) => {
  1018. if (callback) {
  1019. callback(infoRes);
  1020. }
  1021. },
  1022. fail: (failRes) => {
  1023. //假如没有授权,login 之后,不做处理,需要用户点击授权时候,再进行处理用户登录
  1024. console.log('getUserInfo failRes:', failRes)
  1025. }
  1026. });
  1027. },
  1028. e => {
  1029. console.log(e)
  1030. });
  1031. },
  1032. fail: (err) => {
  1033. console.error('授权登录失败:' + JSON.stringify(err));
  1034. }
  1035. });
  1036. },
  1037. onLuanchLogin(state, data) {
  1038. let {
  1039. success
  1040. } = data;
  1041. const filters = ['weixin', 'qq', 'sinaweibo'];
  1042. let platform = '';
  1043. uni.getProvider({
  1044. service: 'oauth',
  1045. success: (res) => {
  1046. if (res.provider && res.provider.length) {
  1047. for (let i = 0; i < res.provider.length; i++) {
  1048. if (~filters.indexOf(res.provider[i])) {
  1049. platform = res.provider[i];
  1050. console.log("platform=", platform);
  1051. uni.showToast({
  1052. icon: 'loading',
  1053. title: ''
  1054. })
  1055. this.commit('oauth', {
  1056. value: platform,
  1057. callback: (infoRes) => {
  1058. // console.log("infoRes:", infoRes);
  1059. this.commit('loginUserInfo', {
  1060. infoRes: infoRes,
  1061. success: (res) => {
  1062. console.log("获取用户成功");
  1063. if (success) {
  1064. success(res);
  1065. }
  1066. uni.hideToast();
  1067. },
  1068. fail: (res) => {
  1069. console.error("获取用户失败");
  1070. uni.hideToast();
  1071. }
  1072. });
  1073. }
  1074. })
  1075. }
  1076. }
  1077. this.hasProvider = true;
  1078. }
  1079. },
  1080. fail: (err) => {
  1081. console.error('获取服务供应商失败:' + JSON.stringify(err));
  1082. }
  1083. });
  1084. },
  1085. initAdapter(state, callback) {
  1086. // if (state.bOpenBluetooth) return;
  1087. //初始化蓝牙模块
  1088. BLE.openBluetoothAdapter({
  1089. success: (res) => {
  1090. state.bOpenSuccess = true;
  1091. state.bOpenBluetooth = true;
  1092. if (callback)
  1093. callback(res);
  1094. //监听断开事件
  1095. BLE.onBLEConnectionStateChange();
  1096. },
  1097. fail: (fail) => {
  1098. state.bOpenSuccess = false;
  1099. state.bOpenBluetooth = false;
  1100. uni.showToast({
  1101. title: '蓝牙尚未开启!',
  1102. icon: 'none'
  1103. })
  1104. },
  1105. complete: (complete) => {
  1106. console.log("complete bListenAdapterStateChange:", state.bListenAdapterStateChange);
  1107. if (state.bListenAdapterStateChange) return;
  1108. state.bListenAdapterStateChange = true;
  1109. uni.onBluetoothAdapterStateChange((res) => {
  1110. console.log('adapterState changed, now is', res)
  1111. // 手机蓝牙状态
  1112. state.bOpenBluetooth = res.available;
  1113. if (state.bOpenBluetooth && !state.bOpenSuccess) {
  1114. this.commit('initAdapter');
  1115. }
  1116. })
  1117. }
  1118. });
  1119. },
  1120. onGetDevices(state, context) {
  1121. let {
  1122. success
  1123. } = context;
  1124. if (!state.BLEConnectDevice) return;
  1125. // #ifdef APP-PLUS
  1126. let deviceId = state.BLEConnectDevice.deviceId;
  1127. uni.getBLEDeviceRSSI({
  1128. deviceId,
  1129. success: res => {
  1130. let _dis = Math.pow(Math.E, (Math.abs(res.RSSI) - 66.78) / 16.56);
  1131. if (_dis > state.BLEConnectDevice.limitDis) {
  1132. if (state.cIndex != -1) {
  1133. uni.showToast({
  1134. title: '设备断开连接!',
  1135. icon: 'none',
  1136. duration: 2000,
  1137. mask: true
  1138. })
  1139. }
  1140. setTimeout(() => {
  1141. BLE.closeBLEConnection(deviceId);
  1142. state.cIndex = -1;
  1143. state.bConnection = false;
  1144. }, 1000)
  1145. } else {
  1146. // console.log("DIS~~~~~~~~~", _dis);
  1147. if (success) {
  1148. success({
  1149. RSSI: res.RSSI,
  1150. DIS: _dis
  1151. });
  1152. }
  1153. }
  1154. }
  1155. })
  1156. // #endif
  1157. },
  1158. //清除连接的开启的timeout
  1159. onUnloadCreateBLEConnectionTimeout(state) {
  1160. if (state.getBLEDeviceServicesTimeout) {
  1161. clearTimeout(state.getBLEDeviceServicesTimeout);
  1162. state.getBLEDeviceServicesTimeout = null;
  1163. }
  1164. },
  1165. //连接蓝牙
  1166. onCreateBLEConnection(state, context) {
  1167. let {
  1168. index,
  1169. item,
  1170. initItem = false,
  1171. getSuccess = null,
  1172. getinitAdapter = null
  1173. } = context;
  1174. BLE.createBLEConnection(item, success => {
  1175. state.bConnection = true;
  1176. if (state.getBLEDeviceServicesTimeout) {
  1177. clearTimeout(state.getBLEDeviceServicesTimeout);
  1178. state.getBLEDeviceServicesTimeout = null;
  1179. }
  1180. state.getBLEDeviceServicesTimeout = setTimeout(function() {
  1181. BLE.getBLEDeviceServices(item, () => {
  1182. uni.showToast({
  1183. title: '连接成功',
  1184. icon: 'loading',
  1185. duration: 2000,
  1186. mask: true
  1187. })
  1188. //是否需要设置item,默认不设置
  1189. if (initItem) {
  1190. state.cIndex = index;
  1191. state.BLEConnectDevice = item;
  1192. }
  1193. if (getSuccess)
  1194. getSuccess();
  1195. },(serviceList)=>{
  1196. //搜索服务失败后
  1197. state.BLEGetServices = serviceList;
  1198. });
  1199. }, 2000);
  1200. // this.commit('onGetDevices', {
  1201. // success: () => {
  1202. // if (state.getBLEDeviceServicesTimeout) {
  1203. // clearTimeout(state.getBLEDeviceServicesTimeout);
  1204. // state.getBLEDeviceServicesTimeout = null;
  1205. // }
  1206. // state.getBLEDeviceServicesTimeout = setTimeout(function() {
  1207. // BLE.getBLEDeviceServices(item, () => {
  1208. // uni.showToast({
  1209. // title: '连接成功',
  1210. // icon: 'loading',
  1211. // duration: 2000,
  1212. // mask: true
  1213. // })
  1214. // //是否需要设置item,默认不设置
  1215. // if (initItem) {
  1216. // state.cIndex = index;
  1217. // state.BLEConnectDevice = item;
  1218. // }
  1219. // state.bConnection = true;
  1220. // if (getSuccess)
  1221. // getSuccess();
  1222. // });
  1223. // }, 500);
  1224. // // uni.$emit('connectionSuccess');
  1225. // if (state.BLERSSIInterval) {
  1226. // clearInterval(state.BLERSSIInterval);
  1227. // state.BLERSSIInterval = null;
  1228. // }
  1229. // state.BLERSSIInterval = setInterval(() => {
  1230. // this.commit('onGetDevices', {
  1231. // success: () => {}
  1232. // });
  1233. // }, 1000)
  1234. // }
  1235. // })
  1236. },
  1237. fail => {
  1238. console.log("***fail:",fail);
  1239. if (fail.errCode === 10012) {
  1240. console.log("连接超时,请重试!");
  1241. uni.showToast({
  1242. title: '连接失败,请重试',
  1243. icon: 'none',
  1244. duration: 2000,
  1245. mask: true
  1246. })
  1247. } else if (fail.errCode === 10013) {
  1248. console.log("连接失败,蓝牙地址无效!");
  1249. uni.showToast({
  1250. title: '连接失败,蓝牙地址无效',
  1251. icon: 'none',
  1252. duration: 2000,
  1253. mask: true
  1254. })
  1255. } else if (fail.errCode === 10000) {
  1256. // console.log("连接失败,初始化 uni.openBluetoothAdapter 调用之后使用");
  1257. // uni.showToast({
  1258. // title: '连接失败,检查手机蓝牙是否开启',
  1259. // icon: 'none',
  1260. // duration: 2000,
  1261. // mask: true
  1262. // })
  1263. this.commit('initAdapter', () => {
  1264. if (getinitAdapter) {
  1265. getinitAdapter();
  1266. }
  1267. })
  1268. // this.initAdapter(() => {
  1269. // this.onDevice(item, e);
  1270. // });
  1271. } else {
  1272. // err.errCode10003原因多种:蓝牙设备未开启或异常导致无法连接;蓝牙设备被占用或者上次蓝牙连接未断开导致无法连接
  1273. console.log("连接失败,请重试!");
  1274. uni.showToast({
  1275. title: '设备未开启或被占用异常导致无法连接。',
  1276. icon: 'none',
  1277. duration: 2000,
  1278. mask: true
  1279. })
  1280. }
  1281. });
  1282. },
  1283. //直接处理成功
  1284. onCreateBLESuccess(state, context) {
  1285. let {
  1286. item,
  1287. getSuccess = null
  1288. } = context;
  1289. BLE.createBLEConnection(item, success => {
  1290. state.BLEConnectDevice = item;
  1291. state.bConnection = true;
  1292. if (getSuccess) {
  1293. getSuccess();
  1294. }
  1295. // if (state.BLERSSIInterval) {
  1296. // clearInterval(state.BLERSSIInterval);
  1297. // state.BLERSSIInterval = null;
  1298. // }
  1299. // state.BLERSSIInterval = setInterval(() => {
  1300. // this.commit('onGetDevices', {
  1301. // success: () => {}
  1302. // });
  1303. // }, 1000)
  1304. // uni.showToast({
  1305. // icon: "none",
  1306. // title: "连接设备成功!",
  1307. // duration: 2000
  1308. // })
  1309. },
  1310. fail => {
  1311. if (fail.errCode === 10012) {
  1312. console.log("连接超时,请重试!");
  1313. uni.showToast({
  1314. title: '连接超时,检查对应设备是否开启?',
  1315. icon: 'none',
  1316. duration: 2000,
  1317. mask: true
  1318. })
  1319. } else if (fail.errCode === 10013) {
  1320. console.log("连接失败,蓝牙地址无效!");
  1321. uni.showToast({
  1322. title: '蓝牙地址无效,检查设备是否正常?',
  1323. icon: 'none',
  1324. duration: 2000,
  1325. mask: true
  1326. })
  1327. } else if (fail.errCode === 10000) {
  1328. console.log("连接失败,初始化 wx.openBluetoothAdapter 调用之后使用");
  1329. uni.showToast({
  1330. title: '连接失败,检查手机蓝牙是否开启?',
  1331. icon: 'none',
  1332. duration: 2000,
  1333. mask: true
  1334. })
  1335. } else {
  1336. console.log("连接失败,请重试!");
  1337. uni.showModal({
  1338. title: '连接失败',
  1339. content: '设备未开启或被占用。请重启app和设备后重新连接。'
  1340. })
  1341. // uni.showToast({
  1342. // title: '设备未开启或被占用异常导致无法连接。',
  1343. // icon: 'none',
  1344. // duration: 2000,
  1345. // mask: true
  1346. // })
  1347. }
  1348. });
  1349. },
  1350. //获取蓝牙服务
  1351. onGetBLEDeviceServices(state, context) {
  1352. let {
  1353. item,
  1354. success = null,
  1355. } = context;
  1356. BLE.getBLEDeviceServices(item, success,(serviceList)=>{
  1357. state.BLEGetServices = serviceList;
  1358. });
  1359. },
  1360. onCloseBLEConnection(state, context) {
  1361. console.log("onCloseBLEConnection");
  1362. let {
  1363. getSuccess = null,
  1364. } = context;
  1365. if (state.BLERSSIInterval) {
  1366. clearInterval(state.BLERSSIInterval);
  1367. state.BLERSSIInterval = null;
  1368. }
  1369. BLE.closeBLEConnection(state.BLEConnectDevice.deviceId, () => {
  1370. state.cIndex = -1;
  1371. state.BLEConnectDevice = null;
  1372. //蓝牙服务
  1373. state.BLEGetServices = null;
  1374. if (getSuccess) {
  1375. getSuccess();
  1376. }
  1377. state.bConnection = false;
  1378. uni.closeBluetoothAdapter({
  1379. success(res) {
  1380. console.log(res)
  1381. state.bOpenBluetooth = false;
  1382. }
  1383. })
  1384. });
  1385. },
  1386. //不关闭 adapter
  1387. onOnlyCloseBLEConnection(state, context) {
  1388. let {
  1389. getSuccess = null
  1390. } = context;
  1391. if (state.BLERSSIInterval) {
  1392. clearInterval(state.BLERSSIInterval);
  1393. state.BLERSSIInterval = null;
  1394. }
  1395. BLE.closeBLEConnection(state.BLEConnectDevice.deviceId, () => {
  1396. state.cIndex = -1;
  1397. state.BLEConnectDevice = null;
  1398. //蓝牙服务
  1399. state.BLEGetServices = null;
  1400. if (getSuccess) {
  1401. getSuccess();
  1402. }
  1403. state.bConnection = false;
  1404. });
  1405. },
  1406. onWriteBLEConnectionValue(state, context) {
  1407. let {
  1408. getSuccess = null,
  1409. getFail = null,
  1410. value
  1411. } = context;
  1412. if (!state.BLEConnectDevice && state.cIndex == -1) {
  1413. uni.showToast({
  1414. title: '蓝牙设备未连接',
  1415. icon: 'none'
  1416. })
  1417. }
  1418. // console.log(22);
  1419. //记录蓝牙盒子当前的指令
  1420. state.currentInstruction = value;
  1421. // 发送 3 :开启原始数据
  1422. // 发送4 :关闭原始数据
  1423. // 发送 5 :开启步数
  1424. // 发送6 :关闭步数
  1425. if (value == "3") {
  1426. state.instructionState.bOpen = true;
  1427. } else if (value == "4") {
  1428. state.instructionState.bOpen = false;
  1429. } else if (value == "5") {
  1430. state.instructionState.bSteps = true;
  1431. } else if (value == "6") {
  1432. state.instructionState.bSteps = false;
  1433. }
  1434. let retryCount = 3;
  1435. BLE.writeBLECharacteristicValue(value,retryCount,getSuccess,getFail);
  1436. },
  1437. //获取距离
  1438. onGetRSSITransDistance(state, context) {
  1439. let {
  1440. RSSI
  1441. } = context;
  1442. BLE.getRSSITransDistance(RSSI);
  1443. },
  1444. //添加使用的前端信息
  1445. gOnAddClientInfo(state, context) {
  1446. uni.getSystemInfo({
  1447. success: (res) => {
  1448. // console.log("系统信息:", res);
  1449. let clientInfo = res;
  1450. clientInfo.clientSystem = res.system;
  1451. reqUtil.requestData(config.URL.ADDCLIENTINFO, clientInfo, 'POST').then(
  1452. res => {
  1453. console.warn(res);
  1454. },
  1455. e => {}
  1456. );
  1457. },
  1458. fail: (err) => {},
  1459. complete: () => {}
  1460. })
  1461. },
  1462. //创建快速打击对象
  1463. gCreateFilterObj(state) {
  1464. state.filter = null;
  1465. state.deviceMs = 1;
  1466. state.filter = new o0Project.Filter();
  1467. console.log("gCreateFilterObj");
  1468. },
  1469. //更新快速打击对象数据
  1470. gUpdateFilter(state, context) {
  1471. let {
  1472. data,
  1473. callback
  1474. } = context;
  1475. let {
  1476. min,
  1477. s,
  1478. ms
  1479. } = data;
  1480. var msGap = ms - state.deviceMs;
  1481. state.deviceMs = ms;
  1482. while (msGap < 0) {
  1483. msGap += 1000;
  1484. }
  1485. // console.log(data);
  1486. let {
  1487. ax,
  1488. ay,
  1489. az
  1490. } = data.acc;
  1491. state.filter.Update(new o0.Vector3(ax, ay, az), msGap, callback); //我自己的更新acc的函数
  1492. },
  1493. /**
  1494. * 限制开始游戏
  1495. */
  1496. glimitPlayGame(state, context) {
  1497. let {
  1498. gameTags,
  1499. callback
  1500. } = context;
  1501. //没有标签直接进去
  1502. if (gameTags.length == 0) {
  1503. callback();
  1504. return;
  1505. }
  1506. let canPlay = false;
  1507. gameTags.forEach((item) => {
  1508. //如果存在普通模式
  1509. if (item.tagMode === 'normal') {
  1510. canPlay = true;
  1511. }
  1512. if (canPlay)
  1513. return;
  1514. })
  1515. if (!canPlay) {
  1516. //2.如果普通模式不存在,就判断是否连接硬件了
  1517. if (!state.BLEConnectDevice || state.cIndex == -1) {
  1518. uni.showToast({
  1519. title: "请连接标签对应的设备模式!",
  1520. icon: "none",
  1521. duration: 2000,
  1522. mask: true
  1523. })
  1524. return;
  1525. }
  1526. //3.如果连接了设备,判断但是处于什么模式
  1527. gameTags.forEach((item, index, selfArr) => {
  1528. //如果存在普通模式
  1529. if (item.tagMode === state.BLEConnectDevice.usageMode) {
  1530. canPlay = true;
  1531. }
  1532. if (canPlay)
  1533. return;
  1534. if (index == selfArr.length - 1) {
  1535. uni.showToast({
  1536. title: "游戏没有当前设备模式!",
  1537. icon: "none",
  1538. duration: 2000,
  1539. mask: true
  1540. })
  1541. }
  1542. })
  1543. }
  1544. if (canPlay) {
  1545. callback();
  1546. }
  1547. },
  1548. checkAppUpdata(state) {
  1549. //#ifdef APP-PLUS
  1550. let _temp = {
  1551. appid: plus.runtime.appid,
  1552. version: state.version,
  1553. versionCode: state.versionCode,
  1554. platform: state.platform.toLocaleLowerCase(),
  1555. }
  1556. //#endif
  1557. //#ifdef H5
  1558. //此 _temp 测试用
  1559. let _temp = {
  1560. appid: '1',
  1561. version: "2.0.0",
  1562. versionCode: "20122202",
  1563. platform: "android",
  1564. }
  1565. //#endif
  1566. reqUtil.requestData(config.URL.APPVERSIONUPDATE, _temp).then(res => {
  1567. console.log('requestData APPVERSIONUPDATE =====', res);
  1568. let _data = res.data;
  1569. if (res.code == 0 && res.data.status === 1) {
  1570. uni.showModal({ //提醒用户更新
  1571. title: "更新提示",
  1572. content: res.data.note,
  1573. success: (res) => {
  1574. if (res.confirm) {
  1575. if (state.platform.toLocaleLowerCase() == "android") {
  1576. plus.runtime.openURL(_data.url, function(res) {
  1577. plus.nativeUI.alert("本机没安装应用宝,请安装后获取新版本。");
  1578. }, 'com.tencent.android.qqdownloader');
  1579. } else {
  1580. //ios
  1581. plus.runtime.openURL(_data.url);
  1582. }
  1583. }
  1584. }
  1585. })
  1586. } else {
  1587. uni.showToast({
  1588. title: '当前为最新版本',
  1589. icon: 'none',
  1590. duration: 2000,
  1591. mask: true
  1592. })
  1593. }
  1594. },
  1595. e => {
  1596. console.log(e)
  1597. });
  1598. },
  1599. }
  1600. })
  1601. export default store