wx.showModal 请求并发时处理

针对安卓设备上wx.showModal请求并发导致的多个弹窗提示问题,解决方案是设置全局变量isShowModel来控制弹窗显示。当登录状态过期且isShowModel为false时,显示模态框,用户操作后更新isShowModel为true,避免重复弹出。在登录成功后将isShowModel重置为false。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:wx.showModal 请求并发时 安卓出现多个弹窗提示 ios 只出现一次
解决:安卓出现多次提示

解决方法:在全局变量中加一个标识isShowModel 默认为false,请求封装时 根据后端状态码进行判断如果为失效状态码 并且 isShowModel 为false 时 弹出模态框 ,当用户点击确定或取消时isShowModel 修改为true 登录时isShowModel 修改为false 即可

在这里插入图片描述

app.js

    globalData: {
        isShowModel: false,
    }

登录 代码

wx.requeset({
	...
	success:{
		 getApp().globalData.isShowModel = false
	}
})

部分封装的核心代码

function success(res) {
	if (res.success) {
		return res
	} else {
		if (!res.success && res.errCode == "USER50001") {
			wx.removeStorage({
				key: 'Authorization',
			})
			wx.removeStorage({
				key: 'Cookie',
			})

			if (!getApp().globalData.isShowModel) {
				wx.showModal({
					title: "提示",
					content: '登录已过期,请重新登录!',
					success: function (res) {
						if (res.confirm) {
							wx.reLaunch({
								url: '/pages/login/index',
							})
							getApp().globalData.isShowModel = true

						} else if (res.cancel) {
							wx.reLaunch({
								url: '/pages/login/index',
							})
							getApp().globalData.isShowModel = true
						}
					}
				})
			}
		} else {
			wx.showToast({
				icon: 'none',
				title: res.msg,
			})
		}

	}
}
Page({ /** * 页面的初始数据 */ data: { imgList:[], content:'' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, // 111111上传图片 chooseimage(){ let that=this; let remaining=9-that.data.imgList.length; if(remaining<=0){ wx.showToast({ title: '最多只能上传9张图片', icon:'none' }) return; } wx.chooseImage({ count:remaining,//count表示数量,上传的个数 sizeType:['original'],//所选的图片的尺寸 sourceType:['album','camera'],//选择图片的来源 success(res){ wx.showLoading({ title: '上传中', }) // lingshi // 获取临文件路径数组 const tempFilePaths = res.tempFilePaths; // 将新选择的图片路径追加到imgList中 let imgList = that.data.imgList.concat(tempFilePaths); // 如果图片数量超过9,则截取前9张 // ? if (imgList.length > 9) { imgList = imgList.slice(0, 9); // 可以给出提示 wx.showToast({ title: '最多只能选择9张图片', icon: 'none' }); } // ? // 更新数据 that.setData({ imgList: imgList }); wx.hideLoading(); wx.showLoading({ title: '上传中', }) wx.chooseImage({ count: 9 - that.data.imgList.length, // 计算还可以选择多少张 sizeType: ['original'], sourceType: ['album', 'camera'], success(res) { const tempFilePaths = res.tempFilePaths; let imgList = that.data.imgList.concat(tempFilePaths); that.setData({ imgList: imgList }); }, // ? complete() { wx.hideLoading(); // 无论成功失败,选择结束就隐藏loading } }) } }) }, // 查看图片 skanImage(e){ let imgList=this.data.imgList let index=e.currentTarget.dataset.index wx.previewImage({ urls: imgList, current:imgList[index] }) }, //删除图片 deleteimage(e){ let that=this let dele=e.currentTarget.dataset.src let imgList=that.data.imgList wx.showModal({ title: '删除图片', content: '确定删除这种图片吗', success(res){ if (res.confirm) { for(var i=0;i<imgList.length;i++){ if(imgList[i]==dele){ imgList.splice(i,1) that.setData({ imgList }) } } } } }) }, //获取文案 getcontent(e){ let content=e.detail.value this.setData({ content }) }, //点击发布 submitPost() { const { textContent, images } = this.data; if (!textContent && images.length === 0) { wx.showToast({ title: '内容不能为空', icon: 'none' }); return; // 正确引入工具模块 const util = require('./utils.js'); } // 假设只有一个图片上传接口 wx.uploadFile({ url: 'https://yourdomain.com/api/upload', filePath: images[0], name: 'file', formData: { user: 'testUser' }, success(res) { const imageUrl = JSON.parse(res.data).url; // 将文本内容和图片URL提交到服务器 wx.request({ url: 'https://yourdomain.com/api/post', method: 'POST', data: { content: textContent, image: imageUrl }, success(response) { wx.showToast({ title: '发布成功' }); setTimeout(() => { wx.navigateBack(); // 返回上一页 }, 1500); } }); } }); } }) 我的代码是这样的哪里出错了,还有就是每次我上传图片的候,上传完没点那个上传图片的按钮,然后就又出现那个上传图片的页面了,就是点一次上传页面出现两次,怎么回事,改成不点不上传的形式,并且把错误的地方给改正
最新发布
07-04
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值