uniapp微信小程序开发工具本地获取指定页面二维码

只能临时使用,线上还是要访问后端拿二维码

代码:

javascript 复制代码
<template>
	<view>
		<form @submit="sendinfo">
			<view class="uni-form-item uni-column">
				<view class="title">页面路径</view>
				<input class="uni-input" name="input" v-model="path" placeholder="pages/index/index" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">宽度</view>
				<input class="uni-input" name="input" v-model="width" placeholder="二维码宽度" />
			</view>
			<view class="uni-btn-v">
				<button form-type="submit">获取二维码</button>
			</view>
		</form>
		<view style="text-align:center;">
			<image v-if="codeimg" :src="codeimg" style="width:300rpx;height:300rpx;" @click="showimg" mode="widthFix">
			</image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				path: "pages/index/index", //""
				width: 500,
				codeimg: "",
			}
		},
		onLoad() {

		},
		methods: {
			sendinfo() {
				if (!this.path) return uni.showToast({
					title: "请输入路径",
					icon: 'error'
				});
				if (uni.getStorageSync('access_token')) {
					this.getcode();
				} else
					wx.request({
						url: 'https://api.weixin.qq.com/cgi-bin/token',
						method: "GET",
						data: {
							appid: '',
							secret: '',
							grant_type: 'client_credential'
						},
						success: res => {
							console.log("huoqutoken", res)
							uni.setStorageSync('access_token', res.data.access_token);
							this.getcode();
						}
					})
			},
			getcode() {
				let access_token = uni.getStorageSync('access_token');
				wx.request({
					url: 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=' + access_token, //
					method: "POST",
					responseType: "arrayBuffer",
					data: {
						"path": this.path,
						"width": this.width
					},
					success: info => {
						uni.showToast({
							title: "获取成功"
						});
						// console.log("code",info.data)
						const fileSystem = wx.getFileSystemManager();
						const filePath = wx.env.USER_DATA_PATH + '/tempImage' + Date.now() + '.png'; // 临时文件路径
						// 写入文件
						fileSystem.writeFile({
							filePath: filePath,
							data: info.data, // 直接使用字节流 一定要写
							encoding: 'binary',
							success: (res) => {
								console.error('文件保存:', res);
								this.codeimg = filePath;
								wx.saveImageToPhotosAlbum({
									filePath: filePath
								})
							},
							fail: (err) => {
								console.error('文件保存失败:', err);
							}
						});
					}
				})
			},
			showimg() {
				wx.previewImage({
					current: this.codeimg, // 当前显示图片的http链接
					urls: [this.codeimg] // 需要预览的图片http链接列表
				})
			},
		}
	}
</script>

<style scoped>
	.uni-form-item {
		display: flex;
		border-bottom: 2rpx solid #eee;
		margin: 10rpx 20rpx;
		padding: 10rpx 0;
	}

	.uni-form-item .title {
		width: 200rpx;
	}

	.uni-form-item .uni-input {
		flex: 1;
	}

	.uni-btn-v {
		width: 400rpx;
		margin: 50rpx auto;
	}
</style>
相关推荐
栀秋66613 小时前
🌟从“抽卡式编程”到规范驱动: 深度解析「Vibe Coding」的三层跃迁
微信小程序·llm·vibecoding
Reece_13 小时前
微信小程序接入微信支付全流程指南(CloudBase / JSAPI / 真机可用)
微信·微信小程序·小程序
毕设源码-郭学长18 小时前
【开题答辩全过程】以 酒店预约微信小程序为例,包含答辩的问题和答案
微信小程序·小程序
week_泽19 小时前
小程序云数据库查询操作_2
数据库·小程序
说私域20 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的线上线下流量转化运营策略研究
人工智能·小程序·开源
项目題供诗20 小时前
微信小程序黑马优购(项目)(十三)
微信小程序·小程序
San3020 小时前
拒绝手写代码!用 Vibe Coding + SDD 范式“搓”一个微信计分小程序
微信小程序·trae·vibecoding
一点晖光20 小时前
uview 的u-tabs组件在微信小程序中会出现横向滚动条
微信小程序·安卓·横向滚动条
万岳科技系统开发21 小时前
开源知识付费源码:实现在线课程系统与会员管理
开发语言·小程序
青青子衿越21 小时前
微信小程序垂直滚动效果显示通知消息(无限循环滚动)
微信小程序·小程序