store.js 89 KB

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