k94314517
2024-09-19 4df9fdf92a0a3461d53a08d797a63145af48e0c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.doumee.service.business.third;
 
import cn.emay.sdk.client.SmsSDKClient;
import cn.emay.sdk.core.dto.sms.common.ResultModel;
import cn.emay.sdk.core.dto.sms.request.SmsSingleRequest;
import cn.emay.sdk.core.dto.sms.response.SmsResponse;
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.model.InterfaceLog;
import com.doumee.service.business.InterfaceLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.util.Date;
 
@Service
public class EmayService {
    private static SmsSDKClient client ;
 
    public SmsSDKClient getClient() {
        return client;
    }
 
    public void setClient(SmsSDKClient client) {
        this.client = client;
    }
    @Autowired
    @Lazy
    private InterfaceLogService interfaceLogService  ;
    @Autowired
    @Lazy
    private SystemDictDataBiz  systemDictDataBiz;
 
    private   void saveInterfaceLog(String path,String name,String req ,String res) {
        if(interfaceLogService !=null){
            InterfaceLog hkMonitoryLogDO=new InterfaceLog();
            hkMonitoryLogDO.setType(0);
            hkMonitoryLogDO.setCreateDate(new Date());
            hkMonitoryLogDO.setIsdeleted(0);
            hkMonitoryLogDO.setRequest(req);
            hkMonitoryLogDO.setRepose(res);
            hkMonitoryLogDO.setName(name);
            hkMonitoryLogDO.setUrl(path);
            interfaceLogService.create(hkMonitoryLogDO);
        }
    }
    private  static String  ip;
    private static int port;
    private static String appSecret;
    private static String appKey;
    @PostConstruct//启动初始化
    public int initClient(){
        ip = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_IP).getCode();
        port = Integer.parseInt(systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_PORT).getCode());
        appKey = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_APPKEY).getCode();
        appSecret = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_APPSECRET).getCode();
        return 0;
    }
 
    public   boolean sendSingleSms(String mobile,String content)  {
        try {
            client = new SmsSDKClient(ip,port,appKey,appSecret);
 
            if(client == null){
                return false;
            }
            String customSmsId = "1";
            String extendedCode = "01";
 
            SmsSingleRequest request = new SmsSingleRequest(mobile, content, customSmsId, extendedCode, "");
            ResultModel<SmsResponse> result = client.sendSingleSms(request);
            saveInterfaceLog(ip+"/"+port,"【短信】发送", JSONObject.toJSONString(result),JSONObject.toJSONString(result));
            if (result.getCode().equals("SUCCESS")) {
                System.out.println("请求成功");
                SmsResponse response = result.getResult();
                System.out.println("sendSingleSms:" + response.toString());
                return  true;
            } else {
                System.out.println("请求失败");
                return false;
            }
        } catch (Exception e) {
 
        }
        return false;
    }
 
 
}