const user = { state: { identity: uni.getStorageSync('identity')?uni.getStorageSync('identity'):'理论教练', vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: ''}, vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {user:{}}, vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {}, vuex_TenantId: uni.getStorageSync('vuex_TenantId') ? uni.getStorageSync('vuex_TenantId') : '', vuex_role: { // schoolManager manager: '校长', schoolFinance: '驾校财务', coach: '实操教练', examSiteCoach: '考场模拟教练', bookingSimulationTeacher: '模拟器老师' }, apiOk: true }, mutations: { // 更新用户身份 upDateIdentity(state, val) { state.identity = val uni.setStorageSync('identity', val); }, update_vuex_cityInfo(state, payload) { state.vuex_cityInfo = payload uni.setStorageSync('vuex_cityInfo', payload); }, update_vuex_loginInfo(state, payload) { state.vuex_loginInfo = payload uni.setStorageSync('vuex_loginInfo', payload); }, update_vuex_userInfo(state, payload) { state.vuex_userInfo = payload uni.setStorageSync('vuex_userInfo', payload); }, upDateTenantId(state, val) { state.vuex_TenantId = val uni.setStorageSync('vuex_TenantId', val); uni.$u.http.setConfig((config) => { config.header['tenant-id'] = val return config }) }, update_apiOk(state, payload) { state.apiOk = payload }, goLogin(state) { uni.clearStorageSync() state.vuex_loginInfo = {} state.vuex_userInfo = {} state.vuex_TenantId = '' uni.navigateTo({ url: '/pages/login/login' }) } }, actions: { // 获取用户信息 async getUserInfo({commit}) { const http = uni.$u.http let config = { custom: { auth: false }, header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'} } const res = await http.post('business/coach/getCoachInfo',config) commit('update_vuex_userInfo',res.data) }, // 刷新token async refreshToken({state, commit}) { return new Promise(async(resolve, reject)=>{ if(!state.apiOk) { return state.refreshTokenFn } commit('update_apiOk',false) state.refreshTokenFn = null const http = uni.$u.http let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } } let refreshToken = state.vuex_loginInfo.refreshToken state.refreshTokenFn = await http.post('system/auth/refresh-token?refreshToken='+ refreshToken, config) if(state.refreshTokenFn.data||state.refreshTokenFn.data.accessToken) { commit('update_apiOk',true) commit('update_vuex_loginInfo',state.refreshTokenFn.data) uni.$u.http.setConfig((config) => { config.header.Authorization = 'Bearer ' + state.refreshTokenFn.data.accessToken return config }) resolve(state.refreshTokenFn) }else { commit('update_apiOk',true) commit('goLogin') reject('刷新token失败了') } }) }, getCity({commit}) { return new Promise((resolve, reject) => { // #ifdef APP-PLUS getCityInfo(resolve, reject,commit) // #endif // #ifdef H5 console.log('h5经纬度的代码') uni.getLocation({ type: 'wgs84', // type: 'gcj02', success: function(res) { console.log('当前位置的经度:' + res.longitude); // console.log('当前位置的纬度:' + res); gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,); } }) // #endif }) }, } } function getCityInfo(resolve, reject, commit) { // import { requestSingleFreshLocation } from '@/common/js/qqLatLng.js' let qqLatLng = require('@/common/js/qqLatLng.js') try{ uni.showLoading({ title: '正在更新位置...' }) qqLatLng.requestSingleFreshLocation().then(res=>{ let result = res.location if(result.latitude===0) { console.log('gogogo来了吗没有获取到经纬度?') // openGps() uni.getLocation({ // type: 'wgs84', type: 'gcj02', success: function(res) { console.log('只为弹出权限当前位置的经度:' + res.longitude); // console.log('当前位置的纬度:' + res); getCityInfo(resolve, reject, commit) uni.hideLoading() }, fail() { uni.showToast({ title: '您的定位权限已关闭,请手动开启定位权限', icon: 'none' }) uni.hideLoading() } }) }else { console.log('腾讯云经纬度') console.log(result) let obj = { lat: result.latitude, lng: result.longitude, city: result.city, // cityCode: result.code||result.cityCode, province: result.province, district: result.district, name: result.name, } commit('update_vuex_cityInfo', obj) resolve(obj) uni.hideLoading() } }) }catch(e){ console.log('来到这是里了吗?腾讯云经纬度了') uni.hideLoading() uni.getLocation({ // type: 'wgs84', type: 'gcj02', success: function(res) { console.log('当前位置的经度:' + res.longitude); // console.log('当前位置的纬度:' + res); gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,); }, fail(e) { console.log(e) uni.hideLoading() } }) } } // h5高德地图 function gaoDeFn(longitude,latitude,commit,resolve, reject,) { uni.showLoading({ title: '正在更新位置...' }) uni.request({ method: 'GET', url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', data: { key: 'a0dde4c05390e689ea2c19d8ec447f62', location: `${longitude},${latitude}`, output: 'JSON' }, success: (res) => { console.log(res) // 数据结构见下方 let result = res.data.regeocode.addressComponent let obj = { lat: latitude, lng: longitude, city: result.city, cityCode: result.adcode, province: result.province, district: result.district } commit('update_vuex_cityInfo', obj) console.log('h5获得的最终位置对象') console.log(obj) resolve(obj) uni.hideLoading() }, fail: res => { reject(new Error('获取地理位置信息失败')) uni.hideLoading() } }) } export default user