Files
ClassContent/项目合集/校园失物招领/min-tpl/pages/push/push.js
2024-09-27 02:06:13 +08:00

198 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const {
index,
util
} = getApp().globalData
Page({
/**
* 页面的初始数据
*/
data: {
ossUrl: 'https://losta.oss-cn-shanghai.aliyuncs.com',
showTips: true,
fileList: [],
thingTypeIndex: 0,
thingTypeArr: [
{ title: '失物招领', type: 1 },
{ title: '寻物启事', type: 2 },
],
thingCategoryIndex: 0,
thingCategoryArr: [
{ title: '证件', type: 1 },
{ title: '服装', type: 2 },
{ title: '数码', type: 3 },
{ title: '日用品', type: 4 },
{ title: '其他', type: 5 },
],
pushParams: {
user_id: 0,
thing_title: '',
thing_introduce: '',
thing_img: '',
thing_type: 1,
thing_category: 1
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if (util.checkIsLogin()) {
this.setData({ thingTypeIndex: options.id - 1 })
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
onShow: function () {
setTimeout(()=> {
this.setData({ showTips: false })
},4000)
},
thingTitleInput(e) {
this.setData({
['pushParams.thing_title']: e.detail.value
});
},
thingIntrInput(e) {
this.setData({
['pushParams.thing_introduce']: e.detail.value
})
},
changeThingTypeIndex(e) {
this.setData({ thingTypeIndex: e.target.dataset.id })
},
changeThingCategoryIndex(e) {
this.setData({ thingCategoryIndex: e.target.dataset.id })
},
async PushThing() {
let userInfo = wx.getStorageSync('user');
let {
fileList, pushParams,
thingTypeIndex, thingTypeArr,
thingCategoryIndex, thingCategoryArr
} = this.data;
if (pushParams.thing_title.length == 0) {
wx.showToast({
title: '标题必填',
icon: 'none',
duration: 2000
});
return false;
}
if (pushParams.thing_introduce.length == 0 || pushParams.thing_introduce.length > 500) {
wx.showToast({
title: '介绍必填且字数不能大于500',
icon: 'none',
duration: 2000
});
return false;
}
if (pushParams.thing_img.length == 0) {
wx.showToast({
title: '图片必须上传',
icon: 'none',
duration: 2000
});
return false;
}
pushParams.user_id = userInfo.user_id;
pushParams.thing_type = thingTypeArr[thingTypeIndex].type;
pushParams.thing_category = thingCategoryArr[thingCategoryIndex].type;
console.log("请求参数:", pushParams)
let res = await index.push(pushParams);
console.log(res);
util.showToast({
title: '发布成功',
icon: 'success',
duration: 2000,
close: () => {
wx.navigateBack()
}
})
},
async afterRead(event) {
const { file } = event.detail;
this.setData({ fileList: [ file ] })
console.log("file === ", file)
let arr = file.url.split(".")
let fileType = arr[arr.length - 1]; // 文件后缀
let ossConfig = await index.token();
let fileName = `${util.random()}.${fileType}`;
console.log("fileName === ", fileName)
wx.uploadFile({
url: this.data.ossUrl,
filePath: file.url,
name: 'file',
formData: { key: fileName, ...ossConfig },
success: (res) => {
console.log("uploadFile success ", res)
if (res.statusCode == 204) {
this.setData({
['pushParams.thing_img']: `${this.data.ossUrl}/${fileName}`
});
} else {
wx.showToast({
title: "图片上传错误,稍后再试",
icon: 'none',
duration: 3000
});
}
}
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})