AliyunSMS.java 10.3 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 65 66 67 68
	/**
	 * 
	 * @param phone
	 * @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;
邓实川 committed
78
		if (templateCode != 4) {
邓实川 committed
79 80 81 82 83 84 85 86 87 88
			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();

89
			if (remainder == null || remainder <= 0) {
邓实川 committed
90 91 92 93 94 95 96
				System.err.println("企业 " + orgCode + " :套餐余额不足,请充钱");
				return null;
			}
			if (date != null && date.getTime() <= new Date().getTime()) {
				System.err.println("企业 " + orgCode + " :短信套餐已过期");
				return null;
			}
97 98 99 100
		}

		QyzxUseRecord qyzxUseRecord = new QyzxUseRecord();

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 114

		switch (templateCode) {
		case 1:
115
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
116
			qyzxUseRecord.setScene(QyzxScene.MSG_AUTHENTICATION.getMessage());
yuquan.zhu committed
117 118
			break;
		case 2:
119
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_CONFIRMATION.getCode());
120
			qyzxUseRecord.setScene(QyzxScene.MSG_LOGIN_CONFIRM.getMessage());
yuquan.zhu committed
121 122
			break;
		case 3:
123
			request.putQueryParameter(CODE_NAME, PinType.LOGIN_ABNORMAL.getCode());
124
			qyzxUseRecord.setScene(QyzxScene.MSG_LOGIN_CONFIRM.getMessage());
yuquan.zhu committed
125 126
			break;
		case 4:
127
			request.putQueryParameter(CODE_NAME, PinType.REGISTER.getCode());
128
			qyzxUseRecord.setScene(QyzxScene.MSG_REGISTER.getMessage());
yuquan.zhu committed
129 130
			break;
		case 5:
131
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_PASSWORD.getCode());
132
			qyzxUseRecord.setScene(QyzxScene.MSG_CHANGE_PWD.getMessage());
yuquan.zhu committed
133 134
			break;
		case 6:
135
			request.putQueryParameter(CODE_NAME, PinType.CHANGE_INFORMATION.getCode());
136
			qyzxUseRecord.setScene(QyzxScene.MSG_CHANGE_IMP.getMessage());
yuquan.zhu committed
137 138
			break;
		default:
139
			request.putQueryParameter(CODE_NAME, PinType.AUTHENTICATION.getCode());
140
			qyzxUseRecord.setScene(QyzxScene.MSG_AUTHENTICATION.getMessage());
yuquan.zhu committed
141 142
		}
		// 【8小时人事管家】验证码934169,您正在尝试修改登录密码,请妥善保管账户信息。
143
		request.putQueryParameter(TEMPLATE_PARAM, "{\"code\":\"" + code + "\"}");
yuquan.zhu committed
144
		CommonResponse response = null;
145
		String a = null;
yuquan.zhu committed
146 147
		try {
			response = client.getCommonResponse(request);
148 149

			a = response.getData();
yuquan.zhu committed
150 151 152 153 154
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
邓实川 committed
155
		Integer id = null;
156
		if (a != null && a.contains("OK")) {
邓实川 committed
157 158 159 160 161 162 163 164
			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");
邓实川 committed
165
				qyzxUseRecord.setOrgCode(orgCode);
邓实川 committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
				qyzxUseRecord.insert();

				id = qyzxUseRecord.getId();

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

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

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

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

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

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
195 196 197 198
		request.setDomain(DOMAIN);
		request.setVersion(VERSION);
		request.setAction(ACTION_QUERY);
		request.putQueryParameter("RegionId", REGION_ID);
yuquan.zhu committed
199 200
		request.putQueryParameter("PhoneNumber", phone);
		request.putQueryParameter("SendDate", date);
201 202
		request.putQueryParameter("PageSize", PAGE_SIZE);
		request.putQueryParameter("CurrentPage", CURRENT_PAGE);
yuquan.zhu committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		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
226
	 * @return
yuquan.zhu committed
227 228
	 */
	@SuppressWarnings("deprecation")
229
	public String remind(String name, String htname, String time, String phone) {
230

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

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

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

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

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

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

			a = response.getData();

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

		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
287
			qyzxUseRecord.setOrgCode(orgCode);
288 289 290 291 292 293 294 295 296
			qyzxUseRecord.insert();

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

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

}