package org.jeecg.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.bean.HttpConf;
import org.jeecg.modules.bean.LoginReq;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.service.AppletLoginService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class AppletLoginServiceImpl implements AppletLoginService {
    //微信小程序appid(遗产里的景德镇)
    private static final String mpAppId = "wx5589eaf7bccec24f";
    //微信小程序appSecret(遗产里的景德镇)
    private static final String mpAppSecret = "1c473c3163cd08ea848101c55d982e22";

    @Resource
    private IHanHaiMemberService memberService;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private HttpConf httpConf;




    /**
     * 微信小程序登录接口
     * @param loginReq
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    public Result<Object> appletLogin(LoginReq loginReq) {
        Result<Object> result = new Result<>();
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isBlank(loginReq.getCode())) {
            throw new JeecgBootException("小程序code为空");
        }
        String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mpAppId + "&secret=" + mpAppSecret+ "&js_code=" + loginReq.getCode() + "&grant_type=authorization_code";
        //使用工具类
        JSONObject json_test = httpConf.getJSONObject(loginUrl);
        String wxOpenid = json_test.getString("openid");
        String sessionKey = json_test.getString("session_key");
        if (StringUtils.isBlank(wxOpenid)) {
            throw new JeecgBootException("未获取到openid");
        }

        HanHaiMember member = memberService.lambdaQuery().eq(HanHaiMember::getAppletOpenid,wxOpenid).one();
        if (member == null) {
            //如果user等于null说明该用户第一次登录，数据库没有该用户信息。
            loginReq.setOpenid(wxOpenid);
            loginReq.setSession_key(sessionKey);
            member = new HanHaiMember();
//            member.setSesssionKey(sessionKey);
            member.setAppletOpenid(wxOpenid);
            member.setNickName(loginReq.getNickName());
            member.setHeadImage(loginReq.getHeadimgurl());
            // 生成token返回给小程序端
            String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid);
            memberService.save(member);
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
            // 设置超时时间
            redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100);
            map.put("userInfo", member);
            map.put("token", token);

            result.setCode(200);
            result.setResult(map);
            return result;
        } else {
//            member.setHeadImage(loginReq.getHeadimgurl());
//            member.setSessionKey(sessionKey);
//            memberService.saveOrUpdate(member);
            // 生成token返回给小程序端
            String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid);
            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
            // 设置超时时间
            redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100);
            map.put("userInfo", member);
            map.put("token", token);
            //用户id做im账号
            result.setResult(map);
            result.setCode(200);
//            result.setMessage(member.getNickName()+"已于"+member.getCreateTime()+"注册成功！请勿重复注册！");
//            // 未绑定手机号，跳转授权绑定手机号
//            if (StringUtils.isBlank(member.getPhone())) {
//                result.setMessage("该微信用户尚未绑定手机号，请授权手机进行绑定");
//                result.setCode(905);
//                result.setResult(map);
//                return result;
//            }
        }
        return result;
    }


//    /**
//     * 微信小程序登录绑定手机号码接口
//     * @param loginReq
//     * @return
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public Result<Object> bindPhone(LoginReq loginReq) {
//        Result<Object> result = new Result<>();
//        if (StringUtils.isEmpty(loginReq.getEncryptedData())) {
//            result.setCode(500);
//            result.setMessage("请授权手机号");
//            return result;
//        }
//        String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mpAppId + "&secret=" + mpAppSecret+ "&js_code=" + loginReq.getCode() + "&grant_type=authorization_code";
//        CloseableHttpClient client = null;
//        CloseableHttpResponse response = null;
//        try {
//            // 创建http GET请求
//            HttpGet httpGet = new HttpGet(loginUrl);
//            client = HttpClients.createDefault();
//            // 执行请求
//            response = client.execute(httpGet);
//            HttpEntity entity = response.getEntity();//得到返回数据
//            String resultStr = EntityUtils.toString(entity);
//            JSONObject json_test = JSONObject.parseObject(resultStr);
//            String wxOpenid = json_test.getString("openid");
//            if (StringUtils.isEmpty(wxOpenid)) {
//                return result.error500("未获取到openid");
//            }
//            String sessionKey = json_test.getString("session_key");
//            // 保存sessionKey,用于解析手机号码
//            loginReq.setOpenid(wxOpenid);
//            loginReq.setSession_key(sessionKey);
//            String userPhone = getPhoneNumber(loginReq);
//            HanHaiMember one = memberService.lambdaQuery().eq(HanHaiMember::getAppletOpenid, wxOpenid).one();
//            if(one!=null){
//                return result.OK("请不要重复注册");
//            }
//            HanHaiMember member = new HanHaiMember();
//            member.setSessionKey(sessionKey);
//            member.setAppletOpenid(wxOpenid);
//            member.setNickName(loginReq.getNickName());
//            member.setHeadImage(loginReq.getHeadimgurl());
//            member.setPhone(userPhone);
//
//
//            //判断用户是否存在
//            HanHaiMember boolOne = memberService.lambdaQuery().eq(HanHaiMember::getAccount, loginReq.geta()).one();
//            boolean bool = false;
//            if(one != null){
//                bool = true;
//            }
//
//            if(!bool){
//                member.setId(boolOne.getId());
//                memberService.updateById(member);
//            }else{
//                memberService.save(member);
//            }
//
//
//
//            return result.OK("注册成功",member);
//        }catch(Exception e) {
//            e.printStackTrace();
//            return Result.error("操作失败，请联系系统管理员！");
//        }
//
//    }
//
//
//
//    /**
//     * 解析电话号码
//     * @param loginReq
//     * @return
//     */
//    public String getPhoneNumber(LoginReq loginReq) {
//        String encryptedData = loginReq.getEncryptedData();
//        String session_key = loginReq.getSession_key();
//        String iv = loginReq.getIv();
//        // 被加密的数据
//        byte[] dataByte = Base64.decodeBase64(encryptedData);
//        // 加密秘钥
//        byte[] keyByte = Base64.decodeBase64(session_key);
//        // 偏移量
//        byte[] ivByte = Base64.decodeBase64(iv);
//        try {
//            // 如果密钥不足16位，那么就补足.  这个if 中的内容很重要
//            int base = 16;
//            if (keyByte.length % base != 0) {
//                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
//                byte[] temp = new byte[groups * base];
//                Arrays.fill(temp, (byte) 0);
//                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
//                keyByte = temp;
//            }
//            // 初始化
//            Security.addProvider(new BouncyCastleProvider());
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
//            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
//            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
//            parameters.init(new IvParameterSpec(ivByte));
//            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
//            byte[] resultByte = cipher.doFinal(dataByte);
//            if (null != resultByte && resultByte.length > 0) {
//                String result = new String(resultByte, "UTF-8");
//                JSONObject userPhoneObj = JSONObject.parseObject(result);
//                Map<String, Object> userMap = new HashMap<>();
//                for (Map.Entry<String, Object> entry : userPhoneObj.entrySet()) {
//                    userMap.put(entry.getKey(), entry.getValue());
//                }
//                // 获取phoneNumber 带区号
//                String userPhone = (String) userMap.get("phoneNumber");
//                return userPhone;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
