store.js 99 KB

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