function parseUser(json) {
  if (json.code != "200") {
    return null;
  }
  var data = json.data;
  if (data == null || data == undefined) {
    return null;
  }
  var username = data.username;
  if (username == null || username == undefined || username == "") {
    return null;
  }
  return username;
}


export async function messageFun(e) {
  if (e.data && e.data.accessToken) {


    var token = e.data.accessToken;

    if (token == null || token == undefined) {

      return false;
    }

    try {

      console.log(token);
      var url =
        'https://portal.lishui.gov.cn:18080/api_2021/public/index.php/index/MicroPortalLogin/getUserInfoByAccessToken?accessToken=' + token;
      var user = await fetch(url);
      console.log(url);
      var contentType = user.headers.get('Content-Type');
      console.log(contentType);
      console.log("return user");

      var json = await user.json();
      var username = parseUser(json);
      console.log(json);
      console.log("welcome:" + username);
      if (username == null) {
        return false;
      }

      console.log("finishing");
    } catch (e) {
      return false;
    }


  } else {

    return false;

  }

  return true;

}
