package com.moral.yunfushao.activity;
|
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.text.TextUtils;
|
import android.view.View;
|
|
import com.lzy.okgo.cache.CacheMode;
|
import com.moral.andbrickslib.utils.GsonUtil;
|
import com.moral.andbrickslib.utils.NetworkUtil;
|
import com.moral.andbrickslib.utils.StatusBarUtil;
|
import com.moral.yunfushao.wxapi.WXEntryActivity;
|
import com.moral.yunfushao.wxapi.data.AccessTokenBean;
|
import com.moral.yunfushao.wxapi.data.TokenEffectivenessBean;
|
import com.moral.yunfushao.MainActivity;
|
import com.moral.yunfushao.MainApp;
|
import com.moral.yunfushao.R;
|
import com.moral.yunfushao.base.BaseActivity;
|
import com.moral.yunfushao.common.WXConstants;
|
import com.moral.yunfushao.httputils.HttpCallBack;
|
import com.moral.yunfushao.httputils.WXHttpUtil;
|
import com.moral.yunfushao.utils.SharedPreferencesUtil;
|
|
|
/**
|
* Created by haijiang on 2017/2/20.
|
*/
|
|
public class SplashActivity extends BaseActivity implements Runnable {
|
private AccessTokenBean bean = null;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
StatusBarUtil.setStatusBarDrawable(SplashActivity.this, R.drawable.trans_rect_bg);
|
}
|
|
@Override
|
protected void getBundleExtras(Bundle extras) {
|
|
}
|
|
@Override
|
protected int getLayoutId() {
|
return R.layout.ac_splash_layout;
|
}
|
|
@Override
|
protected void initViews() {
|
Thread delayThread = new Thread(this);
|
delayThread.start();
|
}
|
|
@Override
|
protected void initListener() {
|
|
}
|
|
@Override
|
protected void initData() {
|
|
}
|
|
@Override
|
protected void processClick(View view) {
|
|
}
|
|
@Override
|
public void onClick(View v) {
|
|
}
|
|
@Override
|
protected void onErrorPageClick() {
|
|
}
|
|
@Override
|
public void run() {
|
try {
|
Thread.sleep(1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
bean = SharedPreferencesUtil.getUtil(this).getWXToken();
|
|
if (NetworkUtil.isAvailable(this)) {
|
//网络可用状态是否
|
if (bean == null) {
|
goHome();
|
//非微信登陆
|
} else {
|
//微信登陆
|
// checkWXLogin();
|
checkTokenEffective();
|
}
|
}
|
}
|
|
/**
|
* 判断是否token有效
|
*/
|
private void checkTokenEffective() {
|
//https:api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
|
|
String url = "https:api.weixin.qq.com/sns/auth?access_token=" + bean.getAccess_token() + "&openid=" + WXConstants.APP_ID;
|
WXHttpUtil.doGet(url, CacheMode.DEFAULT, new HttpCallBack() {
|
@Override
|
public void onSuccess(String res, String msg) {
|
TokenEffectivenessBean bean = GsonUtil.toObj(msg, TokenEffectivenessBean.class);
|
if (bean.getErrcode() == 0) {
|
//成功授权
|
checkWXLogin();
|
}
|
}
|
|
@Override
|
public void showLoadingDialog() {
|
|
}
|
|
@Override
|
public void onFail(int errno, String s) {
|
//无效直接
|
goHome();
|
}
|
});
|
}
|
|
/**
|
* 检查是否登陆失效
|
*/
|
private void checkWXLogin() {
|
//
|
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid= " +
|
WXConstants.APP_ID +
|
"&secret=" + "应用密钥" + "&code="+
|
WXEntryActivity.code +
|
"&grant_type=authorization_code";
|
// String url = "https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID";
|
WXHttpUtil.doGet(url, CacheMode.DEFAULT, new HttpCallBack() {
|
@Override
|
public void onSuccess(String res, String msg) {
|
AccessTokenBean accessTokenBean = GsonUtil.toObj(msg, AccessTokenBean.class);
|
|
}
|
|
@Override
|
public void showLoadingDialog() {
|
//
|
}
|
|
@Override
|
public void onFail(int errno, String s) {
|
goHome();
|
}
|
});
|
}
|
|
private void goHome() {
|
if (!TextUtils.isEmpty(MainApp.userId)) {
|
Intent intent = new Intent(this, MainActivity.class);
|
startActivity(intent);
|
} else {
|
Intent intent = new Intent(this, LoginActivity.class);
|
startActivity(intent);
|
}
|
finish();
|
}
|
}
|