2020-06-11 15:06:19 +08:00

353 lines
8.1 KiB
JavaScript

// components/createProj/createProj.js
//去除字符串两边留白
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
var App = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
title: '',
details: '',
adds: null,
tag: null,
price: '',
isfree: false,
imgList: [],
customBar: null,
showSubmit: false,
showError: false,
photos: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
customBar: App.globalData.CustomBar
})
wx.hideShareMenu();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
wx.setTabBarStyle({
color: '#666666',
selectedColor: '#449ffc',
backgroundColor: '#ffffff',
borderStyle: 'black'
})
// 449ffc
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
ChooseImage() {
wx.chooseImage({
count: 1, //默认9
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: (res) => {
//上传图片
var that = this;
var userInfo = wx.getStorageSync('userInfoDetail')
let path = 'cherryStore/userImg/' + userInfo._id + '/' + (new Date()).getTime() + this.data.photos.length;
wx.cloud.uploadFile({
cloudPath: path, // 上传至云端的路径
filePath: res.tempFilePaths[0], // 小程序临时文件路径
success: res => {
// 返回文件 ID
// console.log(res.fileID)
that.data.photos.push(res.fileID)
console.log(this.data.photos)
},
fail: console.error
})
if (this.data.imgList.length != 0) {
this.setData({
imgList: this.data.imgList.concat(res.tempFilePaths)
})
} else {
this.setData({
imgList: res.tempFilePaths
})
}
console.log(this.data.imgList)
}
});
},
ViewImage(e) {
wx.previewImage({
urls: this.data.imgList,
current: e.currentTarget.dataset.url
});
},
DelImg(e) {
var that = this;
wx.showModal({
title: '亲!',
content: '确定要删除这张照片吗?',
cancelText: '放弃',
confirmText: '删掉',
success: res => {
if (res.confirm) {
this.data.imgList.splice(e.currentTarget.dataset.index, 1);
this.setData({
imgList: this.data.imgList
})
var argument = this.data.photos[e.currentTarget.dataset.index]
var list = [];
list.push(argument)
this.data.photos.splice(e.currentTarget.dataset.index, 1);
wx.cloud.deleteFile({
fileList: list
}).then(res => {
// handle success
console.log(res.fileList)
}).catch(error => {
// handle error
})
}
}
})
},
input(e) {
// console.log(e.detail.value)
switch (e.target.dataset.inputtype) {
case 1: //title
if (e.detail.value) this.data.title = e.detail.value;
else this.data.title = null;
break;
case 2: //detail
if (e.detail.value)
this.setData({
details: e.detail.value
})
else
this.setData({
details: null
})
break;
case 3: //price
e.detail.value = e.detail.value.trim();
if (e.detail.value) {
let judgePrice = isNaN(e.detail.value)
if (parseInt(e.detail.value) === 0) {
this.data.isfree = true;
this.data.price = 0;
} else if (judgePrice) {
this.data.isfree = false;
this.data.price = null;
} else {
this.data.isfree = false;
this.data.price = parseInt(e.detail.value);
}
} else {
this.data.isfree = false;
this.data.price = null;
}
break;
case 4: //adds
if (e.target.dataset.tagtype === this.data.adds)
this.setData({
adds: null
})
else
this.setData({
adds: e.target.dataset.tagtype
})
break;
case 5: //tag
if (e.target.dataset.tagtype === this.data.tag)
this.setData({
tag: null
})
else
this.setData({
tag: e.target.dataset.tagtype
})
break;
}
console.log(this.data)
},
// 申请发送模板消息
requestSubscribeMessage() {
this.hideModal()
wx.requestSubscribeMessage({
tmplIds: ['PPngF-ljr1S6yixiorFJ4bj8t8mP5odUhHEnu_08Smw'],
}).then(res => {
console.log(res)
if(res['PPngF-ljr1S6yixiorFJ4bj8t8mP5odUhHEnu_08Smw'] == 'reject') {
// 用户点击不同意
this.submit(false)
} else {
// 用户同意
this.submit()
}
}).catch(err => {
console.log(err)
})
},
submit(needSend = true) {
var userInfo = wx.getStorageSync('userInfoDetail');
wx.showLoading({
title: '上传中',
mask: true
})
if (this.data.photos.length !== this.data.imgList.length) {
wx.showToast({
title: '请等待图片上传完成',
image: '../../images/info.png',
duration: 2000
})
return;
}
var d = this.data;
var data = {
stats: 1,
userInfos: userInfo,
title: d.title,
details: d.details,
adds: d.adds,
tag: d.tag,
price: d.price,
isfree: d.isfree,
photos: d.photos,
needSend: needSend
}
console.log(data);
wx.cloud.callFunction({
name: 'sM_examine',
data: data
}).then(res => {
wx.hideLoading({})
if (res.result.errCode !== 200) {
wx.showToast({
title: '错误码' + res.result.errCode,
image: '../../images/info.png',
duration: 2000
})
console.log(res.result)
} else {
wx.showToast({
title: '成功',
icon: 'success',
duration: 2000
})
wx.setStorageSync('refreshMain', true)
wx.setStorageSync('refreshMine', true)
this.setData({
adds: null,
tag: null,
isfree: false,
imgList: [],
showSubmit: false,
showError: false,
photos: [],
title: '',
details: '',
price: '',
})
}
}).catch(err => {
wx.hideLoading({})
console.log(err)
wx.showToast({
title: '错误请重试',
image: '../../images/info.png',
duration: 2000
})
})
},
// 点击我填完了校验内容
judge(e) {
let d = this.data
if (d.title && d.details && d.adds && d.tag && d.price !== null) {
this.setData({
showSubmit: true
})
} else {
this.setData({
showError: true
})
}
},
//点击确认上传,判断是否注册
judgeHasSign(e) {
var userInfo = wx.getStorageSync('userInfoDetail');
if (!userInfo.isSign) {
wx.showModal({
title: '警告',
content: '您还未注册',
cancelText: '放弃',
confirmText: '去注册',
success: res => {
if (res.confirm) {
wx.navigateTo({
url: '../signin/signin?needrefresh=true'
})
}
}
})
} else {
// 校验通过,申请发送完成的信息
this.requestSubscribeMessage(e);
}
},
hideModal(e) {
this.setData({
showSubmit: false,
showError: false
})
},
})