[TOC]
# 作为公用方法使用
```
导出
module.exports={
onShareAppMessage:function(url,customtitle,imgSrc){},
baocun(imageUrl){},
.....
}
引入
const core = require('../../utils/core.js');
```
## 保存图片
```
// 保存图片到相册
baocun(imageUrl) {
//获取相册授权
let $this = this
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() { //这里是用户同意授权后的回调
$this.baocunImg(imageUrl);
},
fail() { //这里是用户拒绝授权后的回调
wx.showModal({
title: '提示',
content: '若不打开授权,则无法将图片保存在相册中!',
showCancel: true,
cancelText: '暂不授权',
confirmText: '去授权',
success: function (res) {
if (res.confirm) {
wx.openSetting({
//调起客户端小程序设置界面,返回用户设置的操作结果。
})
} else {
console.log('用户点击取消')
}
}
})
}
})
} else { //用户已经授权过了
$this.baocunImg(imageUrl);
}
}
})
},
baocunImg(imageUrl) {
let src = imageUrl
wx.getImageInfo({
src,
success: function (ret) {
console.log(ret);
var path = ret.path;
wx.saveImageToPhotosAlbum({
filePath: path,
success: function (res) {
wx.showToast({
title: '保存成功',
})
wx.previewImage({
current: '',
urls: [url]
})
},
fail: function (res) {
console.log(res)
wx.showToast({
title: '保存失败',
icon: 'none'
})
}
})
}
});
}
```
## 分享
```
onShareAppMessage: function (url, customtitle, imgSrc) {
// app.matomo.trackEvent("点击", '分享', url)
console.log("点击", '分享', url)
console.log("点击", '分享', customtitle)
console.log("点击", '分享', imgSrc)
let userinfo_id = wx.getStorageSync('userinfo').id,
title = '',
desc = '',
imageUrl = baseUrl + 'share-card.jpg';
if (customtitle) {
title = customtitle
};
if (imgSrc) {
imageUrl = imgSrc
}
url = url || '/pages/index/index/index';
url = url.indexOf('?') != -1 ? url + '&' : url + '?';
return {
title: title,
desc: desc,
path: url + 'mid=' + userinfo_id,
imageUrl,
}
},
```
## 获取手机号
```
获取手机号之前获取code
onLoad:function(options){
let $this = this
wx.login({
success: res => {
console.log(res)
$this.setData({
code: res.code
})
}
})
}
```
```
// 获取手机号
getphonenumber: function (e) {
let $this = this;
if (e.detail.errMsg !== "getPhoneNumber:ok") {
return;
}
// 检查登录态是否过期
wx.checkSession({
success(res) {
console.log(res)
let data = {
code: $this.data.code,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData
}
$this.memberPhone(data)
},
fail(err) {
// session_key 已经失效,需要重新执行登录流程
console.log(err)
wx.login({
success: res => {
$this.setData({
code: res.code
})
// app.setCache("code", res.code)
let data = {
code: res.code,
iv: e.detail.iv,
encryptedData: e.detail.encryptedData
}
$this.memberPhone(data)
}
})
wx.showToast({
title: '获取手机号失败,请重试',
icon: "none"
})
}
})
},
```
```
//获取手机号接口
memberPhone(data) {
let $this = this
http('post', 'memberPhone', {
...data
}, function (res) {
if (res.code == 200) {
let phone = res.result
$this.setData({
phone
})
wx.setStorageSync('phone', phone);
wx.showToast({
title: '成功',
})
} else {
wx.showToast({
title: '获取手机号失败,请重试',
icon: "none"
})
}
})
},
```
## 登录授权
```
//登录授权
bindgetuserinfo(e) {
return new Promise((resolve, reject) => {
console.log("login")
let $this = this;
let channel = wx.getStorageSync('apppath')
wx.login({
success: function (ret) {
console.log(ret)
if (ret.code) {
wx.getUserInfo({
success(res2) {
http('post', 'wxLogin', {
code: ret.code,
encryptedData: res2.encryptedData,
iv: res2.iv,
userInfos: JSON.stringify(res2.userInfo),
signature: res2.signature,
channel,
}, function (login_res) {
console.log(login_res)
if (login_res.code == 200) {
let data = login_res.result
console.log(data)
wx.setStorageSync('userinfo', data.userInfo);
wx.setStorageSync('phone', data.userInfo.mobile);
wx.setStorageSync('userName', data.userInfo.nickname);
wx.setStorageSync('userId', data.userId);
// 设置统计代码的openid
// console.log($this)
// $this.matomo.setUserId(data.userInfo.openid)
resolve(true);
} else {
wx.showToast({
title: '获取用户登录态失败:' + login_res.message,
icon: "none"
})
}
})
}
})
}
},
fail: function (rett) {
wx.showToast({
title: '获取用户登录态失败:' + rett,
icon: "none"
})
reject(false);
}
})
})
}
```