liukangdong
2024-09-27 597d26b944e1d8bd5772d1f9fac5ec92ba7d592a
server/service/src/main/java/com/doumee/biz/zbom/impl/ZbomCRMServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.biz.zbom.ZbomCRMService;
import com.doumee.biz.zbom.model.crm.*;
import com.doumee.biz.zbom.model.crm.response.CRMBaseResponse;
@@ -17,14 +18,22 @@
import com.doumee.dao.business.CustomerLogMapper;
import com.doumee.dao.business.model.CrmInterfaceLog;
import com.doumee.dao.business.model.CustomerLog;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.business.model.Users;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
@@ -43,6 +52,8 @@
    private CrmInterfaceLogMapper crmInterfaceLogMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
@@ -244,24 +255,66 @@
     * @return
     */
    @Override
    public    String getCrmGoUrl(String userName,Integer type){
    public    String getCrmGoUrl(String userName,Integer type,String iamId){
        try {
            // 创建StringBuffer对象用来操作字符串
            int _t = (int)(System.currentTimeMillis());
            String pageKey ="mp";//客户列表页面码
            String fileUrl = systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_CRM_API_URL_MP).getCode();
            if(Constants.equalsInteger(type,Constants.ONE)){
                pageKey = "mpAddIntention";//新增意向页面码
                fileUrl = systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_CRM_API_URL_MP_ADDINTENTION).getCode();
            }
            String  appkey =  (String) redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_AUTH_API_KEY);
            String  url =   redisTemplate.opsForValue().get(Constants.RedisKeys.ZBOM_CRM_API_URL)
                    +CRMConstants.IntegerUrl.GO_CRM_AUTH_URL
                    + "?timestamp="+_t
                    +"&agent_phone_number=" + URLEncoder.encode(userName, "UTF-8")
                    + "&bindKey="+pageKey+"&sign="+ DigestUtils.md5Hex( _t + appkey);;
                    + "?bindKey="+pageKey
                    + "&tick=" + this.getTick(iamId, fileUrl);
            return url;
        }catch (Exception e){
            e.printStackTrace();
        }
       throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"获取跳转地址失败哦!");
    }
    public String getTick(String iamId, String fileUrL) throws UnsupportedEncodingException {
        Map<String,Object> map = new HashMap<>();
        //用户IAM主键
        map.put("ssouserid",iamId);
        //售前小程序appid
        map.put("appid",systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_IAM_BUSINESS_APPID).getCode());
        //跳转地址
        map.put("rediUrl",fileUrL);
        //时间戳
        map.put("timestamp",System.currentTimeMillis()/1000+"");
        //C端小程序appid
        map.put("sourceappid",systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_IAM_CUSTOMER_APPID).getCode());
        System.out.println(JSONObject.toJSONString(map));
        //获取tick
        String tick = ZbomCRMServiceImpl.encrypt(
                systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_CRM_API_URL_PRIVATE_KEY).getCode()
                ,JSONObject.toJSONString(map)
                ,systemDictDataBiz.queryByCode(Constants.ZBOM,Constants.ZBOM_CRM_API_URL_INIT_VECTOR).getCode());
        String tickEncoder = URLEncoder.encode(tick,"UTF-8");
        return tickEncoder;
    }
    public static String encrypt(String key, String value,String initVector) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64Utils.encodeToString(encrypted);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}