//index.js
const app = getApp()
const updateManager = wx.getUpdateManager()
var clipboardData = ""
var isusecliData = false
const db = wx.cloud.database()
Page({
data: {
avatarUrl: './user-unlogin.png',
userInfo: {},
logged: false,
takeSession: false,
requestResult: '',
imgTmp: [],
videoTmp: '',
inputTxt: '',
writePhotosAlbum: false,
postUrl: "",
sourceTempUrl: [],
fileIDs: [],
alreadyInDB: true,
dbids: [],
},
onLoad: function() {
wx.stopPullDownRefresh()
if (!wx.cloud) {
wx.redirectTo({
url: '../chooseLib/chooseLib',
})
return
}
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log("检查更新:"+ res.hasUpdate)
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: 'UPDATE',
content: 'The new version is ready,do you want to installed?',
cancelText: 'NO',
confirmText: 'YES',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
wx.showModal({
title: 'UPDATE',
content: 'The new version download failed,please check the network and try again later!',
confirmText: 'YES',
showCancel: false,
success: function (res) {
}
})
})
})
}
})
wx.getSetting({
success: res => {
// 获取用户信息
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
// console.log("获取信息001")
wx.getUserInfo({
success: res => {
this.setData({
avatarUrl: res.userInfo.avatarUrl,
userInfo: res.userInfo,
logged: true
})
}
})
}else{
logged: false
}
//获取相册权限
this.setData({
writePhotosAlbum: res.authSetting['scope.writePhotosAlbum']
})
}
})
// 获取剪贴板内容
var that = this
wx.getClipboardData({
success(res) {
clipboardData = res.data
if (clipboardData != null && clipboardData != "" && clipboardData != undefined) {
var re = new RegExp("(https ?| ftp | file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]", 'g');
var redlurl = re.exec(clipboardData);
if(redlurl != null){
redlurl = redlurl[0]
var str = "oasis.weibo.cn"
if(redlurl.indexOf(str) != -1){
wx.showModal({
title: 'Tips',
content: 'Do you want to use the clipboard contents directly?\n' + clipboardData,
cancelText: 'NO',
confirmText: 'YES',
success(res) {
if (res.confirm) {
isusecliData = true
that.dlsubmit()
}
}
})
}
}
}
}
})
},
// 下拉刷新
onPullDownRefresh: function(){
this.setData({
imgTmp: [],
videoTmp: '',
inputTxt: ''
})
this.onLoad()
},
// 点击输入框时清除输入框
cleanInput: function(){
var that = this
that.setData({
inputTxt: ''
})
},
onGetUserInfo: function(e) {
// console.log("获取信息")
if (!this.data.logged && e.detail.userInfo) {
this.setData({
logged: true,
avatarUrl: e.detail.userInfo.avatarUrl,
userInfo: e.detail.userInfo
})
}
},
// 获取资源
dlsubmit: function(e){
var mythis=this;
mythis.setData({
imgTmp: [],
videoTmp: '',
})
var dlurl = "";
if(isusecliData){
dlurl = clipboardData
isusecliData = false
}else{
dlurl = e.detail.value.dlurl;
}
console.log("输入链接:" + dlurl);
var re = new RegExp("(https ?| ftp | file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]", 'g');
var redlurl = re.exec(dlurl);
console.log(redlurl);
if (redlurl != null){
redlurl = redlurl[0];
var str = "oasis.weibo.cn";
if(redlurl.indexOf(str) != -1){
console.log("获取链接:" + redlurl);
mythis.setData({
postUrl: redlurl
})
// mythis.getFromDB()
wx.showLoading({
title: 'downloading...',
})
setTimeout(function () {
wx.hideLoading()
}, 2000)
wx.request({
url: redlurl,
header: { 'content-type': 'application/x-www-form-urlencoded' },
success(res) {
console.log(res);
if (res.statusCode == 200) {
var data = res.data;
console.log(data);
wx.cloud.callFunction({
name: 'utils',
data: {
data: data,
action: 'parseHTML'
},
success: function (res) {
// 得到img/video地址
var sourceurl = res.result
console.log("云函数调用结果:" + sourceurl)
if (sourceurl.length === 1) {
if (sourceurl[0].indexOf("https://wx") === -1 && sourceurl[0].indexOf("https://lz.sinaimg.cn") === -1) {
// 视频
mythis.downloadAndSaveVideo(sourceurl[0])
} else {
// 下载并保存---图片
mythis.downloadAndSaveImg(sourceurl)
}
} else {
// 下载并保存---图片
mythis.downloadAndSaveImg(sourceurl)
}
},
fail: function (e) {
console.log("云函数调用错误:" + e)
wx.showModal({
title: 'Download Failed',
content: 'Please try again later~',
showCancel: false,
confirmText: 'YES',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
}
})
}
},
fail(e) {
wx.showModal({
title: 'Download Failed',
content: 'Please try again later~',
showCancel: false,
confirmText: 'YES',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
}
})
}else{
wx.showModal({
title: 'Tips',
content: 'Please enter the correct url!(only support oasis)',
showCancel: false,
confirmText: 'YES',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
}
}else{
wx.showModal({
title: 'Tips',
content: 'Please enter the correct url!(only support oasis)',
showCancel: false,
confirmText: 'YES',
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
}
},
checkWriteAlbum: function(){
if (!this.data.writePhotosAlbum) {// 未授权相册
wx.authorize({
scope: 'scope.write