Commit c10dfeb8 by 宋毅

tj

parent 1df5a8a4
......@@ -16,6 +16,41 @@ class AccessAuthAPI extends APIBase {
return system.getResult(userList);
}
/**
* 发送短信---用于有自己的key 和 密钥
* @param {*} pobj {mobile为要发送的手机号,tmplCode为发送短信编码,需在阿里开通,signName为短信头描述信息}
* @param {*} query
* @param {*} req
*/
async fetchOtherVCode(pobj, query, req) {
var appkey = req.app.appkey;
if (!pobj.mobile) {
return system.getResult(null, "电话号码不能为空.");
}
if (!pobj.tmplCode) {
return system.getResult(null, "模版编码不能为空.");
}
if (!pobj.signName) {
return system.getResult(null, "短信签名不能为空.");
}
if (pobj.tmplCode && pobj.tmplCode.indexOf("SMS_") < 0) {
return system.getResult(null, "模版编码有误");
}
if (!pobj.accessKeyId) {
return system.getResult(null, "模版accessKeyId不能为空.");
}
if (!pobj.accessKeySecret) {
return system.getResult(null, "模版accessKeySecret不能为空.");
}
var cacheKeyStr = appkey + "_" + pobj.mobile;
var tmpReslut = await this.cacheManager["VCodeCache"].getCache(cacheKeyStr);
if (tmpReslut) {
return system.getResultFail(system.redoFail, "操作过于频繁,请勿重复获取", system.redoFail);
}
pobj.reqType = "otherVcode";
var vcodeResult = await this.cacheManager["VCodeCache"].cache(cacheKeyStr, pobj, 120, null);
return system.getResult(vcodeResult);
}
/**
* 发送短信
* @param {*} pobj {mobile为要发送的手机号,tmplCode为发送短信编码,需在阿里开通,signName为短信头描述信息}
* @param {*} query
......@@ -45,7 +80,7 @@ class AccessAuthAPI extends APIBase {
}
var tmpReslut = await this.cacheManager["VCodeCache"].getCache(appkey + "_" + mobile);
if (tmpReslut) {
return system.getResultFail(system.redoFail, "操作过于频繁,请勿重复获取",system.redoFail);
return system.getResultFail(system.redoFail, "操作过于频繁,请勿重复获取", system.redoFail);
}
var vcodeResult = await this.cacheManager["VCodeCache"].cache(appkey + "_" + mobile, tmplCode, 120, signName);
return system.getResult(vcodeResult);
......@@ -64,7 +99,7 @@ class AccessAuthAPI extends APIBase {
}
var tmpReslut = await this.cacheManager["VCodeCache"].getCache(appkey + "_" + mobile);
if (tmpReslut) {
return system.getResultFail(system.redoFail, "操作过于频繁,请勿重复获取",system.redoFail);
return system.getResultFail(system.redoFail, "操作过于频繁,请勿重复获取", system.redoFail);
}
var vcodeResult = this.cacheManager["VCodeCache"].cache(appkey + "_" + mobile, null, 120);
return system.getResult(vcodeResult);
......@@ -358,12 +393,14 @@ class AccessAuthAPI extends APIBase {
if (!pobj.newPwd) {
return system.getResult(null, "新密码不能未空.");
}
var cacheCode = await this.cacheManager["VCodeCache"].cache(appkey + "_" + pobj.mobile, null);
var cacheKeyStr = appkey + "_" + pobj.mobile;
var cacheCode = await this.cacheManager["VCodeCache"].cache(cacheKeyStr, null);
if (!cacheCode || pobj.vcode != cacheCode.vcode) {
return system.getResultFail(-1, "验证码校验不成功,请重新获取验证码验证.", system.verifyVCodeFail);
}
//按照appid和电话查询出要修改的用户
var ruser = await this.userSve.modiPasswordByMobile(appid, pobj.mobile, pobj.newPwd);
this.cacheManager["VCodeCache"].invalidate(cacheKeyStr);//清除验证码缓存
return system.getResult(ruser);
}
/**
......@@ -397,7 +434,8 @@ class AccessAuthAPI extends APIBase {
if (!pobj.vcode) {
return system.getResult(null, "验证码不能未空.");
}
var cacheCode = await this.cacheManager["VCodeCache"].cache(appkey + "_" + pobj.mobile, null);
var cacheKeyStr = appkey + "_" + pobj.mobile;
var cacheCode = await this.cacheManager["VCodeCache"].cache(cacheKeyStr, null);
if (!cacheCode || pobj.vcode != cacheCode.vcode) {
return system.getResultFail(system.verifyVCodeFail, "验证码校验不成功,请重新获取验证码验证.", system.verifyVCodeFail);
}
......@@ -419,6 +457,7 @@ class AccessAuthAPI extends APIBase {
var opencode = super.getUUID();
var authUrl = req.app.authUrl + "?opencode=" + opencode;
await this.cacheManager["OpenCodeCache"].cache(opencode, ruser, 120);
this.cacheManager["VCodeCache"].invalidate(cacheKeyStr);//清除验证码缓存
return system.getResultSuccess({ auth_url: authUrl, opencode: opencode });
}
......
......@@ -17,18 +17,27 @@ class VCodeCache extends CacheBase {
return "g_vcode:appkey_"
}
async buildCacheVal(cachekey, inputkey, val, ex, ...items) {
//inputkey采用appkey_mobile的形式
var mobile = inputkey.split("_")[1];
var tmplCode = val;
var signName = items ? items[0] : "";
var pobj = val;
var vcode = await this.smsUtil.getUidStr(6, 10);
// var content=tmpl.replace(/\|vcode\|/g,vcode);
//this.smsUtil.sendMsg(mobile,content);
if (!tmplCode && !signName) {
this.smsUtil.sendMsg(mobile, vcode);
} //tmplCode为发送短信编码,需在阿里开通,signName为短信头描述信息,二者没有传递则用默认的发送验证码
if (pobj.reqType && pobj.reqType == "otherVcode") {
//【XXX】您的验证码是:602639(5分钟内有效),工作人员不会索取,请勿泄露。-------描述在模板中配置
pobj.msgContent = vcode;
this.smsUtil.aliOtherSendMsg(pobj.mobile, pobj.accessKeyId, pobj.accessKeySecret, pobj.tmplCode,
pobj.signName, JSON.stringify({ code: vcode }));
}
else {
this.smsUtil.aliSendMsg(mobile, tmplCode, signName, JSON.stringify({ code: vcode }));
//inputkey采用appkey_mobile的形式
var mobile = inputkey.split("_")[1];
var tmplCode = val;
var signName = items ? items[0] : "";
// var content=tmpl.replace(/\|vcode\|/g,vcode);
//this.smsUtil.sendMsg(mobile,content);
if (!tmplCode && !signName) {
this.smsUtil.sendMsg(mobile, vcode);
} //tmplCode为发送短信编码,需在阿里开通,signName为短信头描述信息,二者没有传递则用默认的发送验证码
else {
this.smsUtil.aliSendMsg(mobile, tmplCode, signName, JSON.stringify({ code: vcode }));
}
}
return JSON.stringify({ vcode: vcode });
}
......
const system=require("../system");
const system = require("../system");
const Core = require('@alicloud/pop-core');
class SmsClient{
constructor(){
this.smsTeml="http://123.57.156.109:4103/api/Send";
this.restClient=system.getObject("util.restClient");
this.aliclient=new Core({
accessKeyId: 'LTAI4FtNp3wcqFzaADvo1WtZ',
accessKeySecret: 'VBKn1Anx4UmMF0LKNz7PVaCFG1phcg',
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25'
});
}
class SmsClient {
constructor() {
this.smsTeml = "http://123.57.156.109:4103/api/Send";
this.restClient = system.getObject("util.restClient");
this.aliclient = new Core({
accessKeyId: 'LTAI4FtNp3wcqFzaADvo1WtZ',
accessKeySecret: 'VBKn1Anx4UmMF0LKNz7PVaCFG1phcg',
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25'
});
async aliSendMsg(to,tmplcode,signName,jsonContent){
var params = {
"RegionId": "default",
"PhoneNumbers": to,
"SignName": signName,
"TemplateCode": tmplcode,
"TemplateParam": jsonContent
}
var requestOption = {
method: 'POST'
};
this.aliclient.request('SendSms', params, requestOption).then((result) => {
console.log(JSON.stringify(result));
}, (ex) => {
console.log(ex);
})
}
async aliSendMsg(to, tmplcode, signName, jsonContent) {
var params = {
"RegionId": "default",
"PhoneNumbers": to,
"SignName": signName,
"TemplateCode": tmplcode,
"TemplateParam": jsonContent
}
var requestOption = {
method: 'POST'
};
this.aliclient.request('SendSms', params, requestOption).then((result) => {
console.log(JSON.stringify(result));
}, (ex) => {
console.log(ex);
})
}
async sendMsg(to,content){
var txtObj ={
"appId":8,
"mobilePhone":to,
"content":content
}
return this.restClient.execPost(txtObj,this.smsTeml);
async aliOtherSendMsg(to, accessKeyId, accessKeySecret, tmplcode, signName, jsonContent) {
var params = {
"RegionId": "default",
"PhoneNumbers": to,
"SignName": signName,
"TemplateCode": tmplcode,
"TemplateParam": jsonContent
};
var requestOption = {
method: 'POST'
};
var otherAliclient = new Core({
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25'
});
otherAliclient.request('SendSms', params, requestOption).then((result) => {
console.log(JSON.stringify(result));
}, (ex) => {
console.log(ex);
});
}
async sendMsg(to, content) {
var txtObj = {
"appId": 8,
"mobilePhone": to,
"content": content
}
async getUidStr(len, radix) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
var uuid = [], i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
return this.restClient.execPost(txtObj, this.smsTeml);
}
async getUidStr(len, radix) {
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
var uuid = [], i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random() * 16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('');
}
return uuid.join('');
}
}
module.exports=SmsClient;
module.exports = SmsClient;
// var sms=new SmsClient();
// sms.aliSendMsg("13381139519","SMS_173946419","iboss",JSON.stringify({code:"hello"}));
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