store.js 91 KB

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