<template>
	<view class="column centerV">
		<view class="codeimg">
			<view v-if="wechatImg!=null" class="centerV">
				<image :src="wechatImg" style="width: 100%;" mode="aspectFit" :show-menu-by-longpress="true" ></image>
				<view class="centerV">点击保存图片至本地！</view>
			</view>
			<view v-else>
				<image src="https://hzyy.obs.cn-north-4.myhuaweicloud.com/app/shop/images/no_data.png" style="width: 100%;"
					mode="aspectFill"></image>
				<view class="centerV">暂无二维码，请点击生成二维码</view>
			</view>
		</view>
		<view class="centerV generate" @click="generatecode" v-if="wechatImg==null">点击生成</view>
		<view class="centerV generate" @click="saveImage" v-else>保存图片</view>
	</view>
</template>

<script>
	import {
		getcomapycode,
		generateComapycode
	} from '@/api/companyUser'
	export default {
		data() {
			return {
				wechatImg: null,
				companyId: ''
			}
		},
		onLoad(option) {
			this.companyId = option.companyId
			this.getcomapycodes()
		},
		methods: {
			saveImage() {
				wx.downloadFile({
				  url: this.wechatImg, //仅为示例，并非真实的资源
				  success (res) {
					  console.log(res)
				    // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
				    if (res.statusCode === 200) {
						wx.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function(res) {
								wx.showToast({
									title: '已保存至相册',
									icon: 'none',
									duration: 1000
								})
							},
							fail: function(err) {
								console.log(err);
								wx.showToast({
									title: '保存失败',
									icon: 'none',
									duration: 1000
								})
							}
						})
				      wx.playVoice({
				        filePath: res.tempFilePath
				      })
				    }
				  }
				})
				
			},
			generatecode() {
				const data = {
					companyId: this.companyId,
					appId: wx.getAccountInfoSync().miniProgram.appId
				}
				generateComapycode(data).then(res => {
					if (res.code == 200) {
						this.getcomapycodes()
						console.log(res)
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			getcomapycodes() {
				const data = {
					companyId: this.companyId,
					appId: wx.getAccountInfoSync().miniProgram.appId
				}
				getcomapycode(data).then(res => {
					if (res.code == 200) {
						// 核心js代码：
						this.wechatImg = res.data

					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.centerV {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.codeimg {
		width: 80%;
		margin: 40rpx 0;
	}

	.generate {
		background-color: #2BC7B9;
		width: 70%;
		margin: 0 auto;
		border-radius: 40rpx;
		padding: 20rpx 0;
		color: #fff;
		margin-top: 30rpx;
	}
</style>