store.js 91 KB

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