import { toast } from '../../utils/extendApi'
import { reqLogin, reqUserInfo } from '../../utils/user'
import { getStorage, setStorage } from '../../utils/storage'
//导入ComponentWithStore方法
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
import { userStore } from '../../stores/userstore'

ComponentWithStore({//用ComponentWithStore方法代替Page方法构造页面？

  //让页面和Store建立关联
  storeBindings: {
    store: userStore,
    fields: ['id', 'identify'],
    actions: ['setId', 'setIdentify']
  },


  methods:{

    login(){

      //获取临时登录凭证code
      wx.login({
        success: async ({ code }) => {
          if (code) {
            const { data } = await reqLogin(code)
            setStorage('id', data.id)
            //存储到Store里
            this.setId(data.id)
            //成功后获取信息
            setStorage('identify', data.identify)
            this.setIdentify(data.identify)
            //根据identify执行跳转
            if (userStore.identify === 0) {
              wx.redirectTo({
                url: '/pages/identify/identify' // 未认证用户跳转的页面
              });
            }
            else if (userStore.identify === 1) {
              console.log('yes')
              wx.switchTab({
                url: '/pages/home/home',
                success:()=>{
                  console.log("hahahah")
                },
                fail:()=>{
                  console.log("fail")
                }
                
              });
            } else if (userStore.identify === 2) {
              wx.switchTab({
                url: '/pages/home/home' //教师用户跳转的页面，统一跳转到主页
              });
            } else {
              toast({ title: '系统出错' });
            }
          } else {
            //授权失败
            toast({ title: '授权失败，请重新授权' });
          }

        }
      });
    },
    // //获取用户信息
    // async getAndSaveUserInfo () {
    //   const {data} = await reqUserInfo ()

    //   //存储到本地保存
    //   setStorage('userInfo',data)
    //   //将用户信息存储到Store对象
    //   this.setUserInfo(data)
    // }
  }


});

