123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- const app= getApp();
- Page({
- data: {
- contact: '',
- phone: '',
- description: '',
- imageList: [],
- showNotification: true,
- countDown: 3,
- isFormValid: false,
- category: '投诉',
- isPreviewMode: false,
- title: '',
- content: '',
- replyTime: '',
- replyContent: '',
- formSubmitted: false
- },
- onLoad: function(options) {
- this.startCountDown();
- // 检查是否是预览模式
- if (options.mode === 'preview') {
- const id = options.id;
- // 获取数据
- this.getDataById(id);
- // 设置为预览模式
- this.setData({
- isPreviewMode: true
- });
- } else {
- // 非预览模式才检查表单有效性
- this.checkFormValidity();
- }
- },
- onShow: function() {
- // 检查是否是从成功页面返回
- if (this.data.formSubmitted) {
- // 重置表单数据
- this.resetForm();
- // 重置提交状态标记
- this.setData({
- formSubmitted: false
- });
- }
- },
- startCountDown: function() {
- let that = this;
- let timer = setInterval(function() {
- if (that.data.countDown > 0) {
- that.setData({
- countDown: that.data.countDown - 1
- });
- } else {
- clearInterval(timer);
- }
- }, 1000);
- },
- goBack: function() {
- wx.navigateBack();
- },
- radioChange: function(e) {
- this.setData({
- category: e.detail.value
- });
- this.checkFormValidity();
- },
- inputContact: function(e) {
- this.setData({
- contact: e.detail.value
- });
- this.checkFormValidity();
- },
- inputPhone: function(e) {
- const value = e.detail.value;
- const phoneNumber = value.replace(/\D/g, '');
- this.setData({
- phone: phoneNumber
- });
- this.checkFormValidity();
- },
- validatePhone: function(phone) {
- const phoneReg = /^1[3-9]\d{9}$/;
- return phoneReg.test(phone);
- },
- inputDescription: function(e) {
- this.setData({
- description: e.detail.value
- });
- this.checkFormValidity();
- },
- chooseImage: function() {
- let that = this;
- if (that.data.imageList.length >= 10) {
- wx.showToast({
- title: '最多只能上传10张图片',
- icon: 'none'
- });
- return;
- }
- wx.chooseMedia({
- count: 10 - that.data.imageList.length,
- mediaType: ['image'],
- sourceType: ['album', 'camera'],
- sizeType: ['compressed'],
- success: function(res) {
- let tempFiles = res.tempFiles;
- let validFiles = [];
- for (let i = 0; i < tempFiles.length; i++) {
- const file = tempFiles[i];
- if (file.size <= 10 * 1024 * 1024) {
- validFiles.push(file);
- } else {
- wx.showToast({
- title: '图片大小不能超过10M',
- icon: 'none'
- });
- }
- }
- if (validFiles.length > 0) {
- let newImageList = that.data.imageList.concat(validFiles);
- that.setData({
- imageList: newImageList
- });
- }
- }
- });
- },
- previewImage: function(e) {
- let index = e.currentTarget.dataset.index;
- wx.previewImage({
- current: this.data.imageList[index],
- urls: this.data.imageList
- });
- },
- deleteImage: function(e) {
- let index = e.currentTarget.dataset.index;
- let imageList = this.data.imageList;
- imageList.splice(index, 1);
- this.setData({
- imageList: imageList
- });
- },
- checkFormValidity: function() {
- const { contact, phone, description, category } = this.data;
- // 只检查必填项:联系人、联系电话和内容说明
- const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== '';
- this.setData({
- isFormValid: isValid
- });
- return isValid;
- },
- onInputChange: function(e) {
- const { field } = e.currentTarget.dataset;
- const { value } = e.detail;
- this.setData({
- [field]: value
- });
- this.checkFormValidity();
- },
- bindPickerChange: function(e) {
- this.checkFormValidity();
- },
- // 添加submitForm方法
- submitForm: function() {
- if (!this.checkFormValidity()) {
- let errorMsg = '';
- if (!this.data.contact.trim()) {
- errorMsg = '请填写联系人';
- } else if (!this.validatePhone(this.data.phone)) {
- errorMsg = '请输入正确的联系电话';
- } else if (!this.data.description.trim()) {
- errorMsg = '请填写内容说明';
- }
- wx.showToast({
- title: errorMsg,
- icon: 'none'
- });
- return;
- }
- debugger;
- const fileManager = wx.getFileSystemManager();
- this.data.imageList.map(imgInfo=>{
- const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
- imgInfo.base64=base64;
- return imgInfo;
- })
- // 构建提交数据
- const submitData = {
- category: this.data.category,
- contact: this.data.contact,
- phone: this.data.phone,
- description: this.data.description,
- images: this.data.imageList
- };
- console.log('提交的数据:', submitData);
- wx.showLoading({
- title: '提交中...',
- });
- debugger
- wx.request({
- url: app.globalData.interfaceUrls.feedback,
- method: 'POST',
- header: {
- 'content-type': 'application/json', // 默认值
- 'token': app.globalData.userWxInfo.token,
- 'source': "wc",
- '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
- },
- data:submitData,
- success(res) {
- wx.hideLoading();
- if(res.data.code=='200'){
- wx.navigateTo({
- url: '/pages/tousujianyiSuccess/tousujianyiSuccess',
- });
- }
- },
- fail(error) {
- wx.hideLoading()
- utils.simleInfo('登记失败,请稍后再试')
- }
- })
- // 在提交成功后设置标记
- this.setData({
- formSubmitted: true
- });
- },
- submitRepair: function() {
- this.submitForm();
- },
- // 根据ID获取数据
- getDataById: function(id) {
- // 这里应该是从服务器获取数据
- // 但为了演示,我们从本地数据中获取
- const pages = getCurrentPages();
- const prevPage = pages[pages.length - 2]; // 获取上一个页面
- if (prevPage && prevPage.data.noticeList) {
- const item = prevPage.data.noticeList.find(item => item.id == id);
- debugger
- if (item) {
- const formatTime = (timeString) => {
- if (!timeString) return ''; // 如果时间为空,返回空字符串
- const date = new Date(timeString);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份补零
- const day = String(date.getDate()).padStart(2, '0'); // 日期补零
- return `${year}-${month}-${day}`;
- };
- this.setData({
- category: item.category || '投诉',
- description: item.description || '',
- contact: item.feedbackperson || '',
- description: item.replynote || '',
- phone: item.contactnumber || '',
- imageList: item.attachments || [],
- replyTime: formatTime(item.replytime) || '',
- replyContent: item.replycontent || ''
- });
- }
- }
- },
- // 添加重置表单的方法
- resetForm: function() {
- this.setData({
- contact: '',
- phone: '',
- description: '',
- imageList: []
- });
- }
- });
|