123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733 |
- const app = getApp();
- Page({
- data: {
- address: app.globalData.currentAccountInfo.address,
- contact: '',
- phone: '',
- repairType: '',
- repairTypeValue: '',
- repairTypeMap: [
- {name: '换表', value: '1'},
- {name: '换前阀', value: '2'},
- {name: '换后阀', value: '3'},
- {name: '换前后阀', value: '4'},
- {name: '其他', value: '5'}
- ],
- description: '',
- imageList: [],
- showNotification: true,
- countDown: 3,
- isFormValid: false,
- isPreviewMode: false,
- replyTime: '',
- replyContent: '',
- id: '',
- mode: '',
- isReplied: false,
- formSubmitted: false,
- isSubmitting: false, // 新增保存标志位
- lastSubmitTime: 0, // 上次提交时间(通过防抖(Debounce)技术,限制用户在一定时间内只能提交一次。)
- },
- onLoad: function (options) {
- const isReplied = options.isReplied === 'true';
- // 更新地址信息,确保是最新的户号地址
- this.setData({
- address: app.globalData.currentAccountInfo.address
- });
- if (options.mode === 'preview') {
- this.setData({
- isPreviewMode: true,
- showNotification: false,
- id: options.id,
- mode: options.mode,
- isReplied: isReplied
- });
- this.loadPreviewData(options.id);
- } else {
- this.startCountDown();
- this.setData({
- id: options.id || '',
- mode: options.mode || '',
- isReplied: isReplied
- });
- }
- },
- 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);
- },
- closeNotification: function () {
- if (this.data.countDown <= 0) {
- this.setData({
- showNotification: false
- });
- }
- },
- goBack: function () {
- wx.navigateBack();
- },
- 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);
- },
- showRepairTypeSelector: function () {
- let that = this;
- // 只显示名称列表
- const typeNames = this.data.repairTypeMap.map(item => item.name);
-
- wx.showActionSheet({
- itemList: typeNames,
- success: function (res) {
- const selectedType = that.data.repairTypeMap[res.tapIndex];
- that.setData({
- repairType: selectedType.name,
- repairTypeValue: selectedType.value
- });
- that.checkFormValidity();
- }
- });
- },
- 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) {
- wx.showLoading({
- title: '图片压缩中...',
- mask: true
- });
-
- let processedFiles = new Array(validFiles.length);
- let processCount = 0;
-
- // 处理每个图片
- validFiles.forEach((file, index) => {
- that.compressImage(file.tempFilePath).then(compressedPath => {
- processCount++;
- // 获取压缩后图片的信息
- wx.getFileInfo({
- filePath: compressedPath,
- success: (fileInfo) => {
- console.log('原始大小:', file.size / 1024, 'KB');
- console.log('压缩后大小:', fileInfo.size / 1024, 'KB');
-
- // 在对应位置添加压缩后的图片路径
- processedFiles[index] = {
- tempFilePath: compressedPath,
- size: fileInfo.size
- };
-
- // 检查是否所有图片都已处理完成
- if (processCount === validFiles.length) {
- // 过滤掉可能的空值,并按顺序合并
- let newImageList = that.data.imageList.concat(processedFiles.filter(item => item));
- that.setData({
- imageList: newImageList
- });
- wx.hideLoading();
- }
- },
- fail: (err) => {
- console.error('获取文件信息失败:', err);
- processCount++;
- // 如果失败,也要检查是否处理完所有图片
- if (processCount === validFiles.length) {
- wx.hideLoading();
- }
- }
- });
- }).catch(err => {
- console.error('图片压缩失败:', err);
- processCount++;
- if (processCount === validFiles.length) {
- wx.hideLoading();
- }
- });
- });
- }
- }
- });
- },
- // 使用canvas进行图片压缩
- compressImage: function(imagePath) {
- return new Promise((resolve, reject) => {
- // 获取图片信息
- wx.getImageInfo({
- src: imagePath,
- success: (res) => {
- // 先获取原图大小,单位KB
- wx.getFileInfo({
- filePath: imagePath,
- success: (fileInfo) => {
- const originalSize = fileInfo.size / 1024; // 原始大小,单位KB
- // 根据原图大小确定压缩质量
- let targetQuality = 0.8; // 默认压缩质量
-
- if (originalSize <= 500) {
- // 如果原图已经小于500KB,保持较高质量
- targetQuality = 0.9;
- } else if (originalSize <= 1024) {
- // 1MB以下,适当压缩
- targetQuality = 0.7;
- } else if (originalSize <= 2048) {
- // 2MB以下,中等压缩
- targetQuality = 0.5;
- } else if (originalSize <= 5120) {
- // 5MB以下,较大压缩
- targetQuality = 0.3;
- } else {
- // 超过5MB,大幅压缩
- targetQuality = 0.2;
- }
- wx.createSelectorQuery()
- .select('#compressCanvas')
- .fields({ node: true, size: true })
- .exec((canvasRes) => {
- if (!canvasRes || !canvasRes[0] || !canvasRes[0].node) {
- // 如果找不到canvas节点,则返回原图
- resolve(imagePath);
- return;
- }
-
- const canvas = canvasRes[0].node;
- const ctx = canvas.getContext('2d');
- const image = canvas.createImage();
-
- image.onload = () => {
- // 计算要缩放的尺寸,保持宽高比
- let ratio = 1;
- // 根据原图大小调整尺寸
- if (originalSize > 5120) { // 5MB以上
- ratio = Math.min(1, 1200 / res.width, 1200 / res.height);
- } else if (originalSize > 2048) { // 2MB以上
- ratio = Math.min(1, 1500 / res.width, 1500 / res.height);
- } else if (originalSize > 1024) { // 1MB以上
- ratio = Math.min(1, 1800 / res.width, 1800 / res.height);
- } else {
- ratio = Math.min(1, 2000 / res.width, 2000 / res.height);
- }
-
- const targetWidth = Math.round(res.width * ratio);
- const targetHeight = Math.round(res.height * ratio);
-
- // 设置canvas尺寸
- canvas.width = targetWidth;
- canvas.height = targetHeight;
- // 清除画布
- ctx.clearRect(0, 0, targetWidth, targetHeight);
- // 绘制图片
- ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
- // 压缩参数
- const compressOptions = {
- canvas: canvas,
- width: targetWidth,
- height: targetHeight,
- destWidth: targetWidth,
- destHeight: targetHeight,
- fileType: 'jpg',
- quality: targetQuality
- };
-
- // 第一次尝试压缩
- const tryCompress = (quality, attempt = 1) => {
- compressOptions.quality = quality;
-
- wx.canvasToTempFilePath({
- ...compressOptions,
- success: (result) => {
- // 检查压缩后的大小
- wx.getFileInfo({
- filePath: result.tempFilePath,
- success: (compressedInfo) => {
- const compressedSize = compressedInfo.size / 1024; // 压缩后大小,单位KB
- console.log(`压缩后大小 (质量:${quality})`, compressedSize, 'KB');
- // 如果压缩后大小仍大于500KB且尝试次数未达上限,继续压缩
- if (compressedSize > 500 && attempt < 3) {
- // 递减质量
- const newQuality = Math.max(0.1, quality - 0.2);
- console.log(`尝试重新压缩,质量降低至${newQuality}`);
- tryCompress(newQuality, attempt + 1);
- }
- // 如果压缩后小于200KB且质量非最高,尝试提高质量
- else if (compressedSize < 200 && quality < 0.9 && attempt < 3) {
- // 递增质量
- const newQuality = Math.min(0.9, quality + 0.2);
- console.log(`尝试重新压缩,质量提高至${newQuality}`);
- tryCompress(newQuality, attempt + 1);
- }
- else {
- // 返回压缩后的图片
- resolve(result.tempFilePath);
- }
- },
- fail: (error) => {
- console.error('获取压缩后图片信息失败:', error);
- resolve(result.tempFilePath);
- }
- });
- },
- fail: (error) => {
- console.error('Canvas转图片失败:', error);
- // 如果转换失败则返回原图
- resolve(imagePath);
- }
- });
- };
-
- // 开始尝试压缩
- tryCompress(targetQuality);
- };
-
- image.onerror = () => {
- console.error('图片加载失败');
- resolve(imagePath);
- };
-
- image.src = imagePath;
- });
- },
- fail: (error) => {
- console.error('获取原始图片信息失败:', error);
- resolve(imagePath);
- }
- });
- },
- fail: (error) => {
- console.error('获取图片信息失败:', error);
- resolve(imagePath);
- }
- });
- });
- },
- 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,
- address,
- repairType,
- repairTypeValue,
- description
- } = this.data;
- const hasAddress = address && address.trim() !== '';
- const hasContact = contact && contact.trim() !== '';
- const hasPhone = phone && phone.trim() !== '';
- const hasValidPhone = this.validatePhone(phone);
- const hasRepairType = repairType && repairType.trim() !== '';
- const hasRepairTypeValue = repairTypeValue && repairTypeValue.trim() !== '';
- const hasDescription = description && description.trim() !== '';
- const isValid = hasAddress && hasContact && hasPhone && hasValidPhone && hasRepairType && hasRepairTypeValue && hasDescription;
- 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();
- },
- submitRepair: function () {
- if (!this.checkFormValidity()) {
- wx.showToast({
- title: '请填写完整信息',
- icon: 'none'
- });
- return;
- }
- if (!this.validatePhone(this.data.phone)) {
- wx.showToast({
- title: '请输入正确的手机号',
- icon: 'none'
- });
- return;
- }
- const submitData = {
- address: this.data.address,
- contact: this.data.contact,
- phone: this.data.phone,
- repairType: this.data.repairType,
- repairTypeValue: this.data.repairTypeValue,
- description: this.data.description,
- images: this.data.imageList
- };
- console.log('提交的数据:', submitData);
- wx.showLoading({
- title: '提交中...',
- });
- setTimeout(() => {
- wx.hideLoading();
- wx.showToast({
- icon: 'success',
- duration: 2000,
- success: function () {
- setTimeout(() => {
- wx.navigateTo({
- url: '/pages/baoxiuSuccess/baoxiuSuccess',
- });
- }, 2000);
- }
- });
- }, 1500);
- },
- loadPreviewData: function (id) {
- wx.showLoading({
- title: '加载中...',
- });
- // 从上一个页面获取数据
- const pages = getCurrentPages();
- const prevPage = pages[pages.length - 2]; // 获取上一个页面
- if (prevPage && prevPage.data && prevPage.data.noticeList) {
- // 根据id查找对应的报修项
- const item = prevPage.data.noticeList.find(item => item.id == id);
- debugger
- if (item) {
- // 找到对应的报修类型名称
- let repairTypeName = '';
- const repairTypeValue = item.repairtype || '';
- const repairTypeItem = this.data.repairTypeMap.find(type => type.name === repairTypeValue);
- if (repairTypeItem) {
- repairTypeName = repairTypeItem.name;
- }
- // 格式化时间
- 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({
- address: item.address || '',
- contact: item.contact || '',
- phone: item.contactnumber || '',
- repairType: repairTypeName,
- repairTypeValue: repairTypeValue,
- description: item.faultdescription || '',
- imageList: item.attachments || [],
- replyTime: item.isReplied ? formatTime(item.repairtime) : '',
- replyContent: item.isReplied ? item.remark : ''
- });
- }
- }
- wx.hideLoading();
- },
- submitForm: function () {
- // 如果正在提交中,直接返回
- if (this.data.isSubmitting) {
- return;
- }
- if (!this.data.address || this.data.address.trim() === '') {
- wx.showToast({
- title: '请填写地址',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.data.contact || this.data.contact.trim() === '') {
- wx.showToast({
- title: '请填写联系人',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.data.phone || this.data.phone.trim() === '') {
- wx.showToast({
- title: '请填写联系电话',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.validatePhone(this.data.phone)) {
- wx.showToast({
- title: '请输入正确的手机号',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.data.repairType || this.data.repairType.trim() === '') {
- wx.showToast({
- title: '请选择报修类型',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.data.repairTypeValue || this.data.repairTypeValue.trim() === '') {
- wx.showToast({
- title: '请选择报修类型',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- if (!this.data.description || this.data.description.trim() === '') {
- wx.showToast({
- title: '请填写故障说明',
- icon: 'none'
- });
- this.setData({
- isSubmitting: false
- });
- return;
- }
- const now = Date.now();
- const lastSubmitTime = this.data.lastSubmitTime;
- // 如果距离上次提交时间小于 2 秒,直接返回
- if (now - lastSubmitTime < 2000) {
- // wx.showToast({
- // title: '请勿重复提交',
- // icon: 'none',
- // });
- return;
- }
- // 更新上次提交时间
- this.setData({
- lastSubmitTime: now,
- });
- const fileManager = wx.getFileSystemManager();
- this.data.imageList.map(imgInfo => {
- const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
- imgInfo.base64 = base64;
- return imgInfo;
- })
- const submitData = {
- address: this.data.address,
- contact: this.data.contact,
- phone: this.data.phone,
- repairType: this.data.repairTypeValue,
- description: this.data.description,
- images: this.data.imageList,
- userName: app.globalData.currentAccountInfo.username,
- userNum: app.globalData.currentAccountInfo.usernumber
- };
- // 设置正在提交中 防止重复点击提交按钮
- this.setData({
- isSubmitting: true,
- });
- console.log('提交的数据:', submitData);
- wx.showLoading({
- title: '提交中...',
- mask: true
- });
- const that = this;
- wx.request({
- url: app.globalData.interfaceUrls.repairRegistration,
- 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/baoxiuSuccess/baoxiuSuccess',
- });
- }
- that.setData({
- isSubmitting: false
- });
- },
- fail(error) {
- wx.hideLoading();
- wx.showToast({
- title: '登记失败,请稍后再试',
- icon: 'none'
- });
- that.setData({
- isSubmitting: false
- });
- },
- complete: () => {
- this.setData({
- isSubmitting: false, // 提交完成,重置标志位,可继续提交
- formSubmitted: true // 在提交成功后设置标记,返回将重置表单
- });
- },
- })
- },
- inputAddress: function (e) {
- this.setData({
- address: e.detail.value
- });
- },
- onShow: function () {
- // 检查是否是从成功页面返回
- if (this.data.formSubmitted) {
- // 重置表单数据
- this.resetForm();
- // 重置提交状态标记
- this.setData({
- formSubmitted: false
- });
- } else if (!this.data.isPreviewMode) {
- // 非预览模式下,更新地址信息
- this.setData({
- address: app.globalData.currentAccountInfo.address
- });
- }
- },
- // 添加重置表单的方法
- resetForm: function () {
- this.setData({
- contact: '',
- phone: '',
- repairType: '',
- repairTypeValue: '',
- description: '',
- imageList: []
- });
- },
- });
|