master
lion 3 days ago
parent e658a198bc
commit cc173c272c

File diff suppressed because it is too large Load Diff

@ -0,0 +1,35 @@
import { ROOTPATH } from '@/common/config.js'
import store from '@/store'
// Reverse geocoding is proxied through our authenticated backend. The service key
// stays on the server, while saved coordinates and native maps remain GCJ-02.
export default class Tianditu {
reverseGeocoder(options) {
const { latitude, longitude } = options.location || {}
if (!Number.isFinite(Number(latitude)) || !Number.isFinite(Number(longitude)) ||
latitude == null || longitude == null || latitude === '' || longitude === '') {
const error = { message: '缺少有效定位坐标' }
if (options.fail) options.fail(error)
if (options.complete) options.complete(error)
return
}
const role = Number(store.state.vuex_my_role) === 1 ? 'nurse' : 'admin'
return uni.request({
url: `${ROOTPATH}/api/${role}/map/reverse-geocode`,
method: 'GET',
header: { Authorization: `Bearer ${store.state.vuex_token}` },
timeout: 12000,
data: { lng: longitude, lat: latitude, coordinate_type: 'gcj02' },
success: response => {
const data = response.data
if (response.statusCode === 200 && data && !data.errcode && data.result && data.result.address) {
if (options.success) options.success(data)
} else if (options.fail) {
options.fail({ message: data && data.errmsg || '天地图地址查询失败,请稍后重试' })
}
},
fail: error => { if (options.fail) options.fail(error) },
complete: response => { if (options.complete) options.complete(response) }
})
}
}

@ -71,7 +71,7 @@
</template>
<script>
import QQMapWX from '@/libs/qqmap-wx-jssdk.js'
import Tianditu from '@/libs/tianditu.js'
import moment from "@/libs/moment.min";
import { ROOTPATH } from "@/common/config"
export default {
@ -159,9 +159,7 @@ export default {
console.error(e)
},
load() {
this.qqmapsdk = new QQMapWX({
key: 'I5FBZ-LMN33-BK63F-OGUO7-XE3JK-WJBP5'
});
this.tianditu = new Tianditu();
},
timeConfirm (e) {
this.form.visit_time = `${e.year}-${e.month}-${e.day} ${e.hour}:${e.minute}:${e.second}`
@ -176,7 +174,7 @@ export default {
this.form.lat = res[1]?.latitude
this.form.lng = res[1]?.longitude
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: {
latitude: this.form.lat,
longitude: this.form.lng

@ -37,7 +37,7 @@
</template>
<script>
import QQMapWX from '@/libs/qqmap-wx-jssdk.js'
import Tianditu from '@/libs/tianditu.js'
import { ROOTPATH } from "@/common/config"
export default {
data() {
@ -69,9 +69,7 @@ export default {
},
methods: {
load() {
this.qqmapsdk = new QQMapWX({
key: 'I5FBZ-LMN33-BK63F-OGUO7-XE3JK-WJBP5'
});
this.tianditu = new Tianditu();
},
getLoaction() {
return new Promise((resolve, reject) => {
@ -83,7 +81,7 @@ export default {
this.form.lat = res[1]?.latitude
this.form.lng = res[1]?.longitude
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: {
latitude: this.form.lat,
longitude: this.form.lng

@ -113,7 +113,7 @@
</template>
<script>
import QQMapWX from '@/libs/qqmap-wx-jssdk.js'
import Tianditu from '@/libs/tianditu.js'
import { ROOTPATH } from "@/common/config"
export default {
data() {
@ -315,9 +315,7 @@ export default {
this.$refs[refName].emitEvent()
},
load() {
this.qqmapsdk = new QQMapWX({
key: 'I5FBZ-LMN33-BK63F-OGUO7-XE3JK-WJBP5'
});
this.tianditu = new Tianditu();
},
getLoaction() {
return new Promise((resolve, reject) => {
@ -329,7 +327,7 @@ export default {
this.form.lat = res[1]?.latitude
this.form.lng = res[1]?.longitude
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: {
latitude: this.form.lat,
longitude: this.form.lng

@ -110,7 +110,7 @@
</template>
<script>
import QQMapWX from '@/libs/qqmap-wx-jssdk.js'
import Tianditu from '@/libs/tianditu.js'
import {
ROOTPATH
} from "@/common/config"
@ -349,7 +349,7 @@
const location = Array.isArray(res) ? res[1] : res
if(!location) throw new Error('定位失败')
await new Promise((resolve, reject) => {
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: { latitude: location.latitude, longitude: location.longitude },
success: (r) => resolve(r),
fail: (err) => reject(err)
@ -400,9 +400,7 @@
})
},
load() {
this.qqmapsdk = new QQMapWX({
key: 'I5FBZ-LMN33-BK63F-OGUO7-XE3JK-WJBP5'
});
this.tianditu = new Tianditu();
},
getLoaction() {
return new Promise((resolve, reject) => {
@ -414,7 +412,7 @@
this.form.latitude = res[1]?.latitude
this.form.longitude = res[1]?.longitude
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: {
latitude: this.form.latitude,
longitude: this.form.longitude

@ -175,7 +175,7 @@
import {
ROOTPATH
} from '@/common/config.js'
import QQMapWX from '@/libs/qqmap-wx-jssdk.js'
import Tianditu from '@/libs/tianditu.js'
import moment from '@/libs/moment.min.js'
import {
getAgeByIdcard
@ -196,7 +196,7 @@
isShowImg: false,
id: '',
flag: false, //未开始护理时,是否更新过定位(真为更新过定位)
qqmapsdk: null,
tianditu: null,
inputStyle: {
fontSize: "34rpx",
color: "#36596A",
@ -251,9 +251,7 @@
})
},
load() {
this.qqmapsdk = new QQMapWX({
key: 'I5FBZ-LMN33-BK63F-OGUO7-XE3JK-WJBP5'
});
this.tianditu = new Tianditu();
},
selectPick(e, item) {
@ -332,7 +330,7 @@
this.form.lat = this.location.lat
this.form.lng = this.location.lng
this.qqmapsdk.reverseGeocoder({
this.tianditu.reverseGeocoder({
location: {
latitude: this.location.lat,
longitude: this.location.lng

@ -0,0 +1,31 @@
const fs = require('fs')
const vm = require('vm')
const assert = require('assert')
const path = require('path')
let request
const store = { state: { vuex_my_role: 1, vuex_token: 'test-token' } }
const source = fs.readFileSync(path.join(__dirname, '../libs/tianditu.js'), 'utf8')
.replace(/^import .*$/gm, '').replace('export default class Tianditu', 'class Tianditu')
const context = { ROOTPATH: 'https://example.test', store, uni: { request: options => { request = options } } }
vm.createContext(context)
vm.runInContext(source + '\nthis.client = new Tianditu()', context)
let success, failure, complete
context.client.reverseGeocoder({ location: { latitude: 31.7, longitude: 119.9 }, success: data => { success = data }, fail: error => { failure = error }, complete: () => { complete = true } })
assert.equal(request.url, 'https://example.test/api/nurse/map/reverse-geocode')
assert.equal(request.header.Authorization, 'Bearer test-token')
assert.equal(request.data.coordinate_type, 'gcj02')
assert.equal(request.data.lng, 119.9)
request.success({ statusCode: 200, data: { result: { address: '天地图地址', formatted_addresses: { recommend: '' } } } })
assert.equal(success.result.address + success.result.formatted_addresses.recommend, '天地图地址')
request.complete({})
assert(complete)
request.success({ statusCode: 200, data: { errcode: 502, errmsg: '服务不可用' } })
assert.equal(failure.message, '服务不可用')
store.state.vuex_my_role = 2
context.client.reverseGeocoder({ location: { latitude: 31.7, longitude: 119.9 } })
assert.equal(request.url, 'https://example.test/api/admin/map/reverse-geocode')
request = undefined
context.client.reverseGeocoder({ location: { latitude: '', longitude: 119.9 }, fail: error => { failure = error } })
assert.equal(request, undefined)
assert.equal(failure.message, '缺少有效定位坐标')
console.log('PASS: nurse/admin routing, auth, GCJ-02, result compatibility, error callbacks, invalid coordinates')
Loading…
Cancel
Save