You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
794 lines
18 KiB
794 lines
18 KiB
uploadImgs |
|
/** |
|
* 好建小程序技术开发 |
|
* 官网:https://www.haojian.cn/ |
|
* 如有小程序方面的需求,欢迎咨询 |
|
*/ |
|
|
|
|
|
let util = require('util.js'); |
|
let QQMapWX = require('qqmap-wx-jssdk.js'); |
|
|
|
/** |
|
* 滚动到顶部 |
|
* @that 当前 |
|
* @scrollTop 滚动距离 |
|
*/ |
|
function goTopEvent(that, scrollTop) { |
|
let isPageScrollTo = false; |
|
if (wx.pageScrollTo) { |
|
isPageScrollTo = true; |
|
} else { |
|
isPageScrollTo = false; |
|
} |
|
if (scrollTop > 300 && !that.data.showGoTop) { |
|
that.setData({ |
|
showGoTop: true, |
|
isPageScrollTo |
|
}); |
|
} else if (scrollTop < 300 && that.data.showGoTop) { |
|
that.setData({ |
|
showGoTop: false, |
|
isPageScrollTo |
|
}); |
|
} |
|
} |
|
//判断是否固定到顶部 |
|
function scrollIsTabFixed(that, event) { |
|
if (that.tabOffsetTop !== null) { |
|
console.log(that.tabOffsetTop) |
|
const isTabFixed = that.data.isTabFixed, |
|
tmpIsTabFixed = event.scrollTop >= that.tabOffsetTop; |
|
if (isTabFixed != tmpIsTabFixed) that.setData({ |
|
isTabFixed: tmpIsTabFixed, |
|
}); |
|
} |
|
} |
|
|
|
//更新tab距离顶部距离 |
|
function updateTabOffsetTop(that) { |
|
if (wx.createSelectorQuery) { |
|
setTimeout(function() { |
|
const selectorQuery = wx.createSelectorQuery(); |
|
selectorQuery.select('#tab').boundingClientRect(); |
|
selectorQuery.selectViewport().scrollOffset(); |
|
selectorQuery.exec((res) => { |
|
//console.log(res) |
|
if (!res[0] || !res[1]) return; |
|
if (res[1].scrollTop > 0) { |
|
if (res[0].top > 0) { |
|
that.tabOffsetTop = res[1].scrollTop + res[0].top; |
|
} else { |
|
that.tabOffsetTop = (res[1].scrollTop - Math.abs(res[0].bottom)) + res[0].height * 2; |
|
} |
|
} else { |
|
that.tabOffsetTop = res[0].top; |
|
} |
|
}); |
|
}, 1500) |
|
} |
|
} |
|
/** |
|
* 没有时间限制的弹框 |
|
* @title 提醒内容 |
|
*/ |
|
function showClickModal(title) { |
|
wx.hideLoading(); |
|
wx.showModal({ |
|
title: '提示', |
|
content: title, |
|
showCancel: false, |
|
success(_res) {} |
|
}); |
|
} |
|
|
|
/** |
|
* 显示有时间限制的弹框 |
|
* @title 提醒内容 |
|
*/ |
|
function showTimeToast(title) { |
|
wx.showToast({ |
|
title, |
|
duration: 2000, |
|
icon: 'none', |
|
mask: true |
|
}); |
|
} |
|
/** |
|
* 保存数据事件 |
|
* @key 本地缓存中指定的key |
|
* @data 需要存储的内容 |
|
*/ |
|
function setInfo(key, data) { |
|
try { |
|
wx.setStorageSync(key, data); |
|
} catch (event) { |
|
console.log(event); |
|
} |
|
} |
|
/** |
|
* 获取已保存数据 |
|
* @key 本地缓存中置顶的key |
|
*/ |
|
function getInfo(key) { |
|
try { |
|
let value = wx.getStorageSync(key); |
|
if (value) { |
|
return value; |
|
} |
|
return {}; |
|
} catch (event) { |
|
console.log(event); |
|
} |
|
return null; |
|
} |
|
/** |
|
* 上传图片 |
|
* @res 图片上传的api |
|
* @path 需要上传的图片 |
|
*/ |
|
|
|
function singleUpload(path) { |
|
return wx.pro.uploadFile({ |
|
url: wx.getStorageSync('serverurl') + 'api/upload', |
|
filePath: path, |
|
name: 'file' |
|
}); |
|
} |
|
|
|
/** |
|
* 上传图片 |
|
* @type 1/相册 2/相机 3相册-相机 |
|
* @scenario 应用名称 |
|
* @num 上传的图片最大数量 |
|
* func Function |
|
*/ |
|
function uploadImgs(type, num, func) { |
|
let tempFile = ''; |
|
let sourceType = []; |
|
if (type == 1) { |
|
sourceType = ['album'] |
|
} |
|
if (type == 2) { |
|
sourceType = ['camera'] |
|
} |
|
if (type == 3) { |
|
sourceType = ['album', 'camera'] |
|
} |
|
wx.pro.chooseImage({ |
|
count: num, //最多可以选择的图片张数,默认9 |
|
sizeType: ['original'], // 指定是压缩图 |
|
sourceType: sourceType // 可以指定来源是相册还是相机,默认二者都有 |
|
}).then((res) => { |
|
console.log(res.tempFiles[0].size) |
|
tempFile = res.tempFilePaths; |
|
wx.showLoading({ |
|
title: '上传中,请稍等...' |
|
}); |
|
if (res.tempFiles[0].size < 6194304) { |
|
let pros = []; |
|
tempFile.forEach((path) => { |
|
pros.push(singleUpload(path)); |
|
}); |
|
return wx.pro.all(pros); |
|
} else { |
|
wx.showToast({ |
|
title: `图片大于6兆,上传失败`, |
|
icon: 'none', |
|
duration: 2000 |
|
}) |
|
} |
|
|
|
}).then((_res) => { |
|
wx.hideLoading(); |
|
if (JSON.parse(_res[0].data).code == '200') { |
|
func(JSON.parse(_res[0].data).data.src, tempFile); |
|
} else { |
|
wx.showLoading({ |
|
title: '上传失败' |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
/** |
|
* 查看大图 |
|
* @imgUrl 图片路径 |
|
* @imgList 图片列表 |
|
*/ |
|
function seeBigImg(imgUrl, imgList) { |
|
if (imgList.length === 0) { |
|
wx.previewImage({ |
|
urls: [imgUrl] // 需要预览的图片http链接列表 |
|
}); |
|
} else { |
|
wx.previewImage({ |
|
current: imgUrl, // 当前显示图片的http链接 |
|
urls: imgList // 需要预览的图片http链接列表 |
|
}); |
|
} |
|
} |
|
|
|
function downloadFile(img) { |
|
return wx.pro.downloadFile({ |
|
url: img |
|
}); |
|
} |
|
|
|
function saveImage(res) { |
|
return wx.pro.saveVideoToPhotosAlbum({ |
|
filePath: res.tempFilePath, |
|
}); |
|
} |
|
|
|
/** |
|
* 下载多图 |
|
*/ |
|
function downMoreImg(imgs) { |
|
let idx = 0; |
|
Promise.all(imgs.map((url, index) => { |
|
console.log(url) |
|
return new Promise((resolve, reject) => { |
|
wx.downloadFile({ |
|
url: url, |
|
success: function(res) { |
|
wx.saveImageToPhotosAlbum({ |
|
filePath: res.tempFilePath, |
|
success: function(file) { |
|
resolve(file); |
|
}, |
|
fail: function(e) { |
|
console.info("wx.saveImageToPhotosAlbum error" + JSON.stringify(e)); |
|
resolve(e); |
|
} |
|
}) |
|
}, |
|
fail: function(e) { |
|
console.info("wx.downloadFile error" + JSON.stringify(e)); |
|
resolve(e); |
|
} |
|
}).onProgressUpdate((res) => { |
|
if (res.progress == 100) { |
|
idx += 1; |
|
if (Number(idx) === Number(imgs.length)) { |
|
wx.hideLoading(); |
|
wx.showToast({ |
|
title: '下载完成' |
|
}); |
|
} |
|
console.log('下载进度:' + res.progress); |
|
} |
|
}) |
|
}); |
|
})) |
|
} |
|
/** |
|
*上传视频 |
|
**/ |
|
function upLoadVideo(obj) { |
|
obj.success = obj.success ? obj.success : (e) => {}; |
|
wx.chooseVideo({ |
|
sourceType: ['album', 'camera'], |
|
maxDuration: 30, |
|
camera: 'back', |
|
success(res) { |
|
let _res = singleUpload(res.tempFilePath); |
|
_res.then((res) => { |
|
obj.success(JSON.parse(res.data).data.src) |
|
}) |
|
|
|
} |
|
}) |
|
} |
|
|
|
/** |
|
* 判断是否授权使用相册功能 |
|
*/ |
|
function authorize(func) { |
|
wx.getSetting({ |
|
success(res) { |
|
if (!res.authSetting['scope.writePhotosAlbum']) { |
|
wx.authorize({ |
|
scope: 'scope.writePhotosAlbum', |
|
success(ress) { |
|
let fail = ress.errMsg.indexOf("authorize:fail"); |
|
console.log(fail) |
|
if (fail > -1) { |
|
func(false) |
|
} else { |
|
func(true) |
|
} |
|
}, |
|
fail(ress) { |
|
let fail = ress.errMsg.indexOf("authorize:fail"); |
|
console.log(fail) |
|
if (fail > -1) { |
|
func(false) |
|
} else { |
|
func(true) |
|
} |
|
} |
|
}) |
|
} else { |
|
func(true) |
|
} |
|
} |
|
}) |
|
} |
|
/** |
|
* 通过经纬度获取地址信息 |
|
* @lat 经度 |
|
* @lng 纬度 |
|
*/ |
|
function conversionAddress(lat, lng, func) { |
|
let demo = new QQMapWX({ |
|
key: 'NKDBZ-Q6RWU-TO7VE-2MRPR-PLDV3-VZFOZ' // 必填 |
|
}); |
|
demo.reverseGeocoder({ |
|
location: { |
|
latitude: lat, |
|
longitude: lng |
|
}, |
|
success(res) { |
|
console.log(res) |
|
func(res) |
|
} |
|
}); |
|
} |
|
/** |
|
* 拒绝后再次前往设置权限 |
|
* @that 当前 |
|
* @types 来源 |
|
*/ |
|
function wxOpenSet(types, func) { |
|
if (!wx.getSetting) { |
|
wx.hideLoading(); |
|
wx.showModal({ |
|
title: '提示', |
|
content: '当前微信版本过低,请升级至高版本', |
|
showCancel: false |
|
}); |
|
func(""); |
|
} |
|
wx.getSetting({ |
|
success: (res) => { |
|
let authSett = res.authSetting; |
|
if (!authSett[types]) { |
|
wx.hideLoading(); |
|
wx.showModal({ |
|
title: '用户未授权', |
|
content: '如需正常使用该功能,请点击“确定”并在授权管理中选中“通讯地址”,然后打开开关,即可使用', |
|
showCancel: false, |
|
success: (suc) => { |
|
if (suc.confirm) { |
|
// 调起客户端小程序设置界面 |
|
wx.openSetting({ |
|
success: (_res) => { |
|
wx.getLocation({ |
|
type: 'wgs84', |
|
success(res1) { |
|
func("success", res1) |
|
}, |
|
fail(res1) { |
|
func("fail", res1) |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
} |
|
}); |
|
} |
|
} |
|
}) |
|
} |
|
/** |
|
* 获取位置权限 |
|
* @that 当前 |
|
* @types 来源 |
|
* @func Function |
|
*/ |
|
|
|
//定位 |
|
function currentLocation(t, func) { |
|
if (Number(t) === 0) { |
|
wx.getLocation({ |
|
type: 'wgs84', |
|
success(res) { |
|
func("success", res) |
|
}, |
|
fail() { |
|
wxOpenSet('scope.userLocation', (status, row) => { |
|
func(status, row) |
|
}); |
|
} |
|
}); |
|
} |
|
if (Number(t) === 1) { |
|
wx.chooseLocation({ |
|
success(res1) { |
|
func("success", res1) |
|
}, |
|
fail() { |
|
wxOpenSet('scope.userLocation', (status, row) => { |
|
if (status === "success") { |
|
conversionAddress(row.latitude, row.longitude, (r_row) => { |
|
func(status, r_row) |
|
}); |
|
} else { |
|
func(status, row) |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
if (Number(t) === 2) { |
|
wx.chooseAddress({ |
|
success(res) { |
|
func("success", res) |
|
}, |
|
fail() { |
|
wxOpenSet('scope.address', (status, row) => { |
|
if (status === "success") { |
|
conversionAddress(row.latitude, row.longitude, (r_row) => { |
|
func(status, r_row) |
|
}); |
|
} else { |
|
func(status, row) |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
if (Number(t) === 3) { |
|
wx.getLocation({ |
|
type: 'wgs84', |
|
success(res) { |
|
conversionAddress(res.latitude, res.longitude, (r_row) => { |
|
func("success", r_row) |
|
}); |
|
}, |
|
fail() { |
|
wxOpenSet('scope.userLocation', (status, row) => { |
|
if (status === "success") { |
|
conversionAddress(row.latitude, row.longitude, (r_row) => { |
|
console.log(r_row) |
|
func(status, r_row) |
|
}); |
|
} else { |
|
func(status, row) |
|
} |
|
}); |
|
} |
|
}); |
|
} |
|
} |
|
/** |
|
* 打电话 |
|
* @phone 手机号 |
|
*/ |
|
function phoneCall(phone) { |
|
if (!phone) { |
|
showClickModal('暂无联系电话!'); |
|
return; |
|
} |
|
wx.makePhoneCall({ |
|
phoneNumber: phone |
|
}); |
|
} |
|
/** |
|
* 打开地图 |
|
* @lat 经度 |
|
* @lng 纬度 |
|
*/ |
|
function openMap(lat, lng) { |
|
wx.openLocation({ |
|
latitude: Number(lat), |
|
longitude: Number(lng), |
|
scale: Number(28) |
|
}); |
|
} |
|
/** |
|
* 列表数据处理 |
|
* @data api返回的列表信息 |
|
* @list 本地存储数据的参数名称 |
|
* @offset 请求api的页数 |
|
*/ |
|
function dataListHandle(that, data, list, offset) { |
|
wx.stopPullDownRefresh(); |
|
if (data.count > 0) { |
|
if (offset === 0) { |
|
list = data.results; |
|
} else { |
|
list = list.concat(data.results); |
|
} |
|
} else if (offset === 0) { |
|
list = []; |
|
} |
|
|
|
let hasNext = true; |
|
if (data.hasOwnProperty('next')) { |
|
console.log('count:' + data.count + ';next:' + data.next); |
|
if (data.next === 0) { |
|
that.state.hasmore = false; |
|
hasNext = false; |
|
} else { |
|
that.state.hasmore = true; |
|
hasNext = true; |
|
} |
|
} else { |
|
that.state.hasmore = false; |
|
} |
|
that.state.pageOnShow = true; |
|
that.state.isOnReachBottom = true; |
|
that.state.isonPullDownRefresh = false; |
|
that.state.scrolltolower = true; |
|
wx.hideLoading(); |
|
return { |
|
list, |
|
hasNext, |
|
count: data.count |
|
}; |
|
} |
|
|
|
/** |
|
* 移出token |
|
*/ |
|
function removeAccessToken() { |
|
try { |
|
wx.removeStorageSync('token'); |
|
wx.removeStorageSync('expire_at'); |
|
} catch (event) { |
|
console.log(event); |
|
} |
|
} |
|
|
|
/** |
|
* 获取token |
|
*/ |
|
function getAccessToken() { |
|
let accessToken = getInfo('token'); |
|
if (!accessToken) { |
|
return ""; |
|
} |
|
|
|
let expireAt = getInfo('expire_at'); |
|
let now = Date.parse(new Date()); |
|
if (expireAt) { |
|
if (now < expireAt) { |
|
return accessToken; |
|
} |
|
} |
|
removeAccessToken(); |
|
return ""; |
|
} |
|
|
|
|
|
/** |
|
* 处理并保存token |
|
*/ |
|
function setToken(token) { |
|
setInfo('token', token.token); |
|
setInfo('refresh_token', token.refresh_token); |
|
let expireIn = (token.token_expire * 1000); |
|
setInfo('expire_at', expireIn); |
|
} |
|
|
|
/** |
|
* 获取token |
|
*/ |
|
function getToken() { |
|
//调用登录接口 |
|
return wx.pro.login({}).then((res) => { |
|
//console.log(res.code) |
|
let val = { |
|
code: res.code |
|
} |
|
let url = 'api/Oauth/createToken'; |
|
return util.httpRequest(url, val, 'POST'); |
|
}).then((data) => { |
|
console.log(data) |
|
if (data.result === 'success') { |
|
setToken(data.results); |
|
} |
|
return data; |
|
}); |
|
} |
|
|
|
/** |
|
* 获取用户信息 |
|
*/ |
|
function getPersonInfo() { |
|
let url = 'user/info'; |
|
return util.httpRequest(url).then((res) => { |
|
setInfo("userInfo", res.results) |
|
console.log(res) |
|
return res |
|
}) |
|
} |
|
/** |
|
* 绑定用户信息 |
|
*/ |
|
function userInfoBind(event, callback) { |
|
let detail = event.detail; |
|
if (detail.hasOwnProperty('userInfo')) { |
|
wx.showLoading({ |
|
title: '绑定中...', |
|
mask: true |
|
}); |
|
let val = { |
|
name: detail.userInfo.nickName, |
|
preimg: detail.userInfo.avatarUrl |
|
}; |
|
|
|
let url = 'user/save'; |
|
util.httpRequest(url, val, 'POST').then((res) => { |
|
console.log(JSON.stringify(res)); |
|
wx.hideLoading(); |
|
if (res.result === 'success') { |
|
callback(res) |
|
wx.showToast({ |
|
title: '绑定成功!', |
|
icon: 'none', |
|
duration: 2000 |
|
}) |
|
} else { |
|
wx.showToast({ |
|
title: '绑定失败!', |
|
icon: 'none', |
|
duration: 2000 |
|
}) |
|
} |
|
|
|
}); |
|
} |
|
} |
|
/** |
|
* 绑定手机号 |
|
*/ |
|
function bindPhone(event) { |
|
// console.log(JSON.stringify(event.detail)); |
|
if (event.detail.errMsg !== 'getPhoneNumber:ok') { |
|
wx.navigateTo({ |
|
url: '/pages/personCenter/pages/bindPhone/bindPhone', |
|
}) |
|
return; |
|
} |
|
let val = { |
|
encryptedData: event.detail.encryptedData, |
|
iv: event.detail.iv |
|
}; |
|
let url = "api/user/wxBindMobile" |
|
return util.httpRequest(url, val, 'POST') |
|
} |
|
/** |
|
* 保存用户信息 |
|
*/ |
|
function keepUserInfo(data, func) { |
|
console.log(data) |
|
if (data.detail.hasOwnProperty("userInfo")) { |
|
let url = 'api/user/saveInfo'; |
|
let val = { |
|
nick_name: data.detail.userInfo.nickName, |
|
face: data.detail.userInfo.avatarUrl, |
|
gender: data.detail.userInfo.gender |
|
} |
|
console.log(val) |
|
util.httpRequest(url, val, "POST").then((res) => { |
|
console.log(res) |
|
if (res.result === "success") { |
|
func(res) |
|
} else { |
|
showClickModal(res.msg) |
|
} |
|
}) |
|
} |
|
} |
|
/** |
|
* 请求支付 |
|
* @url 请求的api地址 |
|
* @val 请求api的参数参数信息 |
|
* @fun 返回Function |
|
*/ |
|
function payment(url, val, func) { |
|
util.httpRequest(url, val, 'POST').then((info) => { |
|
console.log(JSON.stringify(info)); |
|
wx.hideLoading(); |
|
if (info.result === "success") { |
|
if (info.type === "finish") { |
|
func('success', info.msg, info); |
|
} else { |
|
wx.requestPayment({ |
|
timeStamp: info.data.timeStamp, //时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间 |
|
nonceStr: info.data.nonceStr, //随机字符串,长度为32个字符以下。 |
|
package: info.data.package, //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=* |
|
signType: info.data.signType, |
|
paySign: info.data.paySign, //签名 |
|
success(_res) { |
|
func('success', '支付成功', info); |
|
}, |
|
fail(res) { |
|
console.log(JSON.stringify(res)); |
|
let message = ''; |
|
if (res.hasOwnProperty('errMsg')) { |
|
if (res.errMsg === 'requestPayment:fail cancel') { |
|
message = '取消支付'; |
|
} else { |
|
message = res.errMsg; |
|
} |
|
} else { |
|
message = '支付失败'; |
|
} |
|
|
|
func('fali', message, info); |
|
} |
|
}); |
|
} |
|
} else { |
|
func('update', info.msg, info) |
|
} |
|
}) |
|
} |
|
|
|
function bindUser(event) { |
|
if (event.hasOwnProperty("uid")) { |
|
let url = 'user/bind?share_uid=' + event.uid; |
|
util.httpRequest(url).then((res) => {}) |
|
} |
|
} |
|
|
|
function config() { |
|
/** |
|
* 判断当前基础库是否支持获取微信绑定的手机号事件 |
|
*/ |
|
let isPhone = false; |
|
return wx.pro.getSystemInfo().then((res) => { |
|
if (res.model.indexOf("iPhone") != -1) { |
|
isPhone = true; |
|
} |
|
let url = 'other/config'; |
|
return util.httpRequest(url) |
|
}).then((res) => { |
|
console.log(res) |
|
console.log(isPhone) |
|
if (Number(res.results) === 0 && isPhone) { |
|
return { |
|
phoneIsSee: false |
|
} |
|
} else { |
|
return { |
|
phoneIsSee: true |
|
} |
|
} |
|
}); |
|
} |
|
|
|
module.exports = { |
|
|
|
goTopEvent, |
|
scrollIsTabFixed, |
|
updateTabOffsetTop, |
|
showClickModal, //可点击的模态弹框 |
|
showTimeToast, //不可点击的模态弹框 |
|
uploadImgs, //上传图片 |
|
dataListHandle, //请求的列表数据处理 |
|
phoneCall, //拨打电话 |
|
openMap, //查看地图 |
|
seeBigImg, //查看大图 |
|
downMoreImg, //下载多图 |
|
upLoadVideo, //上传视频, |
|
setStorage: setInfo, //存缓存 |
|
getStorage: getInfo, //获取缓存 |
|
authorize, //判断是否授权使用相册功能 |
|
getToken, //获取token |
|
getAccessToken, //判断token是否过期 |
|
removeAccessToken, //移出token |
|
keepUserInfo, //保存用户信息, |
|
currentLocation, //授权地址信息 |
|
getPersonInfo, |
|
userInfoBind, |
|
bindPhone, |
|
payment, |
|
bindUser, |
|
config |
|
}; |