store.js 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990
  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. gIntelligentUpdateSandbagAlgorithm(state, context) {
  1954. let {
  1955. data,
  1956. callback = null
  1957. } = context;
  1958. let {
  1959. ax,
  1960. ay,
  1961. az
  1962. } = data.acc;
  1963. let {
  1964. gx,
  1965. gy,
  1966. gz
  1967. } = data.gyro;
  1968. let {
  1969. ms
  1970. } = data;
  1971. let msGap = ms - state.deviceMs;
  1972. state.deviceMs = ms;
  1973. while (msGap < 0) {
  1974. msGap += 1000;
  1975. }
  1976. let [hit, dir] = state.sandbagAlgorithm.Update(ax, az, gz, -gx, msGap); //灯带方向
  1977. if (dir != undefined) {
  1978. let temp = state.sandbagAlgorithm.getTempValue(dir);
  1979. if (callback) {
  1980. callback(temp);
  1981. }
  1982. // let hitCount = temp.hitCount;
  1983. }
  1984. },
  1985. /**
  1986. * 开启加速计,陀螺仪,并且模拟ble发送
  1987. * @param {Object} state
  1988. */
  1989. gStartSimulateBLEUpdate(state) {
  1990. //todo 区分平台,目前是android 模块
  1991. if (state.globalMyAttitude == null) {
  1992. state.globalMyAttitude = uni.requireNativePlugin("MyAttitude");
  1993. let globalEvent = uni.requireNativePlugin('globalEvent');
  1994. globalEvent.addEventListener('updateAccAndGyro', function(e) {
  1995. //updateAccAndGyro{"accelerometer":[-0.178375244140625,9.327804565429688,2.6168670654296875],"gyroscope":[0.0018310546875,-0.0019989013671875,0.0027923583984375]}
  1996. //console.log('updateAccAndGyro' + JSON.stringify(e));
  1997. let _accArray = e.accelerometer;
  1998. let _gyroArray = e.gyroscope;
  1999. state.globalAccData.ax = _accArray[0];
  2000. state.globalAccData.ay = _accArray[1];
  2001. state.globalAccData.az = _accArray[2];
  2002. state.globalGyroData.gx = _gyroArray[0];
  2003. state.globalGyroData.gy = _gyroArray[1];
  2004. state.globalGyroData.gz = _gyroArray[2];
  2005. let box = {};
  2006. box["acc"] = state.globalAccData;
  2007. box["gyro"] = state.globalGyroData;
  2008. box["ms"] = e.ms;
  2009. //更新数据给webview,在game-play-web||game-play-sub绑定
  2010. //后面更新数据都用此接口
  2011. uni.$emit('updateBLEDeviceData', box);
  2012. });
  2013. }
  2014. var ret = state.globalMyAttitude.onStartAccAndGyro();
  2015. if (!ret.accelerometer || !ret.gyroscope) {
  2016. uni.showModal({
  2017. title: '提示',
  2018. content: '是否初始化,加速计:' + ret.accelerometer + ',陀螺仪:' + ret.gyroscope
  2019. })
  2020. }
  2021. console.log('gStartSimulateBLEUpdate:', ret);
  2022. },
  2023. /**
  2024. * 停止加速计,陀螺仪
  2025. * @param {Object} state
  2026. */
  2027. gStopSimulateBLEUpdate(state) {
  2028. //todo 区分平台,目前是android 模块
  2029. let globalEvent = uni.requireNativePlugin('globalEvent');
  2030. globalEvent.removeEventListener('updateAccAndGyro');
  2031. var ret = state.globalMyAttitude.onStopAccAndGyro();
  2032. console.log('gStopSimulateBLEUpdate:', ret);
  2033. state.globalMyAttitude = null;
  2034. },
  2035. /**
  2036. * 限制开始游戏
  2037. */
  2038. glimitPlayGame(state, context) {
  2039. let {
  2040. gameTags,
  2041. callback
  2042. } = context;
  2043. //没有标签直接进去
  2044. if (gameTags.length == 0) {
  2045. callback();
  2046. return;
  2047. }
  2048. let canPlay = false;
  2049. gameTags.forEach((item) => {
  2050. //如果存在普通模式
  2051. if (item.tagMode === 'normal') {
  2052. canPlay = true;
  2053. }
  2054. if (canPlay)
  2055. return;
  2056. })
  2057. if (!canPlay) {
  2058. //2.如果普通模式不存在,就判断是否连接硬件了
  2059. if (!state.BLEConnectDevice || state.cIndex == -1) {
  2060. uni.showToast({
  2061. title: "请连接标签对应的设备模式!",
  2062. icon: "none",
  2063. duration: 2000,
  2064. mask: true
  2065. })
  2066. return;
  2067. }
  2068. //3.如果连接了设备,判断但是处于什么模式
  2069. gameTags.forEach((item, index, selfArr) => {
  2070. //如果存在普通模式
  2071. if (item.tagMode === state.BLEConnectDevice.usageMode) {
  2072. canPlay = true;
  2073. }
  2074. if (canPlay)
  2075. return;
  2076. if (index == selfArr.length - 1) {
  2077. uni.showToast({
  2078. title: "游戏没有当前设备模式!",
  2079. icon: "none",
  2080. duration: 2000,
  2081. mask: true
  2082. })
  2083. }
  2084. })
  2085. }
  2086. if (canPlay) {
  2087. callback();
  2088. }
  2089. },
  2090. checkAppUpdata(state) {
  2091. //#ifdef APP-PLUS
  2092. let _temp = {
  2093. appid: plus.runtime.appid,
  2094. version: state.version,
  2095. versionCode: state.versionCode,
  2096. platform: state.platform.toLocaleLowerCase(),
  2097. }
  2098. //#endif
  2099. //#ifdef H5
  2100. //此 _temp 测试用
  2101. let _temp = {
  2102. appid: '1',
  2103. version: "2.0.0",
  2104. versionCode: "20122202",
  2105. platform: "android",
  2106. }
  2107. //#endif
  2108. reqUtil.requestData(config.URL.APPVERSIONUPDATE, _temp).then(res => {
  2109. console.log('requestData APPVERSIONUPDATE =====', res);
  2110. let _data = res.data;
  2111. if (res.code == 0 && res.data.status === 1) {
  2112. uni.showModal({ //提醒用户更新
  2113. title: "更新提示",
  2114. content: res.data.note,
  2115. success: (res) => {
  2116. if (res.confirm) {
  2117. if (state.platform.toLocaleLowerCase() == "android") {
  2118. plus.runtime.openURL(_data.url, function(res) {
  2119. plus.nativeUI.alert("本机没安装应用宝,请安装后获取新版本。");
  2120. }, 'com.tencent.android.qqdownloader');
  2121. } else {
  2122. //ios
  2123. plus.runtime.openURL(_data.url);
  2124. }
  2125. }
  2126. }
  2127. })
  2128. } else {
  2129. uni.showToast({
  2130. title: '当前为最新版本',
  2131. icon: 'none',
  2132. duration: 2000,
  2133. mask: true
  2134. })
  2135. }
  2136. },
  2137. e => {
  2138. console.log(e)
  2139. });
  2140. },
  2141. /**
  2142. * 蓝牙BLE迁移到store.js
  2143. */
  2144. // 初始化蓝牙模块适配器
  2145. B_OpenBluetoothAdapter(state, context) {
  2146. let {
  2147. success,
  2148. fail,
  2149. complete
  2150. } = context;
  2151. //#ifdef H5
  2152. console.warn('h5不加载蓝牙模块');
  2153. return;
  2154. //#endif
  2155. uni.openBluetoothAdapter({
  2156. success: res => {
  2157. if (success)
  2158. success(res);
  2159. },
  2160. fail: res => {
  2161. if (fail)
  2162. fail(res);
  2163. },
  2164. complete: res => {
  2165. if (complete)
  2166. complete(res);
  2167. }
  2168. })
  2169. },
  2170. /**
  2171. * 关闭蓝牙模块适配器
  2172. */
  2173. B_CloseBluetoothAdapter() {
  2174. //#ifdef H5
  2175. console.warn('h5不加载蓝牙模块');
  2176. return;
  2177. //#endif
  2178. uni.closeBluetoothAdapter({
  2179. success: res => {
  2180. console.log('断开蓝牙模块成功');
  2181. uni.showToast({
  2182. icon: "none",
  2183. title: "蓝牙已经断开!",
  2184. mask: false,
  2185. duration: 3000
  2186. });
  2187. }
  2188. });
  2189. },
  2190. B_OnBLEConnectionStateChange(state) {
  2191. let _self = this;
  2192. //#ifdef H5
  2193. console.warn('h5不加载蓝牙模块');
  2194. return;
  2195. //#endif
  2196. uni.onBLEConnectionStateChange(res => {
  2197. // 该方法回调中可以用于处理连接意外断开等异常情况
  2198. console.log(`蓝牙连接状态 -------------------------->`, JSON.stringify(res));
  2199. if (!res.connected) {
  2200. //要在连接的状态下 ,并且获取过匹配列表有参数的情况下,才提示重连
  2201. if (!state.bShowBLEConnectModal && state.bVerifiedConnection) {
  2202. state.bShowBLEConnectModal = true;
  2203. let _connectItem = Object.assign({}, state.BLEConnectDevice);
  2204. let _cIndex = state.cIndex;
  2205. //断开连接后reset一下参数
  2206. _self.commit("onResetBLEConnection");
  2207. let _tip = state.bGamePlaying ? "蓝牙已断开是否重新连接蓝牙?重连需要重启游戏!" : "蓝牙已断开是否重新连接蓝牙";
  2208. uni.showModal({
  2209. title: _tip,
  2210. success: (sRes) => {
  2211. state.bShowBLEConnectModal = false;
  2212. if (sRes.confirm) {
  2213. console.log("重连的_cIndex:", _cIndex);
  2214. _self.commit("onCreateBLEConnection", {
  2215. item: _connectItem,
  2216. index: _cIndex,
  2217. initItem: true,
  2218. getSuccess: () => {
  2219. //此处不检查外部设备
  2220. uni.$emit('retryConnectBLESuccess');
  2221. },
  2222. getinitAdapter: () => {}
  2223. })
  2224. }
  2225. }
  2226. })
  2227. } else {
  2228. //断开连接后reset一下参数
  2229. _self.commit("onResetBLEConnection");
  2230. }
  2231. uni.$emit('callbackCloseBLE');
  2232. }
  2233. });
  2234. },
  2235. /**
  2236. * 连接低功耗蓝牙设备。
  2237. * 若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。
  2238. */
  2239. B_CreateBLEConnection(state, context) {
  2240. let {
  2241. item,
  2242. success,
  2243. fail
  2244. } = context;
  2245. this.commit('initBLEData');
  2246. state.deviceId = item.deviceId;
  2247. let timeout = 10000;
  2248. // console.log("B_CreateBLEConnection deviceId ==", state.deviceId)
  2249. uni.createBLEConnection({
  2250. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  2251. deviceId: state.deviceId,
  2252. timeout: timeout,
  2253. success: res => {
  2254. if (success) {
  2255. success(res);
  2256. }
  2257. },
  2258. fail: res => {
  2259. if (fail)
  2260. fail(res);
  2261. }
  2262. });
  2263. },
  2264. /**
  2265. * 获取设备的服务ID
  2266. */
  2267. B_GetBLEDeviceServices(state, context) {
  2268. let {
  2269. retryCount,
  2270. item,
  2271. callback,
  2272. getServiceList,
  2273. getFail,
  2274. } = context;
  2275. let serviceList = [];
  2276. let _self = this;
  2277. uni.getBLEDeviceServices({
  2278. deviceId: item.deviceId,
  2279. success: res => {
  2280. // console.log("getBLEDeviceServices==", JSON.stringify(res), item);
  2281. serviceList = res.services;
  2282. if (getServiceList) {
  2283. getServiceList(serviceList);
  2284. }
  2285. if (serviceList.length == 0) {
  2286. if (retryCount > 0) {
  2287. retryCount--;
  2288. context.retryCount = retryCount;
  2289. setTimeout(() => {
  2290. this.commit('B_GetBLEDeviceServices', context);
  2291. }, 500)
  2292. return;
  2293. }
  2294. if (getFail) {
  2295. getFail();
  2296. }
  2297. uni.hideToast();
  2298. uni.showModal({
  2299. title: '提示',
  2300. content: '1.获取服务失败,请尝试重新连接设备。\r\n2.尝试重启设备再重新连接。'
  2301. })
  2302. }
  2303. for (let i = 0; i < serviceList.length; i++) {
  2304. let service = serviceList[i];
  2305. if (service.uuid.toLocaleLowerCase() === item.PRIMARY_SERVICE
  2306. .toLocaleLowerCase()) {
  2307. state.serviceId = service.uuid;
  2308. //开始获取指定服务的特征值
  2309. _self.commit('B_GetBLEDeviceCharacteristics', {
  2310. item: item,
  2311. callback: callback
  2312. });
  2313. break;
  2314. }
  2315. }
  2316. },
  2317. fail: failRes => {
  2318. console.log('device services:', failRes.services);
  2319. uni.hideToast();
  2320. uni.showModal({
  2321. title: '连接错误',
  2322. content: '请尝试重新连接设备。'
  2323. })
  2324. if (getFail) {
  2325. getFail();
  2326. }
  2327. }
  2328. });
  2329. },
  2330. /**
  2331. * 获取指定服务的特征值
  2332. */
  2333. B_GetBLEDeviceCharacteristics(state, context) {
  2334. let {
  2335. item,
  2336. callback
  2337. } = context;
  2338. let deviceId = state.deviceId;
  2339. let serviceId = state.serviceId;
  2340. let characteristicsList = [];
  2341. uni.getBLEDeviceCharacteristics({
  2342. deviceId,
  2343. serviceId,
  2344. success: res => {
  2345. if (item.PRIMARY_NOTIFY != '') {
  2346. state.notifyCharacteristicId = item.PRIMARY_NOTIFY;
  2347. this.commit('B_NotifyBLECharacteristicValueChange', {
  2348. callback: callback
  2349. });
  2350. }
  2351. if (item.PRIMARY_WRITE != '') {
  2352. state.writeCharacteristicId = item.PRIMARY_WRITE;
  2353. }
  2354. },
  2355. fail: res => {
  2356. console.log('device getBLEDeviceCharacteristics failed:', JSON.stringify(res))
  2357. }
  2358. })
  2359. },
  2360. //启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。
  2361. //注意:必须设备的特征值支持notify或者indicate才可以成功调用,具体参照 characteristic 的 properties 属性
  2362. B_NotifyBLECharacteristicValueChange(state, context) {
  2363. let {
  2364. callback
  2365. } = context;
  2366. // 启用notify功能
  2367. // console.log("启用notify功能");
  2368. uni.notifyBLECharacteristicValueChange({
  2369. state: true,
  2370. deviceId: state.deviceId,
  2371. serviceId: state.serviceId,
  2372. characteristicId: state.notifyCharacteristicId,
  2373. success: (res) => {
  2374. this.commit('B_OnBLECharacteristicValueChange', {
  2375. callback: callback
  2376. });
  2377. },
  2378. fail: (res) => {
  2379. uni.showToast({
  2380. title: 'notify启动失败',
  2381. icon: "none",
  2382. mask: true
  2383. });
  2384. }
  2385. })
  2386. },
  2387. //监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。
  2388. B_OnBLECharacteristicValueChange(state, context) {
  2389. let {
  2390. callback
  2391. } = context;
  2392. // console.log("onBLECharacteristicValueChange success");
  2393. if (callback) {
  2394. callback({
  2395. notifyCharacteristicId: state.notifyCharacteristicId,
  2396. writeCharacteristicId: state.writeCharacteristicId
  2397. })
  2398. }
  2399. uni.onBLECharacteristicValueChange(function(res) {
  2400. //如果是ota 更新服务,这里处理通知ota更新数据
  2401. if (res.serviceId.toLocaleLowerCase() === state.UUID_OTA_SERVICE) {
  2402. uni.$emit("OTAValueChange", res);
  2403. return;
  2404. }
  2405. //serviceId 0000FFF0-0000-1000-8000-00805F9B34FB characteristic 0000FFF1-0000-1000-8000-00805F9B34FB
  2406. // console.log(`store ${res.serviceId} characteristic ${res.characteristicId} has changed, now is ${res.value}`);
  2407. let box = {};
  2408. var resValue = BLE.ab2hext(res.value); //16进制字符串
  2409. if (state.currentInstruction == "V" && state.showToast) {
  2410. var resValueStr = BLE.hexToString(resValue);
  2411. let _info = {
  2412. type: 'version',
  2413. instruction: 'V',
  2414. value: resValueStr.replace(/\{|}/g, '').trim()
  2415. }
  2416. uni.$emit('listenerBLE', _info);
  2417. state.currentInstruction = "";
  2418. state.showToast = false;
  2419. return;
  2420. } else if (state.currentInstruction == 'M' && state.bListenerMac) {
  2421. var resValueStr = BLE.hexToString(resValue);
  2422. let _info = {
  2423. type: 'mac',
  2424. instruction: 'M',
  2425. value: resValueStr.replace(/\{|}/g, '').trim()
  2426. }
  2427. uni.$emit('listenerBLE', _info);
  2428. state.currentInstruction = "";
  2429. state.bListenerMac = false;
  2430. return;
  2431. }
  2432. //步数数据解析
  2433. if (state.bListenerJson) {
  2434. var resValueStr = BLE.hexToString(resValue);
  2435. console.log(resValueStr);
  2436. box["Json"] = resValueStr;
  2437. uni.$emit('updateBLEDeviceJson', box);
  2438. }
  2439. //解析4组加速计数据
  2440. if (state.bListenerAccArray) {
  2441. let ms1 = BLE.hex2int(resValue.substr(2, 4));
  2442. let min1 = BLE.hex2int(resValue.substr(6, 2));
  2443. let s1 = BLE.hex2int(resValue.substr(8, 2));
  2444. let ax1Str = resValue.substr(10, 4);
  2445. let ay1Str = resValue.substr(14, 4);
  2446. let az1Str = resValue.substr(18, 4);
  2447. let ax1 = BLE.hexToSignedInt(ax1Str) / 32768 * 16;
  2448. let ay1 = BLE.hexToSignedInt(ay1Str) / 32768 * 16;
  2449. let az1 = BLE.hexToSignedInt(az1Str) / 32768 * 16;
  2450. let ms2 = BLE.hex2int(resValue.substr(22, 4));
  2451. let min2 = BLE.hex2int(resValue.substr(26, 2));
  2452. let s2 = BLE.hex2int(resValue.substr(28, 2));
  2453. let ax2Str = resValue.substr(30, 4);
  2454. let ay2Str = resValue.substr(34, 4);
  2455. let az2Str = resValue.substr(38, 4);
  2456. let ax2 = BLE.hexToSignedInt(ax2Str) / 32768 * 16;
  2457. let ay2 = BLE.hexToSignedInt(ay2Str) / 32768 * 16;
  2458. let az2 = BLE.hexToSignedInt(az2Str) / 32768 * 16;
  2459. let ms3 = BLE.hex2int(resValue.substr(42, 4));
  2460. let min3 = BLE.hex2int(resValue.substr(46, 2));
  2461. let s3 = BLE.hex2int(resValue.substr(48, 2));
  2462. let ax3Str = resValue.substr(50, 4);
  2463. let ay3Str = resValue.substr(54, 4);
  2464. let az3Str = resValue.substr(58, 4);
  2465. let ax3 = BLE.hexToSignedInt(ax3Str) / 32768 * 16;
  2466. let ay3 = BLE.hexToSignedInt(ay3Str) / 32768 * 16;
  2467. let az3 = BLE.hexToSignedInt(az3Str) / 32768 * 16;
  2468. let ms4 = BLE.hex2int(resValue.substr(62, 4));
  2469. let min4 = BLE.hex2int(resValue.substr(66, 2));
  2470. let s4 = BLE.hex2int(resValue.substr(68, 2));
  2471. let ax4Str = resValue.substr(70, 4);
  2472. let ay4Str = resValue.substr(74, 4);
  2473. let az4Str = resValue.substr(78, 4);
  2474. let ax4 = BLE.hexToSignedInt(ax4Str) / 32768 * 16;
  2475. let ay4 = BLE.hexToSignedInt(ay4Str) / 32768 * 16;
  2476. let az4 = BLE.hexToSignedInt(az4Str) / 32768 * 16;
  2477. let accArray = [{
  2478. ax: ax1,
  2479. ay: ay1,
  2480. az: az1,
  2481. ms: ms1,
  2482. min: min1,
  2483. s: s1
  2484. }, {
  2485. ax: ax2,
  2486. ay: ay2,
  2487. az: az2,
  2488. ms: ms2,
  2489. min: min2,
  2490. s: s2
  2491. }, {
  2492. ax: ax3,
  2493. ay: ay3,
  2494. az: az3,
  2495. ms: ms3,
  2496. min: min3,
  2497. s: s3
  2498. }, {
  2499. ax: ax4,
  2500. ay: ay4,
  2501. az: az4,
  2502. ms: ms4,
  2503. min: min4,
  2504. s: s4
  2505. }]
  2506. for (let i = 0; i < accArray.length; i++) {
  2507. let _temp = accArray[i];
  2508. box["acc"] = {
  2509. ax: _temp.ax,
  2510. ay: _temp.ay,
  2511. az: _temp.az,
  2512. };
  2513. box["min"] = _temp.min;
  2514. box["s"] = _temp.s;
  2515. box["ms"] = _temp.ms;
  2516. uni.$emit('updateBLEDeviceData', box);
  2517. }
  2518. }
  2519. //原始数据解析
  2520. if (state.bListenerUpdate) {
  2521. //R/L 左手还是右手
  2522. let handle = resValue.substr(6, 2);
  2523. let axStr = resValue.substr(8, 4);
  2524. let ayStr = resValue.substr(12, 4);
  2525. let azStr = resValue.substr(16, 4);
  2526. //一个字节最大只能表示265 2个字节可以表示65536 然后你这边做一下转换 例如00 3E 3*16+14=62 ----->0.03 g
  2527. //(真实值= AD值 /32768 * 16)
  2528. //0.003 = 62 / 2768 * 16
  2529. let ax = BLE.hexToSignedInt(axStr) / 32768 * 16;
  2530. let ay = BLE.hexToSignedInt(ayStr) / 32768 * 16;
  2531. let az = BLE.hexToSignedInt(azStr) / 32768 * 16;
  2532. let gxStr = resValue.substr(20, 4);
  2533. let gyStr = resValue.substr(24, 4);
  2534. let gzStr = resValue.substr(28, 4);
  2535. //角速度(AD值/32768 * 2000) 角速度单位是 °/s 范围是 正负2000
  2536. let gx = BLE.hexToSignedInt(gxStr) / 32768 * 2000;
  2537. let gy = BLE.hexToSignedInt(gyStr) / 32768 * 2000;
  2538. let gz = BLE.hexToSignedInt(gzStr) / 32768 * 2000;
  2539. //毫秒
  2540. let ms = BLE.hex2int(resValue.substr(2, 4));
  2541. //分
  2542. let min = BLE.hex2int(resValue.substr(32, 2));
  2543. //秒
  2544. let s = BLE.hex2int(resValue.substr(34, 2));
  2545. // ax / 10, az / 10, gz, -gx, data.ms ----- old
  2546. // let [hit, dir] = state.sandbagAlgorithm.Update(-ay, az, -gz, -gy, data.ms);
  2547. box["handle"] = BLE.hexToString(handle);
  2548. /**
  2549. * 纠正手柄传感器轴向。
  2550. * 以方向键向上为 -g = y ,左右按钮向上 -g = x, 按键面向上 -g = z;
  2551. * 陀螺仪旋转方向:逆时针为负-,顺时针为正+;
  2552. */
  2553. box["acc"] = {
  2554. ax: -ay,
  2555. ay: -ax,
  2556. az: az
  2557. };
  2558. box["gyro"] = {
  2559. gx: -gy,
  2560. gy: -gx,
  2561. gz: gz
  2562. };
  2563. box["min"] = min;
  2564. box["s"] = s;
  2565. box["ms"] = ms;
  2566. //16进制参数
  2567. box["hex"] = resValue;
  2568. //更新数据给webview,在game-play-web||game-play-sub绑定
  2569. //后面更新数据都用此接口
  2570. uni.$emit('updateBLEDeviceData', box);
  2571. }
  2572. //todo 解析跳绳模式
  2573. if (state.bListenerHexUpdate) {
  2574. //报头,固定
  2575. let byte1 = resValue.substr(0, 2);
  2576. //功能码,todo
  2577. let byte2 = resValue.substr(2, 2);
  2578. // 报文类型,后续区分在这里,目前是跳绳模式类型
  2579. let byte3 = resValue.substr(4, 2);
  2580. //报文内容,长度以 2为单位 动态变化
  2581. let byteData = resValue.substr(6, resValue.length - 10)
  2582. //报文按照长度2切割
  2583. let byteArray = byteData.replace(/(\d{2})(?=(?:\d{2})+(?!\d))/g, '$1,').split(',');
  2584. console.log("byteArray:", byteArray);
  2585. // 校验
  2586. let byteVerify = resValue.substr(resValue.length - 4, 2);
  2587. // 报尾,结束
  2588. let byteEnd = resValue.substr(resValue.length - 2, 2);
  2589. //todo 暂时先这样触发
  2590. if (byteArray.length == 2 && byteArray[0] != byteArray[1]) {
  2591. if (byteArray[0] == '00') {
  2592. state.bRopeKeyOne = 1;
  2593. } else {
  2594. //'01'
  2595. state.bRopeKeyTwo = 1;
  2596. }
  2597. if (state.bRopeKeyOne == 1 && state.bRopeKeyTwo == 1) {
  2598. state.bRopeKeyOne = 0;
  2599. state.bRopeKeyTwo = 0;
  2600. box["Rope"] = 1;
  2601. uni.$emit('updateBLEDeviceData', box);
  2602. }
  2603. }
  2604. }
  2605. });
  2606. },
  2607. /**
  2608. * 开启跳绳模式
  2609. */
  2610. B_OpenRopeSkipping() {
  2611. // this.commit('onWriteBLEConnectionValue', {
  2612. // value: "5B310101FE5D",
  2613. // bSendHex: true,
  2614. // bOpen: true
  2615. // });
  2616. console.log('B_OpenRopeSkipping', 3);
  2617. this.commit('onWriteBLEConnectionValue', {
  2618. value: "3"
  2619. });
  2620. },
  2621. /**
  2622. * 关闭跳绳模式
  2623. */
  2624. B_CloseRopeSkipping() {
  2625. // this.commit('onWriteBLEConnectionValue', {
  2626. // value: "5B210100FF5D",
  2627. // bSendHex: true,
  2628. // bOpen: false
  2629. // });
  2630. this.commit('onWriteBLEConnectionValue', {
  2631. value: "4"
  2632. });
  2633. },
  2634. //向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
  2635. B_WriteBLECharacteristicValue(state, context) {
  2636. let {
  2637. value,
  2638. retryCount,
  2639. success,
  2640. getFail,
  2641. bSendHex,
  2642. } = context;
  2643. if (!value && value === '') {
  2644. uni.showToast({
  2645. title: "指令为空"
  2646. })
  2647. return;
  2648. }
  2649. let _sendData;
  2650. if (bSendHex) {
  2651. state.bRopeKeyTwo = 0;
  2652. state.bRopeKeyOne = 0;
  2653. //如果是发送16进制
  2654. state.bListenerHexUpdate = true;
  2655. //重置原 update
  2656. state.bListenerUpdate = false;
  2657. let typedArray = new Uint8Array(value.match(/[\da-f]{2}/gi).map(function(h) {
  2658. return parseInt(h, 16)
  2659. }))
  2660. _sendData = typedArray.buffer;
  2661. } else {
  2662. state.bListenerHexUpdate = false;
  2663. //记录一下指令
  2664. state.currentInstruction = value;
  2665. //V 获取版本,M 获取mac 地址
  2666. if (value == "V")
  2667. state.showToast = true;
  2668. if (value == "M") {
  2669. state.showToast = true;
  2670. state.bListenerMac = true;
  2671. }
  2672. if (value == "4") {
  2673. state.bListenerUpdate = false;
  2674. }
  2675. if (value == "3") {
  2676. if (state.bListenerUpdate) {
  2677. // uni.showToast({
  2678. // title: "原始数据已开启"
  2679. // })
  2680. console.warn("原始数据已开启");
  2681. // return;
  2682. }
  2683. state.bListenerUpdate = true;
  2684. }
  2685. if (value == "6") {
  2686. // state.bListenerJson = false;
  2687. state.bListenerAccArray = false;
  2688. }
  2689. if (value == "5") {
  2690. // if (state.bListenerJson) {
  2691. // uni.showToast({
  2692. // title: "步数数据已开启"
  2693. // })
  2694. // return;
  2695. // }
  2696. // state.bListenerJson = true;
  2697. if (state.bListenerAccArray) {
  2698. uni.showToast({
  2699. title: "加速计组数据已开启"
  2700. })
  2701. return;
  2702. }
  2703. state.bListenerAccArray = true;
  2704. }
  2705. _sendData = BLE.str2ab(value);
  2706. }
  2707. // console.log("currentInstruction:", state.currentInstruction, value, state.bListenerUpdate, state
  2708. // .bListenerMac,
  2709. // state.bListenerHexUpdate);
  2710. // console.log("state.deviceId:", state.deviceId, state.serviceId, state.writeCharacteristicId);
  2711. uni.writeBLECharacteristicValue({
  2712. deviceId: state.deviceId,
  2713. serviceId: state.serviceId,
  2714. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  2715. characteristicId: state.writeCharacteristicId,
  2716. // 这里的value是ArrayBuffer类型
  2717. value: _sendData,
  2718. success: (res) => {
  2719. console.log('writeBLECharacteristicValue success', res.errMsg, value,
  2720. retryCount);
  2721. if (success) {
  2722. success(res);
  2723. }
  2724. },
  2725. fail: (fail) => {
  2726. console.log(fail)
  2727. //重新写入
  2728. if (retryCount > 0) {
  2729. console.log("writeBLECharacteristicValue Rewrite ===================>" +
  2730. value + " == " + retryCount);
  2731. retryCount--;
  2732. setTimeout(() => {
  2733. this.commit("B_WriteBLECharacteristicValue", {
  2734. value,
  2735. retryCount,
  2736. success,
  2737. getFail
  2738. });
  2739. }, 300);
  2740. }
  2741. if (getFail) {
  2742. getFail(fail);
  2743. }
  2744. }
  2745. })
  2746. },
  2747. /**
  2748. * 断开蓝牙连接
  2749. */
  2750. B_CloseBLEConnection(state, context) {
  2751. let {
  2752. deviceId,
  2753. success = null
  2754. } = context;
  2755. uni.closeBLEConnection({
  2756. deviceId,
  2757. success: res => {
  2758. if (success)
  2759. success();
  2760. },
  2761. fail: (fail) => {
  2762. console.log("关闭蓝牙失败", fail);
  2763. }
  2764. });
  2765. },
  2766. //获取手机中蓝牙已匹配的设备
  2767. B_GetBondedDevices(state, context) {
  2768. // console.log("getBondedDevices ====>");
  2769. let {
  2770. success = null,
  2771. deviceId = null
  2772. } = context;
  2773. //Android
  2774. function android_bluetooth_list(deviceName, deviceId, count, callback) {
  2775. console.log("===》调用android_bluetooth_list count:", count);
  2776. var main = plus.android.runtimeMainActivity();
  2777. var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
  2778. var BAdapter = BluetoothAdapter.getDefaultAdapter();
  2779. var Context = plus.android.importClass("android.content.Context");
  2780. var lists = BAdapter.getBondedDevices();
  2781. plus.android.importClass(lists);
  2782. var len = lists.size();
  2783. // var BluetoothManager = plus.android.importClass("android.bluetooth.BluetoothManager");
  2784. // var BluetoothDevice = plus.android.importClass("android.bluetooth.BluetoothDevice");
  2785. // var ProfileType = plus.android.importClass("android.bluetooth.ProfileType");
  2786. // plus.android.importClass(BluetoothManager);
  2787. // plus.android.importClass(BluetoothDevice);
  2788. // plus.android.importClass(ProfileType);
  2789. // var UUID = plus.android.importClass("java.util.UUID");
  2790. // var uuid = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB");
  2791. // var gDevice = BAdapter.getRemoteDevice("C5:5C:19:04:00:30");
  2792. // plus.android.importClass(gDevice);
  2793. // var bluetoothSocket = gDevice.createInsecureRfcommSocketToServiceRecord(uuid);
  2794. // plus.android.importClass(bluetoothSocket);
  2795. // console.log("是否连接:",bluetoothSocket.isConnected());
  2796. // var deviceLists = BluetoothManager.getConnectedDevices();
  2797. // plus.android.importClass(deviceLists);
  2798. // console.log("deviceLists.len=" + deviceLists.size());
  2799. console.log("getBondedDevices.len=", len, "deviceId:", deviceId);
  2800. //测试start===》 如果没有配对的设备,重新获取
  2801. // count--;
  2802. // if(count >0){
  2803. // setTimeout(()=>{
  2804. // android_bluetooth_list("BGBox", deviceId, count, callback);
  2805. // },1000)
  2806. // return;
  2807. // }
  2808. //测试end
  2809. if (len == 0) {
  2810. //如果没有配对的设备,重新获取
  2811. count--;
  2812. if (count > 0) {
  2813. setTimeout(() => {
  2814. android_bluetooth_list("BGBox", deviceId, count, callback);
  2815. }, 2000)
  2816. } else {
  2817. if (callback)
  2818. callback(null);
  2819. }
  2820. } else {
  2821. var iterator = lists.iterator();
  2822. let temp = null;
  2823. plus.android.importClass(iterator);
  2824. while (iterator.hasNext()) {
  2825. var d = iterator.next();
  2826. plus.android.importClass(d);
  2827. // if (d.getName().indexOf(deviceName) > -1)
  2828. // console.log(d.getAddress(), deviceId);
  2829. if (deviceId == null && d.getName().indexOf(deviceName) > -1) {
  2830. let typeEnum = {
  2831. 0: 'Unknown',
  2832. 1: 'Classic',
  2833. 2: 'Le',
  2834. 3: 'Dual'
  2835. };
  2836. let bondStateEnum = {
  2837. 10: 'None',
  2838. 11: 'Bonding',
  2839. 12: 'Bonded'
  2840. };
  2841. temp = {
  2842. name: d.getName(),
  2843. address: d.getAddress(),
  2844. type: {
  2845. 'value': d.getType(),
  2846. 'name': typeEnum[d.getType()]
  2847. },
  2848. bondState: {
  2849. 'value': d.getBondState(),
  2850. 'name': bondStateEnum[d.getBondState()]
  2851. }
  2852. }
  2853. console.log("获取到设备,并且跳出:", temp);
  2854. break;
  2855. } else if (d.getAddress() == deviceId) {
  2856. // console.log(d.getName());
  2857. // console.log(d.getAddress());
  2858. // Classic 1
  2859. // Dual 3
  2860. // Le 2
  2861. // Unknown 0
  2862. // console.log(d.getType());
  2863. // Bonded 12
  2864. // Bonding 11 Indicates bonding (pairing) is in progress with the remote device.
  2865. // None 10
  2866. // console.log(d.getBondState());
  2867. let typeEnum = {
  2868. 0: 'Unknown',
  2869. 1: 'Classic',
  2870. 2: 'Le',
  2871. 3: 'Dual'
  2872. };
  2873. let bondStateEnum = {
  2874. 10: 'None',
  2875. 11: 'Bonding',
  2876. 12: 'Bonded'
  2877. };
  2878. temp = {
  2879. name: d.getName(),
  2880. address: d.getAddress(),
  2881. type: {
  2882. 'value': d.getType(),
  2883. 'name': typeEnum[d.getType()]
  2884. },
  2885. bondState: {
  2886. 'value': d.getBondState(),
  2887. 'name': bondStateEnum[d.getBondState()]
  2888. }
  2889. }
  2890. console.log("获取到设备,并且跳出:", temp);
  2891. break;
  2892. }
  2893. }
  2894. // return temp;
  2895. if (temp == null && count > 0) {
  2896. count--;
  2897. setTimeout(() => {
  2898. android_bluetooth_list("BGBox", deviceId, count, callback);
  2899. }, 2000)
  2900. } else {
  2901. if (callback)
  2902. callback(temp);
  2903. }
  2904. }
  2905. }
  2906. function ios_bluetooth_list(deviceName, callback) {
  2907. //原生操作 首先需要通过 uni.requireNativePlugin("ModuleName") 获取 module
  2908. var SLABluetoothModule = uni.requireNativePlugin("SLABluetoothUniPlugin-SLAModule")
  2909. if (SLABluetoothModule) {
  2910. SLABluetoothModule.getConnectionDevicesAsync({
  2911. serviceUUIDs: "FFF0"
  2912. }, (res) => {
  2913. console.log("***************getConnectionDevicesAsync:", JSON.stringify(res));
  2914. if (res.data) {
  2915. console.log("获取到对应的匹配设备!");
  2916. let typeEnum = {
  2917. 0: 'Unknown',
  2918. 1: 'Classic',
  2919. 2: 'Le',
  2920. 3: 'Dual'
  2921. };
  2922. let bondStateEnum = {
  2923. 10: 'None',
  2924. 11: 'Bonding',
  2925. 12: 'Bonded'
  2926. };
  2927. let temp = {
  2928. name: res.data[0].name,
  2929. address: res.data[0].identifier,
  2930. type: {
  2931. 'value': 2,
  2932. 'name': typeEnum[2]
  2933. },
  2934. bondState: {
  2935. 'value': 12,
  2936. 'name': bondStateEnum[12]
  2937. }
  2938. }
  2939. if (callback) {
  2940. callback({
  2941. "device": temp
  2942. });
  2943. }
  2944. } else {
  2945. console.log("匹配设备数据不存在!");
  2946. if (callback) {
  2947. callback({
  2948. "device": null
  2949. });
  2950. }
  2951. }
  2952. })
  2953. } else {
  2954. console.error("不存在SLABluetoothModule ios 插件!");
  2955. if (callback) {
  2956. callback({
  2957. "device": null
  2958. });
  2959. }
  2960. }
  2961. }
  2962. let device = null;
  2963. let _count = 3;
  2964. switch (plus.os.name) {
  2965. case "Android":
  2966. // Android平台: plus.android.*
  2967. android_bluetooth_list("BGBox", deviceId, _count, (data) => {
  2968. device = data;
  2969. console.log("android getBondedDevices 匹配的对象======>", device)
  2970. if (device != null) {
  2971. state.bPhoneMatched = true;
  2972. } else {
  2973. state.bPhoneMatched = false;
  2974. }
  2975. if (success) {
  2976. success(device);
  2977. }
  2978. });
  2979. break;
  2980. case "iOS":
  2981. // iOS平台: plus.ios.*
  2982. ios_bluetooth_list("BGBox", (res) => {
  2983. device = res.device
  2984. if (success) {
  2985. success(device);
  2986. }
  2987. console.log("ios getBondedDevices 匹配的对象======>", device)
  2988. if (device != null) {
  2989. state.bPhoneMatched = true;
  2990. } else {
  2991. state.bPhoneMatched = false;
  2992. }
  2993. });
  2994. break;
  2995. default:
  2996. // 其它平台
  2997. if (success) {
  2998. success(device);
  2999. }
  3000. break;
  3001. }
  3002. // if (success) {
  3003. // success(device);
  3004. // }
  3005. },
  3006. B_OpenBLESetting() {
  3007. function android_bluetooth_setting() {
  3008. var main = plus.android.runtimeMainActivity(); //获取activity
  3009. var Intent = plus.android.importClass('android.content.Intent');
  3010. var Settings = plus.android.importClass('android.provider.Settings');
  3011. var intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); //可设置表中所有Action字段
  3012. main.startActivity(intent);
  3013. }
  3014. function ios_bluetooth_setting() {
  3015. // var UIApplication = plus.ios.import("UIApplication");
  3016. // var application2 = UIApplication.sharedApplication();
  3017. // var NSURL2 = plus.ios.import("NSURL");
  3018. // var setting2 = NSURL2.URLWithString("app-settings:");
  3019. // application2.openURL(setting2);
  3020. // plus.ios.deleteObject(setting2);
  3021. // plus.ios.deleteObject(NSURL2);
  3022. // plus.ios.deleteObject(application2);
  3023. // plus.runtime.openURL({
  3024. // action: 'App-Prefs:root=Bluetooth'
  3025. // }, function(e) {
  3026. // console.log(JSON.stringify(e));
  3027. // });
  3028. plus.runtime.launchApplication({
  3029. action: 'App-Prefs:root=Bluetooth'
  3030. }, function(e) {
  3031. console.log(JSON.stringify(e));
  3032. });
  3033. }
  3034. let device = null;
  3035. switch (plus.os.name) {
  3036. case "Android":
  3037. // Android平台: plus.android.*
  3038. android_bluetooth_setting();
  3039. break;
  3040. case "iOS":
  3041. // iOS平台: plus.ios.*
  3042. ios_bluetooth_setting();
  3043. break;
  3044. default:
  3045. // 其它平台
  3046. break;
  3047. }
  3048. },
  3049. //获取versionCode状态,用于处理特定的信息显示
  3050. getVersionCode(state) {
  3051. let self = this;
  3052. console.log("state.versionCode:" + state.versionCode);
  3053. reqUtil.requestData(config.URL.GETVERSIONCODE, {
  3054. code: state.versionCode
  3055. }).then(res => {
  3056. console.log('获取版本控制信息 =====' + JSON.stringify(res));
  3057. if (res.code == 0) {
  3058. state.versionCodeState = Object.assign({}, state.versionCodeState, res.data);
  3059. /**
  3060. * 初始化配置表方案
  3061. * 搞一个区分平台上线显示等,根据版本版本号,是否显示,平台限制等区别
  3062. */
  3063. //显示,隐藏游戏栏目
  3064. uni.setTabBarItem({
  3065. index: 2,
  3066. visible: state.versionCodeState.showGame
  3067. })
  3068. //显示,隐藏视频栏目
  3069. uni.setTabBarItem({
  3070. index: 3,
  3071. visible: state.versionCodeState.showVideo
  3072. })
  3073. }
  3074. },
  3075. e => {
  3076. console.log(e)
  3077. });
  3078. },
  3079. /**
  3080. * 任务其他操作
  3081. */
  3082. //获取关卡信息
  3083. getLevelList(state, callback) {
  3084. let self = this;
  3085. reqUtil
  3086. .requestData(config.URL.GETLEVELLIST, {}) //GETLEVELJUMPLIST
  3087. .then(
  3088. res => {
  3089. if (res.code == 0) {
  3090. // state.singlePersonList = res.data.singlePerson;
  3091. // state.multiPersonList = res.data.multiPerson;
  3092. // console.log(res.data);
  3093. let levels = res.data;
  3094. state.levels = levels;
  3095. if (1 === state.currentModeIndex) {
  3096. //关卡是跳绳
  3097. state.singlePersonList = levels.singlePersonMap[0];
  3098. state.multiPersonList = levels.multiPersonMap[0];
  3099. } else if (0 === state.currentModeIndex) {
  3100. //关卡是拳击
  3101. state.singlePersonList = levels.singlePersonMap[1];
  3102. state.multiPersonList = levels.multiPersonMap[1];
  3103. }
  3104. //获取到数据时候,重置actionJump
  3105. self.commit('resetActionJumpTask');
  3106. if (callback)
  3107. callback();
  3108. }
  3109. },
  3110. e => {
  3111. console.log(e);
  3112. }
  3113. );
  3114. },
  3115. //切换关卡列表信息K
  3116. switchLevelList(state, _levelType) {
  3117. // console.log( state.levels.multiPersonMap)
  3118. // console.log( state.levels.singlePersonMap)
  3119. if (state.levels.singlePersonMap && state.levels.singlePersonMap.length >= _levelType)
  3120. state.singlePersonList = state.levels.singlePersonMap[_levelType - 1];
  3121. else
  3122. state.singlePersonList = [];
  3123. if (state.levels.multiPersonMap && state.levels.multiPersonMap.length >= _levelType)
  3124. state.multiPersonList = state.levels.multiPersonMap[_levelType - 1];
  3125. else
  3126. state.multiPersonList = [];
  3127. if (1 === _levelType) {
  3128. //关卡是跳绳
  3129. state.currentModeIndex = 1;
  3130. } else if (2 === _levelType) {
  3131. //关卡是拳击
  3132. state.currentModeIndex = 0;
  3133. }
  3134. // console.log(state.currentModeIndex)
  3135. },
  3136. /**
  3137. * 获取钱包信息
  3138. * @param {Object} state
  3139. * @param {Object} callback
  3140. */
  3141. getUserWallets(state, callback) {
  3142. reqUtil
  3143. .requestData(config.URL.USERGETWALLETINFO, {})
  3144. .then(
  3145. res => {
  3146. if (res.code == 0) {
  3147. console.log('USERGETWALLETINFO:' + JSON.stringify(res.data));
  3148. state.cGold = res.data.gold;
  3149. state.cDiamond = res.data.diamond;
  3150. if (callback)
  3151. callback();
  3152. }
  3153. },
  3154. e => {
  3155. console.log(e);
  3156. }
  3157. );
  3158. },
  3159. setGoldAndDiamond(state, context) {
  3160. let {
  3161. gold,
  3162. diamond
  3163. } = context;
  3164. state.cGold = gold;
  3165. state.cDiamond = diamond;
  3166. },
  3167. /**
  3168. * 抽屉和弹出框
  3169. */
  3170. hideDrawerById(state, context) {
  3171. // let { id } = context;
  3172. //personal-drawer
  3173. uni.getSubNVueById('personal-drawer').hide('slide-in-left', 200);
  3174. },
  3175. showDrawerById(state, context) {
  3176. // let { id } = context;
  3177. //personal-drawer
  3178. uni.getSubNVueById('personal-drawer').show('slide-in-left', 200);
  3179. },
  3180. closeDrawerById(state, context) {
  3181. const subNVue = uni.getSubNVueById('personal-drawer');
  3182. subNVue.close();
  3183. console.log('销毁侧滑栏子窗体');
  3184. },
  3185. showPopupById(state, context) {
  3186. let {
  3187. type,
  3188. item
  3189. } = context;
  3190. console.log(type, item);
  3191. switch (type) {
  3192. case 'levelReward':
  3193. uni.$emit('reward-popup', {
  3194. title: '奖励',
  3195. content: '',
  3196. type: type,
  3197. item: {
  3198. gold: item.rewardGold,
  3199. diamond: item.rewardDiamond,
  3200. honor: item.rewardHonor
  3201. },
  3202. operationItem: item
  3203. });
  3204. break;
  3205. case 'prompt':
  3206. // 向 popup 传递消息
  3207. uni.$emit('reward-popup', {
  3208. title: '奖励',
  3209. content: '',
  3210. type: type,
  3211. item: {
  3212. gold: item.rewardGold,
  3213. diamond: item.rewardDiamond,
  3214. honor: item.rewardHonor
  3215. },
  3216. operationItem: item
  3217. });
  3218. break;
  3219. case 'unlock':
  3220. // 向 popup 传递消息
  3221. uni.$emit('reward-popup', {
  3222. title: '解锁关卡',
  3223. content: '',
  3224. type: type,
  3225. item: {
  3226. consumeGold: item.consumeGold,
  3227. consumeDiamond: item.consumeDiamond
  3228. },
  3229. operationItem: item
  3230. });
  3231. break;
  3232. case 'signIn':
  3233. this.commit("onSendSignInPopup", context);
  3234. break;
  3235. case 'pkFinish':
  3236. // 向 popup 传递消息
  3237. uni.$emit('reward-popup', {
  3238. title: 'pk完成',
  3239. content: '',
  3240. type: type,
  3241. item: item,
  3242. operationItem: item
  3243. });
  3244. break;
  3245. default:
  3246. uni.showToast({
  3247. title: 'showPopup类型为空',
  3248. icon: 'none'
  3249. })
  3250. break;
  3251. }
  3252. // #ifdef APP-PLUS
  3253. //personal-popup
  3254. const subNVue = uni.getSubNVueById('personal-popup')
  3255. subNVue.show('zoom-out', 250)
  3256. // #endif
  3257. },
  3258. closePopupById(state, context) {
  3259. const subNVue = uni.getSubNVueById('personal-popup')
  3260. subNVue.close();
  3261. console.log('销毁奖励子窗体');
  3262. },
  3263. showGuideById(state, context) {
  3264. let {
  3265. type,
  3266. item
  3267. } = context;
  3268. uni.$emit('guide-popup', {
  3269. title: 'guide',
  3270. content: '定制guide',
  3271. type: type,
  3272. item: item,
  3273. operationItem: item
  3274. });
  3275. const subNVue = uni.getSubNVueById('personal-guide')
  3276. subNVue.show('fade-in', 250)
  3277. },
  3278. closeGuideById() {
  3279. const subNVue = uni.getSubNVueById('personal-guide')
  3280. subNVue.close();
  3281. console.log('销毁新手指引子窗体');
  3282. },
  3283. /**
  3284. * 获取签到列表
  3285. * @param {Object} state
  3286. * @param {Object} callback
  3287. */
  3288. getSignInList(state, callback) {
  3289. reqUtil
  3290. .requestData(config.URL.GETSIGNINLIST, {})
  3291. .then(
  3292. res => {
  3293. if (res.code == 0) {
  3294. // console.log('GETSIGNINLIST:' + JSON.stringify(res.data));
  3295. state.signInList = res.data.signInList;
  3296. state.isSignIn = res.data.isSignIn;
  3297. if (callback)
  3298. callback(res.data);
  3299. }
  3300. },
  3301. e => {
  3302. console.log(e);
  3303. }
  3304. );
  3305. },
  3306. /**
  3307. * 签到
  3308. * @param {Object} state
  3309. * @param {Object} callback
  3310. */
  3311. onUserSignIn(state, callback) {
  3312. reqUtil
  3313. .requestData(config.URL.USERSIGNIN, {})
  3314. .then(
  3315. res => {
  3316. console.log('USERSIGNIN:' + JSON.stringify(res));
  3317. if (res.code == 0) {
  3318. state.cGold = res.data.userGold;
  3319. state.cDiamond = res.data.userDiamond;
  3320. uni.showToast({
  3321. title: '签到成功',
  3322. mask: true
  3323. })
  3324. //根据dayType 计算最近一个flag
  3325. for (let i = 0; i < state.signInList.length; i++) {
  3326. if (res.data.signIn.dayType == state.signInList[i].dayType) {
  3327. state.signInList[i] = Object.assign({}, state.signInList[i], res.data
  3328. .signIn);
  3329. break;
  3330. }
  3331. }
  3332. state.isSignIn = true;
  3333. if (callback)
  3334. callback(res.data);
  3335. } else if (res.code == 901) {
  3336. uni.showToast({
  3337. title: '今天已签到',
  3338. mask: true
  3339. })
  3340. }
  3341. },
  3342. e => {
  3343. console.log(e);
  3344. }
  3345. );
  3346. },
  3347. onSendSignInPopup(state, context) {
  3348. let {
  3349. type,
  3350. item
  3351. } = context;
  3352. let _reward = [{
  3353. name: 'gold',
  3354. width: 28,
  3355. height: 28,
  3356. url: '../../../static/common/sideBar/sGold.png',
  3357. selectedUrl: '../../../static/common/subNVue/sign/sign-grey-gold@2x.png',
  3358. explain: '',
  3359. value: 20,
  3360. isSelect: false
  3361. },
  3362. {
  3363. name: 'diamond',
  3364. width: 28,
  3365. height: 28,
  3366. url: '../../../static/common/sideBar/sDiamond.png',
  3367. selectedUrl: '../../../static/common/subNVue/sign/sign-grey-diamond@2x.png',
  3368. explain: '',
  3369. value: 20,
  3370. isSelect: false
  3371. }
  3372. ];
  3373. let list = state.signInList;
  3374. let lists = [];
  3375. for (let i = 0; i < list.length; i++) {
  3376. if (0 !== list[i].gold) {
  3377. lists.push(Object.assign({}, _reward[0], {
  3378. value: list[i].gold
  3379. }))
  3380. } else if (0 !== list[i].diamond) {
  3381. lists.push(Object.assign({}, _reward[1], {
  3382. value: list[i].diamond
  3383. }))
  3384. }
  3385. lists[i].isSelect = list[i].flag == 1 ? true : false;
  3386. }
  3387. // console.log("list:"+JSON.stringify(list));
  3388. // 向 popup 传递消息
  3389. uni.$emit('reward-popup', {
  3390. title: '每日签到',
  3391. content: '',
  3392. type: type,
  3393. lists: lists,
  3394. isSignIn: state.isSignIn
  3395. });
  3396. },
  3397. /**
  3398. * 记录沙袋打击计数
  3399. * @param {Object} state
  3400. * @param {Object} count
  3401. */
  3402. setSandbagHitCount(state, count) {
  3403. // uni.setStorageSync('sandbagHitCount', count);
  3404. state.sandbagHitCount = count;
  3405. },
  3406. addSandbagHitCount(state, context) {
  3407. let {
  3408. count
  3409. } = context;
  3410. let addCount = Number(state.sandbagHitCount) + Number(count);
  3411. // console.log("addCount:" + addCount);
  3412. state.sandbagHitCount = addCount;
  3413. uni.setStorageSync('sandbagHitCount', state.sandbagHitCount);
  3414. },
  3415. getSandbagHitCount(state) {
  3416. const value = uni.getStorageSync('sandbagHitCount');
  3417. if (value) {
  3418. state.sandbagHitCount = value;
  3419. } else {
  3420. state.sandbagHitCount = 0;
  3421. }
  3422. },
  3423. /**
  3424. * 获取用户等hitcount数据
  3425. * 同步沙袋计数
  3426. * @param {Object} state
  3427. */
  3428. getUserData_hitCount(state) {
  3429. //获取本地数据
  3430. this.commit("getSandbagHitCount");
  3431. reqUtil
  3432. .requestData(config.URL.USERDATA_GETHITCOUNT, {}, 'POST')
  3433. .then(
  3434. res => {
  3435. console.log('USERDATA_GETHITCOUNT:' + JSON.stringify(res));
  3436. if (res.code == 0) {
  3437. if (res.data.isReset) {
  3438. this.commit("setSandbagHitCount", 0);
  3439. } else {
  3440. if (res.data.hitCount > state.sandbagHitCount) {
  3441. this.commit("setSandbagHitCount", res.data.hitCount);
  3442. state.netSandbagHitCount = res.data.hitCount;
  3443. } else if (res.data.hitCount < state.sandbagHitCount) {
  3444. //获取时候,小于本地记录的,则同步到网络
  3445. this.commit("uploadUserData_hitCount");
  3446. }
  3447. }
  3448. }
  3449. },
  3450. e => {
  3451. console.log(e);
  3452. }
  3453. );
  3454. },
  3455. uploadUserData_hitCount(state) {
  3456. //如果拳击数没变化,则不用上传
  3457. if (state.netSandbagHitCount == state.sandbagHitCount) return;
  3458. reqUtil
  3459. .requestData(config.URL.USERDATA_UPLOADHITCOUNT, {
  3460. hitCount: state.sandbagHitCount
  3461. }, 'POST')
  3462. .then(
  3463. res => {
  3464. console.log('USERDATA_UPLOADHITCOUNT:' + JSON.stringify(res));
  3465. if (res.code == 0) {
  3466. this.commit("setSandbagHitCount", res.data.hitCount)
  3467. state.netSandbagHitCount = res.data.hitCount;
  3468. }
  3469. },
  3470. e => {
  3471. console.log(e);
  3472. }
  3473. );
  3474. },
  3475. onConnectSocket(state) {
  3476. // let self = this;
  3477. // uni.connectSocket({
  3478. // // #ifdef APP-PLUS
  3479. // url: 'wss://www.9527fun.cn/api_dev/websocket/APPLog/123'
  3480. // // #endif
  3481. // // #ifdef H5
  3482. // // url:'wss://www.9527fun.cn/api_dev/websocket/APPLog/123'
  3483. // url: 'ws://192.168.0.108:9090/api_dev/websocket/APPLog/123'
  3484. // // #endif
  3485. // });
  3486. // uni.onSocketMessage(function(res) {
  3487. // console.log('收到服务器内容:' + res.data);
  3488. // });
  3489. // uni.onSocketOpen(function(res) {
  3490. // console.log('WebSocket连接已打开!');
  3491. // });
  3492. // uni.onSocketError(function(res) {
  3493. // console.log('WebSocket连接打开失败,请检查!');
  3494. // });
  3495. },
  3496. onCloseSocket(state) {
  3497. // uni.onSocketClose(function(res) {
  3498. // console.log('WebSocket 已关闭!');
  3499. // });
  3500. },
  3501. onSendSocketMessage(state, msg) {
  3502. // console.log("发送数据", msg);
  3503. // uni.sendSocketMessage({
  3504. // data: JSON.stringify(msg)
  3505. // });
  3506. },
  3507. //jumpIdenModule 算法插件 start
  3508. jumpIdenModule_SearchData(state, context) {
  3509. if (uni.getSystemInfoSync().platform == "ios") {
  3510. return;
  3511. }
  3512. state.filesList = [];
  3513. let {
  3514. data,
  3515. callback = null
  3516. } = context;
  3517. let url = "http://42.192.165.168:2022/search";
  3518. let _temp = {
  3519. name: state.readDataName
  3520. }
  3521. console.log('requestData search ===== url:' + url);
  3522. reqUtil.requestData(url, _temp,'POST').then(res => {
  3523. let strArray = res.split('\n');
  3524. console.log(strArray);
  3525. for (let i = 0; i < strArray.length-1; i++) {
  3526. let _name = strArray[i].split('.')[0];
  3527. if (_name.Length <= 0) continue;
  3528. let item = Object.assign({}, {
  3529. cname:_name,
  3530. bRatio: false
  3531. });
  3532. state.filesList.push(item);
  3533. }
  3534. uni.showToast({
  3535. title: '获取列表成功!',
  3536. icon: 'none',
  3537. duration: 1000,
  3538. mask: true
  3539. })
  3540. },
  3541. e => {
  3542. console.log(e)
  3543. uni.showToast({
  3544. title: '请求失败!',
  3545. icon: 'none',
  3546. duration: 1000,
  3547. mask: true
  3548. })
  3549. });
  3550. },
  3551. jumpIdenModule_DownloadReadData(state,context) {
  3552. if (uni.getSystemInfoSync().platform == "ios") {
  3553. return;
  3554. }
  3555. let {
  3556. data,
  3557. callback = null
  3558. } = context;
  3559. let downloadUrl = "http://42.192.165.168:2022/download";
  3560. console.log('requestData downloadUrl ===== url:' + downloadUrl);
  3561. reqUtil.requestData(downloadUrl, {name:data.cname},'POST').then(res => {
  3562. //console.log(res);
  3563. uni.setStorageSync('readData', res);
  3564. if(callback)
  3565. callback(res);
  3566. //读取的数据传入android端
  3567. let _temp = jumpIdenModule.onJumpInitInputData({
  3568. data: res
  3569. })
  3570. uni.showToast({
  3571. title: '' + _temp.msg,
  3572. icon: 'none',
  3573. duration: 1000,
  3574. mask: true
  3575. })
  3576. },
  3577. e => {
  3578. console.log(e)
  3579. uni.showToast({
  3580. title: '请求失败!',
  3581. icon: 'none',
  3582. duration: 1000,
  3583. mask: true
  3584. })
  3585. });
  3586. },
  3587. jumpIdenModule_onJumpInit() {
  3588. if (uni.getSystemInfoSync().platform == "ios") {
  3589. return;
  3590. }
  3591. let res = jumpIdenModule.onJumpInit();
  3592. uni.showToast({
  3593. title: ''+ res.code,
  3594. icon: 'none',
  3595. duration: 1000,
  3596. mask: true
  3597. })
  3598. },
  3599. jumpIdenModule_onJumpInitLocalData(){
  3600. if (uni.getSystemInfoSync().platform == "ios") {
  3601. return;
  3602. }
  3603. let res = jumpIdenModule.onJumpInitLocalData();
  3604. uni.showToast({
  3605. title: ''+ res.code,
  3606. icon: 'none',
  3607. duration: 1000,
  3608. mask: true
  3609. })
  3610. },
  3611. jumpIdenModule_onTestJump6AxisDataUpdate() {
  3612. let box = {};
  3613. box["acc"] = {
  3614. ax: 11,
  3615. ay: 22,
  3616. az: 33,
  3617. };
  3618. box["gyro"] = {
  3619. gx: 111,
  3620. gy: 222,
  3621. gz: 333,
  3622. };
  3623. box["ms"] = 33;
  3624. jumpIdenModule.onJump6AxisDataUpdate(box)
  3625. },
  3626. jumpIdenModule_onTestUpdate() {
  3627. jumpIdenModule.onTestUpdate();
  3628. },
  3629. jumpIdenModule_onJump6AxisDataUpdate(state,context){
  3630. let {
  3631. data,
  3632. callback = null
  3633. } = context;
  3634. let res = jumpIdenModule.onJump6AxisDataUpdate({hex:data.hex});
  3635. if(callback)
  3636. callback(res);
  3637. },
  3638. onGetReadDataName(state){
  3639. state.readDataName = uni.getStorageSync('readDataName');
  3640. },
  3641. onSetReadDataName(state, value) {
  3642. uni.setStorageSync('readDataName', value);
  3643. state.readDataName = value;
  3644. },
  3645. //jumpIdenModule 算法插件 end
  3646. }
  3647. })
  3648. export default store