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>
相关推荐
计算机毕设指导632 分钟前
基于微信小程序的宠物走失信息管理系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·宠物
2501_9160088933 分钟前
iOS 能耗检测的工程化方法,构建多工具协同的电量分析与性能能效体系
android·ios·小程序·https·uni-app·iphone·webview
说私域1 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的线上向线下导流运营研究
人工智能·小程序·开源
小小王app小程序开发1 小时前
任务悬赏小程序核心玩法 + 功能全解析:精准匹配与信任构建的变现逻辑
大数据·小程序
头发还在的女程序员1 小时前
陪诊小程序成品|陪诊系统功能|陪诊系统功能(源码)
java·小程序·his系统
玩代码的老秦1 小时前
微信小程序想要分享转发当前页面
小程序
2501_915921431 小时前
重新理解 iOS 的 Bundle Id 从创建、管理到协作的工程策略
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 小时前
当 altool 退出历史舞台,iOS 上传链路的演变与替代方案的工程实践
android·ios·小程序·https·uni-app·iphone·webview
小码哥0682 小时前
陪诊陪护小程序|上门打针|院内陪护|陪诊跑腿服务
小程序·陪诊陪护·医院陪诊问诊小程序源码·医院陪诊问诊小程序系统·医院陪诊问诊源码
00后程序员张2 小时前
Transporter 的局限与替代路径,iOS 上传流程在多平台团队中的演进
android·ios·小程序·https·uni-app·iphone·webview