<template>
  <div>
    <van-tabbar v-model="active" placeholder route :before-change="beforeChange" active-color="#f0896e">
      <van-tabbar-item v-for="(item,index) in list" :key="index" :to="item.router">
        <span>{{item.text}}</span>
        <template #icon="props">
          <img :src="props.active ? item.selectedIconPath : item.iconPath" />
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
import { userApi } from '../api/index.js'
export default {
  data() {
    return {
      active: 0,
      list: [
        {
          iconPath: require('../assets/images/nav/home-off.png'),
          selectedIconPath: require('../assets/images/nav/home-on.png'),
          text: '首页',
          router: '/',
        },
        {
          iconPath: require('../assets/images/nav/book-off.png'),
          selectedIconPath: require('../assets/images/nav/book-on.png'),
          router: '/classRoom',
          text: '老年课堂',
        },
        {
          iconPath: require('../assets/images/nav/map-off.png'),
          selectedIconPath: require('../assets/images/nav/map-on.png'),
          router: '/circleHome',
          text: '养老服务圈',
        },
        // {
        //   iconPath: require('../assets/images/nav/serve-store-off.png'),
        //   selectedIconPath: require('../assets/images/nav/serve-store-on.png'),
        //   router: '/shoppingMall',
        //   text: '养老商城',
        // },
        // {
        //   iconPath: require('../assets/images/nav/my-off.png'),
        //   selectedIconPath: require('../assets/images/nav/my-on.png'),
        //   text: '我的',
        // },
        {
          iconPath: require('../assets/images/nav/hea.png'),
          selectedIconPath: require('../assets/images/nav/hea1.png'),
          router: '/healthAssessmen',
          text: '健康管理',
        },
      ],
      appmark: 'sdzwapp',
    }
  },
  created() {},
  methods: {
    beforeChange(e) {
      if (e == 3) {
        if (!sessionStorage.getItem('userInfo')) {
          this.getUserInfo()
        } else {
          return true
        }
      } else {
        return true
      }
    },
    async userApi() {
      var userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
      //   var mobile = '13895651855'
      const res = await userApi({
        command: 'reguserbyweixinphone',
        contactsPhone: userInfo.mobile,
        // contactsPhone: mobile,
        terminalType: 5,
      })
      sessionStorage.setItem('healthUser', JSON.stringify(res))
      //   this.$toast(userInfo.name + '接口')
      //   this.$dialog
      //     .alert({
      //       message: res.uname + '--------' + userInfo.name,
      //     })
      //     .then(() => {})
      this.$router.push({ path: '/healthAssessmen' })
    },
    /**
     * 通过jssdk方法获取APP本地用户票据
     */
    getUserInfo() {
      var that = this
      lightAppJssdk.user.getTicket({
        success: function (data) {
          //成功回调
          console.log(JSON.stringify(data), '111')
          if (data == '未登录') {
            onLoginApp() //APP用户未登录，调用登录页面
          } else {
            var jsonData = JSON.parse(data)
            if (jsonData.retcode == '000000') {
              var dataValueObj = JSON.parse(jsonData.data)
              var ticket = dataValueObj.ticket //解析出用户票据信息
              userType = dataValueObj.usertype //解析保存用户类型（个人法人）
              that.getUserToken(ticket)
            }
          }
        },
        fail: function (data) {
          //错误返回
          console.log(JSON.stringify(data))
        },
      })
    },

    /**
     * 使用jssdk调用登录页面
     */
    onLoginApp() {
      var that = this
      lightAppJssdk.user.loginapp({
        success: function (data) {
          if (data == '未登录') {
            //取消登录或登录失败，关闭页面
            oncloseWindow()
          } else {
            var dataObj = JSON.parse(data)
            if (dataObj.retcode == '000000') {
              //登录成功，返回用户的票据和用户类型
              var dataValueObj = JSON.parse(dataObj.data)
              that.getUserToken(dataValueObj.ticket) //解析出用户票据信息
              userType = jsonData.usertype //解析保存用户类型（个人法人）
            } else {
              //关闭页面
              oncloseWindow()
            }
          }
        },
        fail: function (data) {
          //关闭页面
          oncloseWindow()
        },
      })
    },
    /**
     * 通过用户票据获取用户的令牌数据，票据使用一次后失效，token时效性较长
     */
    getUserToken(ticket) {
      var that = this
      var params = '{"ticket":"' + ticket + '"}'
      const interfaceContent = {
        app_id: that.appmark,
        servicename: 'ticketValidate',
        params: params,
      }

      vaildInterfacefn(
        'jisnzjk',
        'ticketvalidate',
        JSON.stringify(interfaceContent),
        '2',
        'https://' + urlCreatesign,
        'https://' + urlGateway
      ).then((value) => {
        const data = JSON.parse(value)
        if (data.retcode == '000000') {
          const userToken = JSON.parse(data.data).token
          if (userType == '1') {
            //个人类型获取用户信息
            that.getUserInfoByToken(userToken)
          } else {
            //法人类型获取用户信息
            that.findCorUserByToken(userToken)
          }
        } else {
          lightAppJssdk.notification.alert({
            title: '提示', //可传空
            message: data.msg,
            buttonName: '确认',
            success: function (data) {
              oncloseWindow()
            },
          })
        }
      })
    },

    /**
     * 根据令牌获取个人完整信息
     */
    getUserInfoByToken(userToken) {
      var that = this
      const params = '{"token":"' + userToken + '"}'
      const interfaceContent = {
        app_id: that.appmark,
        servicename: 'findOutsideUserByToken',
        params: params,
      }

      try {
        vaildInterfacefn(
          'jisnzjk',
          'findoutsideuserbytoken',
          JSON.stringify(interfaceContent),
          '2',
          'https://' + urlCreatesign,
          'https://' + urlGateway
        ).then((value) => {
          const data = JSON.parse(value)
          if (data.retcode == '000000') {
            const userInfo = data.data
            sessionStorage.setItem('userInfo', userInfo)
            // that.$toast('调用接口getUserInfoByToken')
            that.userApi()
          } else {
          }
        })
      } catch (error) {}
    },

    /**
     * 根据令牌获取完整法人信息
     */
    findCorUserByToken(userToken) {
      var that = this
      try {
        const params = '{"token":"' + userToken + '"}'
        const interfaceContent = {
          app_id: that.appmark,
          servicename: 'findCorUserByToken',
          params: params,
        }
        vaildInterfacefn(
          'jisnzjk',
          'findcoruserytoken',
          JSON.stringify(interfaceContent),
          '2',
          'https://' + urlCreatesign,
          'https://' + urlGateway
        ).then((value) => {
          const data = JSON.parse(value)

          if (data.retcode == '000000') {
            const userInfo = JSON.stringify(data.data)
            // const userObj = JSON.parse(userInfo)
            sessionStorage.setItem('userInfo', userInfo)
            // document.getElementById('userinfo').innerText = userInfo
            // that.$toast('调用接口findCorUserByToken')
            that.userApi()
          } else {
          }
        })
      } catch (err) {}
    },

    /**
     * 关闭容器
     */
    oncloseWindow() {
      lightAppJssdk.navigation.close({
        success: function (data) {},
        fail: function (data) {},
      })
    },
  },
}
</script>