|  |  | 
 |  |  | package com.dayu.qiheonlinelibrary; | 
 |  |  |  | 
 |  |  | import android.app.Activity; | 
 |  |  | import android.app.Application; | 
 |  |  | import android.content.Context; | 
 |  |  | import android.content.Intent; | 
 |  |  |  | 
 |  |  | import com.dayu.baselibrary.net.subscribers.SubscriberListener; | 
 |  |  | import com.dayu.baselibrary.utils.TipUtil; | 
 |  |  | import com.dayu.baselibrary.utils.ToastUtil; | 
 |  |  | import com.dayu.qiheonlinelibrary.activity.HomeActivityQHOline; | 
 |  |  | import com.dayu.qiheonlinelibrary.bean.LoginResult; | 
 |  |  | import com.dayu.qiheonlinelibrary.bean.PricePlanResult; | 
 |  |  | import com.dayu.qiheonlinelibrary.net.ApiManager; | 
 |  |  | import com.dayu.qiheonlinelibrary.net.BaseResponse; | 
 |  |  | import com.dayu.qiheonlinelibrary.net.subscribers.SubscriberListener; | 
 |  |  | import com.dayu.qiheonlinelibrary.net.RSAUtile; | 
 |  |  |  | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * author: zuo | 
 |  |  | 
 |  |  |  * 备注: | 
 |  |  |  */ | 
 |  |  | public class QHOnLineApplication { | 
 |  |  |     public static QHOnLineApplication qhAloneApplication; | 
 |  |  |     public static QHOnLineApplication qhOnLineApplication; | 
 |  |  |     public Application application; | 
 |  |  |  | 
 |  |  |     public static QHOnLineApplication getInstance() { | 
 |  |  |         if (qhAloneApplication == null) { | 
 |  |  |             qhAloneApplication = new QHOnLineApplication(); | 
 |  |  |     public String tokenStr; | 
 |  |  |     public String adcd;//用户地址码 | 
 |  |  |     public String addressName;//用户地址名称 | 
 |  |  |  | 
 |  |  |     public String arerNumber;//区域号 | 
 |  |  |  | 
 |  |  |     public String planId;//价格ID | 
 |  |  |  | 
 |  |  |     public String electriclePriceStr;//电价 | 
 |  |  |  | 
 |  |  |     public String adminId;//登录的账号id | 
 |  |  |  | 
 |  |  |     public static QHOnLineApplication getInstance(Application context) { | 
 |  |  |         if (qhOnLineApplication == null) { | 
 |  |  |             qhOnLineApplication = new QHOnLineApplication(); | 
 |  |  |         } | 
 |  |  |         return qhAloneApplication; | 
 |  |  |         qhOnLineApplication.application = context; | 
 |  |  |         ApiManager.init(); | 
 |  |  |         return qhOnLineApplication; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static QHOnLineApplication getInstance() { | 
 |  |  |         if (qhOnLineApplication == null) { | 
 |  |  |             qhOnLineApplication = new QHOnLineApplication(); | 
 |  |  |         } | 
 |  |  |         return qhOnLineApplication; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private void getPublicKey(){ | 
 |  |  |         ApiManager.getInstance().requestPost(application, "/login/getPubKey", String.class, null, new SubscriberListener<BaseResponse<String>>() { | 
 |  |  |     public void getPublicKey(Context context, String name, String pw) { | 
 |  |  |         Map<String, Object> data = new HashMap<>(); | 
 |  |  |         data.put("loginType", "account"); | 
 |  |  |  | 
 |  |  |         ApiManager.getInstance().requestPostLoading(context, "http://120.46.45.35:20081/api/login/getPubKey", String.class, null, new SubscriberListener<BaseResponse<String>>() { | 
 |  |  |             @Override | 
 |  |  |             public void onNext(BaseResponse<String> t) { | 
 |  |  |  | 
 |  |  |                 if (t.isSuccess()) { | 
 |  |  |                     if (t.getData() != null) { | 
 |  |  |                         getToken(context, name, pw, t.getData()); | 
 |  |  |                     } | 
 |  |  |                 } else { | 
 |  |  |                     ToastUtil.show(t.getMsg()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public void getToken(Context context, String name, String pw, String pubKey) { | 
 |  |  |         Map<String, Object> data = new HashMap<>(); | 
 |  |  |         data.put("loginType", "account"); | 
 |  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         String dateString = sdf.format(new Date()); | 
 |  |  |         data.put("password", RSAUtile.encryptByPublicKey(pw + "@TIME@" + dateString, pubKey)); | 
 |  |  |         data.put("username", name); | 
 |  |  |         ApiManager.getInstance().requestPostLoading(application, "http://120.46.45.35:20081/api/login/login/getToken", String.class, data, new SubscriberListener<BaseResponse<String>>() { | 
 |  |  |             @Override | 
 |  |  |             public void onNext(BaseResponse<String> t) { | 
 |  |  |                 if (t.isSuccess()) { | 
 |  |  |                     if (t.getData() != null) { | 
 |  |  |                         tokenStr = t.getData(); | 
 |  |  |                         login(context, name, pw, pubKey); | 
 |  |  |                     } | 
 |  |  |                 } else { | 
 |  |  |                     ToastUtil.show(t.getMsg()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public void login(Context context, String name, String pw, String pubKey) { | 
 |  |  |         Map<String, Object> data = new HashMap<>(); | 
 |  |  |         data.put("loginType", "account"); | 
 |  |  |         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         String dateString = sdf.format(new Date()); | 
 |  |  |         data.put("password", RSAUtile.encryptByPublicKey(pw + "@TIME@" + dateString, pubKey)); | 
 |  |  |         data.put("username", name); | 
 |  |  |         ApiManager.getInstance().requestPostLoading(application, "http://120.46.45.35:20081/api/login/login", LoginResult.class, data, new SubscriberListener<BaseResponse<LoginResult>>() { | 
 |  |  |             @Override | 
 |  |  |             public void onNext(BaseResponse<LoginResult> t) { | 
 |  |  |                 if (t.isSuccess()) { | 
 |  |  |                     try { | 
 |  |  |                         if (t.getData() != null && t.getData().getOtherData() != null) { | 
 |  |  |  | 
 |  |  |                             LoginResult.OtherData otherData = t.getData().getOtherData(); | 
 |  |  |                             String tenantAdnm = otherData.getTenantAdnm(); | 
 |  |  |                             List<String> adnmList = otherData.getAdnmList(); | 
 |  |  |                             if (tenantAdnm != null && adnmList != null && !adnmList.isEmpty()) { | 
 |  |  |                                 addressName = tenantAdnm + adnmList.get(0); | 
 |  |  |                             } | 
 |  |  |                             List<String> adcdList = otherData.getAdnmList(); | 
 |  |  |                             if (adcdList != null && !adcdList.isEmpty()) { | 
 |  |  |                                 adcd = t.getData().getOtherData().getAdcdList().get(0); | 
 |  |  |                                 arerNumber = t.getData().getOtherData().getAdcdList().get(0).substring(4); | 
 |  |  |                             } | 
 |  |  |                             adminId = t.getData().getUserId(); | 
 |  |  |                             context.startActivity(new Intent(context, HomeActivityQHOline.class)); | 
 |  |  |                             ((Activity) context).finish(); | 
 |  |  |                         } else { | 
 |  |  |                             TipUtil.show((Activity) context, "登录失败,请重试"); | 
 |  |  |                         } | 
 |  |  |                     } catch (Exception e) { | 
 |  |  |                         e.printStackTrace(); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } else { | 
 |  |  |                     ToastUtil.show(t.getMsg()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取价格信息 | 
 |  |  |      */ | 
 |  |  |     public void getPricePlanVo(Context context) { | 
 |  |  |         Map<String, Object> data = new HashMap<>(); | 
 |  |  |         data.put("priceMethod", 2); | 
 |  |  |         ApiManager.getInstance().requestPostHideLoading(context, "water/waterPricePlan/pageWaterPricePlanVo", PricePlanResult.class, data, new SubscriberListener<BaseResponse<PricePlanResult>>() { | 
 |  |  |             @Override | 
 |  |  |             public void onNext(BaseResponse<PricePlanResult> t) { | 
 |  |  |                 if (t.isSuccess()) { | 
 |  |  |                     if (t.getData() != null) { | 
 |  |  |                         planId = t.getData().getRecords().get(0).getPlanId(); | 
 |  |  |                         electriclePriceStr = t.getData().getRecords().get(0).getElectricityPrice(); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  | } |