SendMessageBiz.java 7.27 KB
package com.cjs.site.biz.pub;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
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.dao.pub.SendMessageLogDao;
import com.cjs.site.dao.user.account.ClientInfoDao;
import com.cjs.site.model.user.account.ClientInfo;
import com.cjs.site.model.user.account.UserInfo;
import com.cjs.site.util.lang.StringUtil;
import com.cjs.site.util.web.ActionUtil;
import com.jianzhou.sdk.BusinessService;

/**
 * 发送短信
 * 
 * @author tongyufu
 *
 */
@Service
public class SendMessageBiz {

    /**存储验证码的session key*/
    public static final String RANDOM_NUM = "randomNums";
    @Value("${${env}.message.username}")
    private String             account;
    @Value("${${env}.message.password}")
    private String             password;
    private Logger             log        = LogManager.getLogger(SendMessageBiz.class);
    @Value("${env}")
    private String             environment;
    @Autowired
    private SendMessageLogDao  sendMsgLogDao;
    @Autowired
    private ClientInfoDao      clientInfoDao;

    /**
     * 发送短信
     * @param phoneNumber
     * @param content
     * @return
     */
    public Map<String, Object> send(String phoneNumber, String content) {
        Map<String, Object> outmap = new HashMap<String, Object>();
        if (StringUtils.trim(phoneNumber).length() != 11) {
            log.info("非法的手机号,请检查后重新输入");
            outmap.put("resultCode", "0");
            outmap.put("resultMsg", "非法的手机号,请检查后重新输入");
            return outmap;
            //            return -3;
            //throw new ServiceException("非法的手机号,请检查后重新输入");
        }

        //        if (!commonBiz.checkOpenTime()) {
        //            log.info("当前非开户时间,不可发送短信");
        //            return -2;
        //            //throw new ServiceException("当前非开户时间,不可发送短信");
        //        }
        int count = sendMsgLogDao.queryCountByPhoneNumber(phoneNumber);
        if (count >= 50) {
            log.info("号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
            outmap.put("resultCode", "0");
            outmap.put("resultMsg", "号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
            return outmap;
            //            return -1;
            //throw new ServiceException("号码:" + phoneNumber + "今日发送短信数目已达到5条,无法继续发送");
        }

        // WebService方式
        int result = 0;
        if (StringUtils.equals(environment, "prod")) {
            BusinessService bs = new BusinessService();
            bs.setWebService(
                "http://www.jianzhou.sh.cn/JianzhouSMSWSServer/services/BusinessService");
            result = bs.sendBatchMessage(account, password, phoneNumber, content);
            log.info("短信【" + phoneNumber + "】发送结果:" + result + "\t发送内容:" + content);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phoneNumber", phoneNumber);
        params.put("message", content);
        params.put("sendResult", result);
        sendMsgLogDao.insertMegLog(params);
        outmap.put("resultCode", "1");
        outmap.put("result", result);
        return outmap;
    }

    /**
     * 发送短信验证码,支持同一页面发送多次时,验证码均可验证
     * 
     * @param phoneNumber
     * @param content
     * @param session
     * @return 发送结果,成功返回""
     */
    @SuppressWarnings("unchecked")
    public String sendValidCode(String phoneNo, String validCode, String content,
                                HttpSession session) {
        if (!StringUtil.isPhoneNo(phoneNo)) {
            return "手机号格式不正确";
        }
        Map<String, Object> result = this.send(phoneNo, content);
        if ("0".equals(result.get("resultCode"))) {
            return "短信发送失败";
        }
        List<Map<String, Object>> randomNums = (List<Map<String, Object>>) session
            .getAttribute("randomNums");
        if (randomNums == null) {
            randomNums = new ArrayList<Map<String, Object>>();
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tel", phoneNo);
        map.put("smsCode", validCode);
        randomNums.add(map);
        session.setAttribute("randomNums", randomNums);
        return "";
    }

    /**
     * 通用发短信方法
     * @param type 1.绑定充值卡短信    2.资金提现短信
     * @param session
     * @return
     */
    public String sendMsg(Integer type, HttpSession session) {
        String validCode = this.generateValidCode(6);
        UserInfo userInfo = ActionUtil.getUser();
        ClientInfo clientInfo = clientInfoDao.queryByClientId(userInfo.getUserId());
        String phoneNo = clientInfo.getMobileTel();
        String contentTop = "";
        switch (type) {
            case 1:
                contentTop = "您正在进行绑定充值银行卡操作,验证码:";
                break;
            case 2:
                contentTop = "您正在进行资金提现操作,验证码:";
            default:
                break;
        }
        String content = contentTop + validCode + ",请确保本人操作。如有疑问,详询400-969-0800。【赵涌牛】";
        String result = this.sendValidCode(phoneNo, validCode, content, session);
        if (StringUtils.isBlank(result)) {
            result = "验证码发送成功";
        }
        return result;
    }

    /**
     * 校对验证码
     * @param validCode
     * @param session
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean compareValidCode(String validCode, HttpSession session) {
        List<Map<String, Object>> randomNums = (List<Map<String, Object>>) session
            .getAttribute("randomNums");
        if (randomNums == null) {
            return false;
        }
        for (Map<String, Object> random : randomNums) {
            if (StringUtils.equals(validCode, (String) random.get("smsCode"))) {
                return true;
            }
        }
        return false;
    }

    /**
     * 生成随机数字验证码
     * 
     * @param length 验证码位数
     * @return 验证码
     */
    public String generateValidCode(Integer length) {
        if (!StringUtils.equals(environment, "prod")) {
            return "111111";
        }
        String validCode = (int) (Math.random() * 1000000) + "";
        if (validCode.length() < length) {
            validCode = StringUtil.lpad(length, validCode);
        }
        return validCode;
    }

    public String getReceipt(int taskId) {
        BusinessService bs = new BusinessService();
        bs.setWebService("http://www.jianzhou.sh.cn/JianzhouSMSWSServer/services/BusinessService");
        return bs.getReceipt(account, password, taskId);
    }

}