- 小程序需要授权的接口
需要授权的接口 | 描述 |
---|---|
wx.getUserInfo | 用户信息 |
wx.getLocation, wx.chooseLocation | 地理位置 |
wx.startLocationUpdateBackground | 后台定位 |
wx.chooseAddress | 通讯地址 |
wx.chooseInvoiceTitle | 发票抬头 |
wx.chooseInvoice | 获取发票 |
wx.getWeRunData | 微信运动步数 |
wx.startRecord , RecorderManager.start(Object object) | 录音功能 |
wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum | 保存到相册 |
camera组件 | 摄像头 |
// 这里以保存图片到相册为例
<button type="primary" bindtap="choose">选择图片</button>
<button bindtap="photoAlbum">保存到图片到系统相册</button>
data: {
src: '',
list: [],
},
choose() {
wx.chooseImage({
success: (res) => {
console.log(res)
this.setData({
src: res.tempFilePaths[0],
list: this.data.list.concat(res.tempFilePaths)
})
console.log(this.data.list)
},
})
},
photoAlbum() {
// 需要授权的接口
let that = this
wx.getSetting({
success(res) {
console.log('设置', res, res.authSetting['scope.writePhotosAlbum'])
// 1.需要判断权限是否被拒接过
if (res.authSetting['scope.writePhotosAlbum'] === false) {
console.log('权限被拒绝过')
wx.showModal({
title: '微信授权',
content: '请先打开"保存到相册"的权限',
success(res) {
if (res.confirm) {
wx.openSetting({
success(e) {
console.log('打开了设置')
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
cosonle.log('首次授权调用了')
// 2.首次授权
// 第一次弹出微信授权窗口:只要用户未点击拒绝或允许,该权限就是首次授权
wx.authorize({
scope: 'wx.writePhotosAlbum',
success() {
wx.saveImageToPhotosAlbum({
filePath: that.data.src
})
}
})
} else {
// 3.已经授权
console.log('已经授权过', that.data.src)
wx.saveImageToPhotosAlbum({
filePath: that.data.src,
success(e) {
console.log('保存成功')
}
})
}
}
})
},
- 获取data中的数据,修改data中的数据
var pageObject = {
data: {
tel: '18720072431',
password: '123456',
code: '',
rightCode: '',
flag: true,
value: '获取',
disabled: false
},
codeFn(e) {
this.data.code = e.detail.value
console.log(this.data.code)
},
// 获取验证码
getCodeFn() {
let that = this
// 随机验证码部分开始
let ran = Math.random() * 89999 + 10000
let num = Math.floor(ran)
this.data.rightCode = num
console.log(this.data.rightCode)
// 结束
// 发送请求生产验证码(前端发送不安全,最后调用后台接口发送)
wx.request({
url: 'https://v.juhe.cn/sms/send',
data: {
mobile: 18720072431,
tpl_id: 34201,
tpl_value: encodeURI('#code#=') + num,
key: 'e34efbc58862ea5cf4703b08635bc06c',
},
success: function(e) {
console.log(e)
// 成功之后开始倒计时
let allTime = 10000
let temp = setInterval(()=>{
allTime-= 1000
that.setData({
value: `${allTime / 1000} 后发送`,
disabled: true
})
// that.data.value = `${allTime / 1000} 后发送`
console.log(allTime)
if(allTime === 0) {
that.setData({
value: '获取',
disabled: false
})
clearInterval(temp)
}
},1000)
}
})
},
// 登录操作
login() {
console.log(this.data.rightCode, this.data.code)
if ((this.data.code + '').length !== 5) {
return wx.showToast({
title: '请输入验证码',
icon: 'none'
})
}
// console.log(this.data.rightCode.toString() === this.data.code.toString())
if (this.data.rightCode.toString() === this.data.code.toString())
wx.showToast({
title: '登录成功!!!'
})
}
}
Page(pageObject)