store.js 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942
  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 o0ProjectRelease from "@/util/util-js/o0ProjectRelease0.1.js"
  12. import deviceData from "@/util/util-js/devices.js"
  13. // 加载算法插件 JumpIdentificationModule
  14. var jumpIdenModule = uni.requireNativePlugin("JumpIdentificationModule")
  15. Vue.use(Vuex)
  16. const store = new Vuex.Store({
  17. state: {
  18. // vLog:'',
  19. //引导层
  20. bGuidePages: false,
  21. //是否显示Canvas
  22. bCanvasShow: true,
  23. /**
  24. * 是否需要强制登录
  25. */
  26. forcedLogin: false,
  27. /**
  28. * 蓝牙实时发送数据的页面的名称
  29. */
  30. currentPageName: '',
  31. hasLogin: false,
  32. userInfo: null,
  33. userName: "匿名",
  34. days: 0,
  35. remainingDays: 0,
  36. signature: '',
  37. avatarUrl: "/static/defaultAvatar.png",
  38. /**
  39. * 新手教程相关
  40. */
  41. // 是否是新用户,
  42. //TODO:登录接口返回的数据来判断
  43. bNewUser: false,
  44. //是否是第一次走新手
  45. bNewGuide: false,
  46. //解锁状态,和首次安装app相关
  47. //注意添加新字段时候。需要到 platform-page/guide.vue 确认需不需要添加新的reset
  48. guideUnlockState: {
  49. firstInstallation: true, //是否首次安装
  50. firstDisconnectBluetooth: true, //是否首次连接蓝牙
  51. firstUnlockJumpUp: true, //是否首次向上跳
  52. firstUnlockLeftAndRightJump: true, //是否首次左右跳
  53. firstUnlockLeftAndRightRotationJump: true,
  54. firstPromptSelectLevel: true //是否首次提示选择关卡
  55. },
  56. token: '',
  57. gender: 0,
  58. //省市区数据
  59. city: {
  60. label: '北京市-北京市-东城区',
  61. cityCode: "110101",
  62. //picker 选择器的index数组
  63. value: [0, 0, 0]
  64. },
  65. birthday: date.formatTime(new Date()),
  66. //默认值
  67. height: 0,
  68. weight: 0,
  69. //最近在玩的列表
  70. playGames: [],
  71. //最近在玩的排行游戏列表
  72. playRankingGames: [],
  73. //全部的游戏列表
  74. allGames: [],
  75. //关注的游戏列表
  76. favoriteGames: [],
  77. // 排行榜里面显示的游戏
  78. rankSelectedGame: null,
  79. rankInfo: null,
  80. cityName: "某市",
  81. /**
  82. * 默认的运动数据
  83. * date.addDaysFromNewDate(new Date(),1)
  84. */
  85. defaultPlanData: {
  86. startTime: date.formatDate(new Date()),
  87. endTime: date.addDaysFromNewDate(new Date(), 6),
  88. targetWeight: 0, //目标公斤
  89. calorie: 360, //运动消耗的卡路里,默认300 大卡
  90. cumulativeCalorie: 0, //累计增重
  91. sportTime: 40, //运动时间
  92. /**
  93. * 本地记录的消耗卡路里数量
  94. */
  95. localCalorie: 0,
  96. /**
  97. * 显示卡路里,过了12点清空
  98. */
  99. showCalorie: 0,
  100. /**
  101. * 总和
  102. */
  103. allCalorie: 0
  104. },
  105. /**
  106. * 计划的运动数据
  107. */
  108. planData: {
  109. startTime: date.formatDate(new Date()),
  110. endTime: date.formatDate(new Date()), //date.addDaysFromNewDate(new Date(),1),
  111. targetWeight: 0, //目标公斤
  112. cumulativeCalorie: 0, //累计增重
  113. calorie: 300, //运动消耗的卡路里
  114. sportTime: 40, //运动时间
  115. /**
  116. * 本地记录的消耗卡路里数量
  117. */
  118. localCalorie: 0,
  119. /**
  120. * 显示卡路里,过了12点清空
  121. */
  122. showCalorie: 0,
  123. /**
  124. * 总和
  125. */
  126. allCalorie: 0
  127. },
  128. //记录表盘计划刷新的卡路里
  129. oldArcbarProCalorie: 0,
  130. //记录表盘当前的总卡路里
  131. oldArcbarAllCalorie: 0,
  132. //当前运动的世时间
  133. localSportTime: 0,
  134. //当前运动时间,运动时候记录的时间
  135. runingTime: 0,
  136. //蓝牙设备信息列表
  137. //id 不可变,用于保存到本地区分
  138. BLEInfoList: deviceData.getDeviceList(),
  139. //记录的蓝牙设备,保存id, 和对应的蓝牙连接数据
  140. BLEDeviceList: [],
  141. //显示的蓝牙设备,取本地数据时候更新
  142. BLEDeviceShowList: [],
  143. //选择连接的蓝牙设备
  144. BLEConnectDevice: null,
  145. //获取到的服务
  146. BLEGetServices: null,
  147. //当前发送给蓝牙的指令,目前是只有蓝牙手柄
  148. currentInstruction: '',
  149. instructionState: {
  150. // bAcc: false, //是否开启加速计状态
  151. // bGyroscope: false, //是否开启陀螺仪状态
  152. bOpen: false, //是否开启指令
  153. bSteps: false, //是否开启步数
  154. },
  155. //绑定的设备
  156. DeviceBindingList: [{
  157. id: 0,
  158. cname: "开启手机数据",
  159. ename: "bindMobilePhoneBandage",
  160. icon: "/static/devicesIcon/bandage.png",
  161. mIcon: "/static/devicesIcon/bandage.png",
  162. bRatio: false,
  163. usageMode: 'bindPhone',
  164. describe: '手机情景下使用',
  165. limitType: 'rebound', //限制回弹版本
  166. deviceType: 'mySelf', //指的是使用手机本身加速计计算
  167. deviceName: 'PHONE', //连接硬件名称
  168. }],
  169. ConnectBindingDevice: null,
  170. cIndex: -1,
  171. //当前是否连接
  172. bConnection: false,
  173. /**
  174. * 通过验证的连接,比如通过mac验证,或者说是首页直连,都设置这个参数,确保已经连上
  175. */
  176. bVerifiedConnection: false,
  177. /**
  178. * 默认不是旧设备,旧设备使用的轴向不一样,名字为 BGBox_202012 ,新设备为 BGBox_202112
  179. */
  180. bOldDeviceType: false,
  181. /**
  182. * Android 出现不匹配的情况下,判断
  183. * ios 暂时没有此类问题
  184. */
  185. bPhoneMatched: false,
  186. //最近连接过的设备
  187. finallyUseDevice: null,
  188. /**
  189. * 用户选择好友的信息
  190. */
  191. finallySelectFriendInfo: null,
  192. //phoneNumber
  193. phoneNumber: '',
  194. mailboxNumber: '',
  195. //是否是第一次设置密码,根据这个值判断.默认未设置,即设置true
  196. setPasswordFirstTime: true,
  197. openid: '',
  198. appleid: '',
  199. //获取验证码
  200. bCodeDisabled: false,
  201. count: 59,
  202. interval: null,
  203. //conversion列表
  204. oldSwiperList: [],
  205. // 计划任务过期
  206. bPlanExpired: false,
  207. // 制定玩计划任务后
  208. bPlanFinish: false,
  209. //标准体重,男女根据当前年龄计算
  210. standardWeight: 0,
  211. //是否安装了微信
  212. bInstallWechat: false,
  213. bHideWeixin: false,
  214. //导航栏高度。这里统一参数,单位px
  215. navHeight: 46,
  216. tabbarHeight: 65,
  217. systemInfo: null, //系统全部信息
  218. clientName: '', //但是客户端名字
  219. system: '', // 系统版本
  220. platform: '', // 平台
  221. appName: '',
  222. version: '1.6.3', //软件版本
  223. versionCode: '21120701', //app.vue 自动获取.目前是h5 设置给了一个默认值
  224. showEndTime: config.endTime, //游戏显示时间字段,后台返回对应此时间之前的数据
  225. //现在游戏显示在对应的平台
  226. gamePlatform: {
  227. 0: 'android',
  228. 1: 'ios',
  229. 2: 'all'
  230. },
  231. globalAcc: null,
  232. globalOri: null,
  233. globalAccData: {
  234. ax: 0,
  235. ay: 0,
  236. az: 0
  237. },
  238. globalGyroData: {
  239. gx: 0,
  240. gy: 0,
  241. gz: 0
  242. },
  243. globalLastGyroData: {
  244. gx: 0,
  245. gy: 0,
  246. gz: 0
  247. },
  248. globalMyAttitude: null,
  249. /**
  250. * 当前显示的模式下标,区分总体的显示模块
  251. * 比如拳击模块体验下,设置 0
  252. * 跳绳为主的模式体验下,设置 1
  253. */
  254. currentModeIndex: 0,
  255. //蓝牙变量操作
  256. /**
  257. * 操作蓝牙设备
  258. */
  259. bOpenBluetooth: false,
  260. /**
  261. * 是否初始化蓝牙适配器
  262. */
  263. bOpenSuccess: false,
  264. /**
  265. * 是否监听蓝牙状态变化
  266. */
  267. bListenAdapterStateChange: false,
  268. /**
  269. * 是否监听寻找新设备事件
  270. */
  271. bListenDeviceFound: false,
  272. // 设备Id
  273. BLEDeviceId: "",
  274. // 服务Id
  275. BLEServiceId: "",
  276. // 特征值id
  277. BLENotifyCharacteristicId: "",
  278. //监听rssi对象
  279. BLERSSIInterval: null,
  280. //蓝牙服务timeout
  281. getBLEDeviceServicesTimeout: null,
  282. //本地游戏地址
  283. // http://192.168.0.112:7456/build/index.html
  284. // http://127.0.0.1:7456/build/index.html
  285. LocationGameUrl: "http://110.43.54.43/t8/",
  286. //快速打击对象
  287. filter: null,
  288. deviceMs: 1,
  289. //改进版的沙袋打击对象
  290. sandbagAlgorithm: null,
  291. sandbagAlgorithmLastTime: new Date().getTime(),
  292. //记录一个沙包打击数量
  293. sandbagHitCount: 0,
  294. netSandbagHitCount: 0,
  295. /**
  296. * 蓝牙参数对象
  297. */
  298. // 设备Id
  299. deviceId: "",
  300. // 服务Id
  301. serviceId: "",
  302. notifyCharacteristicId: "",
  303. writeCharacteristicId: "",
  304. showToast: false,
  305. bListenerUpdate: false,
  306. bListenerJson: false,
  307. bListenerMac: false,
  308. bListenerAccArray: false,
  309. /**
  310. * OTA config
  311. */
  312. UUID_OTA_SERVICE: "f000ffc0-0451-4000-b000-000000000000", //OTA 服务对应的服务的 UUID
  313. UUID_IDENTFY: "f000ffc1-0451-4000-b000-000000000000", //OTA 版本特征值的UUID,读取版本就是写向这个特征值写入:0x00
  314. UUID_BLOCK: "f000ffc2-0451-4000-b000-000000000000", //OTA 写bin文件特征值UUID,发送bin文件就是写这个特征值
  315. /*****/
  316. /**
  317. * 发送16进制时候,返回的刷新数据
  318. */
  319. bListenerHexUpdate: false,
  320. BluetoothAdapter: null,
  321. //版本状态,用于区分部分特定的环境
  322. versionCodeState: {
  323. showGame: false,
  324. showVideo: false,
  325. versionCode: 0,
  326. title: 'none'
  327. },
  328. //显示蓝牙是否断开连接Modal
  329. bShowBLEConnectModal: false,
  330. bGamePlaying: false,
  331. //跳绳条件下计数
  332. bRopeKeyOne: 0,
  333. bRopeKeyTwo: 0,
  334. //钻石,金币
  335. cDiamond: 0,
  336. cGold: 0,
  337. /**
  338. * 任务相关
  339. */
  340. currentJumpTask: null,
  341. multiPersonList: [],
  342. singlePersonList: [],
  343. levels: [],
  344. /**
  345. * 校准识别部分
  346. */
  347. convertAcc: [{
  348. type: 'x',
  349. match: 'ax', //默认匹配一一对应
  350. direction: 1,
  351. }, {
  352. type: 'y',
  353. match: 'ay',
  354. direction: -1,
  355. }, {
  356. type: 'z',
  357. match: 'az',
  358. direction: -1,
  359. }],
  360. convertGyro: [{
  361. type: 'x',
  362. match: 'gx', //默认匹配一一对应
  363. direction: 1,
  364. }, {
  365. type: 'y',
  366. match: 'gy',
  367. direction: 1,
  368. }, {
  369. type: 'z',
  370. match: 'gz',
  371. direction: 1,
  372. }],
  373. /**
  374. * 签到列表
  375. */
  376. signInList: [],
  377. isSignIn: false,
  378. //读取文件的名称
  379. readDataName:"",
  380. filesList: [],
  381. },
  382. mutations: {
  383. /**
  384. * 蓝牙解析后的数据,转化成对应的坐标系方向
  385. * @param {Object} context
  386. */
  387. onConvertDeviceData(state, context) {
  388. let {
  389. data,
  390. callback = null
  391. } = context;
  392. let {
  393. acc,
  394. gyro,
  395. } = data;
  396. //根据已校准的数据转化对应的轴
  397. let outAcc = {
  398. ax: acc[state.convertAcc[0].match] * state.convertAcc[0].direction,
  399. ay: acc[state.convertAcc[1].match] * state.convertAcc[1].direction,
  400. az: acc[state.convertAcc[2].match] * state.convertAcc[2].direction
  401. };
  402. let outGyro = {
  403. gx: gyro[state.convertGyro[0].match] * state.convertGyro[0].direction,
  404. gy: gyro[state.convertGyro[1].match] * state.convertGyro[1].direction,
  405. gz: gyro[state.convertGyro[2].match] * state.convertGyro[2].direction
  406. }
  407. if (callback)
  408. callback({
  409. convertAcc: outAcc,
  410. convertGyro: outGyro
  411. });
  412. },
  413. onAccAndGyroConvertDataGet(state) {
  414. const convertAcc = uni.getStorageSync('convertAcc');
  415. if (convertAcc) {
  416. state.convertAcc = convertAcc;
  417. }
  418. const convertGyro = uni.getStorageSync('convertGyro');
  419. if (convertGyro) {
  420. state.convertGyro = convertGyro;
  421. }
  422. },
  423. onAccAndGyroConvertDataSave(state) {
  424. uni.setStorageSync('convertAcc', state.convertAcc);
  425. uni.setStorageSync('convertGyro', state.convertGyro);
  426. },
  427. onAccAndGyroConvertDataReset(state) {
  428. state.convertAcc = [{
  429. type: 'x',
  430. match: 'ax', //默认匹配一一对应
  431. direction: 1,
  432. }, {
  433. type: 'y',
  434. match: 'ay',
  435. direction: -1,
  436. }, {
  437. type: 'z',
  438. match: 'az',
  439. direction: -1,
  440. }];
  441. state.convertGyro = [{
  442. type: 'x',
  443. match: 'gx', //默认匹配一一对应
  444. direction: 1,
  445. }, {
  446. type: 'y',
  447. match: 'gy',
  448. direction: 1,
  449. }, {
  450. type: 'z',
  451. match: 'gz',
  452. direction: 1,
  453. }];
  454. //重置一下数据保存
  455. this.commit('onAccAndGyroConvertDataSave');
  456. },
  457. /**
  458. * 保存一份本地数据,加速计和陀螺仪
  459. * @param {Object} data
  460. * @param {Object} state
  461. */
  462. setOutAccAndGyroData(state, data) {
  463. uni.setStorageSync('OutAccAndGyroData', data);
  464. },
  465. getOutAccAndGyroData(state) {
  466. const value = uni.getStorageSync('OutAccAndGyroData');
  467. return value;
  468. },
  469. copyOutAccANdGyroData() {
  470. const value = uni.getStorageSync('OutAccAndGyroData');
  471. uni.setClipboardData({
  472. data: value,
  473. success: () => {
  474. uni.showToast({
  475. icon: 'none',
  476. title: '复制成功'
  477. });
  478. }
  479. });
  480. },
  481. setGuideUnlockState(state, _guideUnlockState) {
  482. uni.setStorageSync('guideUnlockState', _guideUnlockState);
  483. state.guideUnlockState = _guideUnlockState;
  484. },
  485. getGuideUnlockState(state, _guideUnlockState) {
  486. //如果存在本地任务
  487. const value = uni.getStorageSync('guideUnlockState');
  488. if (value) {
  489. state.guideUnlockState = value;
  490. }
  491. console.log('获取guideUnlockState', state.guideUnlockState)
  492. },
  493. /**
  494. * 设置当前操作的任务记录到本地
  495. * @param {Object} state
  496. * @param {Object} _taskObj
  497. */
  498. setActionJumpTask(state, _taskObj) {
  499. uni.setStorageSync('currentJumpTask', _taskObj);
  500. state.currentJumpTask = _taskObj;
  501. },
  502. //获取本地任务对象
  503. getActionJumpTask(state) {
  504. //如果存在本地任务
  505. const value = uni.getStorageSync('currentJumpTask');
  506. if (value) {
  507. state.currentJumpTask = value;
  508. }
  509. },
  510. resetActionJumpTask(state) {
  511. state.currentJumpTask = null;
  512. uni.setStorageSync('currentJumpTask', state.currentJumpTask);
  513. },
  514. setFinallSelectFriendInfo(state, friendInfo) {
  515. state.finallySelectFriendInfo = friendInfo;
  516. console.log('finallySelectFriendInfo', JSON.stringify(friendInfo));
  517. },
  518. clearFinallFriendInfo(state) {
  519. state.finallySelectFriendInfo = null;
  520. },
  521. /**
  522. * 获取记录的最后一次连接设备
  523. * @param {Object} state
  524. */
  525. getFinalUseDevice(state) {
  526. // 获取本地存储的最后一次使用的设备
  527. //1.获取最近连接的蓝牙设备, 指蓝牙搜索到的obj
  528. const value = uni.getStorageSync('finallyUseDevice');
  529. if (value) {
  530. state.finallyUseDevice = value;
  531. }
  532. console.log('获取的finallyUseDevice', value);
  533. },
  534. /**
  535. * 存储最后一次使用的设备信息,后面登陆时候,如果存在设备最后一次使用信息,则自动连接
  536. * @param {Object} state
  537. * @param {Object} device
  538. */
  539. setFinallUseDevice(state, device) {
  540. uni.setStorageSync('finallyUseDevice', device);
  541. state.finallyUseDevice = device;
  542. // console.log("保存finallyUseDevice:", state.finallyUseDevice);
  543. // uni.setStorage({
  544. // key: 'finallyUseDevice',
  545. // data: device,
  546. // success: (res) => {
  547. // state.finallyUseDevice = device;
  548. // console.log("保存finallyUseDevice:", res);
  549. // }
  550. // })
  551. },
  552. /**
  553. * 移除记录的use device
  554. */
  555. clearFinallUseDevice(state) {
  556. uni.removeStorage({
  557. key: 'finallyUseDevice',
  558. success: (res) => {
  559. state.finallyUseDevice = null;
  560. console.log("移除finallyUseDevice:", res);
  561. }
  562. })
  563. },
  564. /**
  565. * 设置运动时间
  566. * @param {Object} state
  567. * @param {Object} time
  568. */
  569. setLocalSportTime(state, time) {
  570. // let _sportTime = Number(time);
  571. state.localSportTime = Number(time);
  572. uni.setStorage({
  573. key: 'localSportTime',
  574. data: state.localSportTime,
  575. success: (res) => {
  576. // uni.showToast({
  577. // title: "保存成功"
  578. // })
  579. }
  580. })
  581. },
  582. /**
  583. * 设置卡路里
  584. * @param {Object} state
  585. * @param {Object} calorie
  586. */
  587. setLocalCalorie(state, calorie) {
  588. state.planData.localCalorie = Number(calorie);
  589. uni.setStorage({
  590. key: 'localCalorie',
  591. data: state.planData.localCalorie,
  592. success: (res) => {
  593. // uni.showToast({
  594. // title: "保存成功"
  595. // })
  596. }
  597. })
  598. },
  599. /**
  600. * 设置表盘显示的卡路里,
  601. * @param {Object} state
  602. * @param {Object} calorie
  603. */
  604. setShowCalorie(state, calorie) {
  605. state.planData.showCalorie = Number(calorie);
  606. uni.setStorage({
  607. key: 'showCalorie',
  608. data: state.planData.showCalorie,
  609. success: (res) => {
  610. // uni.showToast({
  611. // title: "保存成功"
  612. // })
  613. }
  614. })
  615. },
  616. /**
  617. * 增加卡路里
  618. * @param {Object} state
  619. * @param {Object} calorie
  620. */
  621. addlocalCalorie(state, calorie) {
  622. //卡路里不能为负数
  623. calorie = Math.abs(calorie);
  624. //当前需要添加的卡路里
  625. let temNum = Number(calorie);
  626. //本地的卡路里
  627. let temNum2 = Number(state.planData.localCalorie);
  628. // 本地加 + 传入的卡路里
  629. let _numberLocal = temNum.add(temNum2);
  630. //显示的卡路里
  631. let _showCalorie = Number(state.planData.showCalorie);
  632. //显示的卡路里加上 本地卡路里 和添加的卡路里之和要小于或等于 计划的卡路里
  633. let _allCalorie = _numberLocal.add(_showCalorie);
  634. // console.log("计算的最后卡路里_showCalorie:", _showCalorie,_allCalorie);
  635. //如果超过计划的,用计划的卡路里记录
  636. if (_allCalorie > state.planData.calorie) {
  637. let _calorie = Number(state.planData.calorie);
  638. //假如更新时候,localCalorie 值不为0,但是还未更新给服务器,此时,不能设置localCalorie 0
  639. //用传入的值,减去 allcalorie 总和和目标的差
  640. let _value = Number(_allCalorie).sub(_calorie);
  641. let _endValue = Number(_numberLocal).sub(_value);
  642. state.planData.localCalorie = Number(_endValue.toFixed(2));
  643. //如果大于或者等于的话,用计划 于 显示的卡路里 差值
  644. // state.planData.localCalorie = Number(_calorie.sub(_showCalorie).toFixed(2));
  645. // //显示最大值
  646. // state.planData.showCalorie = Number(_calorie.sub(state.planData.localCalorie).toFixed(2));
  647. } else {
  648. //
  649. state.planData.localCalorie = Number(_numberLocal.toFixed(2));
  650. // //输入的卡路里 + 自身显示的卡路里
  651. // state.planData.showCalorie = Number(temNum.add(_showCalorie).toFixed(2));
  652. }
  653. console.log("计算的最后卡路里:", state.planData);
  654. if (state.planData.localCalorie < 0) {
  655. //如果修改计划时候,本地数据计算的结果为负数,先取总和,localCalorie负再重置为0
  656. state.planData.showCalorie = _showCalorie.add(state.planData.localCalorie);
  657. state.planData.allCalorie = state.planData.showCalorie;
  658. state.planData.localCalorie = 0;
  659. } else {
  660. state.planData.allCalorie = _showCalorie.add(state.planData.localCalorie);
  661. }
  662. try {
  663. uni.setStorageSync('localCalorie', state.planData.localCalorie);
  664. } catch (e) {
  665. // error
  666. console.error(e);
  667. }
  668. try {
  669. uni.setStorageSync('showCalorie', state.planData.showCalorie);
  670. } catch (e) {
  671. // error
  672. console.error(e);
  673. }
  674. // uni.setStorage({
  675. // key: 'localCalorie',
  676. // data: state.planData.localCalorie,
  677. // success: (res) => {
  678. // // uni.showToast({
  679. // // title: "保存成功"
  680. // // })
  681. // }
  682. // })
  683. // uni.setStorage({
  684. // key: 'showCalorie',
  685. // data: state.planData.showCalorie,
  686. // success: (res) => {
  687. // // uni.showToast({
  688. // // title: "保存成功"
  689. // // })
  690. // }
  691. // })
  692. },
  693. /**
  694. * 每次登陆同步本地数据到服务器
  695. * 当天的数据更新,只更新一次
  696. * @param {Object} state
  697. */
  698. syncLocalDataToServer(state, callback) {
  699. let self = this;
  700. uni.getStorage({
  701. key: 'syncDate',
  702. success: function(res) {
  703. // console.log(res.data);
  704. let oldTime = res.data;
  705. let nDate = new Date();
  706. let newTime = date.formatDate(nDate);
  707. let days = date.datedifference(newTime, oldTime);
  708. // console.log(days);
  709. // 如果相差大于1天,
  710. if (days != 0) {
  711. // 更新存储日期
  712. uni.setStorageSync("syncDate", newTime);
  713. // 执行更新函数
  714. self.commit("syncRequestEvent", {
  715. success: () => {
  716. //清空显示的本地数据
  717. self.commit("setShowCalorie", 0);
  718. state.planData.allCalorie = state.planData.showCalorie.
  719. add(state.planData.localCalorie);
  720. if (callback)
  721. callback({
  722. newDate: nDate
  723. });
  724. }
  725. });
  726. //清空记录的本地运动时间
  727. // console.log('情况运动时间');
  728. self.commit("setLocalSportTime", 0);
  729. //设置本地数据是0
  730. state.localSportTime = 0;
  731. } else {
  732. if (callback)
  733. callback({
  734. newDate: nDate
  735. });
  736. }
  737. // date.getDays.
  738. },
  739. fail: function(res) {
  740. // console.error(res);
  741. // 如果没有日期,则存储一个新的
  742. let nDate = new Date();
  743. let tempTime = date.formatDate(nDate);
  744. uni.setStorageSync("syncDate", tempTime);
  745. if (callback)
  746. callback({
  747. newDate: nDate
  748. });
  749. }
  750. });
  751. },
  752. // 同步数据的请求
  753. syncRequestEvent(state, data) {
  754. let self = this;
  755. let {
  756. success,
  757. fail
  758. } = data;
  759. //需要卡路里取整存服务器,小数点后面,继续保留。以消除累计误差
  760. let oldLocalCalorie = state.planData.localCalorie;
  761. let _localCalorieInt = parseInt(state.planData.localCalorie);
  762. //小数点后面
  763. let _afterLocalCalorie = Number(oldLocalCalorie).sub(_localCalorieInt);
  764. console.log("*******===:", oldLocalCalorie, _localCalorieInt, _afterLocalCalorie);
  765. if (_localCalorieInt === 0) {
  766. // uni.showToast({
  767. // title: '',
  768. // icon: 'loading',
  769. // mask: true,
  770. // duration: 1000
  771. // })
  772. if (success)
  773. success();
  774. return;
  775. }
  776. uni.showToast({
  777. title: '',
  778. icon: 'loading',
  779. mask: false,
  780. duration: 10000
  781. })
  782. //把卡路里累计到cumulativeCalorie 变量,之后传服务器 += Math.round(state.planData.localCalorie)
  783. state.planData.cumulativeCalorie += _localCalorieInt;
  784. //添加到showCalorie 中
  785. state.planData.showCalorie = Number(state.planData.showCalorie).add(_localCalorieInt);
  786. state.planData.allCalorie = state.planData.showCalorie.add(_afterLocalCalorie);
  787. //不管是否同步成功,清空本地记录的卡路里,前一步已添加到cumulativeCalorie
  788. self.commit("setLocalCalorie", _afterLocalCalorie);
  789. self.commit("setShowCalorie", Number(state.planData.showCalorie));
  790. // 把卡路里记录到服务器
  791. reqUtil.requestData(config.URL.FITNESSPROGRAM, state.planData, 'POST').then(
  792. res => {
  793. // console.warn(res);
  794. if (res.code == 0) {
  795. uni.showToast({
  796. title: '同步成功',
  797. mask: true,
  798. duration: 1000
  799. });
  800. //同步一下服务器的返回值
  801. state.planData = Object.assign(state.planData, res.data);
  802. //暂时默认同步拳击1 的卡路里
  803. reqUtil.requestData(config.URL.UPLOADRANKING, {
  804. gameId: '1', //拳击模式 game id = 1;
  805. score: _localCalorieInt,
  806. cityCode: state.city.cityCode,
  807. }, "POST").then(res => {
  808. console.warn('****上传首页拳击卡路里,后面多个模式,可能需要多个不同的游戏id ***', res);
  809. },
  810. e => {
  811. console.log(e)
  812. });
  813. setTimeout(() => {
  814. uni.hideToast();
  815. }, 1000);
  816. if (success)
  817. success();
  818. } else {
  819. state.planData.cumulativeCalorie = Number(state.planData.cumulativeCalorie).sub(
  820. _localCalorieInt);
  821. state.planData.showCalorie = Number(state.planData.showCalorie).sub(
  822. _localCalorieInt);
  823. self.commit("setLocalCalorie", oldLocalCalorie);
  824. if (fail)
  825. fail();
  826. }
  827. console.log("减法 ==", Number(state.planData.cumulativeCalorie).sub(_localCalorieInt));
  828. },
  829. e => {
  830. console.log(e);
  831. state.planData.cumulativeCalorie = Number(state.planData.cumulativeCalorie).sub(
  832. _localCalorieInt);
  833. state.planData.showCalorie = Number(state.planData.showCalorie).sub(_localCalorieInt);
  834. self.commit("setLocalCalorie", oldLocalCalorie);
  835. uni.hideToast();
  836. if (fail)
  837. fail();
  838. }
  839. );
  840. },
  841. //登录成功后,重置一下状态
  842. resetCountDown(state) {
  843. if (state.interval) {
  844. clearInterval(state.interval);
  845. state.interval = null;
  846. state.count = 59;
  847. state.bCodeDisabled = false;
  848. }
  849. },
  850. /**
  851. * 验证码操作 区分手机号和邮箱
  852. * @param {Object} state
  853. * @param {Object} context
  854. */
  855. countDown(state, context) {
  856. let {
  857. account,
  858. type
  859. } = context;
  860. //限制验证码操作
  861. state.bCodeDisabled = true;
  862. state.interval = setInterval(() => {
  863. --state.count;
  864. if (state.count <= 0) {
  865. clearInterval(state.interval);
  866. state.interval = null;
  867. state.count = 59;
  868. state.bCodeDisabled = false;
  869. }
  870. }, 1000);
  871. setTimeout(() => {
  872. clearInterval(state.interval)
  873. state.count = 59;
  874. state.bCodeDisabled = false;
  875. }, 60000);
  876. //GETCODE "phoneNumber": phoneNumber; GETCODEACCORDINGTYPE
  877. reqUtil.requestData(config.URL.GETCODEACCORDINGTYPE, {
  878. account,
  879. type
  880. }).then(res => {
  881. console.log('requestData GETCODE =====', res);
  882. uni.showToast({
  883. title: "发送成功",
  884. mask: true,
  885. duration: 1000
  886. })
  887. },
  888. e => {
  889. console.log(e)
  890. });
  891. },
  892. //获取记录的蓝牙列表数据
  893. getBLEDeviceList(state) {
  894. // console.log("getBLEDeviceList=");
  895. uni.getStorage({
  896. key: 'BLEDeviceList',
  897. success: (res) => {
  898. // console.log("*****getBLEDeviceList=", res);
  899. let _list = res.data;
  900. state.BLEDeviceList = res.data;
  901. //更新BLEDeviceShowList 数组
  902. state.BLEDeviceShowList = [];
  903. _list.forEach(item => {
  904. //如果记录到本地的id,和信息列表中的id一致,则结合
  905. for (let i = 0; i < state.BLEInfoList.length; i++) {
  906. let eq = state.BLEInfoList[i];
  907. if (item.id == eq.id) {
  908. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  909. break;
  910. }
  911. }
  912. })
  913. }
  914. })
  915. },
  916. //添加蓝牙设备数据
  917. addBLEDevice(state, data) {
  918. let devicelist = [];
  919. if (state.BLEDeviceList && state.BLEDeviceList.length != 0) {
  920. devicelist = state.BLEDeviceList;
  921. }
  922. // console.log(data, devicelist)
  923. let bHas = false;
  924. devicelist.forEach((item, index, selfArr) => {
  925. if (item.id == data.id) {
  926. //如果存在,更新保存的数据,比如 mac或者uuid等
  927. selfArr[index] = item = Object.assign({}, item, data);
  928. bHas = true;
  929. }
  930. })
  931. //把当前添加的的蓝牙设信息备作为最后一次连接覆盖到本地
  932. this.commit('setFinallUseDevice', data);
  933. if (!bHas) {
  934. //如果不是更新数据,就添加但却数据到列表
  935. devicelist.push(data);
  936. }
  937. uni.setStorage({
  938. key: 'BLEDeviceList',
  939. data: devicelist,
  940. success: (res) => {
  941. // uni.showToast({
  942. // title: "保存成功"
  943. // })
  944. state.BLEDeviceList = devicelist;
  945. // console.log("devicelist:",devicelist);
  946. //更新BLEDeviceShowList 数组
  947. state.BLEDeviceShowList = [];
  948. devicelist.forEach(item => {
  949. //如果记录到本地的id,和信息列表中的id一致,则结合
  950. for (let i = 0; i < state.BLEInfoList.length; i++) {
  951. let eq = state.BLEInfoList[i];
  952. if (item.id == eq.id) {
  953. // console.log("存储的======================:", item, eq);
  954. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  955. break;
  956. }
  957. }
  958. })
  959. // console.log("state.BLEDeviceShowList:",state.BLEDeviceShowList);
  960. }
  961. })
  962. },
  963. //删除蓝牙设备数据
  964. deleteBLEDevice(state, index) {
  965. let devicelist = [];
  966. devicelist = state.BLEDeviceList;
  967. devicelist.splice(index, 1);
  968. uni.setStorage({
  969. key: 'BLEDeviceList',
  970. data: devicelist,
  971. success: (res) => {
  972. uni.showToast({
  973. title: "删除成功"
  974. })
  975. state.BLEDeviceList = devicelist;
  976. //更新BLEDeviceShowList 数组
  977. state.BLEDeviceShowList = [];
  978. devicelist.forEach(item => {
  979. //如果记录到本地的id,和信息列表中的id一致,则结合
  980. for (let i = 0; i < state.BLEInfoList.length; i++) {
  981. let eq = state.BLEInfoList[i];
  982. if (item.id == eq.id) {
  983. state.BLEDeviceShowList.push(Object.assign({}, item, eq));
  984. break;
  985. }
  986. }
  987. })
  988. }
  989. })
  990. },
  991. //获取计划表数据
  992. getPlanData(state, callback) {
  993. reqUtil.requestData(config.URL.FITNESSPROGRAMGET).then(res => {
  994. // console.log('获取计划表数据:getPlanData=====', res.data);
  995. if (res.code == 0) {
  996. state.planData = Object.assign(state.planData, res.data);
  997. // 处理本地数据
  998. let templocalCalorie = uni.getStorageSync("localCalorie");
  999. // console.log("templocalCalorie:", templocalCalorie, state.planData);
  1000. if (templocalCalorie) {
  1001. templocalCalorie = Number(templocalCalorie);
  1002. if (templocalCalorie > state.planData.calorie) {
  1003. state.planData.localCalorie = state.planData.calorie;
  1004. uni.setStorage({
  1005. key: "localCalorie",
  1006. data: state.planData.localCalorie
  1007. });
  1008. } else {
  1009. state.planData.localCalorie = templocalCalorie;
  1010. }
  1011. } else {
  1012. state.planData.localCalorie = 0;
  1013. }
  1014. //处理本地的cumulativeCalorie
  1015. let tempShowCalorie = uni.getStorageSync("showCalorie");
  1016. // console.log("tempShowCalorie:", tempShowCalorie, state.planData);
  1017. if (tempShowCalorie) {
  1018. tempShowCalorie = Number(tempShowCalorie);
  1019. if (tempShowCalorie > state.planData.calorie) {
  1020. state.planData.showCalorie = state.planData.calorie;
  1021. uni.setStorage({
  1022. key: "showCalorie",
  1023. data: state.planData.showCalorie
  1024. });
  1025. } else {
  1026. state.planData.showCalorie = tempShowCalorie;
  1027. }
  1028. } else {
  1029. state.planData.showCalorie = 0;
  1030. }
  1031. state.planData.allCalorie = state.planData.showCalorie.add(state.planData
  1032. .localCalorie);
  1033. // console.log("======",state.planData )
  1034. state.days = date.datedifference(res.data.startTime, res.data.endTime) + 1;
  1035. let nDate = new Date();
  1036. let tempTime = date.formatDate(nDate);
  1037. state.remainingDays = date.datedifference(tempTime, res.data.endTime) + 1;
  1038. console.log("state.remainingDays======", tempTime, res.data.endTime, state
  1039. .remainingDays);
  1040. }
  1041. // showArcbar
  1042. if (callback) {
  1043. callback();
  1044. }
  1045. },
  1046. e => {
  1047. console.log(e)
  1048. });
  1049. },
  1050. /**
  1051. * 获取全部游戏
  1052. * @param {Object} state
  1053. */
  1054. getAllGame(state, callback) {
  1055. //获得全部游戏列表
  1056. if (state.allGames.length != 0) {
  1057. if (callback)
  1058. callback();
  1059. return;
  1060. }
  1061. reqUtil.requestData(
  1062. config.URL.GAMELIST_BY_RANKING_SHOW_AND_PLATFORM, {
  1063. rankingShow: 1,
  1064. page: 1,
  1065. size: 100
  1066. }
  1067. ).then(res => {
  1068. // console.log('获取默认的全部游戏 =====', res);
  1069. if (res.code == 0) {
  1070. state.allGames = [];
  1071. state.allGames = state.allGames.concat(res.data);
  1072. }
  1073. if (callback)
  1074. callback();
  1075. },
  1076. e => {
  1077. console.log(e);
  1078. if (callback)
  1079. callback();
  1080. }
  1081. );
  1082. },
  1083. getRankGame(state, data) {
  1084. let {
  1085. param,
  1086. callback
  1087. } = data;
  1088. reqUtil.requestData(config.URL.GETRANKRANGR, param).then(res => {
  1089. console.log('获取默认的游戏数据:GETRANKRANGR=====', res);
  1090. if (res.data != null) {
  1091. state.rankSelectedGame = res.data.gameInfo;
  1092. state.rankInfo = res.data.rankInfo;
  1093. //然后获取排名
  1094. }
  1095. if (callback)
  1096. callback(res);
  1097. },
  1098. e => {
  1099. console.log(e)
  1100. });
  1101. },
  1102. /**
  1103. * 苹果端登陆
  1104. * @param {Object} state
  1105. * @param {Object} data
  1106. */
  1107. appleUserInfoLogin(state, data) {
  1108. let self = this;
  1109. let {
  1110. params,
  1111. callback
  1112. } = data;
  1113. console.log('获取用户数据:USERINFOAPPLEADD params=====', params);
  1114. reqUtil.requestData(config.URL.USERINFOAPPLEADD, params, 'POST').then(res => {
  1115. console.log('获取用户数据:USERINFOAPPLEADD =====', res);
  1116. if (res.code == 0) {
  1117. let userInfo = res.data;
  1118. self.commit('login', userInfo);
  1119. console.log("11~~");
  1120. } else if (res.code = 201) {
  1121. state.hasLogin = true;
  1122. }
  1123. console.log("apple callback!!!");
  1124. callback();
  1125. },
  1126. e => {
  1127. console.log(e)
  1128. });
  1129. },
  1130. //添加用户信息和登录
  1131. addUserAvatarAndLogin(state, data) {
  1132. let self = this;
  1133. let {
  1134. params,
  1135. filePath,
  1136. callback
  1137. } = data;
  1138. console.log("addUserAvatarAndLogin 初始化对应数据");
  1139. reqUtil.reqUpload(config.URL.USERINFOADDAVATARINFO, params, filePath).then(res => {
  1140. console.log('获取用户数据:USERINFOADDAVATARINFO =====', res);
  1141. if (res.code == 0) {
  1142. let userInfo = res.data;
  1143. self.commit('login', userInfo);
  1144. } else if (res.code = 201) {
  1145. state.hasLogin = true;
  1146. }
  1147. if (callback) {
  1148. callback();
  1149. }
  1150. },
  1151. e => {
  1152. console.log(e)
  1153. });
  1154. },
  1155. //app登录或h5
  1156. accountLogin(state, callback) {
  1157. let self = this;
  1158. // console.log("app登录或h5 初始化对应数据")
  1159. reqUtil.requestData(config.URL.USERINFOURL).then(res => {
  1160. console.warn('获取用户数据:USERINFOURL =====', res);
  1161. if (res.code == 0) {
  1162. let userInfo = res.data.userInfo;
  1163. state.phoneNumber = res.data.phoneNumber || '';
  1164. state.mailboxNumber = res.data.mailboxNumber || '';
  1165. state.setPasswordFirstTime = res.data.setPasswordFirstTime;
  1166. state.openid = res.data.openid || '';
  1167. self.commit('login', userInfo);
  1168. } else if (res.code = 201) {
  1169. state.hasLogin = true;
  1170. //新用户重置一下新手教程数据
  1171. self.commit('setGuideUnlockState', {
  1172. //是否首次安装
  1173. firstInstallation: true, //是否首次安装
  1174. firstDisconnectBluetooth: true, //是否首次连接蓝牙
  1175. firstUnlockJumpUp: true, //是否首次向上跳
  1176. firstUnlockLeftAndRightJump: true, //是否首次左右跳
  1177. firstUnlockLeftAndRightRotationJump: true,
  1178. firstPromptSelectLevel: true //是否首次提示选择关卡
  1179. });
  1180. }
  1181. if (callback) {
  1182. callback();
  1183. }
  1184. },
  1185. e => {
  1186. console.log(e)
  1187. });
  1188. },
  1189. //小程序页面登录
  1190. login(state, userInfo) {
  1191. console.log("登陆的信息==", userInfo);
  1192. if (userInfo.cityCode == "" || userInfo.cityCode == null) {
  1193. //不存在就设置默认值
  1194. userInfo.cityCode = state.city.cityCode;
  1195. }
  1196. let cityCode = userInfo.cityCode;
  1197. // console.log("cicicicitycode:", cityCode);
  1198. let provinceData = require('@/components/slambb-picker/city-data/province.js').default;
  1199. let cityData = require('@/components/slambb-picker/city-data/city.js').default;
  1200. let areaData = require('@/components/slambb-picker/city-data/area.js').default;
  1201. // 解析citycode
  1202. for (let i = 0; i < provinceData.length; i++) {
  1203. if (provinceData[i].value == cityCode.substr(0, 2)) {
  1204. let currentCityData = cityData[i];
  1205. for (let j = 0; j < currentCityData.length; j++) {
  1206. if (currentCityData[j].value == cityCode.substr(0, 4)) {
  1207. let areaDataTemp = areaData[i][j];
  1208. for (let k = 0; k < areaDataTemp.length; k++) {
  1209. if (areaDataTemp[k].value == cityCode) {
  1210. state.city.value = [i, j, k];
  1211. state.city.label = provinceData[i].label +
  1212. '-' +
  1213. currentCityData[j].label +
  1214. '-' +
  1215. areaDataTemp[k].label;
  1216. state.cityName = currentCityData[j].label;
  1217. break;
  1218. }
  1219. }
  1220. break;
  1221. }
  1222. }
  1223. break;
  1224. }
  1225. }
  1226. state.userInfo = userInfo;
  1227. state.userName = userInfo.username || '新用户';
  1228. state.avatarUrl = userInfo.avatarUrl || '/static/defaultAvatar.png';
  1229. state.gender = userInfo.gender;
  1230. state.signature = userInfo.signature || '';
  1231. state.birthday = userInfo.birthday;
  1232. state.city.cityCode = cityCode.toString();
  1233. state.weight = userInfo.weight || state.weight;
  1234. state.height = userInfo.height || state.height;
  1235. state.hasLogin = true;
  1236. //登录时候 获取一下排行榜里面的游戏
  1237. // this.commit('getRankGame', {
  1238. // param: {
  1239. // type: "china",
  1240. // cityCode: 12345
  1241. // }
  1242. // });
  1243. //登录之后,进入此页面先设置平台参数
  1244. if (state.platform == "ios") {
  1245. uni.setStorageSync("platform", 1);
  1246. } else if (state.platform == "android") {
  1247. uni.setStorageSync("platform", 0);
  1248. }
  1249. //获取版本信息
  1250. this.commit('getVersionCode');
  1251. },
  1252. // 退出登录
  1253. // 删除token 操作
  1254. delectToken(state, data) {
  1255. let self = this;
  1256. let {
  1257. success
  1258. } = data;
  1259. reqUtil.requestData(config.URL.USERLOGINOUT, {}).then(res => {
  1260. console.log('USERLOGINOUT=====', res);
  1261. self.commit("logout");
  1262. try {
  1263. //清空保存的storage
  1264. uni.clearStorageSync();
  1265. } catch (e) {
  1266. // error
  1267. console.error(e);
  1268. }
  1269. if (state.BLERSSIInterval) {
  1270. clearInterval(state.BLERSSIInterval);
  1271. state.BLERSSIInterval = null;
  1272. }
  1273. if (state.BLEConnectDevice) {
  1274. self.commit("onCloseBLEConnection", {
  1275. getSuccess: () => {}
  1276. });
  1277. }
  1278. if (success) {
  1279. success(res);
  1280. }
  1281. },
  1282. e => {
  1283. console.log(e)
  1284. });
  1285. },
  1286. logout(state) {
  1287. state.userInfo = null;
  1288. state.userName = "匿名";
  1289. state.avatarUrl = '/static/defaultAvatar.png';
  1290. state.days = '0';
  1291. state.signature = '';
  1292. state.gender = 0;
  1293. state.hasLogin = false;
  1294. state.phoneNumber = '';
  1295. state.mailboxNumber = '';
  1296. state.openid = '';
  1297. state.appleid = '';
  1298. //处理设备值
  1299. state.cIndex = -1;
  1300. //绑定的设备
  1301. state.ConnectBindingDevice = null;
  1302. //蓝牙设备
  1303. state.BLEConnectDevice = null;
  1304. //蓝牙服务
  1305. state.BLEGetServices = null;
  1306. //最近连接过的设备
  1307. state.finallyUseDevice = null;
  1308. //最近选择的好友
  1309. state.finallySelectFriendInfo = null;
  1310. //记录的蓝牙设备,保存id, 和对应的蓝牙连接数据
  1311. state.BLEDeviceList = [];
  1312. //显示的蓝牙设备,取本地数据时候更新
  1313. state.BLEDeviceShowList = [];
  1314. //记录表盘计划刷新的卡路里
  1315. state.oldArcbarProCalorie = 0;
  1316. //记录表盘当前的总卡路里
  1317. state.oldArcbarAllCalorie = 0;
  1318. //任务相关
  1319. state.currentJumpTask = null;
  1320. },
  1321. loginUserInfo(state, data) {
  1322. let {
  1323. infoRes,
  1324. success,
  1325. fail
  1326. } = data;
  1327. console.log('loginUserInfo');
  1328. reqUtil.requestData(config.URL.WXGETUSERINFO, {
  1329. appid: 'wxd6dfd60729d33d17',
  1330. encryptedData: infoRes.encryptedData,
  1331. iv: infoRes.iv,
  1332. signature: infoRes.signature,
  1333. rawData: infoRes.rawData
  1334. }).then(res => {
  1335. console.log('wxInfoRes=====', res);
  1336. let userInfo = res.data;
  1337. this.commit('login', userInfo);
  1338. if (success) {
  1339. success(res);
  1340. }
  1341. },
  1342. e => {
  1343. console.log(e)
  1344. if (fail) {
  1345. success(fail);
  1346. }
  1347. });
  1348. },
  1349. //这里统一静默处理login
  1350. oauth(state, context) {
  1351. let {
  1352. value,
  1353. callback
  1354. } = context;
  1355. uni.login({
  1356. provider: value,
  1357. success: (res) => {
  1358. // console.log('code:', res);
  1359. reqUtil.requestData(config.URL.USERlOGINURL, {
  1360. appid: 'wxd6dfd60729d33d17',
  1361. code: res.code,
  1362. platfrom: value
  1363. }).then(res => {
  1364. console.log('login token=====', res);
  1365. state.token = res.data.token;
  1366. uni.setStorageSync('token', state.token);
  1367. uni.getUserInfo({
  1368. provider: value,
  1369. success: (infoRes) => {
  1370. if (callback) {
  1371. callback(infoRes);
  1372. }
  1373. },
  1374. fail: (failRes) => {
  1375. //假如没有授权,login 之后,不做处理,需要用户点击授权时候,再进行处理用户登录
  1376. console.log('getUserInfo failRes:', failRes)
  1377. }
  1378. });
  1379. },
  1380. e => {
  1381. console.log(e)
  1382. });
  1383. },
  1384. fail: (err) => {
  1385. console.error('授权登录失败:' + JSON.stringify(err));
  1386. }
  1387. });
  1388. },
  1389. onLuanchLogin(state, data) {
  1390. let {
  1391. success
  1392. } = data;
  1393. const filters = ['weixin', 'qq', 'sinaweibo'];
  1394. let platform = '';
  1395. uni.getProvider({
  1396. service: 'oauth',
  1397. success: (res) => {
  1398. if (res.provider && res.provider.length) {
  1399. for (let i = 0; i < res.provider.length; i++) {
  1400. if (~filters.indexOf(res.provider[i])) {
  1401. platform = res.provider[i];
  1402. console.log("platform=", platform);
  1403. uni.showToast({
  1404. icon: 'loading',
  1405. title: ''
  1406. })
  1407. this.commit('oauth', {
  1408. value: platform,
  1409. callback: (infoRes) => {
  1410. // console.log("infoRes:", infoRes);
  1411. this.commit('loginUserInfo', {
  1412. infoRes: infoRes,
  1413. success: (res) => {
  1414. console.log("获取用户成功");
  1415. if (success) {
  1416. success(res);
  1417. }
  1418. uni.hideToast();
  1419. },
  1420. fail: (res) => {
  1421. console.error("获取用户失败");
  1422. uni.hideToast();
  1423. }
  1424. });
  1425. }
  1426. })
  1427. }
  1428. }
  1429. this.hasProvider = true;
  1430. }
  1431. },
  1432. fail: (err) => {
  1433. console.error('获取服务供应商失败:' + JSON.stringify(err));
  1434. }
  1435. });
  1436. },
  1437. initAdapter(state, callback) {
  1438. //初始化蓝牙模块
  1439. let params = {
  1440. success: (res) => {
  1441. state.bOpenSuccess = true;
  1442. state.bOpenBluetooth = true;
  1443. if (callback)
  1444. callback(res);
  1445. //监听断开事件
  1446. this.commit("B_OnBLEConnectionStateChange");
  1447. },
  1448. fail: (fail) => {
  1449. state.bOpenSuccess = false;
  1450. state.bOpenBluetooth = false;
  1451. uni.showToast({
  1452. title: '蓝牙尚未开启!',
  1453. icon: 'none'
  1454. })
  1455. },
  1456. complete: (complete) => {
  1457. // console.log("complete bListenAdapterStateChange:", state.bListenAdapterStateChange);
  1458. if (state.bListenAdapterStateChange) return;
  1459. state.bListenAdapterStateChange = true;
  1460. uni.onBluetoothAdapterStateChange((res) => {
  1461. // console.log('adapterState changed, now is', res)
  1462. // 手机蓝牙状态
  1463. state.bOpenBluetooth = res.available;
  1464. if (state.bOpenBluetooth && !state.bOpenSuccess) {
  1465. this.commit('initAdapter');
  1466. }
  1467. })
  1468. }
  1469. };
  1470. this.commit("B_OpenBluetoothAdapter", params);
  1471. },
  1472. // todo 未使用到
  1473. onGetDevices(state, context) {
  1474. let {
  1475. success
  1476. } = context;
  1477. if (!state.BLEConnectDevice) return;
  1478. // #ifdef APP-PLUS
  1479. let deviceId = state.BLEConnectDevice.deviceId;
  1480. uni.getBLEDeviceRSSI({
  1481. deviceId,
  1482. success: res => {
  1483. let _dis = Math.pow(Math.E, (Math.abs(res.RSSI) - 66.78) / 16.56);
  1484. if (_dis > state.BLEConnectDevice.limitDis) {
  1485. if (state.cIndex != -1) {
  1486. uni.showToast({
  1487. title: '设备断开连接!',
  1488. icon: 'none',
  1489. duration: 2000,
  1490. mask: true
  1491. })
  1492. }
  1493. setTimeout(() => {
  1494. this.commit("B_CloseBLEConnection", {
  1495. deviceId: deviceId
  1496. })
  1497. state.cIndex = -1;
  1498. state.bConnection = false;
  1499. state.bVerifiedConnection = false;
  1500. }, 1000)
  1501. } else {
  1502. // console.log("DIS~~~~~~~~~", _dis);
  1503. if (success) {
  1504. success({
  1505. RSSI: res.RSSI,
  1506. DIS: _dis
  1507. });
  1508. }
  1509. }
  1510. }
  1511. })
  1512. // #endif
  1513. },
  1514. //清除连接的开启的timeout
  1515. onUnloadCreateBLEConnectionTimeout(state) {
  1516. if (state.getBLEDeviceServicesTimeout) {
  1517. clearTimeout(state.getBLEDeviceServicesTimeout);
  1518. state.getBLEDeviceServicesTimeout = null;
  1519. }
  1520. },
  1521. initBLEData(state) {
  1522. // 设备Id
  1523. state.deviceId = "";
  1524. // 服务Id
  1525. state.serviceId = "";
  1526. state.notifyCharacteristicId = "";
  1527. state.writeCharacteristicId = "";
  1528. state.currentInstruction = "";
  1529. state.showToast = false;
  1530. state.bListenerUpdate = false;
  1531. state.bListenerJson = false;
  1532. state.bListenerMac = false;
  1533. state.bListenerAccArray = false;
  1534. //更新二进制的
  1535. state.bListenerHexUpdate = false;
  1536. },
  1537. //连接蓝牙
  1538. onCreateBLEConnection(state, context) {
  1539. let {
  1540. index,
  1541. item,
  1542. initItem = false,
  1543. getSuccess = null,
  1544. getinitAdapter = null
  1545. } = context;
  1546. uni.showToast({
  1547. title: '连接设备中...',
  1548. icon: 'loading',
  1549. duration: 10000,
  1550. mask: true
  1551. })
  1552. let params = {
  1553. item: item,
  1554. success: (success) => {
  1555. state.bConnection = true;
  1556. if (state.getBLEDeviceServicesTimeout) {
  1557. clearTimeout(state.getBLEDeviceServicesTimeout);
  1558. state.getBLEDeviceServicesTimeout = null;
  1559. }
  1560. state.getBLEDeviceServicesTimeout = setTimeout(() => {
  1561. let retryCount = 5;
  1562. this.commit("B_GetBLEDeviceServices", {
  1563. retryCount: retryCount,
  1564. item: item,
  1565. callback: () => {
  1566. uni.showToast({
  1567. title: '连接成功',
  1568. icon: 'loading',
  1569. duration: 2000,
  1570. mask: true
  1571. })
  1572. //是否需要设置item,默认不设置
  1573. if (initItem) {
  1574. state.cIndex = index;
  1575. state.BLEConnectDevice = item;
  1576. }
  1577. if (getSuccess)
  1578. getSuccess();
  1579. /**
  1580. * 直接连接过成功
  1581. */
  1582. state.bVerifiedConnection = true;
  1583. },
  1584. getServiceList: (serviceList) => {
  1585. //搜索服务失败后
  1586. state.BLEGetServices = serviceList;
  1587. },
  1588. getFail: () => {
  1589. this.commit("B_CloseBLEConnection", {
  1590. deviceId: item.deviceId
  1591. })
  1592. // state.cIndex = -1;
  1593. // state.bConnection = false;
  1594. // state.bVerifiedConnection = false;
  1595. this.commit("onResetBLEConnection");
  1596. }
  1597. });
  1598. }, 2000);
  1599. },
  1600. fail: (fail) => {
  1601. console.log("***fail:", fail);
  1602. if (fail.errCode === 10012) {
  1603. console.log("连接超时,请重试!");
  1604. uni.showToast({
  1605. title: '连接失败,开启设备后尝试重新连接!',
  1606. icon: 'none',
  1607. duration: 3000,
  1608. mask: true
  1609. })
  1610. } else if (fail.errCode === 10013) {
  1611. console.log("连接失败,蓝牙地址无效!");
  1612. uni.showToast({
  1613. title: '连接失败,蓝牙地址无效',
  1614. icon: 'none',
  1615. duration: 2000,
  1616. mask: true
  1617. })
  1618. } else if (fail.errCode === 10000) {
  1619. this.commit('initAdapter', () => {
  1620. if (getinitAdapter) {
  1621. getinitAdapter();
  1622. }
  1623. })
  1624. } else {
  1625. // err.errCode10003原因多种:蓝牙设备未开启或异常导致无法连接;蓝牙设备被占用或者上次蓝牙连接未断开导致无法连接
  1626. console.log("连接失败,请重试!", state.BLEConnectDevice);
  1627. uni.showToast({
  1628. title: '请尝试开启设备和重启app。',
  1629. icon: 'none',
  1630. duration: 2000,
  1631. mask: true
  1632. })
  1633. }
  1634. }
  1635. };
  1636. this.commit("B_CreateBLEConnection", params);
  1637. },
  1638. //devices hardware 连接后验证,这里不需要处理 bVerifiedConnection,通过mac 验证后处理
  1639. onCreateBLESuccess(state, context) {
  1640. let {
  1641. item,
  1642. getSuccess = null
  1643. } = context;
  1644. this.commit("B_CreateBLEConnection", {
  1645. item: item,
  1646. success: success => {
  1647. state.BLEConnectDevice = item;
  1648. state.bConnection = true;
  1649. if (getSuccess) {
  1650. getSuccess();
  1651. }
  1652. },
  1653. fail: fail => {
  1654. if (fail.errCode === 10012) {
  1655. console.log("连接超时,请重试!");
  1656. uni.showToast({
  1657. title: '连接超时,检查对应设备是否开启?',
  1658. icon: 'none',
  1659. duration: 2000,
  1660. mask: true
  1661. })
  1662. } else if (fail.errCode === 10013) {
  1663. console.log("连接失败,蓝牙地址无效!");
  1664. uni.showToast({
  1665. title: '蓝牙地址无效,检查设备是否正常?',
  1666. icon: 'none',
  1667. duration: 2000,
  1668. mask: true
  1669. })
  1670. } else if (fail.errCode === 10000) {
  1671. console.log("连接失败,初始化 wx.openBluetoothAdapter 调用之后使用");
  1672. uni.showToast({
  1673. title: '连接失败,检查手机蓝牙是否开启?',
  1674. icon: 'none',
  1675. duration: 2000,
  1676. mask: true
  1677. })
  1678. } else {
  1679. console.log("连接失败,请重试!");
  1680. uni.showModal({
  1681. title: '连接失败',
  1682. content: '设备未开启或被占用。请重启app和设备后重新连接。'
  1683. })
  1684. // uni.showToast({
  1685. // title: '设备未开启或被占用异常导致无法连接。',
  1686. // icon: 'none',
  1687. // duration: 2000,
  1688. // mask: true
  1689. // })
  1690. }
  1691. }
  1692. });
  1693. },
  1694. //获取蓝牙服务
  1695. onGetBLEDeviceServices(state, context) {
  1696. let {
  1697. item,
  1698. success = null,
  1699. } = context;
  1700. let retryCount = 5;
  1701. this.commit("B_GetBLEDeviceServices", {
  1702. retryCount: retryCount,
  1703. item: item,
  1704. callback: success,
  1705. getServiceList: (serviceList) => {
  1706. state.BLEGetServices = serviceList;
  1707. },
  1708. getFail: () => {
  1709. this.commit("B_CloseBLEConnection", {
  1710. deviceId: item.deviceId
  1711. })
  1712. this.commit("onResetBLEConnection");
  1713. }
  1714. })
  1715. },
  1716. //重置蓝牙连接参数
  1717. onResetBLEConnection(state) {
  1718. state.cIndex = -1;
  1719. state.BLEConnectDevice = null;
  1720. //蓝牙服务
  1721. state.BLEGetServices = null;
  1722. state.bConnection = false;
  1723. state.bVerifiedConnection = false;
  1724. },
  1725. onCloseBLEConnection(state, context) {
  1726. console.log("onCloseBLEConnection", state.BLEConnectDevice);
  1727. let {
  1728. getSuccess = null,
  1729. } = context;
  1730. if (state.BLERSSIInterval) {
  1731. clearInterval(state.BLERSSIInterval);
  1732. state.BLERSSIInterval = null;
  1733. }
  1734. this.commit("B_CloseBLEConnection", {
  1735. deviceId: state.BLEConnectDevice.deviceId,
  1736. success: () => {
  1737. state.cIndex = -1;
  1738. state.BLEConnectDevice = null;
  1739. //蓝牙服务
  1740. state.BLEGetServices = null;
  1741. if (getSuccess) {
  1742. getSuccess();
  1743. }
  1744. state.bConnection = false;
  1745. state.bVerifiedConnection = false;
  1746. uni.closeBluetoothAdapter({
  1747. success(res) {
  1748. console.log(res)
  1749. state.bOpenBluetooth = false;
  1750. }
  1751. })
  1752. }
  1753. });
  1754. },
  1755. //不关闭 adapter
  1756. onOnlyCloseBLEConnection(state, context) {
  1757. let {
  1758. getSuccess = null
  1759. } = context;
  1760. if (state.BLERSSIInterval) {
  1761. clearInterval(state.BLERSSIInterval);
  1762. state.BLERSSIInterval = null;
  1763. }
  1764. this.commit("B_CloseBLEConnection", {
  1765. deviceId: state.BLEConnectDevice.deviceId,
  1766. success: () => {
  1767. state.cIndex = -1;
  1768. state.BLEConnectDevice = null;
  1769. //蓝牙服务
  1770. state.BLEGetServices = null;
  1771. if (getSuccess) {
  1772. getSuccess();
  1773. }
  1774. state.bConnection = false;
  1775. state.bVerifiedConnection = false;
  1776. }
  1777. });
  1778. },
  1779. onWriteBLEConnectionValue(state, context) {
  1780. //#ifdef H5
  1781. console.warn("h5不支持蓝牙:", 'onWriteBLEConnectionValue');
  1782. return;
  1783. //#endif
  1784. let {
  1785. getSuccess = null,
  1786. getFail = null,
  1787. value,
  1788. bSendHex = false,
  1789. bOpen = false,
  1790. } = context;
  1791. if (!state.BLEConnectDevice && state.cIndex == -1) {
  1792. uni.showToast({
  1793. title: '蓝牙设备未连接',
  1794. icon: 'none'
  1795. })
  1796. }
  1797. // console.log(22);
  1798. //记录蓝牙盒子当前的指令
  1799. // state.currentInstruction = value;
  1800. // 发送 3 :开启原始数据
  1801. // 发送4 :关闭原始数据
  1802. // 发送 5 :开启步数
  1803. // 发送6 :关闭步数
  1804. if (value == "3") {
  1805. state.instructionState.bOpen = true;
  1806. } else if (value == "4") {
  1807. state.instructionState.bOpen = false;
  1808. } else if (value == "5") {
  1809. state.instructionState.bSteps = true;
  1810. } else if (value == "6") {
  1811. state.instructionState.bSteps = false;
  1812. } else if (bSendHex && bOpen) {
  1813. state.instructionState.bOpen = true;
  1814. } else if (bSendHex && !bOpen) {
  1815. state.instructionState.bOpen = false;
  1816. }
  1817. let retryCount = 5;
  1818. this.commit("B_WriteBLECharacteristicValue", {
  1819. value,
  1820. retryCount,
  1821. getSuccess,
  1822. getFail,
  1823. bSendHex
  1824. });
  1825. },
  1826. //获取距离
  1827. onGetRSSITransDistance(state, context) {
  1828. let {
  1829. RSSI
  1830. } = context;
  1831. BLE.getRSSITransDistance(RSSI);
  1832. },
  1833. //添加使用的前端信息
  1834. gOnAddClientInfo(state, context) {
  1835. uni.getSystemInfo({
  1836. success: (res) => {
  1837. // console.log("系统信息:", res);
  1838. let clientInfo = res;
  1839. clientInfo.clientSystem = res.system;
  1840. reqUtil.requestData(config.URL.ADDCLIENTINFO, clientInfo, 'POST').then(
  1841. res => {
  1842. console.warn(res);
  1843. },
  1844. e => {}
  1845. );
  1846. },
  1847. fail: (err) => {},
  1848. complete: () => {}
  1849. })
  1850. },
  1851. //创建快速打击对象
  1852. gCreateFilterObj(state) {
  1853. state.filter = null;
  1854. state.deviceMs = 1;
  1855. state.filter = new o0Project.Filter();
  1856. console.log("gCreateFilterObj");
  1857. },
  1858. //更新快速打击对象数据
  1859. gUpdateFilter(state, context) {
  1860. let {
  1861. data,
  1862. callback
  1863. } = context;
  1864. let {
  1865. min,
  1866. s,
  1867. ms
  1868. } = data;
  1869. var msGap = ms - state.deviceMs;
  1870. state.deviceMs = ms;
  1871. while (msGap < 0) {
  1872. msGap += 1000;
  1873. }
  1874. // console.log(data);
  1875. let {
  1876. ax,
  1877. ay,
  1878. az
  1879. } = data.acc;
  1880. let {
  1881. gx,
  1882. gy,
  1883. gz
  1884. } = data.gyro;
  1885. // console.log(ax);
  1886. state.filter.Update(new o0.Vector3(ax, ay, az), msGap, new o0.Vector3(gx, gy, gz),
  1887. callback); //我自己的更新acc的函数
  1888. },
  1889. /**
  1890. * 创建一个沙袋打击对象
  1891. * @param {Object} state
  1892. */
  1893. gCreateSandbagAlgorithm(state) {
  1894. state.sandbagAlgorithm = null;
  1895. state.sandbagAlgorithmLastTime = new Date().getTime();
  1896. state.sandbagAlgorithm = new o0ProjectRelease.SandbagAlgorithm();
  1897. console.log("gCreateSandbagAlgorithm");
  1898. },
  1899. /**
  1900. * 更新 沙袋打击对象 数据
  1901. * @param {Object} state
  1902. * @param {Object} context
  1903. */
  1904. gUpdateSandbagAlgorithm(state, context) {
  1905. let {
  1906. data,
  1907. callback = null
  1908. } = context;
  1909. let {
  1910. ax,
  1911. ay,
  1912. az
  1913. } = data.acc;
  1914. let {
  1915. gx,
  1916. gy,
  1917. gz
  1918. } = data.gyro;
  1919. /**
  1920. * ax / 10, az / 10, gz, -gx, data.ms ----- old
  1921. * ay / 10, -az / 10, gy, gz, data.msK
  1922. * let [hit, dir] = state.sandbagAlgorithm.Update(-ay, az, -gz, -gy, data.ms);
  1923. * 纠正后使用的轴向
  1924. * box["acc"] = {
  1925. ax: -ay,
  1926. ay: -ax,
  1927. az: az
  1928. };
  1929. box["gyro"] = {
  1930. gx: -gy,
  1931. gy: -gx,
  1932. gz: gz
  1933. };
  1934. */
  1935. let {
  1936. ms
  1937. } = data;
  1938. let msGap = ms - state.deviceMs;
  1939. state.deviceMs = ms;
  1940. while (msGap < 0) {
  1941. msGap += 1000;
  1942. }
  1943. let [hit, dir] = state.sandbagAlgorithm.Update(ax, az, -gz, gx, msGap);
  1944. if (dir != undefined) {
  1945. let temp = state.sandbagAlgorithm.getTempValue(dir);
  1946. if (callback) {
  1947. callback(temp);
  1948. }
  1949. // let hitCount = temp.hitCount;
  1950. }
  1951. },
  1952. /**
  1953. * 开启加速计,陀螺仪,并且模拟ble发送
  1954. * @param {Object} state
  1955. */
  1956. gStartSimulateBLEUpdate(state) {
  1957. //todo 区分平台,目前是android 模块
  1958. if (state.globalMyAttitude == null) {
  1959. state.globalMyAttitude = uni.requireNativePlugin("MyAttitude");
  1960. let globalEvent = uni.requireNativePlugin('globalEvent');
  1961. globalEvent.addEventListener('updateAccAndGyro', function(e) {
  1962. //updateAccAndGyro{"accelerometer":[-0.178375244140625,9.327804565429688,2.6168670654296875],"gyroscope":[0.0018310546875,-0.0019989013671875,0.0027923583984375]}
  1963. //console.log('updateAccAndGyro' + JSON.stringify(e));
  1964. let _accArray = e.accelerometer;
  1965. let _gyroArray = e.gyroscope;
  1966. state.globalAccData.ax = _accArray[0];
  1967. state.globalAccData.ay = _accArray[1];
  1968. state.globalAccData.az = _accArray[2];
  1969. state.globalGyroData.gx = _gyroArray[0];
  1970. state.globalGyroData.gy = _gyroArray[1];
  1971. state.globalGyroData.gz = _gyroArray[2];
  1972. let box = {};
  1973. box["acc"] = state.globalAccData;
  1974. box["gyro"] = state.globalGyroData;
  1975. box["ms"] = e.ms;
  1976. //更新数据给webview,在game-play-web||game-play-sub绑定
  1977. //后面更新数据都用此接口
  1978. uni.$emit('updateBLEDeviceData', box);
  1979. });
  1980. }
  1981. var ret = state.globalMyAttitude.onStartAccAndGyro();
  1982. if (!ret.accelerometer || !ret.gyroscope) {
  1983. uni.showModal({
  1984. title: '提示',
  1985. content: '是否初始化,加速计:' + ret.accelerometer + ',陀螺仪:' + ret.gyroscope
  1986. })
  1987. }
  1988. console.log('gStartSimulateBLEUpdate:', ret);
  1989. },
  1990. /**
  1991. * 停止加速计,陀螺仪
  1992. * @param {Object} state
  1993. */
  1994. gStopSimulateBLEUpdate(state) {
  1995. //todo 区分平台,目前是android 模块
  1996. let globalEvent = uni.requireNativePlugin('globalEvent');
  1997. globalEvent.removeEventListener('updateAccAndGyro');
  1998. var ret = state.globalMyAttitude.onStopAccAndGyro();
  1999. console.log('gStopSimulateBLEUpdate:', ret);
  2000. state.globalMyAttitude = null;
  2001. },
  2002. /**
  2003. * 限制开始游戏
  2004. */
  2005. glimitPlayGame(state, context) {
  2006. let {
  2007. gameTags,
  2008. callback
  2009. } = context;
  2010. //没有标签直接进去
  2011. if (gameTags.length == 0) {
  2012. callback();
  2013. return;
  2014. }
  2015. let canPlay = false;
  2016. gameTags.forEach((item) => {
  2017. //如果存在普通模式
  2018. if (item.tagMode === 'normal') {
  2019. canPlay = true;
  2020. }
  2021. if (canPlay)
  2022. return;
  2023. })
  2024. if (!canPlay) {
  2025. //2.如果普通模式不存在,就判断是否连接硬件了
  2026. if (!state.BLEConnectDevice || state.cIndex == -1) {
  2027. uni.showToast({
  2028. title: "请连接标签对应的设备模式!",
  2029. icon: "none",
  2030. duration: 2000,
  2031. mask: true
  2032. })
  2033. return;
  2034. }
  2035. //3.如果连接了设备,判断但是处于什么模式
  2036. gameTags.forEach((item, index, selfArr) => {
  2037. //如果存在普通模式
  2038. if (item.tagMode === state.BLEConnectDevice.usageMode) {
  2039. canPlay = true;
  2040. }
  2041. if (canPlay)
  2042. return;
  2043. if (index == selfArr.length - 1) {
  2044. uni.showToast({
  2045. title: "游戏没有当前设备模式!",
  2046. icon: "none",
  2047. duration: 2000,
  2048. mask: true
  2049. })
  2050. }
  2051. })
  2052. }
  2053. if (canPlay) {
  2054. callback();
  2055. }
  2056. },
  2057. checkAppUpdata(state) {
  2058. //#ifdef APP-PLUS
  2059. let _temp = {
  2060. appid: plus.runtime.appid,
  2061. version: state.version,
  2062. versionCode: state.versionCode,
  2063. platform: state.platform.toLocaleLowerCase(),
  2064. }
  2065. //#endif
  2066. //#ifdef H5
  2067. //此 _temp 测试用
  2068. let _temp = {
  2069. appid: '1',
  2070. version: "2.0.0",
  2071. versionCode: "20122202",
  2072. platform: "android",
  2073. }
  2074. //#endif
  2075. reqUtil.requestData(config.URL.APPVERSIONUPDATE, _temp).then(res => {
  2076. console.log('requestData APPVERSIONUPDATE =====', res);
  2077. let _data = res.data;
  2078. if (res.code == 0 && res.data.status === 1) {
  2079. uni.showModal({ //提醒用户更新
  2080. title: "更新提示",
  2081. content: res.data.note,
  2082. success: (res) => {
  2083. if (res.confirm) {
  2084. if (state.platform.toLocaleLowerCase() == "android") {
  2085. plus.runtime.openURL(_data.url, function(res) {
  2086. plus.nativeUI.alert("本机没安装应用宝,请安装后获取新版本。");
  2087. }, 'com.tencent.android.qqdownloader');
  2088. } else {
  2089. //ios
  2090. plus.runtime.openURL(_data.url);
  2091. }
  2092. }
  2093. }
  2094. })
  2095. } else {
  2096. uni.showToast({
  2097. title: '当前为最新版本',
  2098. icon: 'none',
  2099. duration: 2000,
  2100. mask: true
  2101. })
  2102. }
  2103. },
  2104. e => {
  2105. console.log(e)
  2106. });
  2107. },
  2108. /**
  2109. * 蓝牙BLE迁移到store.js
  2110. */
  2111. // 初始化蓝牙模块适配器
  2112. B_OpenBluetoothAdapter(state, context) {
  2113. let {
  2114. success,
  2115. fail,
  2116. complete
  2117. } = context;
  2118. //#ifdef H5
  2119. console.warn('h5不加载蓝牙模块');
  2120. return;
  2121. //#endif
  2122. uni.openBluetoothAdapter({
  2123. success: res => {
  2124. if (success)
  2125. success(res);
  2126. },
  2127. fail: res => {
  2128. if (fail)
  2129. fail(res);
  2130. },
  2131. complete: res => {
  2132. if (complete)
  2133. complete(res);
  2134. }
  2135. })
  2136. },
  2137. /**
  2138. * 关闭蓝牙模块适配器
  2139. */
  2140. B_CloseBluetoothAdapter() {
  2141. //#ifdef H5
  2142. console.warn('h5不加载蓝牙模块');
  2143. return;
  2144. //#endif
  2145. uni.closeBluetoothAdapter({
  2146. success: res => {
  2147. console.log('断开蓝牙模块成功');
  2148. uni.showToast({
  2149. icon: "none",
  2150. title: "蓝牙已经断开!",
  2151. mask: false,
  2152. duration: 3000
  2153. });
  2154. }
  2155. });
  2156. },
  2157. B_OnBLEConnectionStateChange(state) {
  2158. let _self = this;
  2159. //#ifdef H5
  2160. console.warn('h5不加载蓝牙模块');
  2161. return;
  2162. //#endif
  2163. uni.onBLEConnectionStateChange(res => {
  2164. // 该方法回调中可以用于处理连接意外断开等异常情况
  2165. console.log(`蓝牙连接状态 -------------------------->`, JSON.stringify(res));
  2166. if (!res.connected) {
  2167. //要在连接的状态下 ,并且获取过匹配列表有参数的情况下,才提示重连
  2168. if (!state.bShowBLEConnectModal && state.bVerifiedConnection) {
  2169. state.bShowBLEConnectModal = true;
  2170. let _connectItem = Object.assign({}, state.BLEConnectDevice);
  2171. let _cIndex = state.cIndex;
  2172. //断开连接后reset一下参数
  2173. _self.commit("onResetBLEConnection");
  2174. let _tip = state.bGamePlaying ? "蓝牙已断开是否重新连接蓝牙?重连需要重启游戏!" : "蓝牙已断开是否重新连接蓝牙";
  2175. uni.showModal({
  2176. title: _tip,
  2177. success: (sRes) => {
  2178. state.bShowBLEConnectModal = false;
  2179. if (sRes.confirm) {
  2180. console.log("重连的_cIndex:", _cIndex);
  2181. _self.commit("onCreateBLEConnection", {
  2182. item: _connectItem,
  2183. index: _cIndex,
  2184. initItem: true,
  2185. getSuccess: () => {
  2186. //此处不检查外部设备
  2187. uni.$emit('retryConnectBLESuccess');
  2188. },
  2189. getinitAdapter: () => {}
  2190. })
  2191. }
  2192. }
  2193. })
  2194. } else {
  2195. //断开连接后reset一下参数
  2196. _self.commit("onResetBLEConnection");
  2197. }
  2198. uni.$emit('callbackCloseBLE');
  2199. }
  2200. });
  2201. },
  2202. /**
  2203. * 连接低功耗蓝牙设备。
  2204. * 若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。
  2205. */
  2206. B_CreateBLEConnection(state, context) {
  2207. let {
  2208. item,
  2209. success,
  2210. fail
  2211. } = context;
  2212. this.commit('initBLEData');
  2213. state.deviceId = item.deviceId;
  2214. let timeout = 10000;
  2215. // console.log("B_CreateBLEConnection deviceId ==", state.deviceId)
  2216. uni.createBLEConnection({
  2217. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  2218. deviceId: state.deviceId,
  2219. timeout: timeout,
  2220. success: res => {
  2221. if (success) {
  2222. success(res);
  2223. }
  2224. },
  2225. fail: res => {
  2226. if (fail)
  2227. fail(res);
  2228. }
  2229. });
  2230. },
  2231. /**
  2232. * 获取设备的服务ID
  2233. */
  2234. B_GetBLEDeviceServices(state, context) {
  2235. let {
  2236. retryCount,
  2237. item,
  2238. callback,
  2239. getServiceList,
  2240. getFail,
  2241. } = context;
  2242. let serviceList = [];
  2243. let _self = this;
  2244. uni.getBLEDeviceServices({
  2245. deviceId: item.deviceId,
  2246. success: res => {
  2247. // console.log("getBLEDeviceServices==", JSON.stringify(res), item);
  2248. serviceList = res.services;
  2249. if (getServiceList) {
  2250. getServiceList(serviceList);
  2251. }
  2252. if (serviceList.length == 0) {
  2253. if (retryCount > 0) {
  2254. retryCount--;
  2255. context.retryCount = retryCount;
  2256. setTimeout(() => {
  2257. this.commit('B_GetBLEDeviceServices', context);
  2258. }, 500)
  2259. return;
  2260. }
  2261. if (getFail) {
  2262. getFail();
  2263. }
  2264. uni.hideToast();
  2265. uni.showModal({
  2266. title: '提示',
  2267. content: '1.获取服务失败,请尝试重新连接设备。\r\n2.尝试重启设备再重新连接。'
  2268. })
  2269. }
  2270. for (let i = 0; i < serviceList.length; i++) {
  2271. let service = serviceList[i];
  2272. if (service.uuid.toLocaleLowerCase() === item.PRIMARY_SERVICE
  2273. .toLocaleLowerCase()) {
  2274. state.serviceId = service.uuid;
  2275. //开始获取指定服务的特征值
  2276. _self.commit('B_GetBLEDeviceCharacteristics', {
  2277. item: item,
  2278. callback: callback
  2279. });
  2280. break;
  2281. }
  2282. }
  2283. },
  2284. fail: failRes => {
  2285. console.log('device services:', failRes.services);
  2286. uni.hideToast();
  2287. uni.showModal({
  2288. title: '连接错误',
  2289. content: '请尝试重新连接设备。'
  2290. })
  2291. if (getFail) {
  2292. getFail();
  2293. }
  2294. }
  2295. });
  2296. },
  2297. /**
  2298. * 获取指定服务的特征值
  2299. */
  2300. B_GetBLEDeviceCharacteristics(state, context) {
  2301. let {
  2302. item,
  2303. callback
  2304. } = context;
  2305. let deviceId = state.deviceId;
  2306. let serviceId = state.serviceId;
  2307. let characteristicsList = [];
  2308. uni.getBLEDeviceCharacteristics({
  2309. deviceId,
  2310. serviceId,
  2311. success: res => {
  2312. if (item.PRIMARY_NOTIFY != '') {
  2313. state.notifyCharacteristicId = item.PRIMARY_NOTIFY;
  2314. this.commit('B_NotifyBLECharacteristicValueChange', {
  2315. callback: callback
  2316. });
  2317. }
  2318. if (item.PRIMARY_WRITE != '') {
  2319. state.writeCharacteristicId = item.PRIMARY_WRITE;
  2320. }
  2321. },
  2322. fail: res => {
  2323. console.log('device getBLEDeviceCharacteristics failed:', JSON.stringify(res))
  2324. }
  2325. })
  2326. },
  2327. //启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。
  2328. //注意:必须设备的特征值支持notify或者indicate才可以成功调用,具体参照 characteristic 的 properties 属性
  2329. B_NotifyBLECharacteristicValueChange(state, context) {
  2330. let {
  2331. callback
  2332. } = context;
  2333. // 启用notify功能
  2334. // console.log("启用notify功能");
  2335. uni.notifyBLECharacteristicValueChange({
  2336. state: true,
  2337. deviceId: state.deviceId,
  2338. serviceId: state.serviceId,
  2339. characteristicId: state.notifyCharacteristicId,
  2340. success: (res) => {
  2341. this.commit('B_OnBLECharacteristicValueChange', {
  2342. callback: callback
  2343. });
  2344. },
  2345. fail: (res) => {
  2346. uni.showToast({
  2347. title: 'notify启动失败',
  2348. icon: "none",
  2349. mask: true
  2350. });
  2351. }
  2352. })
  2353. },
  2354. //监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。
  2355. B_OnBLECharacteristicValueChange(state, context) {
  2356. let {
  2357. callback
  2358. } = context;
  2359. // console.log("onBLECharacteristicValueChange success");
  2360. if (callback) {
  2361. callback({
  2362. notifyCharacteristicId: state.notifyCharacteristicId,
  2363. writeCharacteristicId: state.writeCharacteristicId
  2364. })
  2365. }
  2366. uni.onBLECharacteristicValueChange(function(res) {
  2367. //如果是ota 更新服务,这里处理通知ota更新数据
  2368. if (res.serviceId.toLocaleLowerCase() === state.UUID_OTA_SERVICE) {
  2369. uni.$emit("OTAValueChange", res);
  2370. return;
  2371. }
  2372. //serviceId 0000FFF0-0000-1000-8000-00805F9B34FB characteristic 0000FFF1-0000-1000-8000-00805F9B34FB
  2373. // console.log(`store ${res.serviceId} characteristic ${res.characteristicId} has changed, now is ${res.value}`);
  2374. let box = {};
  2375. var resValue = BLE.ab2hext(res.value); //16进制字符串
  2376. if (state.currentInstruction == "V" && state.showToast) {
  2377. var resValueStr = BLE.hexToString(resValue);
  2378. let _info = {
  2379. type: 'version',
  2380. instruction: 'V',
  2381. value: resValueStr.replace(/\{|}/g, '').trim()
  2382. }
  2383. uni.$emit('listenerBLE', _info);
  2384. state.currentInstruction = "";
  2385. state.showToast = false;
  2386. return;
  2387. } else if (state.currentInstruction == 'M' && state.bListenerMac) {
  2388. var resValueStr = BLE.hexToString(resValue);
  2389. let _info = {
  2390. type: 'mac',
  2391. instruction: 'M',
  2392. value: resValueStr.replace(/\{|}/g, '').trim()
  2393. }
  2394. uni.$emit('listenerBLE', _info);
  2395. state.currentInstruction = "";
  2396. state.bListenerMac = false;
  2397. return;
  2398. }
  2399. //步数数据解析
  2400. if (state.bListenerJson) {
  2401. var resValueStr = BLE.hexToString(resValue);
  2402. console.log(resValueStr);
  2403. box["Json"] = resValueStr;
  2404. uni.$emit('updateBLEDeviceJson', box);
  2405. }
  2406. //解析4组加速计数据
  2407. if (state.bListenerAccArray) {
  2408. let ms1 = BLE.hex2int(resValue.substr(2, 4));
  2409. let min1 = BLE.hex2int(resValue.substr(6, 2));
  2410. let s1 = BLE.hex2int(resValue.substr(8, 2));
  2411. let ax1Str = resValue.substr(10, 4);
  2412. let ay1Str = resValue.substr(14, 4);
  2413. let az1Str = resValue.substr(18, 4);
  2414. let ax1 = BLE.hexToSignedInt(ax1Str) / 32768 * 16;
  2415. let ay1 = BLE.hexToSignedInt(ay1Str) / 32768 * 16;
  2416. let az1 = BLE.hexToSignedInt(az1Str) / 32768 * 16;
  2417. let ms2 = BLE.hex2int(resValue.substr(22, 4));
  2418. let min2 = BLE.hex2int(resValue.substr(26, 2));
  2419. let s2 = BLE.hex2int(resValue.substr(28, 2));
  2420. let ax2Str = resValue.substr(30, 4);
  2421. let ay2Str = resValue.substr(34, 4);
  2422. let az2Str = resValue.substr(38, 4);
  2423. let ax2 = BLE.hexToSignedInt(ax2Str) / 32768 * 16;
  2424. let ay2 = BLE.hexToSignedInt(ay2Str) / 32768 * 16;
  2425. let az2 = BLE.hexToSignedInt(az2Str) / 32768 * 16;
  2426. let ms3 = BLE.hex2int(resValue.substr(42, 4));
  2427. let min3 = BLE.hex2int(resValue.substr(46, 2));
  2428. let s3 = BLE.hex2int(resValue.substr(48, 2));
  2429. let ax3Str = resValue.substr(50, 4);
  2430. let ay3Str = resValue.substr(54, 4);
  2431. let az3Str = resValue.substr(58, 4);
  2432. let ax3 = BLE.hexToSignedInt(ax3Str) / 32768 * 16;
  2433. let ay3 = BLE.hexToSignedInt(ay3Str) / 32768 * 16;
  2434. let az3 = BLE.hexToSignedInt(az3Str) / 32768 * 16;
  2435. let ms4 = BLE.hex2int(resValue.substr(62, 4));
  2436. let min4 = BLE.hex2int(resValue.substr(66, 2));
  2437. let s4 = BLE.hex2int(resValue.substr(68, 2));
  2438. let ax4Str = resValue.substr(70, 4);
  2439. let ay4Str = resValue.substr(74, 4);
  2440. let az4Str = resValue.substr(78, 4);
  2441. let ax4 = BLE.hexToSignedInt(ax4Str) / 32768 * 16;
  2442. let ay4 = BLE.hexToSignedInt(ay4Str) / 32768 * 16;
  2443. let az4 = BLE.hexToSignedInt(az4Str) / 32768 * 16;
  2444. let accArray = [{
  2445. ax: ax1,
  2446. ay: ay1,
  2447. az: az1,
  2448. ms: ms1,
  2449. min: min1,
  2450. s: s1
  2451. }, {
  2452. ax: ax2,
  2453. ay: ay2,
  2454. az: az2,
  2455. ms: ms2,
  2456. min: min2,
  2457. s: s2
  2458. }, {
  2459. ax: ax3,
  2460. ay: ay3,
  2461. az: az3,
  2462. ms: ms3,
  2463. min: min3,
  2464. s: s3
  2465. }, {
  2466. ax: ax4,
  2467. ay: ay4,
  2468. az: az4,
  2469. ms: ms4,
  2470. min: min4,
  2471. s: s4
  2472. }]
  2473. for (let i = 0; i < accArray.length; i++) {
  2474. let _temp = accArray[i];
  2475. box["acc"] = {
  2476. ax: _temp.ax,
  2477. ay: _temp.ay,
  2478. az: _temp.az,
  2479. };
  2480. box["min"] = _temp.min;
  2481. box["s"] = _temp.s;
  2482. box["ms"] = _temp.ms;
  2483. uni.$emit('updateBLEDeviceData', box);
  2484. }
  2485. }
  2486. //原始数据解析
  2487. if (state.bListenerUpdate) {
  2488. //R/L 左手还是右手
  2489. let handle = resValue.substr(6, 2);
  2490. let axStr = resValue.substr(8, 4);
  2491. let ayStr = resValue.substr(12, 4);
  2492. let azStr = resValue.substr(16, 4);
  2493. //一个字节最大只能表示265 2个字节可以表示65536 然后你这边做一下转换 例如00 3E 3*16+14=62 ----->0.03 g
  2494. //(真实值= AD值 /32768 * 16)
  2495. //0.003 = 62 / 2768 * 16
  2496. let ax = BLE.hexToSignedInt(axStr) / 32768 * 16;
  2497. let ay = BLE.hexToSignedInt(ayStr) / 32768 * 16;
  2498. let az = BLE.hexToSignedInt(azStr) / 32768 * 16;
  2499. let gxStr = resValue.substr(20, 4);
  2500. let gyStr = resValue.substr(24, 4);
  2501. let gzStr = resValue.substr(28, 4);
  2502. //角速度(AD值/32768 * 2000) 角速度单位是 °/s 范围是 正负2000
  2503. let gx = BLE.hexToSignedInt(gxStr) / 32768 * 2000;
  2504. let gy = BLE.hexToSignedInt(gyStr) / 32768 * 2000;
  2505. let gz = BLE.hexToSignedInt(gzStr) / 32768 * 2000;
  2506. //毫秒
  2507. let ms = BLE.hex2int(resValue.substr(2, 4));
  2508. //分
  2509. let min = BLE.hex2int(resValue.substr(32, 2));
  2510. //秒
  2511. let s = BLE.hex2int(resValue.substr(34, 2));
  2512. // ax / 10, az / 10, gz, -gx, data.ms ----- old
  2513. // let [hit, dir] = state.sandbagAlgorithm.Update(-ay, az, -gz, -gy, data.ms);
  2514. box["handle"] = BLE.hexToString(handle);
  2515. /**
  2516. * 纠正手柄传感器轴向。
  2517. * 以方向键向上为 -g = y ,左右按钮向上 -g = x, 按键面向上 -g = z;
  2518. * 陀螺仪旋转方向:逆时针为负-,顺时针为正+;
  2519. */
  2520. box["acc"] = {
  2521. ax: -ay,
  2522. ay: -ax,
  2523. az: az
  2524. };
  2525. box["gyro"] = {
  2526. gx: -gy,
  2527. gy: -gx,
  2528. gz: gz
  2529. };
  2530. box["min"] = min;
  2531. box["s"] = s;
  2532. box["ms"] = ms;
  2533. //16进制参数
  2534. box["hex"] = resValue;
  2535. //更新数据给webview,在game-play-web||game-play-sub绑定
  2536. //后面更新数据都用此接口
  2537. uni.$emit('updateBLEDeviceData', box);
  2538. }
  2539. //todo 解析跳绳模式
  2540. if (state.bListenerHexUpdate) {
  2541. //报头,固定
  2542. let byte1 = resValue.substr(0, 2);
  2543. //功能码,todo
  2544. let byte2 = resValue.substr(2, 2);
  2545. // 报文类型,后续区分在这里,目前是跳绳模式类型
  2546. let byte3 = resValue.substr(4, 2);
  2547. //报文内容,长度以 2为单位 动态变化
  2548. let byteData = resValue.substr(6, resValue.length - 10)
  2549. //报文按照长度2切割
  2550. let byteArray = byteData.replace(/(\d{2})(?=(?:\d{2})+(?!\d))/g, '$1,').split(',');
  2551. console.log("byteArray:", byteArray);
  2552. // 校验
  2553. let byteVerify = resValue.substr(resValue.length - 4, 2);
  2554. // 报尾,结束
  2555. let byteEnd = resValue.substr(resValue.length - 2, 2);
  2556. //todo 暂时先这样触发
  2557. if (byteArray.length == 2 && byteArray[0] != byteArray[1]) {
  2558. if (byteArray[0] == '00') {
  2559. state.bRopeKeyOne = 1;
  2560. } else {
  2561. //'01'
  2562. state.bRopeKeyTwo = 1;
  2563. }
  2564. if (state.bRopeKeyOne == 1 && state.bRopeKeyTwo == 1) {
  2565. state.bRopeKeyOne = 0;
  2566. state.bRopeKeyTwo = 0;
  2567. box["Rope"] = 1;
  2568. uni.$emit('updateBLEDeviceData', box);
  2569. }
  2570. }
  2571. }
  2572. });
  2573. },
  2574. /**
  2575. * 开启跳绳模式
  2576. */
  2577. B_OpenRopeSkipping() {
  2578. // this.commit('onWriteBLEConnectionValue', {
  2579. // value: "5B310101FE5D",
  2580. // bSendHex: true,
  2581. // bOpen: true
  2582. // });
  2583. console.log('B_OpenRopeSkipping', 3);
  2584. this.commit('onWriteBLEConnectionValue', {
  2585. value: "3"
  2586. });
  2587. },
  2588. /**
  2589. * 关闭跳绳模式
  2590. */
  2591. B_CloseRopeSkipping() {
  2592. // this.commit('onWriteBLEConnectionValue', {
  2593. // value: "5B210100FF5D",
  2594. // bSendHex: true,
  2595. // bOpen: false
  2596. // });
  2597. this.commit('onWriteBLEConnectionValue', {
  2598. value: "4"
  2599. });
  2600. },
  2601. //向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
  2602. B_WriteBLECharacteristicValue(state, context) {
  2603. let {
  2604. value,
  2605. retryCount,
  2606. success,
  2607. getFail,
  2608. bSendHex,
  2609. } = context;
  2610. if (!value && value === '') {
  2611. uni.showToast({
  2612. title: "指令为空"
  2613. })
  2614. return;
  2615. }
  2616. let _sendData;
  2617. if (bSendHex) {
  2618. state.bRopeKeyTwo = 0;
  2619. state.bRopeKeyOne = 0;
  2620. //如果是发送16进制
  2621. state.bListenerHexUpdate = true;
  2622. //重置原 update
  2623. state.bListenerUpdate = false;
  2624. let typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function(h) {
  2625. return parseInt(h, 16)
  2626. }))
  2627. _sendData = typedArray.buffer;
  2628. } else {
  2629. state.bListenerHexUpdate = false;
  2630. //记录一下指令
  2631. state.currentInstruction = value;
  2632. //V 获取版本,M 获取mac 地址
  2633. if (value == "V")
  2634. state.showToast = true;
  2635. if (value == "M") {
  2636. state.showToast = true;
  2637. state.bListenerMac = true;
  2638. }
  2639. if (value == "4") {
  2640. state.bListenerUpdate = false;
  2641. }
  2642. if (value == "3") {
  2643. if (state.bListenerUpdate) {
  2644. // uni.showToast({
  2645. // title: "原始数据已开启"
  2646. // })
  2647. console.warn("原始数据已开启");
  2648. // return;
  2649. }
  2650. state.bListenerUpdate = true;
  2651. }
  2652. if (value == "6") {
  2653. // state.bListenerJson = false;
  2654. state.bListenerAccArray = false;
  2655. }
  2656. if (value == "5") {
  2657. // if (state.bListenerJson) {
  2658. // uni.showToast({
  2659. // title: "步数数据已开启"
  2660. // })
  2661. // return;
  2662. // }
  2663. // state.bListenerJson = true;
  2664. if (state.bListenerAccArray) {
  2665. uni.showToast({
  2666. title: "加速计组数据已开启"
  2667. })
  2668. return;
  2669. }
  2670. state.bListenerAccArray = true;
  2671. }
  2672. _sendData = BLE.str2ab(value);
  2673. }
  2674. // console.log("currentInstruction:", state.currentInstruction, value, state.bListenerUpdate, state
  2675. // .bListenerMac,
  2676. // state.bListenerHexUpdate);
  2677. // console.log("state.deviceId:", state.deviceId, state.serviceId, state.writeCharacteristicId);
  2678. uni.writeBLECharacteristicValue({
  2679. deviceId: state.deviceId,
  2680. serviceId: state.serviceId,
  2681. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  2682. characteristicId: state.writeCharacteristicId,
  2683. // 这里的value是ArrayBuffer类型
  2684. value: _sendData,
  2685. success: (res) => {
  2686. console.log('writeBLECharacteristicValue success', res.errMsg, value,
  2687. retryCount);
  2688. if (success) {
  2689. success(res);
  2690. }
  2691. },
  2692. fail: (fail) => {
  2693. console.log(fail)
  2694. //重新写入
  2695. if (retryCount > 0) {
  2696. console.log("writeBLECharacteristicValue Rewrite ===================>" +
  2697. value + " == " + retryCount);
  2698. retryCount--;
  2699. setTimeout(() => {
  2700. this.commit("B_WriteBLECharacteristicValue", {
  2701. value,
  2702. retryCount,
  2703. success,
  2704. getFail
  2705. });
  2706. }, 300);
  2707. }
  2708. if (getFail) {
  2709. getFail(fail);
  2710. }
  2711. }
  2712. })
  2713. },
  2714. /**
  2715. * 断开蓝牙连接
  2716. */
  2717. B_CloseBLEConnection(state, context) {
  2718. let {
  2719. deviceId,
  2720. success = null
  2721. } = context;
  2722. uni.closeBLEConnection({
  2723. deviceId,
  2724. success: res => {
  2725. if (success)
  2726. success();
  2727. },
  2728. fail: (fail) => {
  2729. console.log("关闭蓝牙失败", fail);
  2730. }
  2731. });
  2732. },
  2733. //获取手机中蓝牙已匹配的设备
  2734. B_GetBondedDevices(state, context) {
  2735. // console.log("getBondedDevices ====>");
  2736. let {
  2737. success = null,
  2738. deviceId = null
  2739. } = context;
  2740. //Android
  2741. function android_bluetooth_list(deviceName, deviceId, count, callback) {
  2742. console.log("===》调用android_bluetooth_list count:", count);
  2743. var main = plus.android.runtimeMainActivity();
  2744. var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
  2745. var BAdapter = BluetoothAdapter.getDefaultAdapter();
  2746. var Context = plus.android.importClass("android.content.Context");
  2747. var lists = BAdapter.getBondedDevices();
  2748. plus.android.importClass(lists);
  2749. var len = lists.size();
  2750. // var BluetoothManager = plus.android.importClass("android.bluetooth.BluetoothManager");
  2751. // var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
  2752. // var ProfileType = plus.android.importClass("android.bluetooth.ProfileType");
  2753. // plus.android.importClass(BluetoothManager);
  2754. // plus.android.importClass(BluetoothDevice);
  2755. // plus.android.importClass(ProfileType);
  2756. // var UUID = plus.android.importClass("java.util.UUID");
  2757. // var uuid = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
  2758. // var gDevice = BAdapter.getRemoteDevice("C5:5C:19:04:00:30");
  2759. // plus.android.importClass(gDevice);
  2760. // var bluetoothSocket = gDevice.createInsecureRfcommSocketToServiceRecord(uuid);
  2761. // plus.android.importClass(bluetoothSocket);
  2762. // console.log("是否连接:",bluetoothSocket.isConnected());
  2763. // var deviceLists = BluetoothManager.getConnectedDevices();
  2764. // plus.android.importClass(deviceLists);
  2765. // console.log("deviceLists.len=" + deviceLists.size());
  2766. console.log("getBondedDevices.len=", len, "deviceId:", deviceId);
  2767. //测试start===》 如果没有配对的设备,重新获取
  2768. // count--;
  2769. // if(count >0){
  2770. // setTimeout(()=>{
  2771. // android_bluetooth_list("BGBox", deviceId, count, callback);
  2772. // },1000)
  2773. // return;
  2774. // }
  2775. //测试end
  2776. if (len == 0) {
  2777. //如果没有配对的设备,重新获取
  2778. count--;
  2779. if (count > 0) {
  2780. setTimeout(() => {
  2781. android_bluetooth_list("BGBox", deviceId, count, callback);
  2782. }, 2000)
  2783. } else {
  2784. if (callback)
  2785. callback(null);
  2786. }
  2787. } else {
  2788. var iterator = lists.iterator();
  2789. let temp = null;
  2790. plus.android.importClass(iterator);
  2791. while (iterator.hasNext()) {
  2792. var d = iterator.next();
  2793. plus.android.importClass(d);
  2794. // if (d.getName().indexOf(deviceName) > -1)
  2795. // console.log(d.getAddress(), deviceId);
  2796. if (deviceId == null && d.getName().indexOf(deviceName) > -1) {
  2797. let typeEnum = {
  2798. 0: 'Unknown',
  2799. 1: 'Classic',
  2800. 2: 'Le',
  2801. 3: 'Dual'
  2802. };
  2803. let bondStateEnum = {
  2804. 10: 'None',
  2805. 11: 'Bonding',
  2806. 12: 'Bonded'
  2807. };
  2808. temp = {
  2809. name: d.getName(),
  2810. address: d.getAddress(),
  2811. type: {
  2812. 'value': d.getType(),
  2813. 'name': typeEnum[d.getType()]
  2814. },
  2815. bondState: {
  2816. 'value': d.getBondState(),
  2817. 'name': bondStateEnum[d.getBondState()]
  2818. }
  2819. }
  2820. console.log("获取到设备,并且跳出:", temp);
  2821. break;
  2822. } else if (d.getAddress() == deviceId) {
  2823. // console.log(d.getName());
  2824. // console.log(d.getAddress());
  2825. // Classic 1
  2826. // Dual 3
  2827. // Le 2
  2828. // Unknown 0
  2829. // console.log(d.getType());
  2830. // Bonded 12
  2831. // Bonding 11 Indicates bonding (pairing) is in progress with the remote device.
  2832. // None 10
  2833. // console.log(d.getBondState());
  2834. let typeEnum = {
  2835. 0: 'Unknown',
  2836. 1: 'Classic',
  2837. 2: 'Le',
  2838. 3: 'Dual'
  2839. };
  2840. let bondStateEnum = {
  2841. 10: 'None',
  2842. 11: 'Bonding',
  2843. 12: 'Bonded'
  2844. };
  2845. temp = {
  2846. name: d.getName(),
  2847. address: d.getAddress(),
  2848. type: {
  2849. 'value': d.getType(),
  2850. 'name': typeEnum[d.getType()]
  2851. },
  2852. bondState: {
  2853. 'value': d.getBondState(),
  2854. 'name': bondStateEnum[d.getBondState()]
  2855. }
  2856. }
  2857. console.log("获取到设备,并且跳出:", temp);
  2858. break;
  2859. }
  2860. }
  2861. // return temp;
  2862. if (temp == null && count > 0) {
  2863. count--;
  2864. setTimeout(() => {
  2865. android_bluetooth_list("BGBox", deviceId, count, callback);
  2866. }, 2000)
  2867. } else {
  2868. if (callback)
  2869. callback(temp);
  2870. }
  2871. }
  2872. }
  2873. function ios_bluetooth_list(deviceName, callback) {
  2874. //原生操作 首先需要通过 uni.requireNativePlugin("ModuleName") 获取 module
  2875. var SLABluetoothModule = uni.requireNativePlugin("SLABluetoothUniPlugin-SLAModule")
  2876. if (SLABluetoothModule) {
  2877. SLABluetoothModule.getConnectionDevicesAsync({
  2878. serviceUUIDs: "FFF0"
  2879. }, (res) => {
  2880. console.log("***************getConnectionDevicesAsync:", JSON.stringify(res));
  2881. if (res.data) {
  2882. console.log("获取到对应的匹配设备!");
  2883. let typeEnum = {
  2884. 0: 'Unknown',
  2885. 1: 'Classic',
  2886. 2: 'Le',
  2887. 3: 'Dual'
  2888. };
  2889. let bondStateEnum = {
  2890. 10: 'None',
  2891. 11: 'Bonding',
  2892. 12: 'Bonded'
  2893. };
  2894. let temp = {
  2895. name: res.data[0].name,
  2896. address: res.data[0].identifier,
  2897. type: {
  2898. 'value': 2,
  2899. 'name': typeEnum[2]
  2900. },
  2901. bondState: {
  2902. 'value': 12,
  2903. 'name': bondStateEnum[12]
  2904. }
  2905. }
  2906. if (callback) {
  2907. callback({
  2908. "device": temp
  2909. });
  2910. }
  2911. } else {
  2912. console.log("匹配设备数据不存在!");
  2913. if (callback) {
  2914. callback({
  2915. "device": null
  2916. });
  2917. }
  2918. }
  2919. })
  2920. } else {
  2921. console.error("不存在SLABluetoothModule ios 插件!");
  2922. if (callback) {
  2923. callback({
  2924. "device": null
  2925. });
  2926. }
  2927. }
  2928. }
  2929. let device = null;
  2930. let _count = 3;
  2931. switch (plus.os.name) {
  2932. case "Android":
  2933. // Android平台: plus.android.*
  2934. android_bluetooth_list("BGBox", deviceId, _count, (data) => {
  2935. device = data;
  2936. console.log("android getBondedDevices 匹配的对象======>", device)
  2937. if (device != null) {
  2938. state.bPhoneMatched = true;
  2939. } else {
  2940. state.bPhoneMatched = false;
  2941. }
  2942. if (success) {
  2943. success(device);
  2944. }
  2945. });
  2946. break;
  2947. case "iOS":
  2948. // iOS平台: plus.ios.*
  2949. ios_bluetooth_list("BGBox", (res) => {
  2950. device = res.device
  2951. if (success) {
  2952. success(device);
  2953. }
  2954. console.log("ios getBondedDevices 匹配的对象======>", device)
  2955. if (device != null) {
  2956. state.bPhoneMatched = true;
  2957. } else {
  2958. state.bPhoneMatched = false;
  2959. }
  2960. });
  2961. break;
  2962. default:
  2963. // 其它平台
  2964. if (success) {
  2965. success(device);
  2966. }
  2967. break;
  2968. }
  2969. // if (success) {
  2970. // success(device);
  2971. // }
  2972. },
  2973. B_OpenBLESetting() {
  2974. function android_bluetooth_setting() {
  2975. var main = plus.android.runtimeMainActivity(); //获取activity
  2976. var Intent = plus.android.importClass('android.content.Intent');
  2977. var Settings = plus.android.importClass('android.provider.Settings');
  2978. var intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); //可设置表中所有Action字段
  2979. main.startActivity(intent);
  2980. }
  2981. function ios_bluetooth_setting() {
  2982. // var UIApplication = plus.ios.import("UIApplication");
  2983. // var application2 = UIApplication.sharedApplication();
  2984. // var NSURL2 = plus.ios.import("NSURL");
  2985. // var setting2 = NSURL2.URLWithString("app-settings:");
  2986. // application2.openURL(setting2);
  2987. // plus.ios.deleteObject(setting2);
  2988. // plus.ios.deleteObject(NSURL2);
  2989. // plus.ios.deleteObject(application2);
  2990. // plus.runtime.openURL({
  2991. // action: 'App-Prefs:root=Bluetooth'
  2992. // }, function(e) {
  2993. // console.log(JSON.stringify(e));
  2994. // });
  2995. plus.runtime.launchApplication({
  2996. action: 'App-Prefs:root=Bluetooth'
  2997. }, function(e) {
  2998. console.log(JSON.stringify(e));
  2999. });
  3000. }
  3001. let device = null;
  3002. switch (plus.os.name) {
  3003. case "Android":
  3004. // Android平台: plus.android.*
  3005. android_bluetooth_setting();
  3006. break;
  3007. case "iOS":
  3008. // iOS平台: plus.ios.*
  3009. ios_bluetooth_setting();
  3010. break;
  3011. default:
  3012. // 其它平台
  3013. break;
  3014. }
  3015. },
  3016. //获取versionCode状态,用于处理特定的信息显示
  3017. getVersionCode(state) {
  3018. let self = this;
  3019. console.log("state.versionCode:" + state.versionCode);
  3020. reqUtil.requestData(config.URL.GETVERSIONCODE, {
  3021. code: state.versionCode
  3022. }).then(res => {
  3023. console.log('获取版本控制信息 =====' + JSON.stringify(res));
  3024. if (res.code == 0) {
  3025. state.versionCodeState = Object.assign({}, state.versionCodeState, res.data);
  3026. /**
  3027. * 初始化配置表方案
  3028. * 搞一个区分平台上线显示等,根据版本版本号,是否显示,平台限制等区别
  3029. */
  3030. //显示,隐藏游戏栏目
  3031. uni.setTabBarItem({
  3032. index: 2,
  3033. visible: state.versionCodeState.showGame
  3034. })
  3035. //显示,隐藏视频栏目
  3036. uni.setTabBarItem({
  3037. index: 3,
  3038. visible: state.versionCodeState.showVideo
  3039. })
  3040. }
  3041. },
  3042. e => {
  3043. console.log(e)
  3044. });
  3045. },
  3046. /**
  3047. * 任务其他操作
  3048. */
  3049. //获取关卡信息
  3050. getLevelList(state, callback) {
  3051. let self = this;
  3052. reqUtil
  3053. .requestData(config.URL.GETLEVELLIST, {}) //GETLEVELJUMPLIST
  3054. .then(
  3055. res => {
  3056. if (res.code == 0) {
  3057. // state.singlePersonList = res.data.singlePerson;
  3058. // state.multiPersonList = res.data.multiPerson;
  3059. // console.log(res.data);
  3060. let levels = res.data;
  3061. state.levels = levels;
  3062. if (1 === state.currentModeIndex) {
  3063. //关卡是跳绳
  3064. state.singlePersonList = levels.singlePersonMap[0];
  3065. state.multiPersonList = levels.multiPersonMap[0];
  3066. } else if (0 === state.currentModeIndex) {
  3067. //关卡是拳击
  3068. state.singlePersonList = levels.singlePersonMap[1];
  3069. state.multiPersonList = levels.multiPersonMap[1];
  3070. }
  3071. //获取到数据时候,重置actionJump
  3072. self.commit('resetActionJumpTask');
  3073. if (callback)
  3074. callback();
  3075. }
  3076. },
  3077. e => {
  3078. console.log(e);
  3079. }
  3080. );
  3081. },
  3082. //切换关卡列表信息K
  3083. switchLevelList(state, _levelType) {
  3084. // console.log( state.levels.multiPersonMap)
  3085. // console.log( state.levels.singlePersonMap)
  3086. if (state.levels.singlePersonMap && state.levels.singlePersonMap.length >= _levelType)
  3087. state.singlePersonList = state.levels.singlePersonMap[_levelType - 1];
  3088. else
  3089. state.singlePersonList = [];
  3090. if (state.levels.multiPersonMap && state.levels.multiPersonMap.length >= _levelType)
  3091. state.multiPersonList = state.levels.multiPersonMap[_levelType - 1];
  3092. else
  3093. state.multiPersonList = [];
  3094. if (1 === _levelType) {
  3095. //关卡是跳绳
  3096. state.currentModeIndex = 1;
  3097. } else if (2 === _levelType) {
  3098. //关卡是拳击
  3099. state.currentModeIndex = 0;
  3100. }
  3101. // console.log(state.currentModeIndex)
  3102. },
  3103. /**
  3104. * 获取钱包信息
  3105. * @param {Object} state
  3106. * @param {Object} callback
  3107. */
  3108. getUserWallets(state, callback) {
  3109. reqUtil
  3110. .requestData(config.URL.USERGETWALLETINFO, {})
  3111. .then(
  3112. res => {
  3113. if (res.code == 0) {
  3114. console.log('USERGETWALLETINFO:' + JSON.stringify(res.data));
  3115. state.cGold = res.data.gold;
  3116. state.cDiamond = res.data.diamond;
  3117. if (callback)
  3118. callback();
  3119. }
  3120. },
  3121. e => {
  3122. console.log(e);
  3123. }
  3124. );
  3125. },
  3126. setGoldAndDiamond(state, context) {
  3127. let {
  3128. gold,
  3129. diamond
  3130. } = context;
  3131. state.cGold = gold;
  3132. state.cDiamond = diamond;
  3133. },
  3134. /**
  3135. * 抽屉和弹出框
  3136. */
  3137. hideDrawerById(state, context) {
  3138. // let { id } = context;
  3139. //personal-drawer
  3140. uni.getSubNVueById('personal-drawer').hide('slide-in-left', 200);
  3141. },
  3142. showDrawerById(state, context) {
  3143. // let { id } = context;
  3144. //personal-drawer
  3145. uni.getSubNVueById('personal-drawer').show('slide-in-left', 200);
  3146. },
  3147. closeDrawerById(state, context) {
  3148. const subNVue = uni.getSubNVueById('personal-drawer');
  3149. subNVue.close();
  3150. console.log('销毁侧滑栏子窗体');
  3151. },
  3152. showPopupById(state, context) {
  3153. let {
  3154. type,
  3155. item
  3156. } = context;
  3157. console.log(type, item);
  3158. switch (type) {
  3159. case 'levelReward':
  3160. uni.$emit('reward-popup', {
  3161. title: '奖励',
  3162. content: '',
  3163. type: type,
  3164. item: {
  3165. gold: item.rewardGold,
  3166. diamond: item.rewardDiamond,
  3167. honor: item.rewardHonor
  3168. },
  3169. operationItem: item
  3170. });
  3171. break;
  3172. case 'prompt':
  3173. // 向 popup 传递消息
  3174. uni.$emit('reward-popup', {
  3175. title: '奖励',
  3176. content: '',
  3177. type: type,
  3178. item: {
  3179. gold: item.rewardGold,
  3180. diamond: item.rewardDiamond,
  3181. honor: item.rewardHonor
  3182. },
  3183. operationItem: item
  3184. });
  3185. break;
  3186. case 'unlock':
  3187. // 向 popup 传递消息
  3188. uni.$emit('reward-popup', {
  3189. title: '解锁关卡',
  3190. content: '',
  3191. type: type,
  3192. item: {
  3193. consumeGold: item.consumeGold,
  3194. consumeDiamond: item.consumeDiamond
  3195. },
  3196. operationItem: item
  3197. });
  3198. break;
  3199. case 'signIn':
  3200. this.commit("onSendSignInPopup", context);
  3201. break;
  3202. case 'pkFinish':
  3203. // 向 popup 传递消息
  3204. uni.$emit('reward-popup', {
  3205. title: 'pk完成',
  3206. content: '',
  3207. type: type,
  3208. item: item,
  3209. operationItem: item
  3210. });
  3211. break;
  3212. default:
  3213. uni.showToast({
  3214. title: 'showPopup类型为空',
  3215. icon: 'none'
  3216. })
  3217. break;
  3218. }
  3219. // #ifdef APP-PLUS
  3220. //personal-popup
  3221. const subNVue = uni.getSubNVueById('personal-popup')
  3222. subNVue.show('zoom-out', 250)
  3223. // #endif
  3224. },
  3225. closePopupById(state, context) {
  3226. const subNVue = uni.getSubNVueById('personal-popup')
  3227. subNVue.close();
  3228. console.log('销毁奖励子窗体');
  3229. },
  3230. showGuideById(state, context) {
  3231. let {
  3232. type,
  3233. item
  3234. } = context;
  3235. uni.$emit('guide-popup', {
  3236. title: 'guide',
  3237. content: '定制guide',
  3238. type: type,
  3239. item: item,
  3240. operationItem: item
  3241. });
  3242. const subNVue = uni.getSubNVueById('personal-guide')
  3243. subNVue.show('fade-in', 250)
  3244. },
  3245. closeGuideById() {
  3246. const subNVue = uni.getSubNVueById('personal-guide')
  3247. subNVue.close();
  3248. console.log('销毁新手指引子窗体');
  3249. },
  3250. /**
  3251. * 获取签到列表
  3252. * @param {Object} state
  3253. * @param {Object} callback
  3254. */
  3255. getSignInList(state, callback) {
  3256. reqUtil
  3257. .requestData(config.URL.GETSIGNINLIST, {})
  3258. .then(
  3259. res => {
  3260. if (res.code == 0) {
  3261. // console.log('GETSIGNINLIST:' + JSON.stringify(res.data));
  3262. state.signInList = res.data.signInList;
  3263. state.isSignIn = res.data.isSignIn;
  3264. if (callback)
  3265. callback(res.data);
  3266. }
  3267. },
  3268. e => {
  3269. console.log(e);
  3270. }
  3271. );
  3272. },
  3273. /**
  3274. * 签到
  3275. * @param {Object} state
  3276. * @param {Object} callback
  3277. */
  3278. onUserSignIn(state, callback) {
  3279. reqUtil
  3280. .requestData(config.URL.USERSIGNIN, {})
  3281. .then(
  3282. res => {
  3283. console.log('USERSIGNIN:' + JSON.stringify(res));
  3284. if (res.code == 0) {
  3285. state.cGold = res.data.userGold;
  3286. state.cDiamond = res.data.userDiamond;
  3287. uni.showToast({
  3288. title: '签到成功',
  3289. mask: true
  3290. })
  3291. //根据dayType 计算最近一个flag
  3292. for (let i = 0; i < state.signInList.length; i++) {
  3293. if (res.data.signIn.dayType == state.signInList[i].dayType) {
  3294. state.signInList[i] = Object.assign({}, state.signInList[i], res.data
  3295. .signIn);
  3296. break;
  3297. }
  3298. }
  3299. state.isSignIn = true;
  3300. if (callback)
  3301. callback(res.data);
  3302. } else if (res.code == 901) {
  3303. uni.showToast({
  3304. title: '今天已签到',
  3305. mask: true
  3306. })
  3307. }
  3308. },
  3309. e => {
  3310. console.log(e);
  3311. }
  3312. );
  3313. },
  3314. onSendSignInPopup(state, context) {
  3315. let {
  3316. type,
  3317. item
  3318. } = context;
  3319. let _reward = [{
  3320. name: 'gold',
  3321. width: 28,
  3322. height: 28,
  3323. url: '../../../static/common/sideBar/sGold.png',
  3324. selectedUrl: '../../../static/common/subNVue/sign/sign-grey-gold@2x.png',
  3325. explain: '',
  3326. value: 20,
  3327. isSelect: false
  3328. },
  3329. {
  3330. name: 'diamond',
  3331. width: 28,
  3332. height: 28,
  3333. url: '../../../static/common/sideBar/sDiamond.png',
  3334. selectedUrl: '../../../static/common/subNVue/sign/sign-grey-diamond@2x.png',
  3335. explain: '',
  3336. value: 20,
  3337. isSelect: false
  3338. }
  3339. ];
  3340. let list = state.signInList;
  3341. let lists = [];
  3342. for (let i = 0; i < list.length; i++) {
  3343. if (0 !== list[i].gold) {
  3344. lists.push(Object.assign({}, _reward[0], {
  3345. value: list[i].gold
  3346. }))
  3347. } else if (0 !== list[i].diamond) {
  3348. lists.push(Object.assign({}, _reward[1], {
  3349. value: list[i].diamond
  3350. }))
  3351. }
  3352. lists[i].isSelect = list[i].flag == 1 ? true : false;
  3353. }
  3354. // console.log("list:"+JSON.stringify(list));
  3355. // 向 popup 传递消息
  3356. uni.$emit('reward-popup', {
  3357. title: '每日签到',
  3358. content: '',
  3359. type: type,
  3360. lists: lists,
  3361. isSignIn: state.isSignIn
  3362. });
  3363. },
  3364. /**
  3365. * 记录沙袋打击计数
  3366. * @param {Object} state
  3367. * @param {Object} count
  3368. */
  3369. setSandbagHitCount(state, count) {
  3370. // uni.setStorageSync('sandbagHitCount', count);
  3371. state.sandbagHitCount = count;
  3372. },
  3373. addSandbagHitCount(state, context) {
  3374. let {
  3375. count
  3376. } = context;
  3377. let addCount = Number(state.sandbagHitCount) + Number(count);
  3378. // console.log("addCount:" + addCount);
  3379. state.sandbagHitCount = addCount;
  3380. uni.setStorageSync('sandbagHitCount', state.sandbagHitCount);
  3381. },
  3382. getSandbagHitCount(state) {
  3383. const value = uni.getStorageSync('sandbagHitCount');
  3384. if (value) {
  3385. state.sandbagHitCount = value;
  3386. } else {
  3387. state.sandbagHitCount = 0;
  3388. }
  3389. },
  3390. /**
  3391. * 获取用户等hitcount数据
  3392. * 同步沙袋计数
  3393. * @param {Object} state
  3394. */
  3395. getUserData_hitCount(state) {
  3396. //获取本地数据
  3397. this.commit("getSandbagHitCount");
  3398. reqUtil
  3399. .requestData(config.URL.USERDATA_GETHITCOUNT, {}, 'POST')
  3400. .then(
  3401. res => {
  3402. console.log('USERDATA_GETHITCOUNT:' + JSON.stringify(res));
  3403. if (res.code == 0) {
  3404. if (res.data.isReset) {
  3405. this.commit("setSandbagHitCount", 0);
  3406. } else {
  3407. if (res.data.hitCount > state.sandbagHitCount) {
  3408. this.commit("setSandbagHitCount", res.data.hitCount);
  3409. state.netSandbagHitCount = res.data.hitCount;
  3410. } else if (res.data.hitCount < state.sandbagHitCount) {
  3411. //获取时候,小于本地记录的,则同步到网络
  3412. this.commit("uploadUserData_hitCount");
  3413. }
  3414. }
  3415. }
  3416. },
  3417. e => {
  3418. console.log(e);
  3419. }
  3420. );
  3421. },
  3422. uploadUserData_hitCount(state) {
  3423. //如果拳击数没变化,则不用上传
  3424. if (state.netSandbagHitCount == state.sandbagHitCount) return;
  3425. reqUtil
  3426. .requestData(config.URL.USERDATA_UPLOADHITCOUNT, {
  3427. hitCount: state.sandbagHitCount
  3428. }, 'POST')
  3429. .then(
  3430. res => {
  3431. console.log('USERDATA_UPLOADHITCOUNT:' + JSON.stringify(res));
  3432. if (res.code == 0) {
  3433. this.commit("setSandbagHitCount", res.data.hitCount)
  3434. state.netSandbagHitCount = res.data.hitCount;
  3435. }
  3436. },
  3437. e => {
  3438. console.log(e);
  3439. }
  3440. );
  3441. },
  3442. onConnectSocket(state) {
  3443. let self = this;
  3444. uni.connectSocket({
  3445. // #ifdef APP-PLUS
  3446. url: 'wss://www.9527fun.cn/api_dev/websocket/APPLog/123'
  3447. // #endif
  3448. // #ifdef H5
  3449. // url:'wss://www.9527fun.cn/api_dev/websocket/APPLog/123'
  3450. url: 'ws://192.168.0.108:9090/api_dev/websocket/APPLog/123'
  3451. // #endif
  3452. });
  3453. uni.onSocketMessage(function(res) {
  3454. console.log('收到服务器内容:' + res.data);
  3455. });
  3456. uni.onSocketOpen(function(res) {
  3457. console.log('WebSocket连接已打开!');
  3458. });
  3459. uni.onSocketError(function(res) {
  3460. console.log('WebSocket连接打开失败,请检查!');
  3461. });
  3462. },
  3463. onCloseSocket(state) {
  3464. uni.onSocketClose(function(res) {
  3465. console.log('WebSocket 已关闭!');
  3466. });
  3467. },
  3468. onSendSocketMessage(state, msg) {
  3469. console.log("发送数据", msg);
  3470. uni.sendSocketMessage({
  3471. data: JSON.stringify(msg)
  3472. });
  3473. },
  3474. //jumpIdenModule 算法插件 start
  3475. jumpIdenModule_SearchData(state, context) {
  3476. state.filesList = [];
  3477. let {
  3478. data,
  3479. callback = null
  3480. } = context;
  3481. let url = "http://42.192.165.168:2022/search";
  3482. let _temp = {
  3483. name: state.readDataName
  3484. }
  3485. console.log('requestData search ===== url:' + url);
  3486. reqUtil.requestData(url, _temp,'POST').then(res => {
  3487. let strArray = res.split('\n');
  3488. console.log(strArray);
  3489. for (let i = 0; i < strArray.length-1; i++) {
  3490. let _name = strArray[i].split('.')[0];
  3491. if (_name.Length <= 0) continue;
  3492. let item = Object.assign({}, {
  3493. cname:_name,
  3494. bRatio: false
  3495. });
  3496. state.filesList.push(item);
  3497. }
  3498. uni.showToast({
  3499. title: '获取列表成功!',
  3500. icon: 'none',
  3501. duration: 1000,
  3502. mask: true
  3503. })
  3504. },
  3505. e => {
  3506. console.log(e)
  3507. uni.showToast({
  3508. title: '请求失败!',
  3509. icon: 'none',
  3510. duration: 1000,
  3511. mask: true
  3512. })
  3513. });
  3514. },
  3515. jumpIdenModule_DownloadReadData(state,context) {
  3516. let {
  3517. data,
  3518. callback = null
  3519. } = context;
  3520. let downloadUrl = "http://42.192.165.168:2022/download";
  3521. console.log('requestData downloadUrl ===== url:' + downloadUrl);
  3522. reqUtil.requestData(downloadUrl, {name:data.cname},'POST').then(res => {
  3523. //console.log(res);
  3524. uni.setStorageSync('readData', res);
  3525. if(callback)
  3526. callback(res);
  3527. //读取的数据传入android端
  3528. let _temp = jumpIdenModule.onJumpInitInputData({
  3529. data: res
  3530. })
  3531. uni.showToast({
  3532. title: '' + _temp.msg,
  3533. icon: 'none',
  3534. duration: 1000,
  3535. mask: true
  3536. })
  3537. },
  3538. e => {
  3539. console.log(e)
  3540. uni.showToast({
  3541. title: '请求失败!',
  3542. icon: 'none',
  3543. duration: 1000,
  3544. mask: true
  3545. })
  3546. });
  3547. },
  3548. jumpIdenModule_onJumpInit() {
  3549. let res = jumpIdenModule.onJumpInit();
  3550. uni.showToast({
  3551. title: ''+ res.code,
  3552. icon: 'none',
  3553. duration: 1000,
  3554. mask: true
  3555. })
  3556. },
  3557. jumpIdenModule_onJumpInitLocalData(){
  3558. let res = jumpIdenModule.onJumpInitLocalData();
  3559. uni.showToast({
  3560. title: ''+ res.code,
  3561. icon: 'none',
  3562. duration: 1000,
  3563. mask: true
  3564. })
  3565. },
  3566. jumpIdenModule_onTestJump6AxisDataUpdate() {
  3567. let box = {};
  3568. box["acc"] = {
  3569. ax: 11,
  3570. ay: 22,
  3571. az: 33,
  3572. };
  3573. box["gyro"] = {
  3574. gx: 111,
  3575. gy: 222,
  3576. gz: 333,
  3577. };
  3578. box["ms"] = 33;
  3579. jumpIdenModule.onJump6AxisDataUpdate(box)
  3580. },
  3581. jumpIdenModule_onTestUpdate() {
  3582. jumpIdenModule.onTestUpdate();
  3583. },
  3584. jumpIdenModule_onJump6AxisDataUpdate(state,context){
  3585. let {
  3586. data,
  3587. callback = null
  3588. } = context;
  3589. let res = jumpIdenModule.onJump6AxisDataUpdate({hex:data.hex});
  3590. if(callback)
  3591. callback(res);
  3592. },
  3593. onGetReadDataName(state){
  3594. console.log(1)
  3595. state.readDataName = uni.getStorageSync('readDataName');
  3596. },
  3597. onSetReadDataName(state, value) {
  3598. uni.setStorageSync('readDataName', value);
  3599. state.readDataName = value;
  3600. },
  3601. //jumpIdenModule 算法插件 end
  3602. }
  3603. })
  3604. export default store