<template>
	<view>
		<view v-if="showLogin">
			<login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="loginSuccess" @fail="loginFail" @cancel="loginCancel" />
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 获取用户信息
			ocode: '1015730314',
			// 商户appkey
			appkey: 'EE28EE2C93296F4E',
			// 是否授权可见
			visible: false,
			// 是否启动授权
			showLogin: false,
			// 获取用户信息
			appid: 'wxd6f090391d410534',
			// 用户路由
			userListInfo: {
				routes: ['报修', '我的报修'],
				btns: []
			},
			// 师傅路由
			workerListInfo: {
				routes: ['首页', '工单管理', '待处理池'],
				btns: ['协作', '接单', '转单', '报价', '维修完成']
			},
			// 后勤路由
			logisticsListInfo: {
				routes: ['首页', '工单管理', '待处理池', '通讯录'],
				btns: ['表格编辑', '延时', '派单', '审核', '分段器按钮', '图表']
			},
			// 管理者路由
			adminListInfo: {
				routes: ['首页', '工单管理', '待处理池', '通讯录'],
				btns: ['首页分段器', '表格编辑', '工单管理分段器', '延时', '派单', '审核', '接单', '报价', '维修完成', '图表', '协作', '转单']
			}
		}
	},
	mounted() {
		this.hasUserInfo()
	},
	methods: {
		// 检查用户是否已经授权
		hasUserInfo() {
			const repairsUserInfo = uni.getStorageSync('repairsUserInfo')
			const openId = uni.getStorageSync('openId')
			const college = uni.getStorageSync('college')
			// console.log(repairsUserInfo)
			// console.log(openId)
			// console.log(college)
			if (repairsUserInfo && openId && college) {
				uni.setStorageSync('currentIndexRepairs', 0)
				uni.reLaunch({
					url: '/pagesRepairs/box/box'
				})
			} else {
				// console.log('重新授权')
				this.showLogin = true
				this.visible = true
			}
		},
		// 授权成功回调
		loginSuccess(res) {
			// console.log('成功')
			// console.log(res)
			let wxcode = res.detail.wxcode
			// console.log(wxcode)
			// 获取wxcode后请求登录
			this.login(wxcode)
		},

		// 用户登陆获取个人信息
		async login(wxcode) {
			const res = await this.$myRequest_repairs({
				url: '/login/Openid',
				header: {},
				data: {
					wxcode
				}
			})
			console.log(res)
			if (res.code === '200') {
				uni.setStorageSync('repairsUserInfo', res.data)
				uni.setStorageSync('college', res.data.college)
				uni.setStorageSync('currentIndexRepairs', 0)
				this.getOpenId()
			}
		},
		// 获取用户openId
		getOpenId() {
			// 获取code
			uni.login({
				//使用微信登录
				provider: 'weixin',
				success: async (res) => {
					const result = await this.$myRequest_repairs({
						url: '/login/XOpenid',
						data: {
							code: res.code,
							userId: uni.getStorageSync('repairsUserInfo').userId
						}
					})
					// console.log(result)
					if (result.code === '200') {
						uni.setStorageSync('openId', result.data.openId)
						uni.reLaunch({
							url: '/pagesRepairs/box/box'
						})
					}
				}
			})
		},
		// 授权失败回调
		loginFail() {
			// console.log("授权失败");
			uni.showModal({
				title: '提示',
				content: '授权：请先领取校园卡、并激活！',
				confirmText: '领取',
				success: (res) => {
					if (res.confirm) {
						uni.reLaunch({
							url: '/pages/qr_code/qr_code'
						})
					} else if (res.cancel) {
						uni.reLaunch({
							url: '/pagesClockIn/404/404'
						})
					}
				}
			})
		},
		// 授权取消回调
		loginCancel() {
			// console.log("取消");
			uni.showModal({
				title: '提示',
				content: '请先授权，否则无法使用该功能',
				confirmText: '授权',
				success: (res) => {
					if (res.confirm) {
						uni.reLaunch({
							url: '/pagesRepairs/index/index'
						})
					} else if (res.cancel) {
						uni.reLaunch({
							url: '/pagesClockIn/404/404'
						})
					}
				}
			})
		}
	}
}
</script>

<style lang="scss"></style>
