小程序 IOS里面chooseMedia无法使用,安卓正常,是什么问题?
发布于 4 年前 作者 min58 8430 次浏览 来自 问答

IOS里面提示errMsg: “chooseMedia:fail function cannot run on service

2 回复

请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

wx.chooseMedia({
					count: this.selectNum, //默认9
					success: (chooseImageRes) => {
						
						chooseImageRes.tempFilePaths = []
						for (var i = 0; i < chooseImageRes.tempFiles.length; i++) {
							chooseImageRes.tempFilePaths = chooseImageRes.tempFilePaths.concat(chooseImageRes.tempFiles[i].tempFilePath)
							chooseImageRes.tempFiles[i].path = chooseImageRes.tempFiles[i].tempFilePath
							if(chooseImageRes.tempFiles[i].fileType == 'video'){
								chooseImageRes.tempFiles[i].tempFilePath = chooseImageRes.tempFiles[i].path.replace(/^((https|http|ftp|rtsp|mms)?:\/\/)+tmp/g,'https://cdn-datacenter-oss.itanzi.com')
							}
							
						}
						for (var i = 0; i < chooseImageRes.tempFilePaths.length; i++) {
							
								if(chooseImageRes.tempFiles[i].size <= 10240000){
								var imageWidth = ''
								var imageHeight = ''
								uni.getImageInfo({
								            src: chooseImageRes.tempFilePaths[i],
								            success: function (image) {
												console.log(image);
								                console.log(image.width);
								                console.log(image.height);
												imageWidth = image.width
												imageHeight = image.height
								            }
								        });
							let file = chooseImageRes.tempFilePaths[i];
							let index = file.lastIndexOf('/');
							let len = file.length
							let name = file.substring(index + 1, len)
							// 交给七牛上传
							let newLen = this.selectNum-this.imgList.length
							if (newLen > 0) {
								var that = this;
								qiniuUploader.upload(file, (res) => {
									let inLen = that.selectNum- that.imgList.length
									if (inLen > 0) {
										this.$http.post(this.$http.checkImg, {
											content: "https://cdn-ycd-oss.youchengdu.net/" + res.key + "?imageView2/1/w/200/h/200"
										}).then(function(chekRes) {
											if(chekRes.data.data.errcode == 0){
												that.imgKeyList.push({
														url: "https://cdn-ycd-oss.youchengdu.net/" + res.key,
														type: chooseImageRes.type == 'video' ? 1 : 0,
														thumb: "https://cdn-ycd-oss.youchengdu.net/" + res.key,
														width: imageWidth,
														height: imageHeight
													})
												that.imgList.push(file);
											}
											else{
												uni.showToast({
													title: '图片内容不合法!',
													icon: 'none'
												});
											}
										}, function(err) {
											uni.showToast({
												title: '校验图片失败',
												icon: 'none'
											});
										});
									}
									
									this.$emit('returnImgUrl', that.imgKeyList) //将图片回传父页面
									// 选择成功后
									if(this.imgKeyList.length==this.selectNum){
										this.isShowAdd=false
									}
									// // for (var i = 0; i < that.imgKeyList.length; i++) {
									// 	that.imgKeyList[that.imgKeyList.length-1]= 'https://cdn-datacenter-oss.itanzi.com/' + that.imgKeyList[that.imgKeyList.length-1]
									// // }
									// that.imgKeyList[that.imgKeyList.length-1]= {
									// 	url: that.imgKeyList[that.imgKeyList.length-1],
									// 	type: chooseImageRes.type == 'video' ? 1 : 0
									// }
									// this.$emit('returnImgUrl', that.imgKeyList) //将图片回传父页面
								}, (error) => {
									console.log('error: ' + error);
								}, {
									region: 'SCN',
									key: name,
									uptoken: this.token, // 由其他程序生成七牛 uptoken
								}, (res) => {
									console.log( '正在上传:' +res.progress +"%")
									uni.showLoading({
									    title: res.progress +"%"
									});
								}, () => {
									// 取消上传
								}, () => {
									// `before` 上传前执行的操作
								}, (err) => {
									uni.hideLoading()
									// `complete` 上传接受后执行的操作(无论成功还是失败都执行)
								});
							}
							}
						}
					},
					fail: (fail)=>{
						console.log(fail)
					}
				})
回到顶部