|
|
import qqmapWx from '../../common/sdk/qqmap-wx-jssdk.min.js'; // 引入
import { httpPrefix } from '../../config/site.config.js';
let apiOk =true let refreshTokenFn = null
const user = { state: { vuex_cityInfo: uni.getStorageSync('vuex_cityInfo') ? uni.getStorageSync('vuex_cityInfo') : {city: '杭州市'}, vuex_userInfo: uni.getStorageSync('vuex_userInfo') ? uni.getStorageSync('vuex_userInfo') : {}, vuex_loginInfo: uni.getStorageSync('vuex_loginInfo') ? uni.getStorageSync('vuex_loginInfo') : {}, apiOk: true, NonPlatformStudent: false, }, mutations: { 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); }, update_apiOk(state, payload) { state.apiOk = payload }, goLogin(state) { uni.clearStorageSync() state.vuex_loginInfo = {} state.vuex_userInfo = {} uni.navigateTo({ url: '/pages/userCenter/login/login' }) }, // 为非平台学员开一条路
updateNonPlatformStudent(state, val) { state.NonPlatformStudent = val } }, actions: { getCity({commit}) { return new Promise((resolve, reject) => { // #ifdef APP-PLUS||H5
console.log('h5经纬度的代码') getCityInfo(resolve, reject,commit) // #endif
// #ifdef MP-WEIXIN
uni.authorize({ scope: 'scope.userLocation', success() { getCityInfo(resolve, reject,commit) }, fail: function(res4) { uni.showModal({ title: '提示', content: '小程序想要获取您的地里位置', success: function(res) { if (res.confirm) { uni.openSetting({ success(res) { getCityInfo(resolve, reject,commit) } }); } else if (res.cancel) { console.log('用户点击取消'); } } }) }, }) // #endif
}) }, checkLogin({state, commit}) { let token = state.vuex_loginInfo.accessToken if(!token) { commit('goLogin') return false } return true }, // 获取用户信息
async getUserInfo({state, commit}) { let token = state.vuex_loginInfo.accessToken if(!token) return 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.get('app/student-record/get') if(res.tenantId) { uni.$u.http.setConfig((config) => { config.header['tenant-id'] = res.tenantId return config }) } commit('update_vuex_userInfo',res.data) }, // 刷新token
async refreshToken({state, commit}) { return new Promise(async(resolve, reject)=>{ if(!apiOk&&refreshTokenFn) { return refreshTokenFn } apiOk = false 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 refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config) if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) { apiOk = true commit('update_vuex_loginInfo', refreshTokenFn.data) uni.$u.http.setConfig((config) => { config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken return config }) resolve(refreshTokenFn) }else { apiOk = true commit('goLogin') reject('刷新token失败了') } }) } } }
export default user
// h5高德地图
function gaoDeFn(longitude,latitude,commit,resolve, reject,) { 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) resolve(obj) uni.hideLoading() }, fail: res => { reject(new Error('获取地理位置信息失败')) uni.hideLoading() } }) }
function wxAdsFn(longitude,latitude, commit,resolve, reject,) { console.log(longitude) console.log(latitude) var qqmapKey = new qqmapWx({ key: 'NRWBZ-TKRWV-CSAPH-5PFDS-J4HT6-IWF4O' }) qqmapKey.reverseGeocoder({ location: { latitude: latitude, longitude:longitude }, success(res2) { console.log('城市信息') console.log(res2) let result = res2.result let obj = { lat: latitude, lng: longitude, city: result.address_component.city, cityCode: result.ad_info.adcode, province: result.address_component.province, district: result.address_component.district } console.log(obj) commit('update_vuex_cityInfo', obj) resolve(obj) uni.hideLoading(); }, fail: function(res3) { console.log('出什么问题了?') console.log(res3) let obj = { lat: latitude, lng: longitude } // reject(res3)
commit('update_vuex_cityInfo', obj) uni.hideLoading(); }, }) }
function getCityInfo(resolve, reject, commit) { // uni.showLoading({
// title: '加载中...'
// })
console.log('来到这是里了吗?') uni.getLocation({ type: 'wgs84', // type: 'gcj02',
success: function(res) { console.log('当前位置的经度:' + res.longitude); // console.log('当前位置的纬度:' + res);
// #ifdef H5
gaoDeFn(res.longitude,res.latitude, commit, resolve, reject,); // #endif
// #ifdef MP-WEIXIN
wxAdsFn(res.longitude,res.latitude, commit,resolve, reject,) // #endif
}, fail(e) { console.log(e) uni.hideLoading() } }) }
|