// pages/application/index.js import { ajax, ele_height } from "../../utils/util"; import { base } from "../../config/index" const { globalData: { systemInfo } } = getApp(); Page({ /** * 页面的初始数据 */ data: { multiIndex: [0, 0, 0], multiArray: [], uploadheight: 0, local_image_url: '', group_class: '幼儿组', works_class: '中国画', author: "", phone: wx.getStorageSync('phone') || "", schoolClass: "", teacher: "", teacherPhone: "", area: "", article: "", brief: "" }, area: [], changeSelectArea: [0, 0, 0], change(e) { const key = e.target.dataset.key || ''; const value = e.detail.value; const p = {}; p[key] = value; this.setData(p); }, bindMultiPickerChange: function (e) { this.setData({ multiIndex: e.detail.value }) }, uploadimg(e) { this.setData({ uploadheight: ele_height(e, systemInfo.screenWidth * 0.8) }) }, upload() { wx.chooseMedia({ count: 1, mediaType: ['image'], success: e => { this.setData({ local_image_url: e.tempFiles[0].tempFilePath }) }, }) }, delimg() { wx.showModal({ title: "确定删除该图片?", content: "删除后需重新选择图片", success: (res) => { if (res.confirm) { this.setData({ local_image_url: '' }) } } }) }, bindMultiPickerColumnChange(e) { const { detail: { column, value } } = e; this.changeSelectArea[column] = value; const a0 = this.area[this.changeSelectArea[0]] || { child: [] } const multiArray1 = a0.child.map(v => v.name); let multiArray2 = ((a0.child[this.changeSelectArea[1]] || { child: [] }).child || []).map(v => v.name); this.setData({ multiArray: [this.data.multiArray[0], multiArray1, multiArray2] }) }, getArea() { ajax({ urlType: "fileurl", api: "/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/narea.json?data=" + Date.now() }).then(res => { this.area = res || []; this.initArea(); }) }, initArea() { const a0 = this.area[0] || { child: [] } const multiArray0 = this.area.map(v => v.name); const multiArray1 = a0.child.map(v => v.name); const multiArray2 = (a0.child[0] || { child: [] }).child.map(v => v.name); this.setData({ multiArray: [multiArray0, multiArray1, multiArray2] }) }, onChange(e) { this.setData({ group_class: e.detail }) }, onChangeWorks(e) { this.setData({ works_class: e.detail }) }, updata() { const { group_class, works_class, author, phone, schoolClass, teacher, teacherPhone, area, local_image_url, article, brief } = this.data; const Authorization = wx.getStorageSync('token') || undefined; if (!Authorization) return wx.showToast({ title: '请登录后在提交', icon: "none" }) let baseArea = this.data.multiArray[0][this.data.multiIndex[0]] + this.data.multiArray[1][this.data.multiIndex[1]]; if (this.data.multiArray[2] && this.data.multiArray[2][this.data.multiIndex[2]]) baseArea += this.data.multiArray[2][this.data.multiIndex[2]] if (isNaN(phone)) return wx.showToast({ title: '手机号需填入数字', icon: "none" }) if (!group_class || !works_class || !author || !schoolClass || !area || !local_image_url || !article || !brief) return wx.showToast({ title: '请输入完整信息', icon: "none" }) wx.showLoading({ title: '加载中', mask: true }) wx.uploadFile({ filePath: local_image_url, name: 'file', header: { Authorization }, formData: { name: author, teacherPhone, school: schoolClass, teacher, area: baseArea + area, articleGroup: works_class, birthday: group_class, articleName: article, articleDescribe: brief, phone }, url: base.apiurl + '/article/save', fail: () => { wx.hideLoading() }, success: res => { wx.hideLoading() if (res.statusCode !== 200) { const data = JSON.parse(res.data || "{}") wx.showToast({ title: data.message || "保存失败", icon: "error" }) return } wx.showToast({ title: '报名成功', icon: "none" }); }, fail(err) { wx.showToast({ title: err.message || "上传失败", icon: "error" }); } }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (!wx.getStorageSync('phone')) { const pages = getCurrentPages(); const thisPage = pages[pages.length - 1]; wx.reLaunch({ url: "/pages/login/index?backurl=" + thisPage.route }) return } this.getArea(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: __wxConfig.accountInfo.nickname, path: "/pages/index/index" } } })