uniapp small program code scene parameter stepping pit summary

uniapp encountered a problem in generating applets:

1. The parameter address is too long

2. Receive scene parameter analysis

1. Request server: Obtain the image address of the unlimited applet code and write it into the temporary directory, draw the applet code into the poster to share and forward

uniapp canvas generates poster applet code QR code

            //获取不限制小程序
			getUnlimitedQRCode() {
				uni.showLoading({
					title: '加载中...'
				})
				let that = this;
				var url_link = "lId=" + this.livingId + "&uid=" + this.userInfo.userId
				
				var env_version = "release"
				if (this.$config.dev == 1) {
					env_version = "trial"
				}
				let params = {
					appId: this.userInfo.appId,
					page: 'pages/index/liveDetail',
					scene: encodeURIComponent(url_link),//scene长度不能超过限制,且要encodeURIComponent
					env_version: env_version //'release' 正式版; 'trial'  体验版; 'develop'开发版
				};
                //服务端接口
				getUnlimitedQRCode(
						params
					).then((response) => {
						uni.hideLoading()
						var base64Img = 'data:image/PNG;base64,' + err;
						that.getToLocal(base64Img)
					})
					.catch(err => {
						var base64Img = 'data:image/PNG;base64,' + err;
                        //保存到本地临时目录
						that.getToLocal(base64Img)
					});
			},

             //保存到本地临时目录
			getToLocal(base64data) {
				var that = this
				// var base64data = ""; // base64
				const fsm = wx.getFileSystemManager();
				const FILE_BASE_NAME = 'tmp_base64src'; //自定义文件名
				const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [];
				if (!format) {
					return (new Error('ERROR_BASE64SRC_PARSE'));
				}
				const filePath = `${wx.env.USER_DATA_PATH}/${FILE_BASE_NAME}.${format}`;
				//要转化成 ArrayBuffer 对象的 Base64 字符串 
				const buffer = wx.base64ToArrayBuffer(bodyData);
                //写入文件到临时目录
				fsm.writeFile({
					filePath,//临时目录地址
					data: buffer,
					encoding: 'binary',
					success(r) {
						uni.hideLoading()
						that.qrCode = filePath
					},
					fail() {
						uni.hideLoading()
						return (new Error('ERROR_BASE64SRC_WRITE'));
					},
				});
			},

2. Scan the applet code through the shared poster to enter the applet

Receive the parameters passed by the applet code

async onLoad(options) {
			await this.$onLaunched
			if (options.livingId) { //livingRoom
				this.livingId = options.livingId
			}

			//被邀请用户进入小程序*************start***********
            //接收到的scene要decodeURIComponent
			var decode_link = decodeURIComponent(options.scene)
            //将 lId=1&uid=2格式转化为对象
			let resObj = {}
			let regParam = /([^&=]+)=([\w\W]*?)(&|$|#)/g
			let strParam = decode_link;
			let result
			while ((result = regParam.exec(strParam)) != null) {
				resObj[result[1]] = result[2]
			}

			if (resObj.lId) {
				this.livingId = resObj.lId
			}
			if (resObj.uid) {
				this.inviteUserId = resObj.uid
			}
			//被邀请用户进入小程序*************end***********
},

Through the above operations, you can get the scene parameter object. Welcome to leave a comment and make progress together!

Guess you like

Origin blog.csdn.net/u014724048/article/details/131823162