Skip to content

Commit 60d207c

Browse files
committed
feat: cross-platform
1 parent 923ac70 commit 60d207c

3 files changed

Lines changed: 55 additions & 63 deletions

File tree

pages/API/get-user-info/get-user-info.vue

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616
</block>
1717
</view>
1818
<view class="uni-btn-v">
19-
<!-- #ifdef APP-PLUS -->
20-
<button type="primary" @tap="getUserInfo">获取用户信息</button>
19+
<!-- #ifdef APP-PLUS || MP-ALIPAY -->
20+
<button type="primary" @click="getUserInfo">获取用户信息</button>
2121
<!-- #endif -->
22-
<!-- #ifdef MP-WEIXIN -->
22+
<!-- #ifdef MP-WEIXIN || MP-BAIDU -->
2323
<button type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">获取用户信息</button>
2424
<!-- #endif -->
25-
<!-- #ifdef MP-BAIDU -->
26-
<button type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">获取用户信息</button>
27-
<!-- #endif -->
28-
<button @tap="clear">清空</button>
25+
<button @click="clear">清空</button>
2926
</view>
3027
</view>
3128
</view>
@@ -49,42 +46,42 @@
4946
loginProvider: state => state.loginProvider
5047
})
5148
},
52-
onLoad: function() {},
5349
methods: {
54-
getUserInfo() { //获取用户信息api在微信小程序可直接使用,在5+app里面需要先登录才能调用
50+
// 获取用户信息 API 在小程序可直接使用,在 5+App 里面需要先登录才能调用
51+
getUserInfo() {
5552
uni.getUserInfo({
5653
provider: this.loginProvider,
57-
success: (e) => {
58-
console.log("得到用户信息", e);
54+
success: (result) => {
55+
console.log('getUserInfo success', result);
5956
this.hasUserInfo = true;
60-
this.userInfo = e.userInfo
57+
this.userInfo = result.userInfo;
6158
},
62-
fail: (e) => {
63-
console.log("fail", e);
64-
let content = e.errMsg;
65-
if (~content.indexOf("uni.login")) {
66-
content = "请在登录页面完成登录操作"
59+
fail: (error) => {
60+
console.log('getUserInfo fail', error);
61+
let content = error.errMsg;
62+
if (~content.indexOf('uni.login')) {
63+
content = '请在登录页面完成登录操作';
6764
}
6865
uni.showModal({
69-
title: "获取用户信息失败",
70-
content: "错误原因" + content,
66+
title: '获取用户信息失败',
67+
content: '错误原因' + content,
7168
showCancel: false
72-
})
69+
});
7370
}
74-
})
71+
});
7572
},
76-
mpGetUserInfo(e) {
77-
console.log("得到用户信息", e);
78-
if (e.detail.errMsg !== 'getUserInfo:ok') {
73+
mpGetUserInfo(result) {
74+
console.log('mpGetUserInfo', result);
75+
if (result.detail.errMsg !== 'getUserInfo:ok') {
7976
uni.showModal({
80-
title: "获取用户信息失败",
81-
content: "错误原因" + e.detail.errMsg,
77+
title: '获取用户信息失败',
78+
content: '错误原因' + result.detail.errMsg,
8279
showCancel: false
8380
});
8481
return;
8582
}
8683
this.hasUserInfo = true;
87-
this.userInfo = e.detail.userInfo
84+
this.userInfo = result.detail.userInfo;
8885
},
8986
clear() {
9087
this.hasUserInfo = false;

pages/API/share/share.vue

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<view class="uni-title">分享内容</view>
66
<view class="uni-textarea">
77
<textarea class="textarea" v-model="shareText" />
8-
</view>
8+
</view>
99
<view class="uni-title">分享图片:</view>
1010
<view class="uni-uploader" style="padding:15upx; background:#FFF;">
1111
<view class="uni-uploader__input-box" v-if="!image" @tap="chooseImage"></view>
@@ -35,12 +35,7 @@
3535
</block>
3636
</view>
3737
<!-- #endif -->
38-
<!-- #ifdef MP-WEIXIN -->
39-
<view class="uni-btn-v uni-common-mt">
40-
<button type="primary" open-type="share">分享</button>
41-
</view>
42-
<!-- #endif -->
43-
<!-- #ifdef MP-BAIDU -->
38+
<!-- #ifdef MP -->
4439
<view class="uni-btn-v uni-common-mt">
4540
<button type="primary" open-type="share">分享</button>
4641
</view>
@@ -69,14 +64,14 @@
6964
},
7065
onUnload:function(){
7166
this.shareText='uni-app可以同时发布成原生App、微信小程序、H5,邀请你一起体验!',
72-
this.href = "https://uniapp.dcloud.io",
67+
this.href = 'https://uniapp.dcloud.io',
7368
this.image='';
7469
},
7570
onLoad: function () {
7671
uni.getProvider({
77-
service: "share",
72+
service: 'share',
7873
success: (e) => {
79-
console.log("success", e);
74+
console.log('success', e);
8075
let data = []
8176
for (let i = 0; i < e.provider.length; i++) {
8277
switch (e.provider[i]) {
@@ -116,63 +111,63 @@
116111
});
117112
},
118113
fail: (e) => {
119-
console.log("获取登录通道失败", e);
114+
console.log('获取登录通道失败', e);
120115
uni.showModal({
121-
content:"获取登录通道失败",
116+
content:'获取登录通道失败',
122117
showCancel:false
123118
})
124119
}
125120
});
126121
},
127122
methods: {
128123
async share(e) {
129-
console.log("分享通道:"+ e.id +"; 分享类型:" + this.shareType);
124+
console.log('分享通道:'+ e.id +'; 分享类型:' + this.shareType);
130125
131126
if(!this.shareText && (this.shareType === 1 || this.shareType === 0)){
132127
uni.showModal({
133-
content:"分享内容不能为空",
128+
content:'分享内容不能为空',
134129
showCancel:false
135130
})
136131
return;
137132
}
138133
139134
if(!this.image && (this.shareType === 2 || this.shareType === 0)){
140135
uni.showModal({
141-
content:"分享图片不能为空",
136+
content:'分享图片不能为空',
142137
showCancel:false
143138
})
144139
return;
145140
}
146141
147142
let shareOPtions = {
148143
provider: e.id,
149-
scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : "WXSceneSession", //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
144+
scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
150145
type: this.shareType,
151146
success: (e) => {
152-
console.log("success", e);
147+
console.log('success', e);
153148
uni.showModal({
154-
content: "分享成功",
149+
content: '分享成功',
155150
showCancel:false
156151
})
157152
},
158153
fail: (e) => {
159-
console.log("fail", e)
154+
console.log('fail', e)
160155
uni.showModal({
161156
content: e.errMsg,
162157
showCancel:false
163158
})
164159
},
165160
complete:function(){
166-
console.log("分享操作结束!")
161+
console.log('分享操作结束!')
167162
}
168163
}
169164
170165
switch (this.shareType){
171166
case 0:
172167
shareOPtions.summary = this.shareText;
173168
shareOPtions.imageUrl = this.image;
174-
shareOPtions.title = "欢迎体验uniapp";
175-
shareOPtions.href = "http://uniapp.dcloud.io";
169+
shareOPtions.title = '欢迎体验uniapp';
170+
shareOPtions.href = 'http://uniapp.dcloud.io';
176171
break;
177172
case 1:
178173
shareOPtions.summary = this.shareText;
@@ -182,11 +177,11 @@
182177
break;
183178
case 5:
184179
shareOPtions.imageUrl = this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
185-
shareOPtions.title = "欢迎体验uniapp";
180+
shareOPtions.title = '欢迎体验uniapp';
186181
shareOPtions.miniProgram = {
187-
id:"gh_33446d7f7a26",
188-
path:"/pages/tabBar/component/component",
189-
webUrl:"http://uniapp.dcloud.io",
182+
id:'gh_33446d7f7a26',
183+
path:'/pages/tabBar/component/component',
184+
webUrl:'http://uniapp.dcloud.io',
190185
type:0
191186
};
192187
break;
@@ -198,13 +193,13 @@
198193
shareOPtions.imageUrl = await this.compress();
199194
}
200195
if(shareOPtions.type === 1 && shareOPtions.provider === 'qq'){//如果是分享文字到qq,则必须加上href和title
201-
shareOPtions.href = "http://uniapp.dcloud.io";
202-
shareOPtions.title = "欢迎体验uniapp";
196+
shareOPtions.href = 'http://uniapp.dcloud.io';
197+
shareOPtions.title = '欢迎体验uniapp';
203198
}
204199
uni.share(shareOPtions);
205200
},
206201
radioChange(e){
207-
console.log("type:" + e.detail.value);
202+
console.log('type:' + e.detail.value);
208203
this.shareType = parseInt(e.detail.value);
209204
},
210205
chooseImage() {
@@ -218,15 +213,15 @@
218213
})
219214
},
220215
compress(){//压缩图片 图文分享要求分享图片大小不能超过20Kb
221-
console.log("开始压缩");
216+
console.log('开始压缩');
222217
let img = this.image;
223218
return new Promise((res) => {
224219
var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
225220
console.log('after' + localPath);
226221
// 压缩size
227222
plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
228223
entry.file((file) => {// 可通过entry对象操作图片
229-
console.log("getFile:" + JSON.stringify(file));
224+
console.log('getFile:' + JSON.stringify(file));
230225
if(file.size > 20480) {// 压缩后size 大于20Kb
231226
plus.zip.compressImage({
232227
src: img,
@@ -241,16 +236,16 @@
241236
res(newImg);
242237
}, function(error) {
243238
uni.showModal({
244-
content:"分享图片太大,需要请重新选择图片!",
239+
content:'分享图片太大,需要请重新选择图片!',
245240
showCancel:false
246241
})
247242
});
248243
}
249244
});
250245
}, (e) => {
251-
console.log("Resolve file URL failed: " + e.message);
246+
console.log('Resolve file URL failed: ' + e.message);
252247
uni.showModal({
253-
content:"分享图片太大,需要请重新选择图片!",
248+
content:'分享图片太大,需要请重新选择图片!',
254249
showCancel:false
255250
})
256251
});
@@ -262,4 +257,4 @@
262257

263258
<style>
264259
265-
</style>
260+
</style>

pages/tabBar/template/template.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
name: '二维码生成',
181181
url: 'qrcode'
182182
},
183-
// #ifndef MP-BAIDU
183+
// #ifdef APP-PLUS || H5 || MP-BAIDU
184184
{
185185
name: '图片裁剪',
186186
url: 'crop'

0 commit comments

Comments
 (0)