store.js 93 KB

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