<template>
	<view>
		
		<view class="text-center bg-white codebox">
			<view class="text-xl padding-lr padding-top">
				<text class="text-black">我的邀请码</text>
			</view>
			<view class="text-center solid-bottom text-sm padding text-grey">
				<view>请被邀请人扫码完成注册登录</view>
				<view>邀请成功会您将会根据任务产生收益</view>
			</view>
			<view class="padding">
				<image class="codeimg" mode="widthFix" @click="refCode" :src="userinfo.wxampcode"></image>
				<view class="flex flex-direction">
					<button class="cu-btn round bg-gray margin-top-sm lg" @tap="saveImage">保存我的邀请码</button>
				</view>
			</view>
		</view>
		
		<!-- <view class="text-center bg-white codebox">
			<view class="cu-list grid col-2">
				<view class="cu-item">
					<view class="cuIcon text-red">{{countobj.tuser}}</view>
					<text>今日用户</text>
				</view>
				<view class="cu-item">
					<view class="cuIcon text-red">{{countobj.user}}</view>
					<text>累计用户</text>
				</view>
				<block v-if="brokerinfo.powerreport==1">
				<view class="cu-item">
					<view class="cuIcon text-red">{{countobj.tentry}}</view>
					<text>今日报备</text>
				</view>
				<view class="cu-item">
					<view class="cuIcon text-red">{{countobj.entry}}</view>
					<text>累计报备</text>
				</view>
				</block>
			</view>
		</view> -->
		
		<!-- <view class="padding-sm"></view>
		<view class="padding-xl"></view>
		<view class="cu-bar tabbar bg-white foot">
			<view class="action text-blue" @tap="goNavPage('/pages/broker/broker')">
				<view class="cuIcon-qrcode"></view>
				邀请码
			</view>
			<view class="action" @tap="goNavPage('/pages/broker/myuser')">
				<view class="cuIcon-friend"></view>
				我的用户
			</view>
			<view class="action" @tap="goNavPage('/pages/my/myentry')">
				<view class="cuIcon-upload"></view>
				报备记录
			</view>
			<view class="action" @tap="goNavPage('/pages/broker/myorder')">
				<view class="cuIcon-recharge"></view>
				订单信息
			</view>
			<view class="action" @tap="goNavPage('/pages/broker/income')">
				<view class="cuIcon-moneybag"></view>
				我的收益
			</view>
		</view> -->
		
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				userinfo: false,
				brokerinfo: false,
			}
		},
		onLoad: function() {
			_this = this;
			_this.userinfo = uni.getStorageSync('userinfo') || false;
			_this.brokerinfo = uni.getStorageSync('brokerinfo') || false;
			if (_this.userinfo===false || _this.brokerinfo===false){
				uni.reLaunch({
					url: "/pages/my/my"
				});
				return false;
			}
			if (_this.userinfo.wxampcode==""){
				_this.refCode();
			}
		},
		methods: {
			saveImage: function() {
				uni.downloadFile({
				    url: _this.userinfo.wxampcode, //仅为示例，并非真实的资源
				    success: (res) => {
				        if (res.statusCode === 200) {
				            uni.saveImageToPhotosAlbum({
				            	filePath: res.tempFilePath,
				            	success: function () {
									uni.showModal({
										title: '信息提示',
										content: "邀请码已保存到手机相册，注意查收。",
										showCancel: false
									});
				            	}
				            });
				        }
				    }
				});
			},
			
			refCode: function() {
				_this.$req.ajax({
					path: "my/refcode",
					data: {
						userid: _this.userinfo.id
					}
				}).then((data) => {
					_this.userinfo.wxampcode = data.wxampcode+"?" + Math.random();
					uni.setStorageSync('userinfo', _this.userinfo);
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			goNavPage: function(pageurl) {
				_this.userinfo = _this.checkLogin("/pages/my/my");
				if (_this.userinfo===false){
					return false;
				}
				uni.redirectTo({
					url: pageurl
				});
			},
		}
	}
</script>

<style>
	.codebox{ width: 680rpx; margin: 40rpx auto 0rpx auto; border-radius: 8rpx; overflow: hidden; }
</style>
