[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);
}
})
})
}
```