UserUtilAction.java 16.9 KB
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 413 414 415 416 417 418 419 420 421 422 423 424 425 426
package com.cnlive.strike.user;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.cnlive.libs.base.application.AppConfig;
import com.cnlive.libs.base.logic.Logic;
import com.cnlive.libs.base.logic.callback.DataCallback;
import com.cnlive.libs.data.network.Subscriber;
import com.cnlive.strike.data.network.api.ApiBaseRequest;
import com.cnlive.strike.data.network.api.ApiBaseServiceOpen;
import com.cnlive.strike.data.network.api.ApiBaseSubscriber;
import com.cnlive.strike.data.network.model.BaseInfo;
import com.cnlive.strike.data.network.model.FaceData;
import com.cnlive.strike.data.network.model.TokenData;
import com.cnlive.strike.data.network.model.UserData;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;

import static com.cnlive.libs.base.logic.Config.SUCCESS;


/**
 * Created by Lynn on 2018/10/15.
 */

public class UserUtilAction {
    public static int TOKEN_ERROR_CODE = -900001;
    public static String TOKEN_ERROR_MSG = "用户token为空";

    /**
     * 快捷登录前发送手机验证码
     *
     * @param context
     * @param mobile  手机号
     * @return
     */
    public static Logic.Action<Object, BaseInfo> sendMsgBefQuickLoginAction(final Context context, final String mobile, String countryCode) {
        return (data, callback) -> {
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("appId", AppConfig.getAppId());
            requestMap.put("mobile", mobile);
            requestMap.put("countryCode", countryCode);
            requestMap.put("clientPlat", "a");
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.sendVerifyCodeBefQuickLogin(requestMap)).subscribe(context, callback);
        };

    }

    /**
     * 其他平台账户登录
     *
     * @param context
     * @param thirdPartyPlat 平台类型  1:sina 2:qq 3:weixin 4:renren
     * @param thirdPartyId   第三方用户ID
     *                       [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr];
     *                       注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同
     */
    public static Logic.Action<Object, BaseInfo<UserData>> otherLogin(Context context, String thirdPartyPlat, String thirdPartyId) {
        return (data, callback) -> {
            Map<String, String> requestMap = new HashMap<>();

            requestMap.put("appId", AppConfig.getAppId());
            requestMap.put("thirdPartyPlat", thirdPartyPlat);
            requestMap.put("thirdPartyId", thirdPartyId);
            requestMap.put("frmId", AppConfig.getAppChannel());
            requestMap.put("clientPlat", "a");

            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.loginIn3rdForWJJ(requestMap)).subscribe(context, callback);
        };
    }

    /**
     * 其他平台账户版定手机号并登录
     *
     * @param context
     * @param thirdPartyPlat 平台类型  1:sina 2:qq 3:weixin 4:renren
     * @param thirdPartyId   第三方用户ID
     *                       [qq: 建议使用openid, wx: 建议使用unionid, sina: 建议使用idstr];
     *                       注意:同一用户在登录Sp下不同app时,thirdPartyId必须相同
     */


    public static Logic.Action<Object, BaseInfo<UserData>> otherLoginBindMobile(Context context,
                                                                                String phone,
                                                                                String countryCode,
                                                                                String ems,
                                                                                String thirdPartyPlat,
                                                                                String thirdPartyId,
                                                                                String nickName,
                                                                                String gender,
                                                                                String location,
                                                                                String faceUrl) {
        return (data, callback) -> {
            Map<String, String> requestMap = new HashMap<>();

            requestMap.put("frmId", AppConfig.getAppChannel());
            requestMap.put("clientPlat", "a");
            requestMap.put("uuid", AppConfig.getAppUUID());

            requestMap.put("appId", AppConfig.getAppId());
            requestMap.put("mobile", phone);
            requestMap.put("countryCode", countryCode);
            requestMap.put("verificationCode", ems);

            requestMap.put("thirdPartyPlat", thirdPartyPlat);
            requestMap.put("thirdPartyId", thirdPartyId);
            requestMap.put("nickName", nickName);
            requestMap.put("gender", gender);
            requestMap.put("location", location);
            requestMap.put("faceUrl", faceUrl);


            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.loginIn3rdAndMobileForWJJ(requestMap)).subscribe(context, callback);
        };
    }

    /**
     * 快捷登录
     *
     * @param context
     * @param userPhone 手机号
     * @param userCode  验证码
     * @return
     */
    public static Logic.Action<Object, BaseInfo<UserData>> quickLoginAction(Context context, String userPhone, String userCode, String countryCode) {
        return (data, callback) -> {
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("appId", AppConfig.getAppId());
            requestMap.put("userName", userPhone);
            requestMap.put("verificationCode", userCode);
            requestMap.put("uuid", AppConfig.getAppUUID());
            requestMap.put("frmId", AppConfig.getAppChannel());
            requestMap.put("countryCode", countryCode);
            requestMap.put("clientPlat", "a");
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.quickLogin(requestMap)).subscribe(context, callback);
        };
    }

    /**
     * 登录
     *
     * @param context
     * @param userName 用户名
     * @param pwd      密码
     * @return
     */
    public static Logic.Action<Object, BaseInfo<UserData>> loginAction(Context context, String userName, String pwd) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("userName", userName);
            map.put("pwd", pwd);
            map.put("uuid", AppConfig.getAppUUID());
            map.put("frmId", "");
            map.put("clientPlat", "a");
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.login(map)).subscribe(context, callback);
        };
    }

    /**
     * 查询用户信息
     *
     * @param context
     * @param userId  用户ID
     * @param srcUid  被查询用户ID
     * @param token   查询者平台TOKEN
     * @return
     */
    public static Logic.Action<Object, BaseInfo<UserData>> queryUserInfo(Context context, String userId, String srcUid, String token) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("uid", userId);
            map.put("srcUid", srcUid);
            map.put("clientPlat", "a");
            map.put("token", token);
            if (TextUtils.isEmpty(token)) {
                callback.callback(TOKEN_ERROR_CODE, TOKEN_ERROR_MSG, null);
            }
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.queryUserInfo(map)).subscribe(context, callback);
        };
    }

    /**
     * 修改用户附加信息
     *
     * @param context
     * @param userId  用户id
     * @param extInfo 扩展信息
     * @param token   平台token
     * @return
     */
    public static Logic.Action<Object, BaseInfo> updateUserExtInfo(Context context, String userId, String extInfo, String token) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("uid", userId);
            map.put("extInfo", extInfo);
            map.put("token", token);
            map.put("clientPlat", "a");
            if (TextUtils.isEmpty(token)) {
                callback.callback(TOKEN_ERROR_CODE, TOKEN_ERROR_MSG, null);
            }
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.updateUserExtInfo(map)).subscribe(context, callback);
        };
    }

    /**
     * 修改用户单项信息
     *
     * @param context
     * @param uid     用户Id
     * @param type    nickName-昵称,gender-性别,email-邮箱, location-联系方式
     * @param value   对应的信息,性别,m:男、f:女、n:未知
     * @param token   平台TOKEN
     * @return
     */
    public static Logic.Action<Object, BaseInfo> modifyUserInfo(Context context, String uid, String type, String value, String token) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("uid", uid);
            map.put("type", type);
            map.put("value", value);
            map.put("clientPlat", "a");
            map.put("token", token);
            if (TextUtils.isEmpty(token)) {
                callback.callback(TOKEN_ERROR_CODE, TOKEN_ERROR_MSG, null);
            }
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.modifyUserInfo(map)).subscribe(context, callback);
        };
    }

    /**
     * 注册或修改手机号前发手机验证码
     *
     * @param context
     * @param mobile  手机号
     * @return
     */
    public static Logic.Action<Object, BaseInfo> sendVerifyCodeForUnRegistered(Context context, String mobile, String countryCode) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("mobile", mobile);
            map.put("clientPlat", "a");
            map.put("countryCode", countryCode);
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.sendVerifyCodeForUnRegistered(map)).subscribe(context, callback);
        };
    }

    /**
     * 更新手机号
     *
     * @param context
     * @param uid              用户ID
     * @param mobile           手机号
     * @param verificationCode 手机验证码
     * @param token            平台TOKEN
     * @return
     */
    public static Logic.Action<Object, BaseInfo> updateMobile(Context context, String uid, String mobile, String verificationCode, String token, String countryCode) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("uid", uid);
            map.put("mobile", mobile);
            map.put("verificationCode", verificationCode);
            map.put("clientPlat", "a");
            map.put("countryCode", countryCode);
            map.put("token", token);
            if (TextUtils.isEmpty(token)) {
                callback.callback(TOKEN_ERROR_CODE, TOKEN_ERROR_MSG, null);
            }
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.updateMobile(map)).subscribe(context, callback);
        };
    }

    /**
     * 注册
     *
     * @param context
     * @param userName         邮箱或手机
     * @param pwd              密码
     * @param verificationCode 手机验证码(手机号注册时必填)
     * @return
     */
    public static Logic.Action<Object, BaseInfo<UserData>> register(Context context, String userName, String pwd, String verificationCode) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("userName", userName);
            map.put("pwd", pwd);
            map.put("verificationCode", verificationCode);
            map.put("invitationCode", "");
            map.put("uuid", AppConfig.getAppUUID());
            map.put("frmId", "");
            map.put("clientPlat", "a");
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.register(map)).subscribe(context, callback);
        };
    }


    /**
     * 获取access_token
     *
     * @param context
     * @return
     */
    public static Logic.Action<Object, BaseInfo<TokenData>> accessToken(Context context) {
        return (data, callback) -> {
            Map<String, String> map = new HashMap<>();
            map.put("appId", AppConfig.getAppId());
            map.put("secret", AppConfig.getAppSceret());
            return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.accessToken(map)).subscribe(context, callback);
        };
    }

    /**
     * 上传头像
     *
     * @param context
     * @param uid          用户ID
     * @param face         头像文件
     * @param token        平台TOKEN
     * @param access_token
     * @return
     */
    public static Disposable updateUserFace(Context context, String uid, File face, String token, String access_token, DataCallback<BaseInfo<FaceData>> callback) {
        Map<String, RequestBody> params = new HashMap<>();

        if (TextUtils.isEmpty(token)) {
            callback.callback(TOKEN_ERROR_CODE, TOKEN_ERROR_MSG, null);
        }

        params.put("uid", getRequestString(uid));
        params.put("clientPlat", getRequestString("a"));
        params.put("token", getRequestString(token));
        params.put("access_token", getRequestString(access_token));

        RequestBody requestFace = RequestBody.create(MediaType.parse("multipart/form-data"), face);
        MultipartBody.Part faceBody = MultipartBody.Part.createFormData("face", face.getName(), requestFace);

        List<MultipartBody.Part> parts = new ArrayList<>();
        parts.add(faceBody);


        Subscriber<BaseInfo<FaceData>> subscriber = new Subscriber<BaseInfo<FaceData>>(context, ApiBaseSubscriber.subscriberBuild) {
            @Override
            public void onCompleted(String s, String s1, BaseInfo<FaceData> faceDataBaseInfo) {
                if (callback == null) return;
                callback.callback(SUCCESS, "", faceDataBaseInfo);
            }

            @Override
            public void onError(String s, String s1) {
                if (callback == null) return;
                int errorCode = -1;
                try {
                    errorCode = Integer.valueOf(s);
                } catch (Exception ignored) {
                } finally {
                    callback.callback(errorCode, s1, null);
                }
            }
        };

        return ApiBaseRequest.subscribe(ApiBaseRequest.service(ApiBaseServiceOpen.class).updateUserFace(params, parts), subscriber);
//        return ApiBaseRequest.service(ApiBaseServiceOpen.class, service -> service.updateUserFace(params, parts)).subscribe(context, callback);
    }

    private static RequestBody getRequestString(String value) {
        RequestBody requestString = RequestBody.create(MediaType.parse("text/plain"), value);
        return requestString;
    }

    public static Disposable zipFiles(Context context, List<String> imageFiles, DataCallback<List<String>> callback) {
        if (imageFiles == null || imageFiles.size() == 0) {
            callback.callback(0, "", imageFiles);
        } else {
            return Flowable.just(imageFiles)
                    .observeOn(Schedulers.io())
                    .map(list -> Luban.with(context)
                            .ignoreBy(500)
                            .setTargetDir(getPath())
                            .load(list)
                            .get())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnError(throwable -> callback.callback(0, "", imageFiles))
                    .onErrorResumeNext(Flowable.empty())
                    .subscribe(
                            list -> {
                                List<String> files = new ArrayList<>();
                                for (File file : list) files.add(file.getPath());
                                callback.callback(0, "", files);
                            });
        }
        return null;
    }

    private static String getPath() {
        String path = Environment.getExternalStorageDirectory() + "/strike/image/";
        File file = new File(path);
        if (file.mkdirs()) {
            return path;
        }
        return path;
    }


}