chen hace 4 años
padre
commit
f53cff979c
Se han modificado 4 ficheros con 68 adiciones y 2 borrados
  1. 7 0
      app/controller/phoneMessage.js
  2. 1 0
      app/router.js
  3. 11 2
      app/service/phoneMessage.js
  4. 49 0
      app/util/smsUtil.js

+ 7 - 0
app/controller/phoneMessage.js

@@ -19,6 +19,13 @@ class PhoneMessageController extends Controller {
 
   }
 
+  // 短信提醒
+  async smsRemind() {
+    const res = await this.service.smsRemind(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+
+  }
+
   // OCR
   async getMessage() {
     const res = await this.service.getMessage(this.ctx.request.body);

+ 1 - 0
app/router.js

@@ -254,5 +254,6 @@ module.exports = app => {
   // 验证码
   router.post('phoneMessage', '/api/financial/phoneMessage/sendMessage', controller.phoneMessage.sendMessage);
   router.post('phoneMessage', '/api/financial/phoneMessage/getMessage', controller.phoneMessage.getMessage);
+  router.post('phoneMessage', '/api/financial/phoneMessage/smsRemind', controller.phoneMessage.smsRemind);
 
 };

+ 11 - 2
app/service/phoneMessage.js

@@ -7,6 +7,7 @@ const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 
 const sms = require('../util/aliMessage.js');
+const smsUtil = require('../util/smsUtil.js');
 
 class PhoneMessageService extends CrudService {
 
@@ -27,13 +28,21 @@ class PhoneMessageService extends CrudService {
     }
   }
 
+  // 发送短信提醒:PhoneNumbers, SignName, TemplateCode, TemplateParam
+  async smsRemind(data) {
+    if (data.PhoneNumbers) {
+      const res = await smsUtil.smsAlert(data.PhoneNumbers, '惠金信用信息服务', data.TemplateCode, data.TemplateParam);
+      return res;
+    }
+    return '手机号有误';
+  }
+
+  // OCR : img(base64)
   async getMessage(data) {
     const url = 'https://dm-58.data.aliyun.com/rest/160601/ocr/ocr_business_license.json';
     const AppCode = '4638ef02c8e248e1b12d55b1170e4feb';
     if (data.img) {
-
       const bodys = { image: data.img };
-
       const res = await this.ctx.curl(url, {
         method: 'POST',
         headers: { Authorization: 'APPCODE ' + AppCode },

+ 49 - 0
app/util/smsUtil.js

@@ -0,0 +1,49 @@
+'use strict';
+
+const Core = require('@alicloud/pop-core');
+const _ = require('lodash');
+
+const accessKeyId = 'LTAI4G1eNRqKqqRLP15d3x5h';
+const accessKeySecret = 'CJtcZjAu366mmmscIRXfyml9Hx61Bh';
+
+const client = new Core({
+  accessKeyId,
+  accessKeySecret,
+  endpoint: 'https://dysmsapi.aliyuncs.com',
+  apiVersion: '2017-05-25',
+});
+
+
+/**
+ * sms.smsAlert(手机号,签名,模板code,参数) 发送短信提醒
+ **/
+exports.smsAlert = function(PhoneNumbers, SignName, TemplateCode, TemplateParam) {
+  return new Promise((resolve, reject) => {
+    try {
+      client.request('SendSms', {
+        RegionId: 'cn-hangzhou',
+        PhoneNumbers,
+        SignName,
+        TemplateCode,
+        TemplateParam,
+      }, {
+        method: 'POST',
+      }).then(result => {
+        console.log('res', result);
+        if (result.Message && result.Message == 'OK' && result.Code && result.Code == 'OK') { // 短信发送成功
+          resolve(result);
+        } else {
+          console.log('error', result);
+          reject(result);
+        }
+      }, ex => {
+        console.log('ex', ex);
+        reject(ex);
+      });
+    } catch (error) {
+      console.log('catch  error', error);
+
+      reject(error);
+    }
+  });
+};