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>
相关推荐
毕设源码-赖学姐16 小时前
【开题答辩全过程】以 “饭否”食材搭配指南小程序的设计与实现为例,包含答辩的问题和答案
小程序
nodcloud16 小时前
点可云进销存商城如何部署在微信小程序
微信小程序·小程序
老华带你飞1 天前
畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·畅阅读系统小程序
老华带你飞1 天前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
项目題供诗1 天前
微信小程序开发教程(八)
微信小程序·小程序
00后程序员张2 天前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
低代码布道师2 天前
少儿舞蹈小程序(12)作品列表查询搭建
低代码·小程序
编程迪2 天前
基于Java+Vue开发的家政服务系统源码适配H5小程序APP
小程序·家政小程序·家政系统源码·家政系统·家政源码
拼图2092 天前
微信小程序——云函数【使用使用注意事项】
微信小程序·小程序
fakaifa2 天前
【独立版】智创云享知识付费小程序 v5.0.23+小程序 搭建教程
小程序·uni-app·知识付费·源码下载·智创云享独立版