接口的調用
總結:首先在pom文件中加入阿里公司的jar包,利用springmvc框架寫一個方法,在所需要的地方調用即可。詳細的步驟請看下面的說明。
1.在項目pom文件中加入阿里的jar包
2、在配置文件中加入公司開的戶,和鏈接!短信發送一般都是需要收取費用的。注意,配置文件一定要加載到框架的容器中,方便在代碼中取值。
3、控制層的代碼!!!!!!
/**
* 短信的控制層
*title:
*@author taotk
*@2016年8月12日
*@company
*/
@Controller
@RequestMapping("/sms")
public class MessageController extends BaseController {
private static Logger log = LoggerFactory.getLogger(MessageController.class.getName());
@Autowired
private MessageService smsService;
@RequestMapping(value = "/code/{type}/{phone}")
public MappingJacksonJsonView register(@PathVariable String type, @PathVariable String phone,
HttpServletRequest request) throws ApiException {
log.info("======================sms.code=================");
MappingJacksonJsonView mv = new PinjiaMappingJacksonJsonView();
String sms_model = null;
switch (ESMSType.valueof(Integer.parseInt(type))) {
case Active:
sms_model = Resources.getString("appSMSActiveModelId");
break;
case Change:
sms_model = Resources.getString("appSMSChangeModelId");
break;
case Id:
sms_model = Resources.getString("appSMSIdModelId");
break;
case Register:
sms_model = Resources.getString("appSMSRegisterModelId");
break;
case Pass:
sms_model = Resources.getString("appSMSPassModelId");
default:
break;
}
String dataType = Resources.getString("appSMSDataFormat");
ESMSDataType dataFormat = ESMSDataType.Json;
if (!"json".equals(dataType))
dataFormat = ESMSDataType.Xml;
smsService.getCode(ESMSType.valueof(Integer.parseInt(type)), sms_model, phone, null, dataFormat);
return mv;
}
4、service層的代碼
@Service
public class MessageService {
// 常量參數
final String appKey = Resources.getString("appSMSId");
final String secret = Resources.getString("appSMSKey");
final String url = Resources.getString("appSMSUrl");
/**
* 獲得手機驗證驗證碼
*
* @param code_type
* 獲取短信驗證碼類別
* @param sms_model
* 短信模板
* @param sms_phone
* 需要發送驗證碼的手機
* @param sms_type
* 短信類型,默認為文字短信
* @param data_Type
* 返回數據類型
* @return {@link String}
* @throws ApiException
*/
public String getCode(ESMSType code_type, String sms_model, String sms_phone, String sms_type,
ESMSDataType data_Type) throws ApiException {
TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret, data_Type.getTitle());
AlibabaAliqinFcSmsNumSendRequest smsRequest = new AlibabaAliqinFcSmsNumSendRequest();
smsRequest.setSmsType(StringUtils.isEmpty(sms_type) ? "normal" : sms_type);
smsRequest.setSmsFreeSignName(code_type.getTitle());
smsRequest.setRecNum(sms_phone);
String message =":-,尊敬的用戶您好,拼家系統為您注冊了一個賬戶。賬戶:"+sms_phone+",密碼:"+sms_phone+",您可以登錄拼家網,關注您的裝修流程";
smsRequest.setSmsParamString("{\"code\":\"" + message + "\",\"product\":\"" + "【齊家網】"+ "\"}");
smsRequest.setSmsTemplateCode(sms_model);
AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(smsRequest);
return null;
}
}
5、其他的是一些工具類,和枚舉的類。
(1)
public class Resources {
private static Logger log = LoggerFactory.getLogger(Resources.class);
/** 國際化資源 */
public static ResourceBundle resourceBundle;
public static ResourceBundle wf;
public static ResourceBundle messageBundle;
static {
resourceBundle = ResourceBundle.getBundle("application");
messageBundle = ResourceBundle.getBundle("message");
}
public static void close() {
resourceBundle = null;
}
public static String myString() {
return resourceBundle.toString();
}
/**
* 從資源文件中返回字符串 我們不希望程序崩潰,所以如果沒有找到Key,就直接返回Key
*/
public static String getWebMessage(String key) {
try {
if (!messageBundle.containsKey(key)) {
return "";
}
return messageBundle.getString(key);
} catch (Exception e) {
log.error(e.toString());
e.printStackTrace();
return "";
}
}
public static String getWorkflow(String bizType, String key) {
wf = ResourceBundle.getBundle(bizType + "_wf");
return wf.getString(key);
}
public static String getErrorMessage(String key) {
try {
if (!messageBundle.containsKey(key)) {
return "";
}
return messageBundle.getString(key);
} catch (Exception e) {
log.error(e.toString());
e.printStackTrace();
return "";
}
}
public static String getString(String key) {
try {
if (!resourceBundle.containsKey(key)) {
return "";
}
return resourceBundle.getString(key);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
return "";
}
}
public static int getConfigAsInt(String key) {
return Integer.valueOf(getString(key));
}
/**
* 從資源文件中返回字符串 我們不希望程序崩潰,所以如果沒有找到Key,就直接返回Key
*/
public static String getString(String key, Object[] args) {
try {
return MessageFormat.format(getString(key), args);
} catch (Exception e) {
log.error(e.toString());
e.printStackTrace();
return "";
}
}
}
(2)
public class PinjiaMappingJacksonJsonView extends MappingJacksonJsonView {
protected Object filterModel(Map<String, Object> model) {
Map<?, ?> result = (Map<?, ?>) super.filterModel(model);
if (result.size() == 1) {
return result.values().iterator().next();
} else {
return result;
}
}
}
(3)
/**
* 文字短信驗證碼類型枚舉類
*title:
*@author taotk
*@2016年8月12日
*@company www.51pinjia.com
*/
public enum ESMSType {
Register("注冊驗證"), Login("登錄驗證"), Change("變更驗證"), Id("身份驗證"), Active("活動驗證"), Pass("變更驗證");
private String title;
private ESMSType(String title) {
this.title = title;
}
public static ESMSType valueof(int index) {
return ESMSType.values()[index];
}
public String getTitle() {
return title;
}
}
(4)
/**
* 文字短信驗證碼數據類型類型枚舉類
*title:
*@author taotk
*@2016年8月12日
*@company www.51pinjia.com
*/
public enum ESMSDataType {
Json("json"), Xml("xml");
private String title;
private ESMSDataType(String title) {
this.title = title;
}
public static ESMSDataType valueof(int index) {
return ESMSDataType.values()[index];
}
public String getTitle() {
return title;
}
}