Commit a7267b07 by 孙亚楠

添加阿里短信服务

parent 23c88bfa
......@@ -11,6 +11,7 @@ class ActionAPI extends APIBase {
this.deliverSve = system.getObject("service.deliver.deliverSve");
this.invoicecontentSve = system.getObject("service.common.invoicecontentSve");
this.smsinfoSve = system.getObject("service.common.smsinfoSve");
}
/**
* 接口跳转
......@@ -116,11 +117,15 @@ class ActionAPI extends APIBase {
// 阿里短信服务
case "sendSms": //发送短信
opResult = await this.aliSmsSve.sendSms(action_body);
opResult = await this.smsinfoSve.sendSms(action_body);
break;
case "querySendDetails": //查询发送记录
opResult = await this.aliSmsSve.querySendDetails(action_body);
break;
opResult = await this.smsinfoSve.querySendDetails(action_body);
break;
case "querySmsInfos": //条件查询
opResult = await this.smsinfoSve.querySmsInfos(action_body);
break;
default:
opResult = system.getResult(null, "action_type参数错误");
......
const system = require("../../../system");
const Dao = require("../../dao.base");
class SmsinfoDao extends Dao {
constructor() {
super(Dao.getModelName(SmsinfoDao));
}
}
module.exports = SmsinfoDao;
\ No newline at end of file
const system = require("../../../system");
const settings = require("../../../../config/settings");
const uiconfig = system.getUiConfig2(settings.appKey);
module.exports = (db, DataTypes) => {
return db.define("smsinfo", {
regionId: { type: DataTypes.STRING, field:'region_id', comment:'' },
phoneNumbers: { type: DataTypes.STRING, field:'phone_numbers', comment:'电话' },
templateCode: { type: DataTypes.STRING, field:'template_code', comment:'模板code' },
templateParam: { type: DataTypes.STRING, field:'template_param', comment:'模板参数' },
signName: { type: DataTypes.STRING, field:'sign_name', comment:'签名' },
result: { type: DataTypes.STRING, field:'result', comment:'返回结果' },
success: { type: DataTypes.STRING, field:'success', comment:'是否成功 1 成功 0 失败' },
}, {
paranoid: true, //假的删除
underscored: true,
version: true,
freezeTableName: true,
//freezeTableName: true,
// define the table's name
tableName: 'p_sms_info',
validate: {},
indexes: [
// Create a unique index on email
// {
// unique: true,
// fields: ['email']
// },
//
// // Creates a gin index on data with the jsonb_path_ops operator
// {
// fields: ['data'],
// using: 'gin',
// operator: 'jsonb_path_ops'
// },
//
// // By default index name will be [table]_[fields]
// // Creates a multi column partial index
// {
// name: 'public_by_author',
// fields: ['author', 'status'],
// where: {
// status: 'public'
// }
// },
//
// // A BTREE index with a ordered field
// {
// name: 'title_index',
// method: 'BTREE',
// fields: ['author', {attribute: 'title', collate: 'en_US', order: 'DESC', length: 5}]
// }
]
});
}
\ No newline at end of file
const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
const Core = require('@alicloud/pop-core');
class AliSmsService extends ServiceBase {
const ServiceBase = require("../../sve.base");
class SmsinfoService extends ServiceBase {
constructor() {
super("common", ServiceBase.getDaoName(BusinessscopeService));
super("common", ServiceBase.getDaoName(SmsinfoService));
let aliSmsConfig = settings.aliSmsConfig();
this.aliSmsConfigration = aliSmsConfig;
this.client = new Core({
accessKeyId: aliSmsConfig.accessKeyId,
accessKeySecret: aliSmsConfig.accessKeySecret,
......@@ -21,27 +22,40 @@ class AliSmsService extends ServiceBase {
*/
async sendSms(params) {
let key = this.trim(params.templateName);
let loginTemplateConfig = settings[key];
let loginTemplateConfig = this.aliSmsConfigration.template[key];
if (!loginTemplateConfig) {
return system.getResult(-1, `参数错误 模板不存在`);
}
if(!params.phoneNumbers){
return system.getResult(null,`参数错误 手机号不能为空`);
}
if(!params.hasOwnProperty("templateParam")){
return system.getResult(null,`参数错误 模板参数不能为空`);
}
let data = {
"RegionId": "cn-hangzhou",
"PhoneNumbers":params.phoneNumbers,
"TemplateCode":loginTemplateConfig.code,
"TemplateParam": this.trim(params.templateParam)
"TemplateParam": `{\"code\":\"${params.code}\"}`,
"SignName":this.aliSmsConfigration.signName
}
try {
//创建记录
let _smsInfo = await this.dao.create({
regionId:data.RegionId,
phoneNumbers:data.PhoneNumbers,
templateCode:data.TemplateCode,
templateParam:data.TemplateParam,
signName:data.SignName
});
let res = await this.client.request('SendSms', data, {
method: 'POST'
});
_smsInfo.result = JSON.stringify(res);
if(res.Code=="OK"){
_smsInfo.success = 1;
}else{
_smsInfo.success =0;
}
await _smsInfo.save();
return system.getResult(res);
} catch (error) {
console.log(error);
......@@ -77,5 +91,53 @@ class AliSmsService extends ServiceBase {
return system.getResult(null,`系统错误 错误信息 ${error}`);
}
}
/**
* 条件查询所有的记录
* @param {*} params
*/
async querySmsInfos(params){
let where = {};
if(params.regionId){
where.regionId = this.trim(params.regionId);
}
if(params.phoneNumbers){
where.phoneNumbers = this.trim(params.phoneNumbers);
}
if(params.templateCode){
where.templateCode = this.trim(params.templateCode);
}
if(params.templateParam){
where.templateParam = this.trim(params.templateParam);
}
if(params.signName){
where.signName = this.trim(params.signName);
}
if(params.hasOwnProperty("success")){
where.success = this.trim(params.success);
}
var currentPage = Number(params.currentPage || 1);
var pageSize = Number(params.pageSize || 10);
var orderby = [
["id", 'desc']
];
var attributes = ["id", "regionId", "phoneNumbers", "templateCode", "templateParam","signName","result","success", "created_at"];
try {
var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
return system.getResult(page);
} catch (error) {
console.log(error);
return system.getResult(null,`系统错误 错误信息 ${error}`);
}
}
trim(o) {
if(!o) {
return "";
}
return o.toString().trim();
}
}
module.exports = AliSmsService;
\ No newline at end of file
module.exports = SmsinfoService;
\ No newline at end of file
......@@ -103,6 +103,7 @@ var settings = {
accessKeySecret: 'OBP8jxN5ZTgZnEIFgXHkvBB0QEN4E2',
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25',
signName: "薪必果",
template:{
loginTemplate:{
order:"125944682",
......
<a name="menu">目录</a>
1. [单条发送短信]](#sendSms)
1. [单条发送短信]](#sendSms)
1. [单条发送短信]](#querySmsInfos)
## **<a name="sendSms"> 单条发送短信</a>**
[返回到目录](#menu)
......@@ -15,7 +15,7 @@
"action_body": {
"templateName": "loginTemplate", // 模板类型 loginTemplate(登陆) authenticationTemplate(验证) smsTemplate(短信通知)
"phoneNumbers": "18833836395", // 手机号
"templateParam": "", // 模板参数
"code":"1111", // 模板参数
}
}
......@@ -23,7 +23,18 @@
#### 返回结果
```javascript
{
"status": 0,
"msg": "操作成功",
"data": {
"Message": "OK",
"RequestId": "4476BA38-197F-401E-AEB4-53FC8692E447",
"BizId": "459503377430460570^0",
"Code": "OK"
},
"bizmsg": "empty",
"requestid": "8fa7162c20224aacb88bc80fcc778f90"
}
```
## **<a name="querySendDetails"> 查询发送记录</a>**
......@@ -37,8 +48,12 @@
"action_process": "test",
"action_type": "querySendDetails",
"action_body": {
"phoneNumbers": "18833836395", // 手机号
"sendDate": "20191111", // 发送日期格式 20191111
"regionId": "18833836395", // "cn-hangzhou"
"phoneNumbers": "20191111", // 手机号
"templateCode": "20191111", // 模板编号
"templateParam": "20191111", // code 发送的验证码值
"signName": "薪必果", // 签名
"success": "0", // 发送状态
"pageSize":10, //每页条数 默认10条
"currentPage":1 //当前页数 默认第一页
}
......@@ -49,13 +64,26 @@
#### 返回结果
```javascript
{
"TotalCount": 0,
"Message": "OK",
"RequestId": "1BCB6065-1062-42FF-86C2-B084BE8A7BD4",
"SmsSendDetailDTOs": {
"SmsSendDetailDTO": []
},
"Code": "OK"
"status": 0,
"msg": "操作成功",
"data": {
"count": 1,
"rows": [
{
"id": "1503809242000169",
"regionId": "cn-hangzhou",
"phoneNumbers": "18833836395",
"templateCode": "SMS_181196624",
"templateParam": "{\"code\":\"1111\"}",
"signName": "薪必果",
"result": "{\"Message\":\"OK\",\"RequestId\":\"4476BA38-197F-401E-AEB4-53FC8692E447\",\"BizId\":\"459503377430460570^0\",\"Code\":\"OK\"}",
"success": 1,
"created_at": "2019-12-27T07:07:11.000Z"
}
]
},
"bizmsg": "empty",
"requestid": "12b21b28f26245e6b490caf4f393b5fe"
}
```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment