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>
相关推荐
一匹电信狗1 小时前
【C++】异常详解(万字解读)
服务器·c++·算法·leetcode·小程序·stl·visual studio
我叫黑大帅2 小时前
微信小程序分包:告别加载慢,像拆快递一样简单!
前端·微信小程序
两个月菜鸟5 小时前
vue+微信小程序 五角星
前端·vue.js·微信小程序
说私域9 小时前
基于开源AI智能客服、AI智能名片与S2B2C商城小程序的微商服务优化及复购转介绍提升策略研究
人工智能·小程序
熬耶1 天前
Uniapp之微信小程序自定义底部导航栏形态
微信小程序·小程序·uni-app
IT毕设实战小研1 天前
基于Spring Boot校园二手交易平台系统设计与实现 二手交易系统 交易平台小程序
java·数据库·vue.js·spring boot·后端·小程序·课程设计
weixin_177297220691 天前
剧本杀小程序系统开发:重构推理娱乐生态
小程序·重构·娱乐
IT毕设实战小研2 天前
基于SpringBoot的救援物资管理系统 受灾应急物资管理系统 物资管理小程序
java·开发语言·vue.js·spring boot·小程序·毕业设计·课程设计
誰在花里胡哨2 天前
微信小程序实现陀螺仪卡片景深效果
前端·微信小程序·动效
XXXFIRE2 天前
微信小程序开发实战笔记:全流程梳理
前端·微信小程序