// pages/release/release.js
const util = require('../../util/util.js');
Page({
/**
* 页面的初始数据
*/
data: {
userinfo: null,
content: null,
pics: [],
code: null,
},
// 删除已添加的图片
close(e) {
wx.showModal({
title: '提示',
content: '确认删除?',
success: res => {
if (res.confirm) {
this.setData({
pics: this.data.pics.filter(v => {
return v.path != e.currentTarget.dataset.src
})
})
}
}
})
},
// 添加图片
choose() {
wx.chooseImage({
count: 9 - this.data.pics.length,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: res => {
this.setData({
pics: this.data.pics.concat(res.tempFiles)
})
},
})
},
// 设置content值
input(value) {
this.setData({
content: value.detail.value
})
},
// 发布事件
send(img_urls) {
wx.request({
url: util.url + 'c=wechat&m=insert',
data: {
code: this.data.code,
user_name: this.data.userinfo.nickName,
user_avatar: this.data.userinfo.avatarUrl,
content: this.data.content,
img_urls: img_urls || '',
publish_address: '..'
},
success: res => {
wx.setStorageSync('openid', res.data)
wx.hideLoading();
wx.showToast({
title: '发布成功',
success:res=>{
wx.switchTab({
url:'/pages/index/index'
})
}
})
}
})
},
// 拿到用户信息并尝试发布
getUserInfo(e) {
wx.login({
success: res => {
this.setData({
code: res.code
})
if (e.detail.userInfo) {
wx.setStorageSync('userInfo',e.detail.userInfo);
this.setData({
userinfo: e.detail.userInfo
})
if (!this.data.content) {
wx.showToast({
title: '请输入内容',
icon: 'none'
})
return;
}
if (this.data.pics.length) {
let img_urls = [];
for (let i = 0; i < this.data.pics.length; i++) {
wx.uploadFile({
url: util.url + 'c=wechat&m=upload',
filePath: this.data.pics[i].path,
name: 'f',
success: res => {
img_urls.push(res.data);
if (img_urls.length == this.data.pics.length) {
this.send(img_urls.join(';'))
}
}
})
}
} else {
this.send()
}
} else {
wx.showToast({
title: '无授权暂时不提供发布功能',
icon: 'none'
})
}
},
fail: () => {
wx.showToast({
title: '出錯了',
icon: 'none'
})
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})