<!--
 * @Author: yangpeiqin
 * @Date: 2023-12-11 10:13:55
 * @LastEditors: yangpeiqin
 * @LastEditTime: 2023-12-15 15:35:36
 * @FilePath: \gdtq_admin_app移动端\App.vue
-->
<script>
import { getToken } from '@/utils/auth'
import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载，也可按需进行加载
import Vue from 'vue'
import {getThirdH5Auth,h5GetToken} from '@/api/login.js'

export default {
    onLaunch: function ({query}) {
        console.log('App Launch')
        this.$store.dispatch('appConfig/ConfigList');
		
		if(Vue.prototype.hasHostPlat(["dingH5"]) && Vue.prototype.hasPlat(["enterprises"])) {
			this.handleDingLogin(query)
		} else if(Vue.prototype.hasHostPlat(["dingH5Ins"]) && Vue.prototype.hasPlat(["enterprises"])) {
			this.handleDingInsLogin(query)
		} else {
			this.otherLogin(query)
		}
    },
    onShow: function () {
        console.log('App Show')
		// this.checkTabPer()
		
    },
    onHide: function () {
        console.log('App Hide')
    },
    methods: {
		checkTabPer() {
			if(Vue.prototype.hasPlat(["enterprises"])) {
			
				if(Vue.prototype.$hasPermi('mobile:index:base:driverTask')) {
					// 司机任务tab
					uni.setTabBarItem({
						index:1, //从左到右 0开始
						visible:true ,//默认true false隐藏
					})
				} else {
					// 司机任务tab
					uni.setTabBarItem({
						index:1, //从左到右 0开始
						visible:false ,//默认true false隐藏
					})
				}
			} else {
				// 司机任务tab
				uni.setTabBarItem({
					index:1, //从左到右 0开始
					visible:false ,//默认true false隐藏
				})
			}
			
		},
		otherLogin(query) {
			if (query.token) {
				this.$store.dispatch('setTokenData', {
					token: query.token
				}).then(() => {
					this.getUserInfo()
				})
				return
			}
			
			if (getToken()) {
				this.getUserInfo()
			}
		},
		//钉钉第三方应用登录
		handleDingLogin(query={}) {
			
			if(dd.env.platform == "notInDingTalk") return
			
			if (query.token) {
				this.$store.dispatch('setTokenData', {
					token: query.token
				}).then(() => {
					this.getUserInfo()
				})
				return
			}
			
			if(query && query.corpId) {
				uni.setStorageSync("corpId", query.corpId);
				this.dingLogin(query.corpId)
				
				return
			} else if (getToken()) {
				this.getUserInfo()
			}
			
		},
		//钉钉内部应用登录
		handleDingInsLogin(query={}) {
			
			if(dd.env.platform == "notInDingTalk") return
			
			if (query.token) {
				this.$store.dispatch('setTokenData', {
					token: query.token
				}).then(() => {
					this.getUserInfo()
				})
				return
			}
			
			if(query && query.corpId) {
				uni.setStorageSync("corpId", query.corpId);
				this.dingInsLogin(query.corpId)
				
				return
			} else if (getToken()) {
				this.getUserInfo()
			}
			
		},
		getUserInfo() {
			
			this.$store.dispatch('GetInfo').then(res => {
		
				const {path} = this.$route
				if(path =='/') {
					uni.switchTab({
						url: '/pages/index/index'
					});
				}
				this.checkTabPer()
				
			})
		},
		// 第三方应用获取token
		dingLogin(corpId) {

		  dd.getAuthCode({
		    corpId: corpId,
		    // corpId: 'ding7ea3eb9200c0101c35c2f4657eb6378f',
		    success: (res) => {
				
		      const { code } = res;
		 
		      if(code) {
				  
		        getThirdH5Auth({code,corpId}).then((res) => {
					
					this.$store.state.user.isModel = false
					this.$store.state.user.cacheToken = null
					
					this.$store.dispatch('setTokenData', {
						token: res.data
					}).then(() => {
						this.getUserInfo()
					})
		        }).catch((err) => {
					
					uni.showModal({
						title: '提示',
						content: `${err}`,
						showCancel:false,
						confirmText: '确定',
						success: function(res) {
							if (res.confirm) {
								
							}
						}
					})
					
				})
		      }
		    },
		    fail: (res) => {

				uni.showModal({
					title: '提示',
					content: `${res.errorMessage}`,
					showCancel:false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
							
						}
					}
				})
			},
		    complete: () => {},
		  });
		},
		// 内部应用获取token
		dingInsLogin(corpId) {

		  dd.getAuthCode({
		    corpId: corpId,
		    // corpId: 'ding7ea3eb9200c0101c35c2f4657eb6378f',
		    success: (res) => {
				
		      const { code } = res;
		 
		      if(code) {
				  
		        h5GetToken({code,corpId}).then((res) => {
					
					this.$store.state.user.isModel = false
					this.$store.state.user.cacheToken = null
					
					this.$store.dispatch('setTokenData', {
						token: res.data
					}).then(() => {
						this.getUserInfo()
					})
		        }).catch((err) => {
					
					uni.showModal({
						title: '提示',
						content: `${err}`,
						showCancel:false,
						confirmText: '确定',
						success: function(res) {
							if (res.confirm) {
								
							}
						}
					})
					
				})
		      }
		    },
		    fail: (res) => {

				uni.showModal({
					title: '提示',
					content: `${res.errorMessage}`,
					showCancel:false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
							
						}
					}
				})
			},
		    complete: () => {},
		  });
		},
		
		
        //下载
        doUpData(appUrl) {
            uni.showLoading({
                title: '更新中……'
            })

            uni.downloadFile({//执行下载
                url: appUrl, //下载地址
                success: downloadResult => {//下载成功
                    uni.hideLoading();
                    if (downloadResult.statusCode == 200) {
                        uni.showModal({
                            title: '',
                            content: '更新成功，确定现在重启吗？',
                            confirmText: '重启',
                            confirmColor: '#EE8F57',
                            success: function (res) {
                                if (res.confirm == true) {
                                    plus.runtime.install(//安装
                                        downloadResult.tempFilePath, {
                                        force: true
                                    },
                                        function (res) {
                                            utils.showToast('更新成功，重启中');
                                            plus.runtime.restart();
                                        }
                                    );
                                }
                            }
                        });
                    }
                }
            });
        },
    }
}
</script>

<style lang="scss">
@import '@/common/css/icon.css';
@import "uview-ui/index.scss";
@import "@/assets/styles/uview-ui.scss";

// 默认背景颜色
page {
    background: #f3f4f8;
    width: 100%;
    height: 100%;
}

view {
    width: auto;
	word-wrap: break-word;
	white-space: normal;
	word-break: break-all;
}

.u-cell {
    width: 100%;
}

/*每个页面公共css */
</style>
