//package com.rrtx.services.api;
//
//import cn.hutool.core.util.RandomUtil;
//import com.auth0.jwt.interfaces.Claim;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.rrtx.fap.core.entity.R;
//import com.rrtx.fap.starter.genid.service.IdService;
//import com.rrtx.fap.starter.translate.util.ConvertUtil;
//import com.rrtx.services.api.login.service.ILoginService;
//import com.rrtx.services.common.annotation.Log;
//import com.rrtx.services.common.controller.BaseController;
//import com.rrtx.services.common.entity.User;
//import com.rrtx.services.common.http.HttpPostUtil;
//import com.rrtx.services.common.redis.IRedisService;
//import com.rrtx.services.db.entity.TSysDitcDetail;
//import com.rrtx.services.db.entity.WfkCstInf;
//import com.rrtx.services.db.entity.WfkPersonInf;
//import com.rrtx.services.db.service.IPubPersonInfService;
//import com.rrtx.services.db.service.ITSysDitcDetailService;
//import com.rrtx.services.db.service.IWfkCstInfService;
//import com.rrtx.services.db.service.IWfkPersonInfService;
//import lombok.extern.slf4j.Slf4j;
//import net.sf.json.JSONObject;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.validation.constraints.NotBlank;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//
///**
// * 核心业务控制类
// *  登录、验证码等
// */
//@Slf4j
//@RestController
//@RequestMapping("api")
//public class WxLoginController extends BaseController {
//    @Autowired
//    ILoginService loginService; // 登录相关SQL
//    @Autowired
//    IPubPersonInfService pubPersonInfService;
//    @Autowired
//    IWfkPersonInfService iWfkPersonInfService;
//
//    @Autowired
//    private IdService idService; // 获取序列号
//
//    @Autowired
//    IRedisService superRedisService;
//    @Autowired
//    ITSysDitcDetailService itSysDitcDetailService;
//    @Autowired
//    private IWfkCstInfService iWfkCstInfService;//咨询表
//    /**
//     * 微信登陆授权
//     * @param code
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Log("微信登陆授权")
//    @RequestMapping("getWxOpenid")
//    public R getWxPayOpenid( String code,
//                             String token,
//                            HttpServletRequest request) throws Exception {
//        //在微信的appId
////
//        LambdaQueryWrapper<TSysDitcDetail> sea = new LambdaQueryWrapper<>();
//        sea.eq(TSysDitcDetail::getCode,"APPID");
//        List<TSysDitcDetail> ditcDetails = this.itSysDitcDetailService.list(sea);
////        String appId = "wxa1f93b31a28d069f";//ConvertUtil.translate("WX_PARAMETER", "APPID");
//
//        LambdaQueryWrapper<TSysDitcDetail> dit = new LambdaQueryWrapper<>();
//        dit.eq(TSysDitcDetail::getCode,"APPSECRET");
//        List<TSysDitcDetail> Details = this.itSysDitcDetailService.list(dit);
//        String appId="";
//        String appSecret="";
//        if(ditcDetails.size()>0){
//            appId = ditcDetails.get(0).getValue();//ConvertUtil.translate("WX_PARAMETER", "APPID");
//        }else{
//            return R.code(100,"系统异常！");
//        }
//        if(Details.size()>0){
//            appSecret = Details.get(0).getValue();
//        }else {
//            return R.code(100,"系统异常！");
//        }
//        log.info("appId==="+appId+"appSecret==="+appSecret+"code==="+code);
//        //在微信的secret
//        //String appSecret = "9d601305140210f9c3458feaff5b1b63";//ConvertUtil.translate("WX_PARAMETER", "APPSECRET");
//        //微信通用请求路径
//        String openUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
//        //配置参数
//        StringBuffer paramStr = new StringBuffer();
//        paramStr.append("appid=");
//        paramStr.append(appId);
//        paramStr.append("&secret=");
//        paramStr.append(appSecret);
//        paramStr.append("&code=");
//        paramStr.append(code);
//        paramStr.append("&grant_type=authorization_code");
//        log.info("paramStr==="+paramStr);
//        try {
//            //发送请求
//            String jsonObj = HttpPostUtil.sendPost(openUrl, paramStr.toString());
//            JSONObject js = JSONObject.fromObject(jsonObj);
//            //获取openId
//            String openId = js.getString("openid");//用户唯一标识，请注意，在未关注公众号时，用户访问公众号的网页，也会产生一个用户和公众号唯一的OpenID
//            log.info("openId==="+openId);
//            LambdaQueryWrapper<WfkPersonInf> sear = new LambdaQueryWrapper<WfkPersonInf>();
//            sear.eq(WfkPersonInf::getWxOpenid,openId);
//            List<WfkPersonInf> personInf = this.iWfkPersonInfService.list(sear);
//            User sessionUser = new User();
//            Map maps = new HashMap();
//            token = RandomUtil.randomString(50);
//            sessionUser.setToken("USERLOGIN"+token);
//            if(personInf.size()>0){
//                // 注册到session
//                sessionUser.setId(personInf.get(0).getId());
//                maps.put("openId", openId);
//                maps.put("headPath",personInf.get(0).getHeadPath());//
//                maps.put("wxName",personInf.get(0).getWxName());//
//                maps.put("status",personInf.get(0).getStatus());
//                maps.put("sessionUser",sessionUser);
//                //手机号
//                String phone = personInf.get(0).getPhone();
//                String mobile = ConvertUtil.translate("SMS", "SMS_MOBILE");
//                log.info("手机号："+personInf.get(0).getPhone()+"用户名："+personInf.get(0).getName());
//                if("".equals(personInf.get(0).getPhone()) && "".equals(personInf.get(0).getName())){
//                    maps.put("postType","00");
//                }else{
//                    if(!mobile.equals(phone)){
//                        sessionUser.setIsBandDev("0");//没有资格回复
//                    }else{
//                        sessionUser.setIsBandDev("1");//有资格回复
//                    }
//                    maps.put("postType","01");
//                }
//                super.login(sessionUser);
//            }else {
//                //微信通用请求路径
//                String openUrl2 = "https://api.weixin.qq.com/sns/userinfo";
//                //配置参数
//                StringBuffer paramStr2 = new StringBuffer();
//                paramStr2.append("access_token=");
//                paramStr2.append(js.getString("access_token"));
//                paramStr2.append("&openid=");
//                paramStr2.append(openId);
//                paramStr2.append("&lang=zh_CN");
//                log.info("微信通用请求==="+openUrl2+paramStr2);
//                //发送请求
//                String jsonObj2 = HttpPostUtil.sendPost(openUrl2, paramStr2.toString());
//                JSONObject js2 = JSONObject.fromObject(jsonObj2);
//                log.info("请求返回==="+js2);
//                String nickName = js2.getString("nickname");
//                log.info("微信名称==="+nickName);
//                String headImgurl = js2.getString("headimgurl");
//                log.info("微信名称==="+nickName+"头像名称==="+headImgurl);
//                String id = this.idService.genId()+"";
//                WfkPersonInf inf = new WfkPersonInf();
//                inf.setId(id);
//                inf.setHeadPath(headImgurl);
//                inf.setWxName(nickName);
//                inf.setWxOpenid(openId);
////                inf.setWxUuid(js2.getString("unionid"));
//                inf.setStatus("00");
//                this.iWfkPersonInfService.insertSelective(inf);
//                log.info("完成插入");
//                sessionUser.setId(id);
//                maps.put("openId", openId);
//                maps.put("headPath",headImgurl);//
//                maps.put("wxName",nickName);//
//                maps.put("status","00");
//                maps.put("sessionUser",sessionUser);
//                maps.put("postType","00");
//            }
//
//            return R.code(200, "授权成功").data(maps);
//        } catch (Exception e) {
//            return R.code(100,"授权失败");
//        }
//    }
//    /**
//     * 微信登陆授权
//     * @param code
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Log("微信登陆授权")
//    @RequestMapping("csOpenid")
//    public R csOpenid( String code,
//                             String token,
//                             HttpServletRequest request) throws Exception {
//        //在微信的appId
////
//        try {
////            LambdaQueryWrapper<WfkPersonInf> sear = new LambdaQueryWrapper<WfkPersonInf>();
////            sear.eq(WfkPersonInf::getWxOpenid,"33");
////            List<WfkPersonInf> personInf = this.iWfkPersonInfService.list(sear);
////            User sessionUser = new User();
//            Map maps = new HashMap();
////            token = RandomUtil.randomString(50);
////            sessionUser.setToken("USERLOGIN"+token);
////            if(personInf.size()>0){
////                // 注册到session
////                sessionUser.setId(personInf.get(0).getId());
//////                maps.put("channel",list.get(0).get("channel"));
////                maps.put("headPath",personInf.get(0).getHeadPath());//
////                maps.put("wxName",personInf.get(0).getWxName());//
////                maps.put("status",personInf.get(0).getStatus());
////                maps.put("sessionUser",sessionUser);
////                LambdaQueryWrapper<WfkCstInf> sears = new LambdaQueryWrapper<>();
////                sears.eq(WfkCstInf::getPersonId,personInf.get(0).getId());
////                List<WfkCstInf> infList = this.iWfkCstInfService.list(sears);
////                if(infList.size()>0){
////                    sessionUser.setWxUserId(infList.get(0).getId());
////                    sessionUser.setPhone(infList.get(0).getPhone());
////                    sessionUser.setStatus(infList.get(0).getStatus());
////                    maps.put("postType","01");
////                    if(!"00".equals(infList.get(0).getStatus())){
////                        return R.code(100,"用户状态异常！");
////                    }
////                }else{
////                    maps.put("postType","00");
////                }
////                super.login(sessionUser);
////            }else {
////
////            }
//
//            return R.code(200, "授权成功").data(maps);
//        } catch (Exception e) {
//            return R.code(100,"授权失败");
//        }
//    }
//    /**
//     * 新建用户基本信息
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @Log("新建用户基本信息")
//    @RequestMapping("editBaseInf")
//    public R editBaseInf(@NotBlank(message = "{required}") String id,
//                         @NotBlank(message = "{required}") String name,
//                         @NotBlank(message = "{required}") String phone,
//                         String token,
//                            HttpServletRequest request) throws Exception {
//        WfkPersonInf personInf = new WfkPersonInf();
//        personInf.setId(id);
//        personInf.setName(name);
//        personInf.setPhone(phone);
//        this.iWfkPersonInfService.updateSelectiveById(personInf);
//        User sessionUser = new User();
//        String mobile = ConvertUtil.translate("SMS", "SMS_MOBILE");
//        if(!mobile.equals(phone)){
//            sessionUser.setIsBandDev("0");//无权限
//        }else{
//            sessionUser.setIsBandDev("1");//有权限
//        }
//        Map maps = new HashMap();
//        token = RandomUtil.randomString(50);
//        sessionUser.setToken("USERLOGIN"+token);
//        sessionUser.setId(id);
//        sessionUser.setPhone(phone);
//        maps.put("sessionUser",sessionUser);
//        super.login(sessionUser);
//
//        return R.code(200,"新建用户信息成功").data(maps);
//    }
//
//    @Log("微信登陆授权")
//    @RequestMapping("ceshi")
//    public R ceshi(String code,
//                   String token){
//        //获取openId
//        LambdaQueryWrapper<WfkPersonInf> sear = new LambdaQueryWrapper<WfkPersonInf>();
//        sear.eq(WfkPersonInf::getWxOpenid,"o5QiI1DFn2NAc4Tr8iWan22Re2rY");
//        List<WfkPersonInf> personInf = this.iWfkPersonInfService.list(sear);
//        User sessionUser = new User();
//        Map maps = new HashMap();
//        String mobile = ConvertUtil.translate("SMS", "SMS_MOBILE");
//        if(!mobile.equals(personInf.get(0).getPhone())){
//            sessionUser.setIsBandDev("0");//无权限
//        }else{
//            sessionUser.setIsBandDev("1");//有权限
//        }
//
//        token = RandomUtil.randomString(50);
//        sessionUser.setToken("USERLOGIN"+token);
//            // 注册到session
//            sessionUser.setId(personInf.get(0).getId());
//        maps.put("sessionUser",sessionUser);
//            super.login(sessionUser);
//        return R.code(200,"成功").data(maps);
//    }
//    @Log("校验客户")
//    @RequestMapping("checkCustomerCs")
//    public R checkCustomerCs(@NotBlank(message = "{required}") String jwtToken, String token,HttpServletRequest request
//    ) throws Exception {
//        LambdaQueryWrapper<WfkCstInf> cstInf = new LambdaQueryWrapper<WfkCstInf>();
////        cstInf.eq(WfkCstInf::getCstId,resultMap.get(""));
//        //暂时写死
//        cstInf.eq(WfkCstInf::getCstId,"802100126419050");
//        List<WfkCstInf> wfkCstInfList = this.iWfkCstInfService.list(cstInf);
//        if(wfkCstInfList.size()>0){
//            User session = new User();
//            token = "CHECK"+RandomUtil.randomString(50);
//            session.setId(wfkCstInfList.get(0).getId());
//            session.setCstId(wfkCstInfList.get(0).getCstId());//客户号
//            session.setToken(token);
//            super.login(session);
//            return R.ok().code(200,"").data(session);
//        }else{
//            return R.ok().code(100,"你暂时无权访问此功能");
//        }
//
//    }
//    @Log("校验客户")
//    @RequestMapping("checkCustomer")
//    public R checkCustomer(@NotBlank(message = "{required}") String jwtToken, String token,HttpServletRequest request
//    ) throws Exception {
//        List<WfkCstInf> wfkCstInfList = check(jwtToken);
//        if(wfkCstInfList.size()>0){
//            User session = new User();
//            token = "CHECK"+RandomUtil.randomString(50);
//            session.setId(wfkCstInfList.get(0).getId());
//            session.setCstId(wfkCstInfList.get(0).getCstId());//客户号
//            session.setToken(token);
//            super.login(session);
//            return R.ok().code(200,"").data(session);
//        }else{
//            return R.ok().code(100,"你暂时无权访问此功能");
//        }
//
//    }
//    /**
//     * 校验客户
//     * @param jwtToken
//     * @return
//     * @throws Exception
//     */
//    public List<WfkCstInf> check(String jwtToken)throws Exception {
//        Map<String, Claim> resultMap = null;
////        try{
////            resultMap =   JwtToken.verifyMobileToken(jwtToken);
////            log.info("resultMap:"+resultMap);
////        }catch (Exception ex){
////            throw new BusinessException("JWT解析失败："+ex.getMessage());
////        }
//        String cstId ="";
////        if(resultMap != null && resultMap.get("cstNo") !=null ){
////            Claim cstNoClaim = resultMap.get("cstNo");
////            cstId = cstNoClaim.asString();
////        }else{
////            throw new BusinessException("JWT解析失败:cstNo为null");
////        }
//        LambdaQueryWrapper<WfkCstInf> cstInf = new LambdaQueryWrapper<WfkCstInf>();
////        cstInf.eq(WfkCstInf::getCstId,resultMap.get(""));
//        //暂时写死
//        cstInf.eq(WfkCstInf::getCstId,cstId);
//        List<WfkCstInf> cstInfs = this.iWfkCstInfService.list(cstInf);
//        return cstInfs;
//    }
//
//    public static void main(String[] args) {
//            String s = "{\"openid\":\"o3S6501UGNhO2ppJbOIDru-XaFeQ\",\"nickname\":\"缘\",\"sex\":1,\"language\":\"zh_CN\",\"city\":\"\",\"province\":\"\",\"country\":\"乍得\",\"headimgurl\":\"https:\\/\\/thirdwx.qlogo.cn\\/mmopen\\/vi_32\\/fxrdfHVd8zaiaiao8QS5qzwU78KWbGGicRxtYVfv3hGOYvIkratrwe1D3zVyEPomv9daYr0kV5SBy0RcNovTA46mw\\/132\",\"privilege\":[]}";
//        JSONObject js2 = JSONObject.fromObject(s);
//            String ss = js2.getString("headimgurl");
//        System.out.println(ss);
//
//    }
//
//}
//
