store.js 91 KB

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