wpc
2018-11-27 c52fb0e4d9168e75390b3cf3536d66c06c50d605
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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);
    }
}