import {
  writeLocalStorage,
  readLocalstorage,
  GetQueryString,
  $
} from './utils'
import {
  mobileLogin,
  partnerLogin
} from '../service/course'
// 兼容老版本
function loginPage() {
  // this.showScene(require('../stage/index/scene/course/ChineseMathScene'));
}
export function isLogin() {
  if (readLocalstorage('uid') === '123456') {
    if (GetQueryString('appCode') === '2013') { // 小爱音箱
      if (typeof efunboxJS != "undefined") {
        efunboxJS.showPhoneNumberLogin();
      } else {
        console.log('小艾登录没有安卓方法')
      }
    } else {
      newLogin()
    }
  }
}
// 安卓授权登陆
export function getPhoneLogin(phone) {
  if (phone) this.renderLogin(phone);
}
// 华为登录
export function huaweiLogin(data) {
  const channel = GetQueryString('appCode')
  const deviceCode = GetQueryString('uuid')
  partnerLogin({
    channel,
    deviceCode,
    nikeName: data.displayName,
    openId: data.openId
  }).success(res => {
    writeLocalStorage('uid', res.data.uid);
    writeLocalStorage('channelCode', res.data.channelCode);
    writeLocalStorage('nickName', res.data.nickName);
    writeLocalStorage('mobile', res.data.mobile);
    writeLocalStorage('userCreateTime', res.data.gmtCreated);
    console.log(123123, res.data.grade)
    if (res.data.grade) {
      writeLocalStorage('class', res.data.grade)
    } else {
      writeLocalStorage('class', '')
    }
    loginPage.call(this, '')
  })
}
// 安卓手机号登陆成功
export function phoneSuccess(res) {
  const data = res.data;
  console.log('安卓登陆成功', data)
  writeLocalStorage('uid', data.uid);
  writeLocalStorage('channelCode', data.channelCode);
  writeLocalStorage('nickName', data.nickName);
  writeLocalStorage('mobile', data.mobile);
  writeLocalStorage('userCreateTime', data.gmtCreated);
  if (res.data.grade) {
    writeLocalStorage('class', res.data.grade)
  } else {
    writeLocalStorage('class', '')
  }
  loginPage.call(this, '')
}
// 手机号登录调用接口
export function renderLogin(mobileNo) {
  const deviceCode = GetQueryString('uuid')
  const channel = GetQueryString('appCode')
  // const self = this;
  mobileLogin({
    mobileNo,
    deviceCode,
    channel
  }).success(res => {
    console.log(res)
    writeLocalStorage('uid', res.data.uid);
    writeLocalStorage('channelCode', res.data.channelCode);
    writeLocalStorage('nickName', res.data.nickName);
    writeLocalStorage('mobile', res.data.mobile);
    writeLocalStorage('userCreateTime', res.data.gmtCreated);
    console.log(123123, res.data.grade)
    if (res.data.grade) {
      writeLocalStorage('class', res.data.grade)
    } else {
      writeLocalStorage('class', '')
    }
    loginPage.call(this, '')
  }).fail(error => {
    console.log(error)
  })
}