package com.haicaojie.android.ui.mine;
|
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
|
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.haicaojie.android.ui.invite.ShareBrowserActivity;
|
import com.lcjian.library.util.NetUtils;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
|
import com.tencent.connect.auth.AuthAgent;
|
import com.tencent.mm.opensdk.modelmsg.SendAuth;
|
import com.haicaojie.android.BasicTextHttpResponseHandler;
|
import com.haicaojie.android.R;
|
import com.haicaojie.android.ShoppingApi;
|
import com.haicaojie.android.ShoppingApplication;
|
import com.haicaojie.android.entity.UserInfo;
|
import com.haicaojie.android.ui.BaseActivity;
|
import com.haicaojie.android.util.UnbindDialog;
|
import com.umeng.socialize.UMAuthListener;
|
import com.umeng.socialize.UMShareAPI;
|
import com.umeng.socialize.bean.SHARE_MEDIA;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.Map;
|
|
import de.greenrobot.event.EventBus;
|
|
/**
|
* Created by weikou2015 on 2017/12/15.
|
* 我的信息
|
*/
|
|
public class MyInfoActivity extends BaseActivity implements View.OnClickListener {
|
|
private TextView tv_left, tv_middle, tv_nickName, tv_uid, tv_grade, tv_tbAccount, tv_qqAccount, tv_weChatAccount, tv_mobileAccount;
|
private ImageView iv_portrait;
|
private DisplayImageOptions options;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_myinfo);
|
/*
|
* 计算状态栏高度并设置
|
*/
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
Window window = getWindow();
|
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
|
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
int result = 0;
|
int resourceId = getResources().getIdentifier("status_bar_height",
|
"dimen", "android");
|
if (resourceId > 0) {
|
result = getResources().getDimensionPixelSize(resourceId);
|
}
|
//设置状态栏文字颜色及图标为深色
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
|
result);
|
findViewById(R.id.v_status_bar).setLayoutParams(params);
|
} else {
|
findViewById(R.id.v_status_bar).setVisibility(View.GONE);
|
}
|
EventBus.getDefault().register(this);
|
this.options = new DisplayImageOptions.Builder()
|
.showImageForEmptyUri(R.drawable.ic_default_portrait)
|
.showImageOnFail(R.drawable.ic_default_portrait)
|
.showImageOnLoading(R.drawable.ic_default_portrait)
|
.resetViewBeforeLoading(true).cacheInMemory(true)
|
.cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
|
.considerExifParams(true)
|
.displayer(new CircleBitmapDisplayer(300)).build();
|
tv_left = (TextView) findViewById(R.id.tv_top_bar_left);
|
tv_middle = (TextView) findViewById(R.id.tv_top_bar_middle);
|
tv_nickName = (TextView) findViewById(R.id.tv_info_nickname);
|
tv_uid = (TextView) findViewById(R.id.tv_info_uid);
|
tv_grade = (TextView) findViewById(R.id.tv_info_grade);
|
tv_tbAccount = (TextView) findViewById(R.id.tv_info_tbaccount);
|
tv_qqAccount = (TextView) findViewById(R.id.tv_info_relative_qq);
|
tv_weChatAccount = (TextView) findViewById(R.id.tv_info_relative_wechat);
|
tv_mobileAccount = (TextView) findViewById(R.id.tv_info_relative_mobile);
|
iv_portrait = (ImageView) findViewById(R.id.iv_info_portrait);
|
tv_middle.setText("个人中心");
|
tv_left.setOnClickListener(this);
|
findViewById(R.id.ll_info_relative_qq).setOnClickListener(this);
|
findViewById(R.id.ll_info_tbaccount).setOnClickListener(this);
|
// findViewById(R.id.ll_info_grade).setOnClickListener(this);
|
findViewById(R.id.ll_info_relative_wechat).setOnClickListener(this);
|
findViewById(R.id.ll_info_relative_mobile).setOnClickListener(this);
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
mUid = sp.getString("uid", "0");
|
if (sp.getBoolean("isLogin", false)) {
|
getUserInfo(sp.getString("uid", "0"));
|
}
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.tv_top_bar_left:
|
finish();
|
break;
|
case R.id.ll_info_relative_qq:
|
|
break;
|
// case R.id.ll_info_grade:
|
// Intent intent3 = new Intent(MyInfoActivity.this, ShareBrowserActivity.class);
|
// intent3.putExtra("url", getSharedPreferences("user", Context.MODE_PRIVATE).getString("invite_url", "http:"));
|
// startActivity(intent3);
|
// break;
|
case R.id.ll_info_relative_wechat:
|
if (NetUtils.getNetworkState(MyInfoActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
SingleToast.showToast(MyInfoActivity.this, "网络未连接,请检测网络设置");
|
break;
|
}
|
if (NetUtils.getNetworkState(MyInfoActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
SingleToast.showToast(MyInfoActivity.this, "网络未连接,请检测网络设置");
|
break;
|
}
|
if (tv_weChatAccount.getText().toString().equalsIgnoreCase("去绑定")) {
|
bindWx();
|
} else {
|
UnbindDialog.Builder builder = new UnbindDialog.Builder(this);
|
builder.setMessage("是否更换当前微信").setPositiveButton("更换绑定", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
bindWx();
|
}
|
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).create().show();
|
}
|
break;
|
case R.id.ll_info_tbaccount:
|
if (tv_tbAccount.getText().toString().equalsIgnoreCase("去绑定")) {
|
bindTB();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("closeHint", false);
|
editor.commit();
|
} else {
|
if (tv_weChatAccount.getText().toString().equalsIgnoreCase("去绑定") && tv_mobileAccount.getText().toString().equalsIgnoreCase("去绑定")) {
|
SingleToast.showToast(MyInfoActivity.this, "无法解绑");
|
} else {
|
UnbindDialog.Builder builder = new UnbindDialog.Builder(this);
|
builder.setMessage("是否需要解除绑定?").setPositiveButton("确认", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
unBindHint();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("closeHint", false);
|
editor.commit();
|
dialogInterface.dismiss();
|
}
|
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).create().show();
|
}
|
}
|
break;
|
case R.id.ll_info_relative_mobile:
|
if (tv_mobileAccount.getText().toString().equalsIgnoreCase("去绑定")) {
|
Intent intent = new Intent(MyInfoActivity.this, MobileLoginActvity.class);
|
intent.putExtra("title", "绑定");
|
intent.putExtra("uid", mUid);
|
startActivity(intent);
|
} else {
|
if (tv_weChatAccount.getText().toString().equalsIgnoreCase("去绑定") && tv_tbAccount.getText().toString().equalsIgnoreCase("去绑定")) {
|
SingleToast.showToast(MyInfoActivity.this, "无法解绑");
|
} else {
|
Intent intent = new Intent(MyInfoActivity.this, UnBindMobileActivity.class);
|
intent.putExtra("uid", mUid);
|
startActivity(intent);
|
}
|
}
|
break;
|
}
|
}
|
|
private void bindWx() {
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("closeHint", false);
|
editor.putBoolean("bindwx", true);
|
editor.commit();
|
SendAuth.Req req = new SendAuth.Req();
|
req.scope = "snsapi_userinfo";
|
req.state = "haicaojie";
|
ShoppingApplication.application.mWxApi.sendReq(req);
|
} else {
|
SingleToast.showToast(MyInfoActivity.this, "请先安装微信客户端!");
|
}
|
}
|
|
|
/**
|
* 退出登录
|
*/
|
private void unBindHint() {
|
AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
|
alibcLogin.logout(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i) {
|
unBind();
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
Toast.makeText(MyInfoActivity.this, "解绑失败 " + code + msg,
|
Toast.LENGTH_SHORT).show();
|
}
|
});
|
}
|
|
private void unBind() {
|
ShoppingApi.unBindTaoBao(this, mUid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
SingleToast.showToast(MyInfoActivity.this, jsonObject.optString("data"));
|
getSharedPreferences("user", MODE_PRIVATE).edit().putBoolean("isTBBind", false).commit();
|
tv_tbAccount.setText("去绑定");
|
SingleToast.showToast(MyInfoActivity.this, "解绑成功");
|
}
|
}
|
});
|
}
|
|
private void bindTB() {
|
AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
alibcLogin.showLogin(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i) {
|
bindInfo(AlibcLogin.getInstance().getSession().openId, AlibcLogin.getInstance().getSession().nick, AlibcLogin.getInstance().getSession().avatarUrl, getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0"));
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
Toast.makeText(MyInfoActivity.this, "登录失败",
|
Toast.LENGTH_LONG).show();
|
}
|
});
|
}
|
|
private void bindInfo(String openId, String nickName, String portrait, String uid) {
|
ShoppingApi.bindTaoBao(this, uid, openId, nickName, portrait, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").getJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
if (!StringUtils.isEmpty(info.getWxOpenId())) {
|
tv_weChatAccount.setText(info.getWxName());
|
} else {
|
SingleToast.showToast(MyInfoActivity.this, "绑定失败");
|
tv_weChatAccount.setText("去绑定");
|
}
|
if (!StringUtils.isEmpty(info.getOpenid())) {
|
tv_tbAccount.setText(info.getTbName());
|
} else {
|
SingleToast.showToast(MyInfoActivity.this, "绑定失败");
|
tv_tbAccount.setText("去绑定");
|
}
|
Log.i("mResult", jsonObject.toString());
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
if (StringUtils.isEmpty(info.getOpenid())) {
|
editor.putBoolean("isTBBind", false);
|
} else {
|
editor.putBoolean("isTBBind", true);
|
}
|
if (StringUtils.isEmpty(info.getWxOpenId())) {
|
editor.putBoolean("isWxBind", false);
|
} else {
|
editor.putBoolean("isWxBind", true);
|
}
|
editor.commit();
|
SingleToast.showToast(MyInfoActivity.this, "绑定成功");
|
} else {
|
SingleToast.showToast(MyInfoActivity.this, jsonObject.optString("msg"));
|
AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
alibcLogin.logout(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i) {
|
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
|
}
|
});
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
}
|
|
String mUid = "";
|
|
private void getUserInfo(String uid) {
|
ShoppingApi.getUserInfo2(this, uid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
// Glide.with(MyInfoActivity.this).load(info.getPortrait()).transform(new GlideCircleTransform(MyInfoActivity.this))
|
// .centerCrop().into(iv_portrait);
|
Log.i("mResult", jsonObject.toString());
|
if (info.getRank().equalsIgnoreCase("1")) {
|
tv_grade.setText("铜冠");
|
} else if (info.getRank().equalsIgnoreCase("2")) {
|
tv_grade.setText("银冠");
|
} else if (info.getRank().equalsIgnoreCase("3")) {
|
tv_grade.setText("金冠");
|
} else {
|
tv_grade.setText("普通");
|
}
|
mUid = info.getId();
|
ImageLoader.getInstance().displayImage(info.getPortrait(), iv_portrait, options);
|
tv_nickName.setText(info.getNickName());
|
tv_uid.setText(info.getId());
|
if (!StringUtils.isEmpty(info.getWxOpenId())) {
|
tv_weChatAccount.setText(info.getWxName());
|
} else {
|
tv_weChatAccount.setText("去绑定");
|
}
|
if (!StringUtils.isEmpty(info.getOpenid())) {
|
tv_tbAccount.setText(info.getTbName());
|
} else {
|
tv_tbAccount.setText("去绑定");
|
}
|
if (!StringUtils.isEmpty(info.getPhone())) {
|
tv_mobileAccount.setText(info.getPhone());
|
} else {
|
tv_mobileAccount.setText("去绑定");
|
}
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
}
|
});
|
}
|
|
public void onEventMainThread(UserInfo info) {
|
tv_weChatAccount.setText(info.getNickName());
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
EventBus.getDefault().unregister(this);
|
}
|
}
|