ZhaoonlineBiz.java 2.36 KB
package com.cjs.site.biz.pub;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cjs.site.biz.ServiceException;
import com.cjs.site.util.lang.StringUtil;
import com.cjs.site.util.net.RestClient;

/**
 * 赵涌在线相关
 * 
 * @author tongyufu
 *
 */
@Service
public class ZhaoonlineBiz {

    private Logger     log = LogManager.getLogger();
    @Autowired
    private RestClient restClient;
    @Value("${env}")
    private String     environment;

    /**
     * 检查赵涌在线账号是否存在
     * 
     * @param userId 赵涌在线用户ID
     * @return
     *  1. 客户存在:
        {"result":"01","message":"ok","data":true}<br/>
        2. 客户不存在:
        {"result":"01","message":"ok","data":false}<br/>
        3. identity 参数格式不正确
        {"result":"02","message":"请求参数格式错误","data":null}
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> checkZhaoonlineId(String userId) {
        if (!"prod".equals(environment)) {
            return new HashMap<String, Object>();
        }
        if (StringUtil.isBlank(userId)) {
            throw new ServiceException("请输入赵涌在线账号");
        }
        if (!NumberUtils.isDigits(userId)) {
            throw new ServiceException("赵涌在线账号应为7位数字用户ID或11位手机号码");
        }
        if (userId.length() < 7 || userId.length() > 11) {
            throw new ServiceException("赵涌在线账号应为7位数字用户ID或11位手机号码");
        }

        String uri = "https://appservices.zhaoonline.com";
        uri += "/api/user/" + userId + "/exist";
        Map<String, Object> response = restClient.getForObject(uri, Map.class);
        log.info("检查赵涌在线账号是否存在[" + userId + "]:" + response);
        if ("02".equals(response.get("result"))) {
            throw new ServiceException((String) response.get("message"));
        }
        if (!(Boolean) response.get("data")) {
            throw new ServiceException("赵涌在线账号不存在");
        }
        return response;
    }

}