uni-app

条件编译快捷命令

选中要条件编译的代码块,ctrl + alt + /即可生成正确注释

1
2
3
// #ifdef APP
uni.hideNavigationBarLoading();
// #endif

滚动一屏,sticky失效

需要把 sticky元素放在 一个 view 中,不能放在 template 中

app端选择地图导航

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
toMapAPP(data) {
const {
lat,
lng,
shopName,
name
} = data;
let url = "";
if (plus.os.name == "Android") {
//判断是安卓端
plus.nativeUI.actionSheet({
//选择菜单
title: "选择地图应用",
cancel: "取消",
buttons: [{
title: "百度地图",
},
{
title: "高德地图",
},
],
},
function(e) {
switch (e.index) {
//下面是拼接url,不同系统以及不同地图都有不同的拼接字段
case 1:
url =
`baidumap://map/marker?location=${lat},${lng}&title=${name||shopName}&src=taoliangche&coord_type=gcj02`
break;
case 2:
url =
`androidamap://arroundpoi?sourceApplication=softname&keywords=${name||shopName}&lat=${lat}&lon=${lng}&dev=0`;
break;
default:
break;
}
if (url != "") {
url = encodeURI(url);
plus.runtime.openURL(url, function(e) {
plus.nativeUI.alert("本机未安装指定的地图应用");
});
}
}
);
} else {
// iOS上获取本机是否安装了百度高德地图,需要在manifest里配置
// 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
//(如urlschemewhitelist:"iosamap","baidumap")
plus.nativeUI.actionSheet({
title: "选择地图应用",
cancel: "取消",
buttons: [
// {
// title: "腾讯地图"
// },
{
title: "百度地图",
},
{
title: "高德地图",
},
],
},
function(e) {
switch (e.index) {
// case 1:
// url = `qqmap://map/geocoder?coord=${lat},${lng}&referer=xxx`;
// break;
case 1:
url =
`baidumap://map/direction?origin={{我的位置}}&destination=latlng:${lat},${lng}|name=${shopName||name}&mode=driving&coord_type=bd09ll`;
// coord_type=bd09ll:坐标系类型,必须与传入的经纬度编码一致。
// origin={{我的位置}}:固定写法,表示以用户当前位置为起点。
break;
case 2:
url = `iosamap://path?sourceApplication=掏靓车&keywords=${shopName||name}&dlat=${lat}&dlon=${lng}&dev=0&style=0`;
// lat/lon:目标经纬度(必须使用高德地图的 ​​GCJ-02​​ 坐标系)。
// dev:坐标系类型,0 表示 GCJ-02,1 表示 GPS(WGS-84)。
// style:导航方式,2 表示驾车,0 表示速度最快策略。
break;
default:
break;
}
if (url != "") {
url = encodeURI(url);
plus.runtime.openURL(url, function(e) {
plus.nativeUI.alert("本机未安装指定的地图应用");
});
}
}
);
}
},

app分享微信小程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 分享到微信小程序
share() {
// #ifdef APP
uni.share({
provider: "weixin",
scene: "WXSceneSession",
type: 5,
miniProgram: {
id: '原始小程序 id',
path: '/pages/home/home?id=' + this.cid + '&uid=' + uni.getStorageSync('uid'),
type: 0,
webUrl: "www.123.com"
},
title: this.info.name,
summary: "描述",
imageUrl: "share.png",
success: function(res) {
console.log("success:" + JSON.stringify(res));
},
fail: function(err) {
console.log("fail:" + JSON.stringify(err));
}
});
// #endif
}

注意事项:

  1. 报错:应用与小程序未在同一个开放平台下:因为直接运行到标准基座了,这个时候可以打自定义基座
  2. 报错:share:fail [Share微信分享:-3]Unable to send, https://ask.dcloud.net.cn/article/287
    原因一 miniProgram中的webUrl 未填写,随便填写一个地址就行了
    原因二 uni.share 中的imageUrl 图片大小过大,应该小于20kb
  3. 报错:不支持的分享类型
    检查 uni.share 中miniProgram参数中的id要填写小程序原始id,不是小程序的appid,原始 id在小程序后台-账号设置-账号信息-原始ID