|
|
package com.hh.xuetubao.wxapi;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.content.Intent;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.CountDownTimer;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import com.hh.xuetubao.R;
|
|
|
import com.hh.xuetubao.bean.UserBean;
|
|
|
import com.hh.xuetubao.model.AccountModel;
|
|
|
import com.hh.xuetubao.mvp.BaseMvpActivity;
|
|
|
import com.hh.xuetubao.mvp.CommonPresenter;
|
|
|
import com.hh.xuetubao.mvp.ICommonView;
|
|
|
import com.tencent.mm.opensdk.modelbase.BaseReq;
|
|
|
import com.tencent.mm.opensdk.modelbase.BaseResp;
|
|
|
import com.tencent.mm.opensdk.modelmsg.SendAuth;
|
|
|
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
|
|
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
|
|
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
|
import com.zhy.http.okhttp.OkHttpUtils;
|
|
|
import com.zhy.http.okhttp.callback.StringCallback;
|
|
|
import com.umeng.socialize.weixin.view.WXCallbackActivity;
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
public class WXEntryActivity extends WXCallbackActivity {
|
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
|
|
|
public class WXEntryActivity extends BaseMvpActivity<CommonPresenter, AccountModel> implements ICommonView, IWXAPIEventHandler {
|
|
|
private static final int RETURN_MSG_TYPE_LOGIN = 1;
|
|
|
private static final int RETURN_MSG_TYPE_SHARE = 2;
|
|
|
private IWXAPI mWeixinAPI;
|
|
|
|
|
|
@BindView(R.id.ed_phone)
|
|
|
EditText edPhone;
|
|
|
@BindView(R.id.ed_code)
|
|
|
EditText edCode;
|
|
|
@BindView(R.id.tv_yanzheng)
|
|
|
TextView tvYanzheng;
|
|
|
@BindView(R.id.bdsj_sure)
|
|
|
TextView bdsjSure;
|
|
|
@BindView(R.id.bdsj_fhdl)
|
|
|
TextView bdsjFhdl;
|
|
|
|
|
|
private CountDownTimer timer;
|
|
|
private int time = 60;
|
|
|
|
|
|
@Override
|
|
|
public void initView() {
|
|
|
ButterKnife.bind(this);
|
|
|
mWeixinAPI = WXAPIFactory.createWXAPI(this, WxData.WEIXIN_APP_ID, true);
|
|
|
mWeixinAPI.handleIntent(this.getIntent(), this);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int initLayout() {
|
|
|
return R.layout.activity_bdsj; // R.layout.activity_wxentry
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void initData() {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public CommonPresenter getPresenter() {
|
|
|
return new CommonPresenter();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public AccountModel getModel() {
|
|
|
return new AccountModel();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void getData(Object o, int api, int intent) {
|
|
|
if (api != 1) {
|
|
|
return;
|
|
|
}
|
|
|
switch (intent) {
|
|
|
case 1: // 验证码
|
|
|
UserBean bean = (UserBean) o;
|
|
|
if (bean.getSuccess()) {
|
|
|
Toast.makeText(this, "验证码发送成功", Toast.LENGTH_LONG).show();
|
|
|
time = 60;
|
|
|
|
|
|
timer = new CountDownTimer(60000, 1000) {
|
|
|
@Override
|
|
|
public void onTick(long millisUntilFinished) {
|
|
|
time--;
|
|
|
tvYanzheng.setText(String.valueOf(time) + "s");
|
|
|
tvYanzheng.setFocusable(false);
|
|
|
tvYanzheng.setFocusableInTouchMode(false);
|
|
|
tvYanzheng.setClickable(false);
|
|
|
try {
|
|
|
tvYanzheng.setText(String.valueOf(time) + "s");
|
|
|
tvYanzheng.setFocusable(false);
|
|
|
} catch (Exception e) {
|
|
|
Log.e("zhangtao", e.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFinish() {
|
|
|
tvYanzheng.setText("获取验证码");
|
|
|
tvYanzheng.setFocusable(true);
|
|
|
tvYanzheng.setFocusableInTouchMode(true);
|
|
|
tvYanzheng.setClickable(true);
|
|
|
time = 60;
|
|
|
}
|
|
|
}.start();
|
|
|
} else {
|
|
|
Toast.makeText(this, "验证码发送失败", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
break;
|
|
|
case 3: // 绑定
|
|
|
UserBean bean1 = (UserBean) o;
|
|
|
if (bean1.getSuccess()) {
|
|
|
Toast.makeText(this, "微信绑定成功", Toast.LENGTH_SHORT).show();
|
|
|
this.finish();
|
|
|
} else {
|
|
|
Toast.makeText(this, "微信绑定失败", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@OnClick({R.id.tv_yanzheng, R.id.bdsj_sure, R.id.bdsj_fhdl})
|
|
|
public void onViewClicked(View view) {
|
|
|
switch (view.getId()) {
|
|
|
case R.id.tv_yanzheng: // - > 验证码
|
|
|
if (isCellphone(edPhone.getText().toString()))
|
|
|
mPresenter.getPresenter(1, 1, edPhone.getText().toString(), String.valueOf(1)); // 1 -> 微信或手机号码绑定时发送
|
|
|
else
|
|
|
Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
|
|
|
break;
|
|
|
case R.id.bdsj_sure: // - > 确定
|
|
|
if (isCellphone(edPhone.getText().toString())) {
|
|
|
Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
|
|
|
return;
|
|
|
} else if (edPhone.getText().length() > 0 && edCode.getText().length() > 0) {
|
|
|
Toast.makeText(this, "手机号和验证码不可为空", Toast.LENGTH_LONG).show();
|
|
|
return;
|
|
|
} else {
|
|
|
mPresenter.getPresenter(1, 3, edPhone.getText().toString().trim(), edCode.getText().toString().trim()); // - > 绑定
|
|
|
}
|
|
|
break;
|
|
|
case R.id.bdsj_fhdl: // - > 返回登录
|
|
|
this.finish();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static boolean isCellphone(String str) {
|
|
|
Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$");
|
|
|
Matcher matcher = pattern.matcher(str);
|
|
|
return matcher.matches();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
super.onNewIntent(intent);
|
|
|
setIntent(intent);
|
|
|
mWeixinAPI.handleIntent(intent, this);//必须调用此句话
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onReq(BaseReq baseReq) {
|
|
|
Log.e("-----", "onReq: " + baseReq);
|
|
|
finish();
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void onResp(BaseResp baseResp) {
|
|
|
Log.e("-----", "errStr: " + baseResp.errStr);
|
|
|
Log.e("-----", "openId: " + baseResp.openId);
|
|
|
Log.e("-----", "transaction: " + baseResp.transaction);
|
|
|
Log.e("-----", "errCode: " + baseResp.errCode);
|
|
|
Log.e("-----", "getType: " + baseResp.getType());
|
|
|
Log.e("-----", "checkArgs: " + baseResp.checkArgs());
|
|
|
|
|
|
|
|
|
switch (baseResp.errCode) {
|
|
|
|
|
|
case BaseResp.ErrCode.ERR_AUTH_DENIED:
|
|
|
case BaseResp.ErrCode.ERR_USER_CANCEL:
|
|
|
if (RETURN_MSG_TYPE_SHARE == baseResp.getType()) UIUtils.runOnUIToast("分享失败");
|
|
|
else UIUtils.runOnUIToast("登录失败");
|
|
|
break;
|
|
|
case BaseResp.ErrCode.ERR_OK:
|
|
|
switch (baseResp.getType()) {
|
|
|
case RETURN_MSG_TYPE_LOGIN:
|
|
|
//拿到了微信返回的code,立马再去请求access_token
|
|
|
String code = ((SendAuth.Resp) baseResp).code;
|
|
|
getAccess_token(code);
|
|
|
//就在这个地方,用网络库什么的或者自己封的网络api,发请求去咯,注意是get请求
|
|
|
|
|
|
Log.e("--------", "code: " + code);
|
|
|
// id_tv.setText("code: " + code); //
|
|
|
break;
|
|
|
|
|
|
case RETURN_MSG_TYPE_SHARE:
|
|
|
UIUtils.runOnUIToast("微信分享成功");
|
|
|
finish();
|
|
|
break;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取openid accessToken值用于后期操作
|
|
|
*
|
|
|
* @param code 请求码
|
|
|
*/
|
|
|
private void getAccess_token(final String code) {
|
|
|
String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
|
|
|
+ WxData.WEIXIN_APP_ID
|
|
|
+ "&secret="
|
|
|
+ WxData.APP_SECRET
|
|
|
+ "&code="
|
|
|
+ code
|
|
|
+ "&grant_type=authorization_code";
|
|
|
OkHttpUtils.get().url(path).build().execute(new StringCallback() {
|
|
|
|
|
|
@Override
|
|
|
public void onError(okhttp3.Call call, Exception e) {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onResponse(String response) {
|
|
|
|
|
|
Log.e("-----", "onResponse: " + response);
|
|
|
JSONObject jsonObject = null;
|
|
|
try {
|
|
|
jsonObject = new JSONObject(response);
|
|
|
String openid = jsonObject.getString("openid").toString().trim();
|
|
|
String access_token = jsonObject.getString("access_token").toString().trim();
|
|
|
getUserMesg(access_token, openid);
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取微信的个人信息
|
|
|
*
|
|
|
* @param access_token
|
|
|
* @param openid
|
|
|
*/
|
|
|
private void getUserMesg(final String access_token, final String openid) {
|
|
|
String path = "https://api.weixin.qq.com/sns/userinfo?access_token="
|
|
|
+ access_token
|
|
|
+ "&openid="
|
|
|
+ openid;
|
|
|
|
|
|
|
|
|
OkHttpUtils.get().url(path).build().execute(new StringCallback() {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void onError(okhttp3.Call call, Exception e) {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onResponse(String response) {
|
|
|
Log.e("------", "全部数据: " + response);
|
|
|
JSONObject jsonObject = null;
|
|
|
try {
|
|
|
jsonObject = new JSONObject(response);
|
|
|
String nickname = jsonObject.getString("nickname");
|
|
|
int sex = Integer.parseInt(jsonObject.get("sex").toString());
|
|
|
String headimgurl = jsonObject.getString("headimgurl");
|
|
|
String openid1 = jsonObject.getString("openid");
|
|
|
Log.e("---", "用户基本信息:");
|
|
|
Log.e("---", "nickname:" + nickname);
|
|
|
// nickname_tv.setText("nickname: " +nickname);
|
|
|
Log.e("---", "sex: " + sex);
|
|
|
// sex_tv.setText("sex: " + sex + "");
|
|
|
Log.e("---", "headimgurl:" + headimgurl);
|
|
|
// startLoca(nickname, openid1);
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
UIUtils.runOnUIToast("登陆错误,请重新再试");
|
|
|
|
|
|
}
|
|
|
// finish();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void onComplete(int api) {
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onError(Throwable e, int api) {
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|