card-mini/packagecard/moments/publish/momentpublish.js

522 lines
15 KiB
JavaScript
Raw Normal View History

const app = getApp()
const recorderManager = wx.getRecorderManager()
var innerAudioContext = wx.createInnerAudioContext()
const chooseLocation = requirePlugin('chooseLocation');
const key = 'Y6FBZ-GLQC3-6273Q-3DRTL-W43G5-G6BXN'; //使用在腾讯位置服务申请的key
const referer = '名片-小程序'; //调用插件的app的名称
2021-09-01 16:55:20 +08:00
var location = {
latitude: 39.90877,
longitude: 116.39695
}; //默认坐标点
const category = '生活服务,娱乐休闲';
Page({
/**
* 页面的初始数据
*/
data: {
2021-09-01 16:55:20 +08:00
maringHeight: app.globalData.CustomBar,
fieldList: [{
type: 'text'
}, {
type: 'video'
}, {
type: 'audio'
}, {
type: 'map'
}, {
type: 'select'
}, {
type: 'textarea'
}, {
type: 'link'
}, {
type: 'photo'
}, ], //允许发布的字段列表
valueList: [], //当前添加的内容列表
isShowInput: false, //是否显示文本输入
isShowPhoto: false, //是否显示图片编辑框
isShowVideo: false, //是否显示视频编辑框
isShowLink: false, //是否显示链接输入框
isShowSelect: false, //是否显示选项弹框
curTextValue: '', //当前输入的文本内容
curLinkText: '', //当前链接输入的文本内容
isShowEdit: false, //是否显示编辑
currentIndex: -1, //当前点击的条目索引
photoList: [], //当前选择的图片列表
videoList: [], //当前选择的视频列表
curSelPhotoDisplay: '1', //图片的显示方式 1.九宫格 2.轮播 3.轮播(自动) 4.平铺
curVideoDisplay: '1', //视频展示方式 1.九宫格 2.平铺
curTextAlign: '1', //文字显示排列 1.居左 2.居中 3.居右
curFieldType: '0', //编辑选择的文件类型
selDirection: '1', //选中的插入方向 1.插入到下面 2.插入的上面
isInsert: false, //是否显示插入选项
topBoxHeight: 100, //顶部类型选择框的高度
optionsList: [{
id: '1',
name: '选项一'
}, {
id: '2',
name: '选项二'
}, {
id: '3',
name: '选项三'
}, {
id: '4',
name: '选项四'
}, {
id: '5',
name: '选项五'
}], //选项
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
columnId: options.id
})
this.checkPermission()
2021-09-01 16:55:20 +08:00
wx.getLocation({
success(res) {
2021-09-01 16:55:20 +08:00
location.latitude = res.latitude
location.longitude = res.longitude
}
})
2021-09-01 16:55:20 +08:00
this.countBoxHeight()
},
2021-09-01 16:55:20 +08:00
countBoxHeight() {
var _self = this
var query = wx.createSelectorQuery()
query.select('#top-box').boundingClientRect()
query.exec(res => {
_self.setData({
topBoxHeight: res[0].height + 7
})
})
},
onShow() {
2021-09-01 16:55:20 +08:00
var _self = this
if (chooseLocation.getLocation() != null) {
2021-09-01 16:55:20 +08:00
var item = chooseLocation.getLocation()
var tempItem = {}
tempItem['type'] = 'map'
tempItem['value'] = item
_self.data.valueList.push(tempItem)
_self.setData({
valueList: _self.data.valueList
})
}
wx.getSetting({
withSubscriptions: true,
success(res) {
var isAuth = res.authSetting['scope.record']
_self.setData({
isAuthAudio: isAuth
})
}
})
},
2021-09-01 16:55:20 +08:00
showType(e) {
var type = e.currentTarget.dataset.type
var _self = this
switch (type) {
case 'text':
case 'textarea': //文本域
this.setData({
isShowInput: true
})
break
case 'video': //视频
_self.chooseVideo()
break
case 'audio': //音频
break
case 'photo': //图片
_self.chooseImg()
break
case 'map': //地图
_self.doChooseLocation()
break
case 'select': //选项
break
case 'link': //链接
_self.setData({
isShowLink: true
})
break
}
},
2021-09-01 16:55:20 +08:00
//去选择图片
chooseImg() {
var _self = this
2021-09-01 16:55:20 +08:00
wx.chooseImage({
count: 9,
sourceType: ['album', 'camera'],
success: (res) => {
2021-09-01 16:55:20 +08:00
// _self.doUploadImg(index, res.tempFilePaths, _self.data.uploadCount)
//构建数据
console.log(res)
res.tempFilePaths.forEach(it => {
var tempItem = {}
tempItem['path'] = it
_self.data.photoList.push(tempItem)
})
_self.setData({
photoList: _self.data.photoList,
isShowPhoto: true
})
},
fail: (err) => {
2021-09-01 16:55:20 +08:00
if (err.errMsg.indexOf('fail cancel') == -1) {
wx.showToast({
title: '请重新选择',
icon: 'error'
})
}
}
})
},
2021-09-01 16:55:20 +08:00
//去选择视频
chooseVideo() {
var _self = this
wx.chooseMedia({
count: 9,
mediaType: ['video'],
sourceType: ['album', 'camera'],
maxDuration: 60,
success(res) {
res.tempFiles.forEach(it => {
var tempItem = {}
tempItem['path'] = it.tempFilePath
tempItem['coverPath'] = it.thumbTempFilePath
_self.data.videoList.push(tempItem)
})
2021-09-01 16:55:20 +08:00
_self.setData({
videoList: _self.data.videoList,
isShowVideo: true
})
2021-09-01 16:55:20 +08:00
},
fail(err) {
if (err.errMsg.indexOf('fail cancel') == -1) {
wx.showToast({
title: '请重新选择',
icon: 'error'
})
2021-09-01 16:55:20 +08:00
}
}
})
},
//控制显示
showEditBox(e) {
var index = e.currentTarget.dataset.idx
var _self = this
if (_self.data.currentIndex == index) {
this.setData({
currentIndex: -1,
})
} else {
2021-09-01 16:55:20 +08:00
this.setData({
currentIndex: index
})
}
},
2021-09-01 16:55:20 +08:00
//选择插入的方向
chooseDirection(e) {
var idx = e.currentTarget.dataset.idx
this.setData({
selDirection: idx
})
},
2021-09-01 16:55:20 +08:00
//选择图片的显示方式
choosePhotoDisplay(e) {
var type = e.currentTarget.dataset.type
this.setData({
curSelPhotoDisplay: type
})
},
2021-09-01 16:55:20 +08:00
//选择视频的显示方式
chooseVideoDisplay(e) {
var type = e.currentTarget.dataset.type
this.setData({
curVideoDisplay: type
})
},
//选择文字排列方式
chooseTextAlign(e) {
var type = e.currentTarget.dataset.type
this.setData({
curTextAlign: type
})
},
2021-09-01 16:55:20 +08:00
//编辑选择出入的类型
chooseFieldType(e) {
var type = e.currentTarget.dataset.type
this.setData({
curFieldType: type
})
},
//确定选择的图片
confirmPhoto() {
var _self = this
2021-09-01 16:55:20 +08:00
if (_self.data.photoList.length > 0) {
//上传图片
var tempList = []
_self.data.photoList.forEach(it => {
var item = {}
item['path'] = it.path
tempList.push(item)
})
var tempPhoto = {}
tempPhoto['type'] = 'photo'
tempPhoto['valueList'] = tempList
tempPhoto['displayType'] = _self.data.curSelPhotoDisplay
_self.data.valueList.push(tempPhoto)
_self.setData({
2021-09-01 16:55:20 +08:00
valueList: _self.data.valueList,
photoList: [],
curSelPhotoDisplay: 1,
isShowPhoto: false
})
} else {
wx.showToast({
title: '请选择图片',
icon: 'error'
})
}
},
2021-09-01 16:55:20 +08:00
//确定选择的视频
confirmVideo() {
var _self = this
2021-09-01 16:55:20 +08:00
if (_self.data.videoList.length > 0) {
var tempList = []
_self.data.videoList.forEach(it => {
var item = {}
item['path'] = it.path
item['coverPath'] = it.coverPath
tempList.push(item)
})
var tempVideo = {}
tempVideo['type'] = 'video'
tempVideo['valueList'] = tempList
tempVideo['displayType'] = _self.data.curVideoDisplay
_self.data.valueList.push(tempVideo)
_self.setData({
2021-09-01 16:55:20 +08:00
valueList: _self.data.valueList,
videoList: [],
curVideoDisplay: 1,
isShowVideo: false
})
} else {
wx.showToast({
title: '请选择视频',
icon: 'error'
})
}
},
2021-09-01 16:55:20 +08:00
//确定插入数据
doInsert() {
//根据选择的类型与方向插入数据
},
2021-09-01 16:55:20 +08:00
hideEditBox() {
//隐藏编辑框
this.setData({
2021-09-01 16:55:20 +08:00
currentIndex: -1
})
},
2021-09-01 16:55:20 +08:00
onClose() {
this.setData({
2021-09-01 16:55:20 +08:00
isShowInput: false,
isShowLink: false
})
},
2021-09-01 16:55:20 +08:00
//输入框监听
inputWatch(e) {
var _self = this
2021-09-01 16:55:20 +08:00
_self.setData({
curTextValue: e.detail.value
})
},
2021-09-01 16:55:20 +08:00
//链接输入框监听
inputLink(e) {
var _self = this
_self.setData({
curLinkText: e.detail.value
})
},
2021-09-01 16:55:20 +08:00
//保存输入的链接
doConfirmLink() {
var _self = this
if (_self.data.curLinkText == '') {
wx.showToast({
title: '请输入链接',
icon: 'error'
})
} else {
//保存
var tempText = {}
tempText['type'] = 'link'
tempText['value'] = _self.data.curLinkText
_self.data.valueList.push(tempText)
_self.setData({
valueList: _self.data.valueList,
curLinkText: '',
isShowLink: false
})
}
},
2021-09-01 16:55:20 +08:00
//保存输入的文本
doConfirmInput() {
var _self = this
if (_self.data.curTextValue == '') {
wx.showToast({
title: '请输入内容',
icon: 'error'
})
} else {
2021-09-01 16:55:20 +08:00
//保存
var tempText = {}
tempText['type'] = 'text'
tempText['value'] = _self.data.curTextValue
var textAlign = 'left'
console.log(_self.data.curTextAlign)
switch (_self.data.curTextAlign) {
case '1':
textAlign = 'left'
break
case '2':
textAlign = 'center'
break
case '3':
textAlign = 'right'
break
}
tempText['textAlign'] = textAlign
_self.data.valueList.push(tempText)
_self.setData({
2021-09-01 16:55:20 +08:00
valueList: _self.data.valueList,
curTextValue: '',
isShowInput: false
})
}
},
2021-09-01 16:55:20 +08:00
//申请录音权限
checkPermission() {
var _self = this
wx.getSetting({
success(res) {
var isAuth = res.authSetting['scope.record']
if (typeof (isAuth) == 'undefined') {
wx.authorize({
scope: 'scope.record',
success() {
_self.setData({
isAuthAudio: true
})
},
fail() {
_self.setData({
isAuthAudio: false
})
}
})
} else {
if (!isAuth) {
_self.openSetting()
}
}
}
})
},
2021-09-01 16:55:20 +08:00
openSetting() {
wx.showModal({
title: '提示',
2021-09-01 16:55:20 +08:00
content: '发布内容需要您授权录音权限.',
success(res) {
if (res.confirm) {
2021-09-01 16:55:20 +08:00
wx.openSetting({
withSubscriptions: true,
})
}
}
})
},
2021-09-01 16:55:20 +08:00
//预览图片
viewPhoto(e) {
var url = e.currentTarget.dataset.url
wx.previewImage({
urls: [url],
})
},
//删除图片
delPhoto(e) {
var _self = this
wx.showModal({
title: '提示',
2021-09-01 16:55:20 +08:00
content: '确定要删除该图片吗?',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm) {
2021-09-01 16:55:20 +08:00
_self.data.photoList.splice(e.currentTarget.dataset.index, 1)
_self.setData({
2021-09-01 16:55:20 +08:00
photoList: _self.data.photoList
})
}
}
})
},
2021-09-01 16:55:20 +08:00
//选择定位点
doChooseLocation(e) {
// var index = e.currentTarget.dataset.index
// this.setData({
// curMapIndex: index
// })
var curLoc = JSON.stringify(location)
wx.navigateTo({
url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${curLoc}&category=${category}`
});
},
2021-09-01 16:55:20 +08:00
//编辑条目
editItem(e) {
2021-09-01 16:55:20 +08:00
},
//插入条目
insertItem(e) {
var _self = this
2021-09-01 16:55:20 +08:00
_self.setData({
isInsert: !_self.data.isInsert
})
2021-09-01 16:55:20 +08:00
},
//删除条目
delItem(e) {
wx.showModal({
title: '提示',
content: '确定要删除该条数据吗?',
success(res) {
if (res.confirm) {
2021-09-01 16:55:20 +08:00
}
}
})
},
2021-09-01 16:55:20 +08:00
onHide() {
},
2021-09-01 16:55:20 +08:00
//隐藏各类弹框
closePop() {
this.setData({
isShowPhoto: false,
isShowEdit: false,
isShowVideo: false
})
2021-09-01 16:55:20 +08:00
},
onUnload() {
chooseLocation.setLocation(null)
this.setData({
2021-09-01 16:55:20 +08:00
map: null
})
}
})