store.js 89 KB

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