package com.mortals.xhx.busiz;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PhoneUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.xhx.base.framework.config.InterceptorConfig;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.busiz.wechat.WechatBaseReq;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.code.OneThingRespCodeEnum;
import com.mortals.xhx.feign.rsp.ApiResp;
import com.mortals.xhx.module.member.service.MemberService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信认证
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@CommonsLog
@RequestMapping("/zwfw/wechatinter/")
public class WxWechatApiController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private InterceptorConfig config;
    @Autowired
    private UploadService uploadService;


    /**
     * 微信注册认证
     *
     * @param req
     * @return
     */
    @PostMapping("base/checkCard")
    public ApiResp<String> wxCheckCard(WechatBaseReq req) {
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(OneThingRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(OneThingRespCodeEnum.SUCCESS.getValue());
        try {

            //校验部分参数
            if (!PhoneUtil.isPhone(req.getTel())) {
                rsp.setMsg("电话号码不正确！");
                rsp.setCode(OneThingRespCodeEnum.FAILED.getValue());
                return rsp;
            }

            if (!IdcardUtil.isValidCard(req.getIdcard())) {
                rsp.setMsg("身份证号码不正确！");
                rsp.setCode(OneThingRespCodeEnum.FAILED.getValue());
                return rsp;
            }

            memberService.checkMember(req);


        } catch (Exception e) {
            log.error("微信注册认证失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return rsp;
        }

        log.info("响应【微信注册认证】【响应体】-->  " + JSONObject.toJSONString(rsp));
        return rsp;
    }


}
