uniapp

目录结构

image-20220925155355265

把项目运行到微信开发者工具

  1. manifest.json中填入appid即可

image-20220925155929283

  1. 配置微信开发者工具路径

    工具-设置-运行配置-填写微信开发者工具路径

image-20220925160310506

  1. 在微信开发者工具 设置-安全设置-打开服务端口

image-20220925160638485

  1. 在HBuilderX中 运行-运行到小程序模拟器-微信开发者工具

  2. 在HBuilderX中保存后,即可在小程序开发者工具查看效果

git忽略文件

image-20220925161813094

因为忽略了unpackage中仅有的文件dist,默认unpackage目录不会被git追踪,所以创建一个.gitkeep 的文件进行占位

选择地址

uni.chooseAddress

需要在mainfest.json中,选择源码视图,在”mp-weixin” 节点内添加

“requiredPrivateInfos”: [“getLocation”, “chooseLocation”, “chooseAddress”]

1
2
3
4
5
6
7
8
9
"mp-weixin" : {
/* 小程序特有相关 */
"appid" : "wx5c09b50319662051",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true,
"requiredPrivateInfos": ["getLocation", "chooseLocation", "chooseAddress"]
},

即可正常使用

登陆授权
1
2
3
4
5
6
7
8
9
10
11
12
 <button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>

getUserProfile() {
uni.getUserProfile({
desc: '你的授权信息',
success: (res) => {
console.log(res,'登陆成功')
},
fail: (res) => {
return uni.$showMsg('您取消了登录授权')
}
})

上传图片

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
// 上传图片
const uploadImage = (url: string, su_title: string, err_title: string) => {
return new Promise((resolve, reject) => {
uni.chooseMedia({
count: 1,
mediaType: ['image'],
sizeType: ['compressed'],
success: (res: any) => {
// console.log(res, '图片暂时本地路径');
// 拿到暂时路径后,将图片上传
uni.showLoading({ title: su_title, mask: true })
uni.uploadFile({
url,
filePath: res.tempFiles[0].tempFilePath,
name: 'file',
header: { accept: 'application/json' },
success: (res_img: any) => {
resolve(res_img);
uni.hideLoading();
},
fail: (err_img: any) => {
reject(err_img);
uni.showToast({ title: err_title, icon: 'error', duration: 1000 });
},
});

}
})
})
}
export { uploadImage }

小程序的static不能有中文路径,否则在真机预览中图片将无法显示