星油H5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

147 lines
4.1 KiB

import { defineStore } from 'pinia'
import { getToken, setToken, removeToken } from '@/utils/auth'
import oilSite from '@/api/oil-site'
import serve from 'api/login.js'
import { Notify } from 'vant'
import encode from '@/utils/encode'
import initAMap from '@/utils/amap.js'
import Vue from 'vue'
const instance = defineStore('user', {
state: () => {
return {
username: '',
avatar: '',
token: '',
user: null,
position: {
latitude: '',
longitude: ''
}
}
},
actions: {
getPosition() {
this.position = { latitude: 36.636753, longitude: 119.11914 }
if (navigator?.geolocation?.getCurrentPosition) {
console.log('存在原生定位')
navigator.geolocation.getCurrentPosition(position => {
console.log('原生定位', position)
})
} else {
console.log('原生定位不可用')
}
// return
initAMap().then(() => {
// console.log(3)
let AMap = Vue.prototype.$AMap
let geolocation = new AMap.Geolocation({
// enableHighAccuracy: true // 是否使用高精度定位,默认:true
})
geolocation.getCurrentPosition(function (status, result) {
console.log('geolocation.getCurrentPosition')
console.log('status', status)
console.log('result', result)
// if (status === 'complete') {
// let { lat: latitude, lng: longitude } = result.position
// this.position = { latitude, longitude }
// console.log('this.position', this.position)
// }
})
})
return
if (navigator?.geolocation?.getCurrentPosition) {
navigator.geolocation.getCurrentPosition(
position => {
console.log('position.coords', position.coords)
let { latitude, longitude } = position.coords
this.position = { latitude, longitude }
},
// function (position) {
// console.log('position', position)
// // that.$patch(state => {
// // ;({ latitude: state.position.latitude, longitude: state.position.longitude } = position.coords)
// // localStorage.setItem('position', JSON.stringify(state.position))
// // console.log(state.position, position.coords, '检测到地理位置授权')
// // })
// },
error => {
console.log('error', error)
switch (error.code) {
case error.PERMISSION_DENIED:
Notify({
message: '用户拒绝对获取地理位置的请求。',
type: 'danger',
duration: 1000
})
break
case error.POSITION_UNAVAILABLE:
Notify({
message: '位置信息是不可用的。',
type: 'danger',
duration: 1000
})
break
case error.TIMEOUT:
Notify({
message: '请求用户地理位置超时。',
type: 'danger',
duration: 1000
})
break
case error.UNKNOWN_ERROR:
Notify({
message: '未知错误。',
type: 'danger',
duration: 1000
})
break
}
}
)
} else {
console.log('浏览器不兼容位置api')
}
},
getUser() {
oilSite
.getCompanyInfo({
accessToken: getToken()
})
.then(res => {
this.user = res.data
})
},
login(info) {
let { username, networkId } = info
if (username && networkId) {
return serve
.login({
username,
networkId
})
.then(res => {
if (res.code === 20000) {
// setToken(res.data.accessToken)
// this.getUser()
}
})
}
return Promise.reject()
},
clearToken() {
removeToken()
}
}
})
export default instance