|
|
import { httpPrefix } from '../../config/site.config.js'; // #ifdef APP-PLUS
import { requestSingleFreshLocation } from '@/common/js/qqLatLng.js' // #endif
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 = {} const pages = getCurrentPages(); const page = pages[pages.length - 1].$page.fullPath; //完整路由地址
let url = '/pages/userCenter/login/login' if(page!= url) { console.log('当前页面') console.log(page) setTimeout(()=>{ uni.navigateTo({ url: url+'?redirect='+ page }) },500) } }, // 为非平台学员开一条路
updateNonPlatformStudent(state, val) { state.NonPlatformStudent = val } }, actions: { 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
}) }, 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) console.log('h5获得的最终位置对象') console.log(obj) resolve(obj) uni.hideLoading() }, fail: res => { reject(new Error('获取地理位置信息失败')) uni.hideLoading() } }) }
function getCityInfo(resolve, reject, commit) { try{ 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);
// },
// })
}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, } commit('update_vuex_cityInfo', obj) resolve(obj) } }) }catch(e){ console.log('来到这是里了吗?腾讯云经纬度了') 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() } }) } }
function openGps(){ plus.android.requestPermissions( ['android.permission.ACCESS_FINE_LOCATION'], function(resultObj) { var result = 0; for (var i = 0; i < resultObj.granted.length; i++) { var grantedPermission = resultObj.granted[i]; console.log('已获取的权限:' + grantedPermission); result = 1 } for (var i = 0; i < resultObj.deniedPresent.length; i++) { var deniedPresentPermission = resultObj.deniedPresent[i]; console.log('拒绝本次申请的权限:' + deniedPresentPermission); result = 0 } for (var i = 0; i < resultObj.deniedAlways.length; i++) { var deniedAlwaysPermission = resultObj.deniedAlways[i]; console.log('永久拒绝申请的权限:' + deniedAlwaysPermission); result = -1 } // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
if (result != 1) { //如果用户第一次拒绝后,跳转到**应用**的权限页面
var Intent = plus.android.importClass("android.content.Intent"); var Settings = plus.android.importClass("android.provider.Settings"); var Uri = plus.android.importClass("android.net.Uri"); var mainActivity = plus.android.runtimeMainActivity(); var intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); var uri = Uri.fromParts("package", mainActivity.getPackageName(), null); intent.setData(uri); mainActivity.startActivity(intent); } }, function(error) { console.log('申请权限错误:' + error.code + " = " + error.message); resolve({ code: error.code, message: error.message }); } ); }
|