Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
b951fed7
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
b951fed7
编写于
8月 12, 2024
作者:
zhaofengliang920817
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加getLocation示例的自动化脚本测试。
上级
46d418ce
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
400 addition
and
199 deletion
+400
-199
pages/API/get-location/get-location.test.js
pages/API/get-location/get-location.test.js
+170
-0
pages/API/get-location/get-location.uvue
pages/API/get-location/get-location.uvue
+230
-199
未找到文件。
pages/API/get-location/get-location.test.js
0 → 100644
浏览文件 @
b951fed7
const
PAGE_PATH
=
"
/pages/API/get-location/get-location
"
;
const
platformInfo
=
process
.
env
.
uniTestPlatformInfo
.
toLocaleLowerCase
()
const
isAndroid
=
platformInfo
.
startsWith
(
'
android
'
)
const
isIos
=
platformInfo
.
startsWith
(
'
ios
'
)
const
isApp
=
isAndroid
||
isIos
const
isWeb
=
platformInfo
.
startsWith
(
'
web
'
)
describe
(
"
get-location
"
,
()
=>
{
beforeAll
(
async
()
=>
{
page
=
await
program
.
reLaunch
(
PAGE_PATH
)
await
page
.
waitFor
(
600
)
});
//system 定位
it
(
"
system+type=wgs84+success
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
system
'
,
jest_type
:
'
wgs84
'
,
jest_isAltitude
:
true
,
jest_isGeocode
:
false
,
jest_isHighAccuracy
:
false
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
const
data
=
await
page
.
data
()
const
jest_errCode
=
data
[
'
jest_errCode
'
]
if
(
jest_errCode
>
0
)
{
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
expect
.
any
(
Number
));
}
else
{
//判断经纬度是否在正常范围
expect
((
await
page
.
data
())[
'
jest_longitude
'
]).
toBeGreaterThanOrEqual
(
-
180
);
expect
((
await
page
.
data
())[
'
jest_longitude
'
]).
toBeLessThanOrEqual
(
180
);
expect
((
await
page
.
data
())[
'
jest_latitude
'
]).
toBeGreaterThanOrEqual
(
-
90
);
expect
((
await
page
.
data
())[
'
jest_latitude
'
]).
toBeLessThanOrEqual
(
90
);
//判断海拔是否正确
expect
((
await
page
.
data
())[
'
jest_altitude
'
]).
toEqual
(
expect
.
any
(
Number
));
expect
((
await
page
.
data
())[
'
jest_altitude
'
]).
not
.
toEqual
(
'
0
'
);
}
});
//system 定位
it
(
"
system+type=wgs84+success+geocode=true
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
system
'
,
jest_type
:
'
wgs84
'
,
jest_isAltitude
:
true
,
jest_isGeocode
:
true
,
jest_isHighAccuracy
:
false
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
const
data
=
await
page
.
data
()
const
jest_errCode
=
data
[
'
jest_errCode
'
]
if
(
jest_errCode
>
0
)
{
if
(
isIos
)
{
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
1505603
);
}
else
if
(
isAndroid
)
{
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
1505700
);
}
else
{
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
expect
.
any
(
Number
));
}
}
});
//system 定位
it
(
"
system+type=wgs84+success+altitude=false
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
system
'
,
jest_type
:
'
wgs84
'
,
jest_isAltitude
:
false
,
jest_isGeocode
:
true
,
jest_isHighAccuracy
:
false
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
const
data
=
await
page
.
data
()
const
jest_errCode
=
data
[
'
jest_errCode
'
]
if
(
jest_errCode
>
0
)
{
//如果定位出错
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
expect
.
any
(
Number
));
}
else
{
expect
((
await
page
.
data
())[
'
jest_altitude
'
]).
toEqual
(
'
0
'
);
}
});
//system 定位
it
(
"
system+type=gcj02+fail
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
system
'
,
jest_type
:
'
gcj02
'
,
jest_isAltitude
:
true
,
jest_isGeocode
:
true
,
jest_isHighAccuracy
:
false
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
1505601
);
});
//tencent 定位
it
(
"
tencent+type=gcj02+success
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
tencent
'
,
jest_type
:
'
gcj02
'
,
jest_isAltitude
:
true
,
jest_isGeocode
:
true
,
jest_isHighAccuracy
:
true
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
const
data
=
await
page
.
data
()
const
jest_errCode
=
data
[
'
jest_errCode
'
]
if
(
jest_errCode
>
0
)
{
//如果定位出错
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
expect
.
any
(
Number
));
}
else
{
//判断逆地理编码是否正确
expect
((
await
page
.
data
())[
'
jest_address
'
]).
toEqual
(
expect
.
any
(
String
));
//判断经纬度是否在正常范围
expect
((
await
page
.
data
())[
'
jest_longitude
'
]).
toBeGreaterThanOrEqual
(
-
180
);
expect
((
await
page
.
data
())[
'
jest_longitude
'
]).
toBeLessThanOrEqual
(
180
);
expect
((
await
page
.
data
())[
'
jest_latitude
'
]).
toBeGreaterThanOrEqual
(
-
90
);
expect
((
await
page
.
data
())[
'
jest_latitude
'
]).
toBeLessThanOrEqual
(
90
);
//判断海拔是否正确
expect
((
await
page
.
data
())[
'
jest_altitude
'
]).
toEqual
(
expect
.
any
(
Number
));
expect
((
await
page
.
data
())[
'
jest_altitude
'
]).
not
.
toEqual
(
'
0
'
);
}
});
//tencent 定位
it
(
"
tencent+type=wgs84+fail
"
,
async
()
=>
{
await
page
.
setData
({
jest_provider
:
'
tencent
'
,
jest_type
:
'
wgs84
'
,
jest_isAltitude
:
true
,
jest_isGeocode
:
true
,
jest_isHighAccuracy
:
true
})
await
page
.
callMethod
(
'
jestGetLocation
'
)
await
page
.
waitFor
(
async
()
=>
{
return
await
page
.
data
(
'
jest_complete
'
)
===
true
;
});
if
(
isApp
)
{
expect
((
await
page
.
data
())[
'
jest_errCode
'
]).
toEqual
(
1505607
);
}
});
});
pages/API/get-location/get-location.uvue
浏览文件 @
b951fed7
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;">
<!-- #endif -->
<page-head :title="title"></page-head>
<view style="padding: 4px">
<text class="hello-text">
定位功能默认调用操作系统定位API实现, 也支持腾讯定位。\n
部分手机因gms兼容不好可能导致无法使用系统定位, gcj国标、逆地理信息等功能需调用内置腾讯定位。</text>
</view>
<view class="uni-padding-wrap uni-common-mt">
<!-- #ifdef APP -->
<view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view>
<view class="uni-list" style="margin-bottom: 20px">
<radio-group @change="radioChangePV">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id"
:class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id"
:checked="index === currentProvider">
{{ item.name }}
</radio>
</radio-group>
</view>
<!-- #endif -->
<view class="uni-list-cell-db">定位类型</view>
<view class="uni-list">
<radio-group @change="radioChange">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
:class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item.value"
:checked="index === current">
{{ item.name }}
</radio>
</radio-group>
</view>
<view class="uni-list-cell uni-list-cell-pd" style="margin-top: 20px">
<view class="uni-list-cell-db">高度信息</view>
<switch :checked="altitudeSelect" @change="altitudeChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">开启高精度定位</view>
<switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">是否解析地址信息</view>
<switch :checked="geocodeSelect" @change="geocodeChange" />
</view>
<text>{{ exeRet }}</text>
<view class="uni-btn-v">
<button class="uni-btn" type="default" @tap="getLocationTap">
获取定位
</button>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
export type LocationItem = { id : string, name : string, provider ?: UniProvider }
export type ItemType = { value : 'wgs84' | 'gcj02', name : 'wgs84' | 'gcj02' }
export default {
data() {
return {
title: 'get-location',
altitudeSelect: false,
isHighAccuracySelect: false,
geocodeSelect: false,
exeRet: '',
items: [
{
value: 'wgs84',
name: 'wgs84'
},
{
value: 'gcj02',
name: 'gcj02'
}
] as ItemType[],
providerList: [] as LocationItem[],
current: 0,
currentProvider: 0
}
},
onLoad: function () {
// #ifdef APP
this.getProvider()
// #endif
},
methods: {
getProvider() {
// #ifdef APP
uni.getProvider({
service: "location",
success: (e) => {
console.log("location success:" + JSON.stringify(e), e.providerObjects.length);
let array = e.providerIds as string[]
array.forEach((value : string) => {
let locationProvider = e.providerObjects.find((item) : boolean => {
return item.id == value
})
if (locationProvider != null) {
this.providerList.push({
name: locationProvider.description,
id: locationProvider.id,
provider: e.providerObjects.find((item) : boolean => {
return item.id == locationProvider.id
})
} as LocationItem);
}
})
},
fail: (e) => {
console.log("获取定位服务商失败:", e);
}
});
this.providerList.forEach((value, index) => {
if (value.id == "system") {
this.currentProvider = index
}
})
// #endif
},
altitudeChange: function (e : UniSwitchChangeEvent) {
this.altitudeSelect = e.detail.value
},
geocodeChange: function (e : UniSwitchChangeEvent) {
this.geocodeSelect = e.detail.value
},
highAccuracySelectChange: function (e : UniSwitchChangeEvent) {
this.isHighAccuracySelect = e.detail.value
},
radioChange(e : UniRadioGroupChangeEvent) {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].value === e.detail.value) {
this.current = i;
break;
}
}
},
radioChangePV(e : UniRadioGroupChangeEvent) {
for (let i = 0; i < this.providerList.length; i++) {
if (this.providerList[i].id === e.detail.value) {
this.currentProvider = i;
break;
}
}
if (e.detail.value == "system") {
this.current = 0
} else if (e.detail.value == "tencent") {
this.current = 1
}
},
getLocationTap: function () {
// #ifdef APP
if (this.providerList.length == 0) {
uni.showToast({
title: '未获取到provider,请确定基座中包含location功能',
icon: "error"
})
console.log("未获取到provider,请确定基座中包含location功能")
return
}
// #endif
uni.showLoading({
title: '定位中'
})
uni.getLocation(({
// #ifdef APP
provider: this.providerList[this.currentProvider].id,
// #endif
type: this.items[this.current].value,
altitude: this.altitudeSelect,
isHighAccuracy: this.isHighAccuracySelect,
geocode: this.geocodeSelect,
success: (res : any) => {
uni.hideLoading()
console.log(res);
this.exeRet = JSON.stringify(res)
},
fail: (res : any) => {
uni.hideLoading()
console.log(res);
this.exeRet = JSON.stringify(res)
},
complete: (res : any) => {
uni.hideLoading()
console.log(res);
this.exeRet = JSON.stringify(res)
}
}));
}
}
}
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;">
<!-- #endif -->
<page-head :title="title"></page-head>
<view style="padding: 4px">
<text class="hello-text">
定位功能默认调用操作系统定位API实现, 也支持腾讯定位。\n
部分手机因gms兼容不好可能导致无法使用系统定位, gcj国标、逆地理信息等功能需调用内置腾讯定位。</text>
</view>
<view class="uni-padding-wrap uni-common-mt">
<!-- #ifdef APP -->
<view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view>
<view class="uni-list" style="margin-bottom: 20px">
<radio-group @change="radioChangePV">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id"
:class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id"
:checked="index === currentProvider">
{{ item.name }}
</radio>
</radio-group>
</view>
<!-- #endif -->
<view class="uni-list-cell-db">定位类型</view>
<view class="uni-list">
<radio-group @change="radioChange">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
:class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item.value"
:checked="index === current">
{{ item.name }}
</radio>
</radio-group>
</view>
<view class="uni-list-cell uni-list-cell-pd" style="margin-top: 20px">
<view class="uni-list-cell-db">高度信息</view>
<switch :checked="altitudeSelect" @change="altitudeChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">开启高精度定位</view>
<switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
</view>
<view class="uni-list-cell uni-list-cell-pd">
<view class="uni-list-cell-db">是否解析地址信息</view>
<switch :checked="geocodeSelect" @change="geocodeChange" />
</view>
<text>{{ exeRet }}</text>
<view class="uni-btn-v">
<button class="uni-btn" type="default" @tap="getLocationTap">
获取定位
</button>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
export type LocationItem = { id : string, name : string, provider ?: UniProvider }
export type ItemType = { value : 'wgs84' | 'gcj02', name : 'wgs84' | 'gcj02' }
export default {
data() {
return {
title: 'get-location',
altitudeSelect: false,
isHighAccuracySelect: false,
geocodeSelect: false,
exeRet: '',
items: [
{
value: 'wgs84',
name: 'wgs84'
},
{
value: 'gcj02',
name: 'gcj02'
}
] as ItemType[],
providerList: [] as LocationItem[],
current: 0,
currentProvider: 0,
jest_provider: String,
jest_type: String,
jest_isAltitude: Boolean,
jest_isGeocode: Boolean,
jest_isHighAccuracy: Boolean,
jest_altitude: Number,
jest_longitude: Number,
jest_latitude: Number,
jest_address: String,
jest_errCode: Number,
jest_complete: Boolean
}
},
onLoad: function () {
// #ifdef APP
this.getProvider()
// #endif
},
methods: {
getProvider() {
// #ifdef APP
uni.getProvider({
service: "location",
success: (e) => {
console.log("location success:" + JSON.stringify(e), e.providerObjects.length);
let array = e.providerIds as string[]
array.forEach((value : string) => {
let locationProvider = e.providerObjects.find((item) : boolean => {
return item.id == value
})
if (locationProvider != null) {
this.providerList.push({
name: locationProvider.description,
id: locationProvider.id,
provider: e.providerObjects.find((item) : boolean => {
return item.id == locationProvider.id
})
} as LocationItem);
}
})
},
fail: (e) => {
console.log("获取定位服务商失败:", e);
}
});
this.providerList.forEach((value, index) => {
if (value.id == "system") {
this.currentProvider = index
}
})
// #endif
},
altitudeChange: function (e : UniSwitchChangeEvent) {
this.altitudeSelect = e.detail.value
},
geocodeChange: function (e : UniSwitchChangeEvent) {
this.geocodeSelect = e.detail.value
},
highAccuracySelectChange: function (e : UniSwitchChangeEvent) {
this.isHighAccuracySelect = e.detail.value
},
radioChange(e : UniRadioGroupChangeEvent) {
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].value === e.detail.value) {
this.current = i;
break;
}
}
},
radioChangePV(e : UniRadioGroupChangeEvent) {
for (let i = 0; i < this.providerList.length; i++) {
if (this.providerList[i].id === e.detail.value) {
this.currentProvider = i;
break;
}
}
if (e.detail.value == "system") {
this.current = 0
} else if (e.detail.value == "tencent") {
this.current = 1
}
},
getLocationTap: function () {
// #ifdef APP
if (this.providerList.length == 0) {
uni.showToast({
title: '未获取到provider,请确定基座中包含location功能',
icon: "error"
})
console.log("未获取到provider,请确定基座中包含location功能")
return
}
// #endif
uni.showLoading({
title: '定位中'
})
uni.getLocation(({
// #ifdef APP
provider: this.providerList[this.currentProvider].id,
// #endif
type: this.items[this.current].value,
altitude: this.altitudeSelect,
isHighAccuracy: this.isHighAccuracySelect,
geocode: this.geocodeSelect,
success: (res : any) => {
uni.hideLoading()
this.exeRet = JSON.stringify(res)
},
fail: (res : any) => {
uni.hideLoading()
this.exeRet = JSON.stringify(res)
},
complete: (res : any) => {
uni.hideLoading()
this.exeRet = JSON.stringify(res)
}
}));
},
// 仅用于自动化测试
jestGetLocation() {
this.jest_complete = false
this.jest_errCode = 0
uni.getLocation(({
// #ifdef APP
provider: this.jest_provider,
// #endif
type: this.jest_type,
altitude: this.jest_isAltitude,
isHighAccuracy: this.jest_isHighAccuracy,
geocode: this.jest_isGeocode,
success: (res : any) => {
this.jest_address = res.address
this.jest_longitude = res.longitude
this.jest_latitude = res.latitude
this.jest_altitude = res.altitude
this.jest_complete = true
},
fail: (err : any) => {
this.jest_errCode = err.errCode
this.jest_complete = true
}
}));
}
}
}
</script>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录