AliyunSMS.java 10.7 KB
Newer Older
邓实川 committed
1
package cn.timer.api.utils.aliyun;
yuquan.zhu committed
2

邓实川 committed
3
import java.util.ArrayList;
4
import java.util.Date;
邓实川 committed
5
import java.util.List;
6

7
import org.springframework.beans.factory.annotation.Value;
8
import org.springframework.stereotype.Component;
9
import org.springframework.transaction.annotation.Transactional;
10 11
import org.springframework.web.bind.annotation.RequestParam;

yuquan.zhu committed
12 13 14 15 16 17 18 19 20
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
21
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
22
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
yuquan.zhu committed
23

24 25
import cn.timer.api.bean.qyzx.QyzxEmpLogin;
import cn.timer.api.bean.qyzx.businessService.QyzxRemainingQuantity;
26 27
import cn.timer.api.bean.qyzx.businessService.QyzxScene;
import cn.timer.api.bean.qyzx.businessService.QyzxUseRecord;
28
import cn.timer.api.config.enums.PinType;
yuquan.zhu committed
29 30 31 32 33 34 35 36
import cn.timer.api.dto.qyzx.EntRegisterDto;

/**
 * aliyun阿里云短信服务
 * 
 * @author dsc
 *
 */
37
@Transactional
38
@Component
yuquan.zhu committed
39
public class AliyunSMS {
邓实川 committed
40

41
	@Value("${config-8timer.Aliyun.PROJECT_NAME}")
42
	public String PROJECT_NAME;
yuquan.zhu committed
43

44
	@Value("${config-8timer.Aliyun.REGION_ID}")
45
	public String REGION_ID;
46 47

	@Value("${config-8timer.Aliyun.ACCESSKEY_ID}")
48
	public String ACCESSKEY_ID;
49 50

	@Value("${config-8timer.Aliyun.SECRET}")
51 52
	public String SECRET;

53 54 55 56 57 58 59 60
	final static String CODE_NAME = "TemplateCode";// 短信模板代码参数名
	final static String TEMPLATE_PARAM = "TemplateParam";// 自定义参数
	final static String DOMAIN = "dysmsapi.aliyuncs.com";
	final static String VERSION = "2017-05-25";
	final static String ACTION_SEND = "SendSms"; // 发短信
	final static String ACTION_QUERY = "QuerySendDetails"; // 短信查询
	final static String PAGE_SIZE = "1";
	final static String CURRENT_PAGE = "1";
61

yuquan.zhu committed
62 63
	/**
	 * 
64
	 * @param code
yuquan.zhu committed
65 66 67 68
	 * @param templateCode 1."身份验证验证码" 2."登录确认验证码" 3."登录异常验证码" 4."用户注册验证码"
	 *                     5."修改密码验证码" 6."信息变更验证码"
	 */
	@SuppressWarnings("deprecation")
邓实川 committed
69
	public List<Object> authCode(EntRegisterDto entRegisterDto,
70
			@RequestParam(required = false, defaultValue = "1") Integer templateCode, Integer code) {
71

72 73
		String phone = entRegisterDto.getPhone();

邓实川 committed
74 75 76 77
		QyzxRemainingQuantity quantity = null;
		Integer remainder = null;
		String orderNo = null;
		Integer orgCode = null;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
//		if (templateCode != 4) {
//			orgCode = QyzxEmpLogin.builder().build()
//					.selectOne(new LambdaQueryWrapper<QyzxEmpLogin>().eq(QyzxEmpLogin::getPhone, phone)).getOrgId();
//			quantity = QyzxRemainingQuantity.builder().build()
//					.selectOne(new LambdaQueryWrapper<QyzxRemainingQuantity>()
//							.eq(QyzxRemainingQuantity::getOrgCode, orgCode).eq(QyzxRemainingQuantity::getPmid, 1)
//							.gt(QyzxRemainingQuantity::getRemainder, 0));
//			remainder = quantity.getRemainder();
//			Date date = quantity.getExpireDate();
//			orderNo = quantity.getOrderNo();
//
//			if (remainder == null || remainder <= 0) {
//				System.err.println("企业 " + orgCode + " :套餐余额不足,请充钱");
//				return null;
//			}
//			if (date != null && date.getTime() <= new Date().getTime()) {
//				System.err.println("企业 " + orgCode + " :短信套餐已过期");
//				return null;
//			}
//		}

//		QyzxUseRecord qyzxUseRecord = new QyzxUseRecord();
100

101
		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
yuquan.zhu committed
102 103 104 105
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
106 107 108 109
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_SEND);
		request.putQueryParameter("RegionId", REGION_ID);
110
		request.putQueryParameter("PhoneNumbers", phone);
111
		request.putQueryParameter("SignName", PROJECT_NAME);
yuquan.zhu committed
112 113
		switch (templateCode) {
		case 1:
114
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
115
//			qyzxUseRecord.setScene(QyzxScene.MSG_AUTHENTICATION.getMessage());
yuquan.zhu committed
116 117
			break;
		case 2:
118
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_CONFIRMATION.getCode());
119
//			qyzxUseRecord.setScene(QyzxScene.MSG_LOGIN_CONFIRM.getMessage());
yuquan.zhu committed
120 121
			break;
		case 3:
122
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_ABNORMAL.getCode());
123
//			qyzxUseRecord.setScene(QyzxScene.MSG_LOGIN_CONFIRM.getMessage());
yuquan.zhu committed
124 125
			break;
		case 4:
126
			request.putQueryParameter(CODE_NAME, PinType.REGISTER.getCode());
127
//			qyzxUseRecord.setScene(QyzxScene.MSG_REGISTER.getMessage());
yuquan.zhu committed
128 129
			break;
		case 5:
130
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_PASSWORD.getCode());
131
//			qyzxUseRecord.setScene(QyzxScene.MSG_CHANGE_PWD.getMessage());
yuquan.zhu committed
132 133
			break;
		case 6:
134
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_INFORMATION.getCode());
135
//			qyzxUseRecord.setScene(QyzxScene.MSG_CHANGE_IMP.getMessage());
yuquan.zhu committed
136 137
			break;
		default:
138
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
139
//			qyzxUseRecord.setScene(QyzxScene.MSG_AUTHENTICATION.getMessage());
yuquan.zhu committed
140 141
		}
		// 【8小时人事管家】验证码934169,您正在尝试修改登录密码,请妥善保管账户信息。
142
		request.putQueryParameter(TEMPLATE_PARAM, "{\"code\":\"" + code + "\"}");
yuquan.zhu committed
143
		CommonResponse response = null;
144
		String a = null;
yuquan.zhu committed
145 146
		try {
			response = client.getCommonResponse(request);
147
			a = response.getData();
148
//			System.err.println("response:"+a); // 短信服务响应内容
yuquan.zhu committed
149 150 151 152 153
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
邓实川 committed
154
		Integer id = null;
155
		if (a != null && a.contains("OK")) {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
//			if (templateCode != 4) {
//				qyzxUseRecord.setOriginalTotalNum(remainder); // 原数量
//				qyzxUseRecord.setNowTotalNum(--remainder);
//				qyzxUseRecord.setUserPhone(phone);
//				qyzxUseRecord.setOrderNo(orderNo);
//				qyzxUseRecord.setUseNum(1);
//				qyzxUseRecord.setPmid(quantity.getPmid());
//				qyzxUseRecord.setCreateUser("System");
//				qyzxUseRecord.setOrgCode(orgCode);
//				qyzxUseRecord.insert();
//
//				id = qyzxUseRecord.getId();
//
//				quantity.setRemainder(remainder);
//				quantity.update(new LambdaUpdateWrapper<QyzxRemainingQuantity>()
//						.eq(QyzxRemainingQuantity::getOrgCode, orgCode).eq(QyzxRemainingQuantity::getOrderNo, orderNo));
//			}
邓实川 committed
173 174 175 176 177 178

			List<Object> list = new ArrayList<Object>();

			list.add(id);
			list.add(JSONObject.parseObject(response.getData()));
			return list;// 代表请求成功
179 180 181
		}

		return null;
yuquan.zhu committed
182 183 184 185 186 187 188
	}

	/**
	 * 短信内容查询(短信发送日期,支持查询最近30天的记录)
	 */
	@SuppressWarnings("deprecation")
	public String QuerySendDetails(String phone, String date, String bizId) {
189
		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
yuquan.zhu committed
190 191 192 193
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
194 195 196 197
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_QUERY);
		request.putQueryParameter("RegionId", REGION_ID);
yuquan.zhu committed
198 199
		request.putQueryParameter("PhoneNumber", phone);
		request.putQueryParameter("SendDate", date);
200 201
		request.putQueryParameter("PageSize", PAGE_SIZE);
		request.putQueryParameter("CurrentPage", CURRENT_PAGE);
yuquan.zhu committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		request.putQueryParameter("BizId", bizId);
		String a = null;
		try {
			CommonResponse response = client.getCommonResponse(request);

			a = response.getData();

		} catch (Exception e) {
			e.printStackTrace();
		}
		if (a != null && a.contains("OK")) {
			return a;// 代表请求成功
		}
		return "获取失败";
	}

	/**
	 * 提醒功能
	 * 
	 * @param name   用户名字
	 * @param htname 要提醒的东西的名字
	 * @param time   提醒的时间
	 * @param phone
225
	 * @return
yuquan.zhu committed
226 227
	 */
	@SuppressWarnings("deprecation")
228
	public String remind(String name, String htname, String time, String phone) {
229

230 231 232 233
		Integer orgCode = QyzxEmpLogin.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpLogin>().eq(QyzxEmpLogin::getPhone, phone)).getOrgId();
		QyzxRemainingQuantity quantity = QyzxRemainingQuantity.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxRemainingQuantity>()
234
						.eq(QyzxRemainingQuantity::getOrgCode, orgCode).eq(QyzxRemainingQuantity::getPmid, 1)
235
						.orderByAsc(QyzxRemainingQuantity::getExpireDate));
236 237 238 239 240 241

		Integer remainder = quantity.getRemainder();
		Date date = quantity.getExpireDate();
		String orderNo = quantity.getOrderNo();

		if (quantity == null || remainder == null || remainder <= 0) {
ilal committed
242
			Logoutput("企业 " + orgCode + " :套餐余额不足,请充钱");
243
			return "请先充值购买短信套餐!";
244
		}
245
		if (date != null && date.getTime() <= new Date().getTime()) {
ilal committed
246
			Logoutput("企业 " + orgCode + " :短信套餐已过期");
247 248 249
			return "套餐已过期";
		}

250
		DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESSKEY_ID, SECRET);
yuquan.zhu committed
251 252 253 254
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
255 256 257 258
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_SEND);
		request.putQueryParameter("RegionId", REGION_ID);
yuquan.zhu committed
259
		request.putQueryParameter("PhoneNumbers", phone);
260 261 262
		request.putQueryParameter("SignName", PROJECT_NAME);
		request.putQueryParameter(CODE_NAME, PinType.REMIND.getCode());
		request.putQueryParameter(TEMPLATE_PARAM,
yuquan.zhu committed
263
				"{\"name\":\"" + name + "\", \"htname\":\"" + htname + "\",\"time\":\"" + time + "\"}");
264

265
		String a = null;
yuquan.zhu committed
266 267
		try {
			CommonResponse response = client.getCommonResponse(request);
268 269 270

			a = response.getData();

yuquan.zhu committed
271 272 273 274 275
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
276 277 278 279 280 281 282 283 284 285

		if (a != null && a.contains("OK")) {
			QyzxUseRecord qyzxUseRecord = new QyzxUseRecord();
			qyzxUseRecord.setOriginalTotalNum(remainder); // 原数量
			qyzxUseRecord.setNowTotalNum(--remainder);
			qyzxUseRecord.setUserPhone(phone);
			qyzxUseRecord.setOrderNo(orderNo);
			qyzxUseRecord.setScene(QyzxScene.MSG_REMIND.getMessage());
			qyzxUseRecord.setUseNum(1);
			qyzxUseRecord.setPmid(quantity.getPmid());
邓实川 committed
286
			qyzxUseRecord.setOrgCode(orgCode);
287 288 289 290 291 292 293 294 295
			qyzxUseRecord.insert();

			quantity.setRemainder(remainder);
			quantity.update(new LambdaUpdateWrapper<QyzxRemainingQuantity>()
					.eq(QyzxRemainingQuantity::getOrgCode, orgCode).eq(QyzxRemainingQuantity::getOrderNo, orderNo));

			return a;// 代表请求成功
		}
		return "发送失败";
yuquan.zhu committed
296 297
	}

ilal committed
298 299 300 301 302 303 304 305 306 307 308 309 310
	@Value("${config-8timer.environmental-science}")
	public String environmental_science;
	
	public void Logoutput(String science) {
		
		if(!("pro").equals(environmental_science)) {
			
			System.out.println(science);
		}else {
			System.out.println("");
		}
		
	}
yuquan.zhu committed
311
}