sdk.js 1.1 KB
import Login from 'yoho-activity-sdk/src/public/login';
import Base from 'yoho-activity-sdk/src/public/base';
import yoho from 'common/yoho';
import cookie from 'yoho-cookie';


var env = Base.env();
var loginUrl = Base.loginUrl();
var login = new Login({loginUrl: loginUrl, env: env});

async function getUser() {
  if (yoho.isApp) {
    const user = await Base.getUser();

    if (user && user.uid) {
      cookie.set('app_uid', user.uid);
      cookie.set('app_session_key', user.sessionKey);
      cookie.set('app_version', user.appVersion);
      cookie.set('app_client_type', user.sessionType);
    }

    return user;
  } else {
    return Base.getUser();
  }
}

async function goLogin() {
  if (yoho.isYohoBuy && yoho.isiOS) {
    return login.goLogin.bind(login)(Base.loginUrl());
  } else if (yoho.isYohoBuy && yoho.isAndroid) {
    return new Promise((resolve, reject) => {
      yoho.goLogin({}, () => {
        location.reload();
        resolve();
      }, () => {
        reject();
      });
    });
  } else {
    return login.goLogin.bind(login)(Base.loginUrl());
  }
}

export default {
  goLogin,
  getUser
};