UserOperationController.java 33.5 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
package com.topdraw.business.process.rest;

import cn.hutool.core.util.ObjectUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.annotation.AnonymousAccess;
import com.topdraw.business.module.common.validated.CreateGroup;
import com.topdraw.business.module.common.validated.UpdateGroup;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.app.domain.UserApp;
import com.topdraw.business.module.user.app.domain.UserAppBind;
import com.topdraw.business.module.user.app.service.dto.UserAppDTO;
import com.topdraw.business.module.user.app.service.dto.UserAppSimpleDTO;
import com.topdraw.business.module.user.iptv.domain.UserTv;
import com.topdraw.business.module.user.iptv.growreport.domain.GrowthReport;
import com.topdraw.business.module.user.iptv.growreport.service.dto.GrowthReportRequest;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.business.module.user.weixin.domain.UserWeixin;
import com.topdraw.business.module.user.weixin.service.dto.UserWeixinDTO;
import com.topdraw.business.process.domian.member.MemberOperationBean;
import com.topdraw.business.process.domian.weixin.*;
import com.topdraw.business.process.service.UserOperationService;
import com.topdraw.business.process.service.member.MemberOperationService;
import com.topdraw.common.ResultInfo;
import com.topdraw.util.RedisKeyUtil;
import com.topdraw.config.WechatConfig;
import com.topdraw.exception.BadRequestException;
import com.topdraw.exception.EntityNotFoundException;
import com.topdraw.exception.GlobeExceptionMsg;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.Base64Util;
import com.topdraw.util.JSONUtil;
import com.topdraw.utils.RedisUtils;
import com.topdraw.weixin.service.WeChatConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.naming.ConfigurationException;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.*;

@Api("账号处理")
@RestController
@RequestMapping(value = "/uce/userOperation")
@Slf4j
@CrossOrigin
public class UserOperationController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private UserOperationService userOperationService;
    @Autowired
    private MemberOperationService memberOperationService;
    @Autowired
    private UserTvService userTvService;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private WechatConfig weixinInfoConfig;

    private static final String SUBSCRIBE = "subscribe";
    private static final String UNSUBSCRIBE = "unsubscribe";
    private static final Integer SUBSCRIBE_STATUS = 1;



    /******************************************************* APP ************************************/

    /**
     * app账号退出登录
     * @param userApp app账号
     * @return ResultInfo
     */
    @PostMapping(value = "/appSignOut")
    @ApiOperation("app账号退出登录")
    @AnonymousAccess
    public ResultInfo appSignOut(@Validated @RequestBody UserApp userApp) {
        log.info("app账号退出登录,参数 ==>> [appSignOut#{}]", userApp.getId());
        if (Objects.isNull(userApp.getId())) {
            log.error("app账号退出登录失败,参数错误,id不得为空,[appSignOut#]");
            return ResultInfo.failure("app账号退出登录失败,参数错误,id不得为空");
        }

        return ResultInfo.success(true);
    }

    /**
     * 注销app账号
     * @param userApp app账号
     * @return ResultInfo
     */
    @PostMapping(value = "/appCancellation")
    @ApiOperation("注销app账号")
    @AnonymousAccess
    public ResultInfo appCancellation(@Validated @RequestBody UserApp userApp) {
        log.info("注销app账号,参数 ==>> [appCancellation#{}]", userApp.getId());

        if (Objects.isNull(userApp.getId())) {
            log.error("注销app账号失败,参数错误,id不得为空,[appCancellation#]");
            return ResultInfo.failure("");
        }

        boolean result = this.userOperationService.appCancellation(userApp);

        return ResultInfo.success(result);
    }

    @PostMapping(value = "/updateAppInfo")
    @ApiOperation("修改app账号信息")
    @AnonymousAccess
    public ResultInfo updateAppInfo(@Validated @RequestBody UserApp resources) {
        log.info("修改app账号信息,参数 ==>> [updateAppInfo#{}]", resources);
        Long id = resources.getId();
        if (Objects.isNull(id)) {
            log.error("修改app账号密码,参数错误,id不得为空,[updateAppInfo#{}]", resources);
            return ResultInfo.failure("修改app账号密码失败,参数错误,id不得为空");
        }

        String headimgurl = resources.getHeadimgurl();
        log.info("前端头像地址 ==>> [updateAppInfo#{}]", headimgurl);
        if (StringUtils.isNotBlank(headimgurl) && (headimgurl.contains("http") || headimgurl.contains("https"))) {
            String image = RestTemplateClient.netImage(headimgurl);
            log.info("图片本地化后的图片地址 ==>> [updateAppInfo#{}]", image);
            resources.setHeadimgurl(image);
        }

        UserAppSimpleDTO result = this.userOperationService.updateAppInfo(resources);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/appRegister")
    @ApiOperation("app注册")
    @AnonymousAccess
    public ResultInfo appRegister(@Validated @RequestBody UserApp resources) {
        log.info("app注册 ==>> param ==>> [appRegister#{}]", resources);

        String username = resources.getUsername();
        if (StringUtils.isBlank(username)) {
            log.error("app注册,参数错误,账号不得为空 ");
            return ResultInfo.failure("app注册,参数错误,账号不得为空");
        }

        Integer type = resources.getType();
        if (Objects.isNull(type)) {
            log.error("app注册,参数错误,账号类型不得为空 ");
            return ResultInfo.failure("app注册,参数错误,账号类型不得为空");
        }

        String account = resources.getAccount();
        if (StringUtils.isNotBlank(account)) {
            if (Objects.isNull(resources.getAccountType())) {
                log.error("app注册,参数错误,第三方账号类型不得为空");
                return ResultInfo.failure("app注册,参数错误,第三方账号类型不得为空");
            }
        }

        if (StringUtils.isBlank(resources.getNickname())) {
            resources.setNickname(Base64Utils.encodeToString(username.getBytes()));
        }

        UserAppDTO userAppDTO = this.userOperationService.appRegister(resources);
        return ResultInfo.success(userAppDTO);
    }

    @PostMapping(value = "/appBindThirdAccount")
    @ApiOperation("app账号绑定第三方账号")
    @AnonymousAccess
    public ResultInfo appBindThirdAccount(@Validated @RequestBody UserAppBind resources) {
        log.info("app账号绑定第三方账号,参数 ==>> [appBindThirdAccount#{}]", resources);

        String username = resources.getUsername();
        if (StringUtils.isBlank(username)) {
            log.error("app账号绑定第三方账号,参数错误,账号不得为空 ");
            return ResultInfo.failure("app账号绑定第三方账号,参数错误,账号不得为空");
        }

        String account = resources.getAccount();
        if (StringUtils.isNotBlank(account)) {
            if (Objects.isNull(resources.getAccountType())) {
                log.error("app账号绑定第三方账号,参数错误,第三方账号不得为空");
                return ResultInfo.failure("app账号绑定第三方账号,参数错误,第三方账号不得为空");
            }
        }

        // 第三方账号类型 3:微信;4:QQ;5:微博;6:苹果账号
        Integer accountType = resources.getAccountType();
        if (Objects.isNull(accountType)) {
            log.error("app账号绑定第三方账号,参数错误,第三方账号类型不得为空");
            return ResultInfo.failure("app账号绑定第三方账号,参数错误,第三方账号类型不得为空");
        }

        return this.userOperationService.appBindThirdAccount(resources);
    }

    @PostMapping(value = "/cancelUserAppBind")
    @ApiOperation("取消关联第三方账号")
    @AnonymousAccess
    public ResultInfo cancelUserAppBind(@Validated @RequestBody UserAppBind resources) {
        log.info("取消关联第三方账号, 参数 ==>> [cancelUserAppBind#{}]", resources);

        String account = resources.getAccount();
        if (StringUtils.isBlank(account)) {
            log.error("取消关联第三方账号失败,参数错误了,第三方账号不能为空");
            return ResultInfo.failure("取消关联第三方账号失败,参数错误,第三方账号不能为空");
        }

        // 第三方账号类型 3:微信;4:QQ;5:微博;6:苹果账号
        Integer accountType = resources.getAccountType();
        if (Objects.isNull(accountType)) {
            log.error("取消关联第三方账号失败,参数错误,第三方账号类型不得为空");
            return ResultInfo.failure("取消关联第三方账号失败,参数错误,第三方账号类型不得为空");
        }

        return this.userOperationService.cancelUserAppBind(resources);
    }


    @PostMapping("/appBind")
    @ApiOperation("app绑定大屏")
    @AnonymousAccess
    public ResultInfo appBind(@Validated(value = {BindGroup.class}) @RequestBody BindBean resources) {
        log.info("app绑定大屏, 参数 appBind# resources ==> {}",resources);

        Long memberId = resources.getMemberId();
        if (Objects.isNull(memberId)) {
            return ResultInfo.failure("参数错误, memberId不得为空");
        }

        return this.userOperationService.appBind(resources);
    }

    @PostMapping("/appUnbind")
    @ApiOperation("app解绑")
    @AnonymousAccess
    public ResultInfo appUnbind(@Validated(value = {UnbindGroup.class}) @RequestBody WeixinUnBindBean weixinUnBindBean) {
        log.info("UserOperationController ==> minaUnbind ==>> param  ==> [{}]", weixinUnBindBean);

        Long memberId = weixinUnBindBean.getMemberId();
        if (Objects.isNull(memberId)) {
            log.error("小屏解绑失败,参数错误,memberId不存在");
            return ResultInfo.failure("参数错误,会员id不存在");
        }

        return this.userOperationService.minaUnbind(weixinUnBindBean);
    }


    /******************************************************* weixin ************************************/

    @PostMapping(value = "/saveGrowthReport")
    @ApiOperation("同步大屏成长报告")
    @AnonymousAccess
    public ResultInfo saveGrowthReport(@Validated @RequestBody String resources) {
        log.info("同步大屏成长报告,参数 ==>> [saveGrowthReport#{}]", resources);
        GrowthReportRequest growthReportRequest = JSON.parseObject(new String(Base64Utils.decode(resources.getBytes())), GrowthReportRequest.class);

        String platformAccount = growthReportRequest.getPlatformAccount();
        log.info("同步大屏成长报告消息,大屏账号 ==>> [saveGrowthReport#{}]", platformAccount);
        List<GrowthReportRequest.CategoryContent> playDurationWithCategory = growthReportRequest.getPlayDurationWithCategory();
        log.info("同步大屏成长报告消息主体,消息体 ==>> [saveGrowthReport#{}]", playDurationWithCategory);
        if (!CollectionUtils.isEmpty(playDurationWithCategory)) {
            GrowthReport growthReport = new GrowthReport();
            growthReport.setPlatformAccount(platformAccount);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("playDurationWithCategory", playDurationWithCategory);
            growthReport.setData(jsonObject.toJSONString());
            return this.userOperationService.saveGrowthReport(growthReport);
        }

        return ResultInfo.failure("保存失败");
    }

    @PutMapping(value = "/updateWeixin")
    @ApiOperation("修改UserWeixin")
    @AnonymousAccess
    public ResultInfo updateWeixin(@Validated @RequestBody UserWeixin resources) {
        userOperationService.updateWeixin(resources);
        return ResultInfo.success();
    }

    @RequestMapping(value = "/updateVipByUserId")
    @ApiOperation("通过账号id修改vip")
    @AnonymousAccess
    public ResultInfo updateVipByUserId(@Validated(value = {UpdateGroup.class}) @RequestBody
                                                    UserOperationBean resources) {
        log.info("userOperation ==>> updateVipByUserId ==>> param ==>> [{}]",resources);

        Integer vip = resources.getVip();
        if (Objects.isNull(vip) || vip < 1) {
            return ResultInfo.failure("手动修改vip异常,参数错误,vip为空或者小于1");
        }
        Timestamp vipExpireTime = resources.getVipExpireTime();
        // 微信账号id
        Long userId = resources.getUserId();
        if (Objects.isNull(userId)) {
            return ResultInfo.failure("手动修改vip异常,参数错误,小屏账号id为空");
        }
        UserWeixinDTO userWeixinDTO = this.userOperationService.findById(userId);

        Long memberId = userWeixinDTO.getMemberId();
        MemberDTO memberDTO = this.memberService.findById(memberId);

        MemberOperationBean memberOperationBean = new MemberOperationBean();
        memberOperationBean.setMemberCode(memberDTO.getCode());
        if (vip < memberDTO.getVip()) {
            vip = memberDTO.getVip();
        } else {
            memberOperationBean.setVipExpireTime(vipExpireTime);
        }
        memberOperationBean.setVip(vip);

        MemberDTO memberDTO1 = this.memberOperationService.doUpdateVipByMemberCode(memberOperationBean);

        return ResultInfo.success(memberDTO1);
    }

    @PostMapping(value = "/createWeixinUserAndCreateMember")
    @ApiOperation("新增小屏账户同时创建会员信息")
    @AnonymousAccess
    public ResultInfo createWeixinUserAndMember(@Validated(value = {CreateGroup.class}) @RequestBody UserWeixin resources) {
        log.info("UserOperationController ==> createWeixinUserAndMember ==> param  ==> [{}]",resources);

        UserWeixinDTO result = this.userOperationService.createWeixinUserAndMember(resources);
        return ResultInfo.success(result);
    }

    @PostMapping("/serviceLogin")
    @ApiOperation("微信服务号(H5)登录")
    @AnonymousAccess
    public ResultInfo serviceLogin(@Validated(value = {CreateGroup.class}) @RequestBody UserWeixin resources) {
        log.info("UserOperationController ==> serviceLogin ==>> param  ==> [{}]",resources);

        UserWeixinDTO result = this.userOperationService.serviceLogin(resources);
        return ResultInfo.success(result);
    }

    @PostMapping("/appletLogin")
    @ApiOperation("微信小程序登录")
    @AnonymousAccess
    public ResultInfo appletLogin(@Validated(value = {CreateGroup.class}) @RequestBody UserWeixin resources) {
        log.info("UserOperationController ==> appletLogin ==>> param  ==> [{}]",resources);

        UserWeixinDTO result = this.userOperationService.appletLogin(resources);
        return ResultInfo.success(result);
    }

    @PostMapping("/subscribe")
    @ApiOperation("微信公众号关注")
    @AnonymousAccess
    public ResultInfo subscribe(@Validated @RequestBody SubscribeBeanEvent data) throws Exception {
        log.info("UserOperationController ==> subscribe ==>> param  ==> [{}]",data);

        SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(data.getContent(), SubscribeBean.class);
        // 解析参数
        this.parseSubscribe(subscribeBean);
        boolean subscribe = this.userOperationService.subscribe(subscribeBean);
        if (subscribe) {
            return ResultInfo.success("关注成功");
        } else {
            return ResultInfo.failure("关注失败");
        }
    }

    /**
     *
     * @param subscribeBean
     * @throws IOException
     */
    private void parseSubscribe(SubscribeBean subscribeBean) throws Exception {

        // appId
        String appId = subscribeBean.getAppid();
//        Assert.notNull(appId, GlobeExceptionMsg.APP_ID_IS_NULL);
        // openId
        String openId = subscribeBean.getOpenid();
        Assert.notNull(openId, GlobeExceptionMsg.OPEN_ID_IS_NULL);
        // unionId
        String unionId = subscribeBean.getUnionid();
        Assert.notNull(openId, GlobeExceptionMsg.UNION_ID_IS_NULL);

        // 匹配配置文件中的微信列表信息
        Map<String, String> wxInfoMap = this.getWeixinInfoByAppid(appId);

        // 程序类型
        String appType = wxInfoMap.get("appType");
        // 非订阅号,暂不处理。返回暂不支持
        if (ObjectUtil.notEqual(appType, WeChatConstants.WX_SUBSCRIPTION))
            throw new BadRequestException("非订阅号");


        // 大屏账户信息
        JSONObject redisInfo = null;
        // 缓存的大屏信息,使用unionid即可
        String content = (String) this.redisUtils.get(RedisKeyUtil.genSeSuSubscribeKey(unionId));
        log.info("获取redis中存储的数据,[subscribe#{}]", content);
        if (StringUtils.isNotBlank(content)) {
            // 大屏信息
            redisInfo = JSONObject.parseObject(content);
        }

        // 用户自己搜索关注就没有大屏信息,否则表示扫码关注
        if (Objects.nonNull(redisInfo)) {

            subscribeBean.setIptvUserInfo(redisInfo);
            // 关注来源信息
            log.info("关注来源信息,[subscribe#{}]", redisInfo);
            subscribeBean.setSourceInfo(redisInfo);

            String nickname = redisInfo.get("nickname").toString();
            if (StringUtils.isNotBlank(nickname)) {
                String nicknameDecode = URLDecoder.decode(nickname, "UTF-8");
                String nicknameEncode = Base64Util.encode(nicknameDecode);
                subscribeBean.setNickname(nicknameEncode);
            }

            String headimgurl = redisInfo.get("headimgurl").toString();
            log.info("parseSubscribe ==>> headimgurl ==>> {}", headimgurl);
            if (StringUtils.isNotBlank(headimgurl)) {
                String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8");
                if (StringUtils.isNotBlank(headimgurlDecode)) {
                    if (headimgurl.contains("https") || headimgurl.contains("http")) {
                        headimgurl = this.downloadWeixinImge(headimgurlDecode);
                    }
                    subscribeBean.setHeadimgurl(headimgurl);
                }
            }

        } else {

            // headimgurl
            String headimgurl = subscribeBean.getHeadimgurl();
            if (StringUtils.isNotBlank(headimgurl)) {
                if (headimgurl.contains("https") || headimgurl.contains("http")) {
                    String headImgUrl = this.downloadWeixinImge(headimgurl);
                    subscribeBean.setHeadimgurl(headImgUrl);
                }
            }

        }

    }

    /**
     * 获取配置的微信应用列表
     * @param appid 应用Id
     * @return
     * @throws ConfigurationException
     */
    private Map<String, String> getWeixinInfoByAppid(String appid) throws ConfigurationException {
        if (com.topdraw.utils.StringUtils.isBlank(appid)) {
            throw new RuntimeException("wxAppid can not be null");
        }
        List<Map<String, String>> list = weixinInfoConfig.getWechatAppList();
        Optional<Map<String, String>> weixinInfoOptional = list.stream().filter(o -> o.get("appid").equals(appid)).findFirst();
        if (!weixinInfoOptional.isPresent()) {
            throw new RuntimeException("wxAppid error, appid is : " + appid);
        }
        return weixinInfoOptional.get();
    }

    @PostMapping("/unsubscribe")
    @ApiOperation("微信公众号取关")
    @AnonymousAccess
    public ResultInfo unsubscribe(@Validated @RequestBody SubscribeBeanEvent data) {
        log.info("UserOperationController ==> unsubscribe ==>> param  ==> [{}]",data);

        SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(data.getContent(), SubscribeBean.class);

        String appId = subscribeBean.getAppid();
        Assert.notNull(appId, GlobeExceptionMsg.APP_ID_IS_NULL);
        // openId
        String openId = subscribeBean.getOpenid();
        Assert.notNull(openId, GlobeExceptionMsg.OPEN_ID_IS_NULL);

        subscribeBean.setAppid(appId);
        subscribeBean.setOpenid(openId);

        boolean result = this.userOperationService.unsubscribe(subscribeBean);
        return ResultInfo.success(result);
    }

    @PostMapping("/minaBind")
    @ApiOperation("微信小程序绑定大屏")
    @AnonymousAccess
    public ResultInfo minaBind(@Validated(value = {BindGroup.class}) @RequestBody BindBean resources) {
        log.info("UserOperationController ==> appletBind ==>> param  ==> [{}]",resources);

        Long memberId = resources.getMemberId();
        if (Objects.isNull(memberId)) {
            return ResultInfo.failure("参数错误,memberId 不存在");
        }

        String platformAccount = resources.getPlatformAccount();
        if (StringUtils.isBlank(platformAccount)) {
            return ResultInfo.failure("参数错误,大屏账号不存在");
        }

        boolean result = this.userOperationService.minaBind(resources);
        return ResultInfo.success(result);
    }

    @PostMapping("/minaUnbind")
    @ApiOperation("小屏解绑")
    @AnonymousAccess
    public ResultInfo minaUnbind(@Validated(value = {UnbindGroup.class}) @RequestBody WeixinUnBindBean weixinUnBindBean) {
        log.info("小屏解绑,参数 ==> [minaUnbind#{}]", weixinUnBindBean);

        Long memberId = weixinUnBindBean.getMemberId();
        if (Objects.isNull(memberId)) {
            log.error("小屏解绑失败,参数错误,memberId不存在");
            return ResultInfo.failure("参数错误,无会员id");
        }

        return this.userOperationService.minaUnbind(weixinUnBindBean);

    }

    /**
     * 1.未关注、未绑定
     * 2.已绑定、未关注
     * 3.已关注、未绑定
     * @param data
     * @return
     */
    @PostMapping(value = "/memberPreprocess")
    @ApiOperation("暂存大小屏信息并检查关注与绑定状态")
    @AnonymousAccess
    public ResultInfo memberPreprocess(@RequestBody String data) {

        log.info("UserOperationController ==> saveUserInfo ==>> param ==>> [{}]",data);
        if (StringUtils.isBlank(data)) {
            log.error("预存大小屏账号信息失败,无参数");
            return ResultInfo.failure("参数错误");
        }

        JSONObject json = JSONObject.parseObject(data);

        String unionid = json.getString("unionid");
        if (StringUtils.isBlank(unionid)) {
            log.error("预存大小屏账号信息失败,参数错误,unionid不存在");
            return ResultInfo.failure("参数错误,unionid不存在");
        }


        List<Object> resultList = new ArrayList<>();
        // 大屏侧通过返回值来展示对应的小程序页面
        String result = SUBSCRIBE;
        String platformAccount1 = "";

        // 保存大小屏信息到redis同时返回小屏信息
        UserWeixinDTO userWeixinDTO = this.userOperationService.saveUserInfo(data);

        // 小屏账号不存在
        if (Objects.isNull(userWeixinDTO) || Objects.isNull(userWeixinDTO.getId())) {

            result = UNSUBSCRIBE;
            resultList.add(result);
            resultList.add(platformAccount1);
            log.info("saveUserInfo ==>> result ==>> [{}]",resultList);
            return ResultInfo.success(resultList);

        } else {

            // 账号存在,未关注
            if (!userWeixinDTO.getStatus().equals(SUBSCRIBE_STATUS)) {
                result = UNSUBSCRIBE;
            }

            // 小屏会员
            MemberDTO memberDTO = this.memberService.findById(userWeixinDTO.getMemberId());

            if (Objects.nonNull(memberDTO)) {

                // 检查是否绑定,返回绑定的大屏账户
                UserTvDTO userTvDTO = this.userOperationService.checkBind(memberDTO);
                if (Objects.nonNull(userTvDTO)) {
                    // 绑定的大屏账号
                    platformAccount1 = userTvDTO.getPlatformAccount();
                }

            } else {

                // 数据异常,没有会员
                log.info("userWeixinDTO ==>> [{}]",userWeixinDTO);
                throw new EntityNotFoundException(MemberDTO.class,"code","member is null !!");

            }

            // 已关注
            if (result.equalsIgnoreCase(SUBSCRIBE)) {

                // 未绑定
                if (StringUtils.isBlank(platformAccount1)) {

                    // redis中的大小屏信息
                    String content = (String) this.redisUtils.get(RedisKeyUtil.genSeSuSubscribeKey(unionid));
                    JSONObject iptvUserInfo = JSONObject.parseObject(content);
                    // redis中的大小屏信息
                    log.info("saveUserInfo ==> redis content iptvUserInfo  ==> [{}]",iptvUserInfo);

                    // 大屏账户
                    String platformAccount = iptvUserInfo.getString("platformAccount");

                    if (StringUtils.isBlank(platformAccount)) {
                        log.warn("绑定失败,platformAccount is null ");
                        return ResultInfo.failure("绑定失败");
                    }


                    try {
                        String headimgurl = iptvUserInfo.get("headimgurl").toString();

                        log.info("headimgurl ==>> {}", headimgurl);

                        String nickname = iptvUserInfo.get("nickname").toString();
                        if (StringUtils.isNotBlank(nickname)) {
                            String nicknameDecode = URLDecoder.decode(nickname, "UTF-8");
                            String nicknameEncode = Base64Util.encode(nicknameDecode);
                            memberDTO.setNickname(nicknameEncode);
                        }

                        if (StringUtils.isNotBlank(headimgurl)) {

                            if(headimgurl.contains("https")||headimgurl.contains("http")) {
                                String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8");
//                                String imageEncode = Base64Util.encode(headimgurlDecode);
                                String image = RestTemplateClient.netImage(headimgurlDecode);
                                memberDTO.setAvatarUrl(StringUtils.isNotBlank(image) ? image:headimgurlDecode);
                            }

                        }

                    } catch (Exception e) {
                        log.info("头像解析失败!!!");
                        e.printStackTrace();
                    }

                    // 大小屏绑定,如果已经绑定了别的大屏,则不进行处理,返回已绑定的大屏信息
                    UserTvDTO userTvDTO = this.userOperationService.bind(memberDTO, platformAccount);
                    if (userTvDTO != null) {
                        platformAccount1 = userTvDTO.getPlatformAccount();
                    }

                }

            }

        }

        resultList.add(result);
        resultList.add(platformAccount1);

        // return ["subscribe","platform_account"]
        ResultInfo<Object> success = ResultInfo.success(resultList);

        log.info("saveUserInfo ==> ResultInfo  ==> [{}]",success);
        return success;
    }

    private String downloadWeixinImge(String headimgurl){
        try {
            if (StringUtils.isNotBlank(headimgurl)) {
                return RestTemplateClient.netImage(headimgurl);
            }
        } catch (Exception e) {
            log.info("头像解析失败!!!");
            e.printStackTrace();
        }

        return null;
    }


    /******************************************************* IPTV ************************************/

    @PostMapping(value = "/updateUserTv")
    @ApiOperation("修改大屏账号")
    @AnonymousAccess
    public ResultInfo updateUserTv(@Validated(value = {UpdateGroup.class}) @RequestBody UserTv resources) {
        log.info("UserOperationController ==> updateUserTv ==>> param  ==> [{}]",resources);

        UserTvDTO result = this.userOperationService.updateUserTv(resources);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/createTvUserAndMember")
    @ApiOperation("保存大屏账户同时创建会员信息")
    @AnonymousAccess
    public ResultInfo createTvUserAndMember(@Validated(value = {CreateGroup.class}) @RequestBody UserTv resources) {
        log.info("UserOperationController ==> createTvUserAndMember ==>> param  ==> [{}]",resources);
        String platformAccount = resources.getPlatformAccount();
        if (StringUtils.isBlank(platformAccount)) {
            log.error("保存大屏账户同时创建会员信息异常,参数错误,大屏账号不存在");
            return ResultInfo.failure("参数错误,大屏账号不存在");
        }
        UserTvDTO result = this.userOperationService.createTvUserAndMember(resources);
        return ResultInfo.success(result);
    }

    @RequestMapping(value = "/tvUnbind")
    @ApiOperation("大屏解绑")
    @AnonymousAccess
    public ResultInfo tvUnbind(@Validated(value = {UpdateGroup.class}) @RequestBody TvUnBindBean resources) {
        log.info("UserOperationController ==> unbind ==>> param  ==> [{}]",resources);

        String memberCode = resources.getMemberCode();
        log.info("大屏解绑,前端参数,需要解绑的会员code,memberCode ==>> {}", memberCode);
        if (StringUtils.isBlank(memberCode)) {
            throw new BadRequestException(GlobeExceptionMsg.MEMBER_CODE_IS_NULL);
        }

        String platformAccount = resources.getPlatformAccount();
        log.info("大屏解绑,前端参数,大屏账号,platformAccount ==>> {}", platformAccount);
        if (StringUtils.isBlank(platformAccount)) {
            throw new BadRequestException(GlobeExceptionMsg.IPTV_PLATFORM_ACCOUNT_IS_NULL);
        }

        boolean b = this.userOperationService.tvUnbind(resources);
        if (b) {
            return ResultInfo.success("解绑成功");
        } else return ResultInfo.failure("解绑失败");
    }

    @RequestMapping(value = "/changeMainAccount")
    @ApiOperation("大屏更换主账号")
    @AnonymousAccess
    public ResultInfo changeMainAccount(@Validated(value = {UpdateGroup.class}) @RequestBody BindBean resources) {
        log.info("大屏更换主账号,参数 [changeMainAccount# ==> {}]",resources);

//        Long memberId = resources.getMemberId();
        String memberCode = resources.getMemberCode();
     /*   if (StringUtils.isBlank(memberCode) && Objects.nonNull(memberId)) {
            memberCode = this.memberService.findCodeById(memberId);
        } else if (StringUtils.isNotBlank(memberCode) && Objects.isNull(memberId)) {
            MemberDTO memberDTO = this.memberService.findByCode(memberCode);
            memberCode = memberDTO.getCode();
        }*/

        if (StringUtils.isBlank(memberCode))
            return ResultInfo.failure("会员code不得为空");

        MemberDTO memberDTO = this.memberService.findByCode(memberCode);
        if (Objects.isNull(memberDTO.getId())) {
            log.error("大屏更换主账号失败,会员信息不存在, changeMainAccount# ==> {}", memberCode);
            return ResultInfo.failure("会员信息不存在");
        }

        String platformAccount = resources.getPlatformAccount();
        if (StringUtils.isBlank(platformAccount))
            throw new BadRequestException(GlobeExceptionMsg.IPTV_PLATFORM_ACCOUNT_IS_NULL);

        UserTv userTv = new UserTv();
        userTv.setMemberCode(memberCode);
        userTv.setPlatformAccount(platformAccount);
        boolean b = this.userOperationService.changeMainAccount(userTv);

        return ResultInfo.success(b);
    }

    @PostMapping(value = "/deleteAllCollection")
    @ApiOperation("删除全部收藏")
    @AnonymousAccess
    public ResultInfo deleteAllCollection(@RequestBody String content) {
        log.info("UserOperationController ==> deleteAllCollection ==> param  ==> [{}]",content);

        boolean result = this.userOperationService.deleteAllCollection(content);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/deleteCollection")
    @ApiOperation("删除收藏")
    @AnonymousAccess
    public ResultInfo deleteCollection(@RequestBody String content) {
        log.info("UserOperationController ==> deleteCollection ==> param  ==> [{}]",content);

        boolean result = this.userOperationService.deleteCollection(content);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/addCollection")
    @ApiOperation("添加收藏")
    @AnonymousAccess
    public ResultInfo addCollection(@RequestBody String content) {
        log.info("UserOperationController ==> addCollection ==>> param  ==> [{}]",content);
        if (StringUtils.isNotBlank(content)) {
            boolean result = this.userOperationService.addCollection(content);
            return ResultInfo.success(result);
        }
        return ResultInfo.success();
    }


}