UserOperationController.java 14.3 KB
package com.topdraw.business.process.rest;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.annotation.AnonymousAccess;
import com.topdraw.business.module.member.profile.domain.MemberProfile;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.domain.UserTv;
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.UserWeixinService;
import com.topdraw.business.module.user.weixin.service.dto.UserWeixinDTO;
import com.topdraw.business.process.domian.TempIptvUser;
import com.topdraw.business.process.domian.UnbindGroup;
import com.topdraw.business.process.domian.weixin.BindBean;
import com.topdraw.business.process.domian.weixin.SubscribeBean;
import com.topdraw.business.process.domian.weixin.SubscribeBeanEvent;
import com.topdraw.business.process.domian.weixin.WeiXinUserBean;
import com.topdraw.business.process.service.UserOperationService;
import com.topdraw.common.ResultInfo;
import com.topdraw.config.RedisKeyUtil;
import com.topdraw.exception.BadRequestException;
import com.topdraw.util.Base64Util;
import com.topdraw.util.JSONUtil;
import com.topdraw.utils.RedisUtils;
import com.topdraw.utils.StringUtils;
import com.topdraw.weixin.util.WeChatConstants;
import com.topdraw.weixin.util.WeixinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api("账户处理")
@RestController
@RequestMapping(value = "/ucEngine/api/userOperation")
@Slf4j
public class UserOperationController {

    @Autowired
    private UserOperationService userTvOperationService;
    @Autowired
    private UserWeixinService userWeixinService;
    @Autowired
    private UserTvService userTvService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtils redisUtils;

    @PostMapping(value = "/createUserAndCreateMember")
    @ApiOperation("新增大屏账户同时创建会员信息")
    @AnonymousAccess
    public ResultInfo createUserAndCreateMember(@Validated @RequestBody TempIptvUser resources) {
        UserTv userTv = new UserTv();
        BeanUtils.copyProperties(resources,userTv);
        boolean result = this.userTvOperationService.createMemberByUserTv(userTv);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/createWeixinUserAndCreateMember")
    @ApiOperation("新增小屏账户同时创建会员信息")
    @AnonymousAccess
    public ResultInfo createWeixinUserAndCreateMember(@Validated @RequestBody UserWeixin resources) {

        log.info("Param ==> resource ==> [{}]",resources);

        String appId = resources.getAppid();
        if (StringUtils.isBlank(appId))
            throw new NullPointerException("appId is null !");

        String openId = resources.getOpenid();
        if (StringUtils.isBlank(openId))
            throw new NullPointerException("openId is null !");

        String unionId = resources.getUnionid();
        if (StringUtils.isBlank(unionId))
            throw new NullPointerException("unionId is null !");

        UserWeixinDTO result = this.userTvOperationService.createWeixinUserAndCreateMember(resources);

        return ResultInfo.success(result);
    }

    @GetMapping(value = "/findBindByPlatformAccount/{platformAccount}")
    @AnonymousAccess
    public ResultInfo findBindByPlatformAccount(@PathVariable("platformAccount") String platformAccount) {
        log.info("resources :[{}]",platformAccount);
        List<MemberDTO> result = this.userTvOperationService.findBindByPlatformAccount(platformAccount);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/sendQrCodeMessage")
    @ApiOperation("带参二维码")
    @AnonymousAccess
    public ResultInfo sendQrCodeMessage(@RequestBody String content) {
        log.info("resources :[{}]",content);
        boolean result = this.userTvOperationService.sendQrCodeMessage(content);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/deleteAllCollection")
    @ApiOperation("删除全部收藏")
    @AnonymousAccess
    public ResultInfo deleteAllCollection(@RequestBody String content) {
        log.info("resources :[{}]",content);
        boolean result = this.userTvOperationService.deleteAllCollection(content);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/deleteCollection")
    @ApiOperation("删除收藏")
    @AnonymousAccess
    public ResultInfo deleteCollection(@RequestBody String content) {
        log.info("resources :[{}]",content);
        boolean result = this.userTvOperationService.deleteCollection(content);
        return ResultInfo.success(result);
    }

    @PostMapping(value = "/addCollection")
    @ApiOperation("添加收藏")
    @AnonymousAccess
    public ResultInfo addCollection(@RequestBody String content) {
        log.info("resources :[{}]",content);
        boolean result = this.userTvOperationService.addCollection(content);
        return ResultInfo.success(result);
    }

    @RequestMapping(value = "/unbind")
    @ApiOperation("大屏用户解绑")
    @AnonymousAccess
    public ResultInfo unbind(@Validated @RequestBody TempIptvUser resources) {
        UserTv userTv = new UserTv();
        BeanUtils.copyProperties(resources,userTv);
        String unionid = resources.getUnionid();
//        Long memberId = resources.getMemberId();
        String memberCode1 = resources.getMemberCode();
        if (Objects.nonNull(memberCode1)) {

            MemberDTO memberDTO = this.memberService.getByCode(memberCode1);
            String memberCode = memberDTO.getCode();
            if (StringUtils.isNotBlank(memberCode)) {
                userTv.setMemberCode(memberCode);
                this.userTvOperationService.unbind(userTv);
            }

        } else {

            this.userTvOperationService.unbindByUnionId(userTv,unionid);

        }

        return ResultInfo.success();
    }

    @RequestMapping(value = "/changeMainAccount")
    @ApiOperation("大屏更换主账号")
    @AnonymousAccess
    public ResultInfo changeMainAccount(@Validated @RequestBody TempIptvUser resources) {
        UserTv userTv = new UserTv();
        BeanUtils.copyProperties(resources,userTv);
        Long memberId = resources.getMemberId();
        String memberCode1 = resources.getMemberCode();
        if (Objects.nonNull(memberCode1)) {

            MemberDTO memberDTO = this.memberService.getByCode(memberCode1);

            String memberCode = memberDTO.getCode();
            if (StringUtils.isNotBlank(memberCode)) {
                userTv.setMemberCode(memberCode);
                this.userTvOperationService.changeMainAccount(userTv);
            }

        } else {
            String unionid = resources.getUnionid();
            this.userTvOperationService.changeMainAccountByUnionId(userTv,unionid);
        }

        return ResultInfo.success("update success");
    }

    @PostMapping("/serviceLogin")
    @ApiOperation("微信服务号(H5)登录")
    @AnonymousAccess
    public ResultInfo serviceLogin(@Validated @RequestBody WeiXinUserBean resources) {
        Object o = this.userTvOperationService.serviceLogin(resources);
        return ResultInfo.success(o);
    }

    @PostMapping("/appletLogin")
    @ApiOperation("微信小程序登录")
    @AnonymousAccess
    public ResultInfo appletLogin(@Validated @RequestBody WeiXinUserBean resources) {

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

    @PostMapping("/appletBind")
    @ApiOperation("微信小程序绑定大屏")
    @AnonymousAccess
    public ResultInfo appletBind(@Validated @RequestBody BindBean resources) {
        String unionId = resources.getUnionid();
        if (StringUtils.isBlank(unionId))
            Assert.state(StrUtil.isNotBlank(unionId), "跨屏绑定,请先进行授权");

        String platformAccount = resources.getPlatformAccount();
        if (Objects.isNull(platformAccount))
            Assert.state(StrUtil.isNotBlank(platformAccount), "大屏账户不得为空");

        UserTvDTO userTvDTO = this.userTvService.findByPlatformAccount(platformAccount);
        resources.setPlatformUserId(userTvDTO.getId());
        resources.setPlatformAccount(platformAccount);

        boolean result = this.userTvOperationService.appletBind(resources);
        return ResultInfo.success(result);
    }

    @PostMapping("/subscribe")
    @ApiOperation("微信公众号关注")
    @AnonymousAccess
    public ResultInfo subscribe(@Validated @RequestBody SubscribeBeanEvent data) throws IOException {
        String content = data.getContent();
        SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(content, SubscribeBean.class);
        this.parseSubscribe(subscribeBean);
        boolean result = this.userTvOperationService.subscribe(subscribeBean);
        return ResultInfo.success(result);
    }


    /**
     *
     * @param subscribeBean
     * @throws IOException
     */
    private void parseSubscribe(SubscribeBean subscribeBean) throws IOException {
        if (Objects.nonNull(subscribeBean)) {

            String appId = subscribeBean.getAppId();
            // appId不得为空
            if (StringUtils.isBlank(appId))
                throw new BadRequestException("appId 不存在!");

            // openId
            String openId = subscribeBean.getOpenId();
            if (StringUtils.isBlank(openId))
                throw new BadRequestException("openId 不存在!");

            // unionId
            String unionId = subscribeBean.getUnionid();
            if (StringUtils.isBlank(unionId))
                throw new BadRequestException("unionId 不存在!");

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

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

            // 大屏账户信息
            JSONObject iptvUserInfo = null;
            // 缓存的大屏信息,使用unionid即可
            String content = (String) this.redisUtils.get(RedisKeyUtil.genSeSuSubscribeKey(unionId));
            if (StringUtils.isNotBlank(content)) {
                // 大屏信息
               iptvUserInfo = JSONObject.parseObject(content);
            } else {

                String eventKey = subscribeBean.getEventKey();

                if (StringUtils.isNotBlank(eventKey)) {
                    // 用户扫描带参二维码关注。发消息
                    // 去除固定前缀,获取二维码参数
                    eventKey = eventKey.substring(8);
                    iptvUserInfo = JSONObject.parseObject(eventKey);
                }

            }

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

                String headimgurl = iptvUserInfo.get("headimgurl").toString();
                String nickname = iptvUserInfo.get("nickname").toString();
                if (org.apache.commons.lang3.StringUtils.isNotBlank(nickname)) {
                    String nicknameDecode = URLDecoder.decode(nickname, "UTF-8");
                    String nicknameEncode = Base64Util.encode(nicknameDecode);
                    subscribeBean.setNickname(nicknameEncode);
                }

                if (org.apache.commons.lang3.StringUtils.isNotBlank(headimgurl)) {
                    String headimgurlDecode = URLDecoder.decode(headimgurl, "UTF-8");
                    subscribeBean.setHeadimgurl(headimgurlDecode);
                }

            }

        }
    }

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

        SubscribeBean subscribeBean = JSONUtil.parseMsg2Object(content, SubscribeBean.class);
        boolean result = this.userTvOperationService.unsubscribe(subscribeBean);
        return ResultInfo.success(result);
    }

    /**
     * @param data
     * @description 通过大屏关注的订阅号,因为订阅号不支持带参二维码,
     * 所以需要先跳到H5(带大屏参数),
     * 再做服务号授权(订阅号只能按钮触发获取信息),
     * 再将服务号信息和大屏参数缓存下来(该接口的功能)
     * 然后跳转到订阅号关注页
     * 关注后回调处理时,根据unionid进行相关逻辑
     * @author Hongyan Wang
     * @date 2021/8/24 4:54 下午
     */
    @PostMapping(value = "/saveUserInfo")
    @ApiOperation("保存大屏侧信息")
    @AnonymousAccess
    public ResultInfo saveUserInfo(@RequestBody String data) {

        Assert.notNull(data, "用户数据不可为空");

        JSONObject json = JSONObject.parseObject(data);

        String unionid = json.getString("unionid");
        Assert.state(StrUtil.isNotBlank(unionid), "unionid不可为空");

        String s = this.userTvOperationService.saveUserInfo(data);

        return ResultInfo.success(s);
    }

    @PostMapping(value = "/saveUserWeixinPhone")
    @ApiOperation("保存用户手机号信息")
    @AnonymousAccess
    public ResultInfo saveUserWeixinPhone(@RequestBody WeiXinUserBean resources) {
        MemberProfile s = this.userTvOperationService.saveUserWeixinPhone(resources);
        return ResultInfo.success(s);
    }



}