LoginController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.jyc.threegames.controller;
  2. import android.content.Intent;
  3. import android.os.Environment;
  4. import android.text.TextUtils;
  5. import android.util.Log;
  6. import com.google.gson.Gson;
  7. import com.google.gson.JsonParser;
  8. import com.jyc.threegames.App;
  9. import com.jyc.threegames.bean.ControllerMessage;
  10. import com.jyc.threegames.bean.request.ReqLogin;
  11. import com.jyc.threegames.bean.result.ResGameConfig;
  12. import com.jyc.threegames.bean.result.ResLogin;
  13. import com.jyc.threegames.bean.result.ResUserConfig;
  14. import com.jyc.threegames.net.ResData;
  15. import com.jyc.threegames.net.RetrofitHelper;
  16. import com.jyc.threegames.net.api.LoginService;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.FileReader;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.util.Calendar;
  25. import java.util.List;
  26. import io.reactivex.Observable;
  27. import io.reactivex.ObservableSource;
  28. import io.reactivex.functions.Consumer;
  29. import io.reactivex.functions.Function;
  30. public class LoginController extends BaseController {
  31. private static LoginController mInstance;
  32. private LoginController(){}
  33. private ResLogin mLoginInfo;
  34. private static final String PATH = "/ThreeGames/";
  35. private static final String FILE_NAME = "loginInfo";
  36. public static LoginController getInstance(){
  37. if(mInstance == null){
  38. synchronized (LoginController.class){
  39. if(mInstance == null)
  40. mInstance = new LoginController();
  41. }
  42. }
  43. return mInstance;
  44. }
  45. private static LoginService mService;
  46. protected static LoginService getLoginService(){
  47. if (mService == null){
  48. synchronized (LoginController.class){
  49. if (mService == null)
  50. mService = RetrofitHelper.getDefaultRetrofit(App.SERVER_ADDRESS).create(LoginService.class);
  51. }
  52. }
  53. return mService;
  54. }
  55. public boolean restoreLoginInfoFromSDCardFile(){
  56. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  57. try {
  58. File targetFile = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + PATH + FILE_NAME);
  59. if (targetFile.exists() && targetFile.isFile()){
  60. FileInputStream inputStream = new FileInputStream(targetFile);
  61. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
  62. StringBuilder sb = new StringBuilder("");
  63. String line;
  64. while ((line = br.readLine()) != null){
  65. sb.append(line);
  66. }
  67. br.close();
  68. inputStream.close();
  69. mLoginInfo = new Gson().fromJson(sb.toString(), ResLogin.class);
  70. return true;
  71. }else
  72. return false;
  73. }catch (Exception e){
  74. e.printStackTrace();
  75. Log.e("three_games", "read file error: " + e.getMessage());
  76. }
  77. }
  78. return false;
  79. }
  80. public Observable<ControllerMessage<Boolean>> canLogout(){
  81. return UserConfigController.getInstance().getUserConfig(mLoginInfo.info.id)
  82. .map(new Function<ControllerMessage<ResUserConfig>, ControllerMessage<Boolean>>() {
  83. @Override
  84. public ControllerMessage<Boolean> apply(ControllerMessage<ResUserConfig> message) throws Exception {
  85. return ControllerMessage.getSimpleMessage(message.isSuccess(), message.getMessage(), message.isSuccess() && message.getObject() != null && message.getObject().loginOut == ResUserConfig.CAN_LOGOUT);
  86. }
  87. });
  88. }
  89. private List<ResGameConfig> mGameConfigTempData;
  90. public Observable<ControllerMessage<ResLogin>> login(String userName, String passWord){
  91. return getLoginService().login(userName, passWord)
  92. .concatMap(new Function<ResData<ResLogin>, ObservableSource<ResData<ResLogin>>>() {
  93. @Override
  94. public ObservableSource<ResData<ResLogin>> apply(ResData<ResLogin> data) throws Exception {
  95. if (data.isRequestSuccess() && data.data != null && data.data.info != null && !TextUtils.isEmpty(data.data.token)){
  96. mLoginInfo = data.data;
  97. return getLoginService().getGameConfigs(data.data.info.id).
  98. map(new Function<ResData<List<ResGameConfig>>, ResData<ResLogin>>() {
  99. @Override
  100. public ResData<ResLogin> apply(ResData<List<ResGameConfig>> listData) throws Exception {
  101. if (listData.isRequestSuccess() && listData.data != null) {
  102. mGameConfigTempData = listData.data;
  103. }else {
  104. data.code = ResData.CODE_ERROR;
  105. data.message = listData.message;
  106. data.data = null;
  107. }
  108. return data;
  109. }
  110. });
  111. }else
  112. return Observable.just(data);
  113. }
  114. })
  115. .doOnNext(new Consumer<ResData<ResLogin>>() {
  116. @Override
  117. public void accept(ResData<ResLogin> data) throws Exception {
  118. if (data.isRequestSuccess() && data.data != null && data.data.info != null && !TextUtils.isEmpty(data.data.token)){
  119. mLoginInfo = data.data;
  120. mLoginInfo.gameConfigs = mGameConfigTempData;
  121. writeLoginInfoToSDCard();
  122. }
  123. }
  124. })
  125. .takeLast(1)
  126. .map(new SimpleDataHandleFunction<ResData<ResLogin>, ControllerMessage<ResLogin>>());
  127. }
  128. public void logout(){
  129. this.mLoginInfo = null;
  130. deleteLoginInfoOnSDCard();
  131. }
  132. public boolean inTheTestPeriod(){
  133. if (this.mLoginInfo == null || this.mLoginInfo.gameConfigs == null || this.mLoginInfo.gameConfigs.size() == 0)
  134. return false;
  135. for (ResGameConfig item : this.mLoginInfo.gameConfigs){
  136. String startTime = item.startTime;
  137. Calendar startCalendar = Calendar.getInstance();
  138. startCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startTime.split(":")[0]));
  139. startCalendar.set(Calendar.MINUTE, Integer.parseInt(startTime.split(":")[1]));
  140. String endTime = item.endTime;
  141. Calendar endCalendar = Calendar.getInstance();
  142. endCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(endTime.split(":")[0]));
  143. endCalendar.set(Calendar.MINUTE, Integer.parseInt(endTime.split(":")[1]));
  144. Calendar nowCalendar = Calendar.getInstance();
  145. if (nowCalendar.getTime().getTime() >= startCalendar.getTime().getTime() && nowCalendar.getTime().getTime() <= endCalendar.getTime().getTime())
  146. return true;
  147. }
  148. return false;
  149. }
  150. private void deleteLoginInfoOnSDCard(){
  151. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  152. try {
  153. File targetFile = new File(Environment.getExternalStorageDirectory() + PATH + FILE_NAME);
  154. if (targetFile.exists())
  155. targetFile.delete();
  156. } catch (Exception e){
  157. e.printStackTrace();
  158. }
  159. }
  160. }
  161. private void writeLoginInfoToSDCard(){
  162. if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  163. try {
  164. File sdCardDir = Environment.getExternalStorageDirectory();
  165. File dir = new File(sdCardDir.getCanonicalPath() + PATH);
  166. if (!dir.exists()) {
  167. if (dir.mkdirs()){
  168. continueCreateFile(sdCardDir);
  169. }else
  170. Log.e("three_games", "create sdcard file path error");
  171. }else
  172. continueCreateFile(sdCardDir);
  173. } catch (Exception e){
  174. e.printStackTrace();
  175. Log.e("three_games", "create sdcard file exception: " + e.getMessage());
  176. }
  177. }
  178. }
  179. private void continueCreateFile(File sdCardDir) throws Exception{
  180. File targetFile = new File(sdCardDir.getCanonicalPath() + PATH + FILE_NAME);
  181. targetFile.delete();
  182. if (targetFile.createNewFile()){
  183. String content = new Gson().toJson(mLoginInfo);
  184. FileOutputStream outStream = new FileOutputStream(targetFile);
  185. outStream.write(content.getBytes());
  186. outStream.close();
  187. }else
  188. Log.e("three_games", "create sdcard file error");
  189. }
  190. public ResLogin getCurrentLoginInfo(){
  191. return mLoginInfo;
  192. }
  193. public boolean isCurrentUserAdmin(){
  194. if (mLoginInfo != null){
  195. return mLoginInfo.isAdmin();
  196. }
  197. return false;
  198. }
  199. public String getToken(){
  200. if (mLoginInfo != null && !TextUtils.isEmpty(mLoginInfo.token))
  201. return mLoginInfo.token;
  202. return "";
  203. }
  204. }