From e9a6117963595e674b63ae99eca2be008e15f685 Mon Sep 17 00:00:00 2001 From: "peng.peng" Date: Fri, 12 Jan 2024 13:18:52 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BC=88*=EF=BC=89=E6=8C=87=E6=B4=BE=E5=8F=91?= =?UTF-8?q?=E9=80=81=E7=9F=AD=E4=BF=A1=E5=8A=9F=E8=83=BD=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/data/appointed.js | 36 +++++++++++++++++++++++ api/app/lib/index.js | 5 ++++ api/app/lib/utils/index.js | 17 +++++++++++ api/app/lib/utils/push.js | 36 +++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 api/app/lib/utils/index.js create mode 100644 api/app/lib/utils/push.js diff --git a/api/app/lib/controllers/data/appointed.js b/api/app/lib/controllers/data/appointed.js index 91159b43..921d0074 100644 --- a/api/app/lib/controllers/data/appointed.js +++ b/api/app/lib/controllers/data/appointed.js @@ -1,6 +1,26 @@ 'use strict'; const moment = require('moment') +const reportTypeText = (text) => { + switch (text) { + case 'road': return '道路'; + // + case 'countyRoad': return '县道'; + case 'villageRoad': return '乡道'; + case 'rusticRoad': return '村道'; + // + case 'bridge': return '桥梁'; + case 'culvert': return '涵洞'; + case 'other': return '其他'; + // + case 'conserve': return '养护'; + case 'patrol': return '巡查'; + case 'construction': return '在建'; + default: return text; + } +} + + async function appoint(ctx) { try { const models = ctx.fs.dc.models @@ -8,6 +28,22 @@ async function appoint(ctx) { await models.Report.update({ performerId, handleAdvice, handleState }, { where: { id: recordId } }) + + const { pushBySms } = ctx.app.fs.utils + const report = await models.Report.findOne({ where: { id: recordId } }) + const data = await models.User.findOne({ where: { id: performerId } }) + if (data.phone && data.name && report.projectType) { + await pushBySms({ + phone: [data.phone], + templateCode: 'SMS_464820121', + templateParam: { + report_name: data.name, + type: reportTypeText(report.projectType), + road_name: report.road, + section_name: report.roadSectionStart + report.roadSectionEnd + } + }) + } ctx.status = 204; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); diff --git a/api/app/lib/index.js b/api/app/lib/index.js index da9856ce..0cf88149 100644 --- a/api/app/lib/index.js +++ b/api/app/lib/index.js @@ -7,6 +7,7 @@ const authenticator = require('./middlewares/authenticator'); // const apiLog = require('./middlewares/api-log'); const paasRequest = require('./service/paasRequest'); const schedule = require('./schedule') +const utils = require('./utils') module.exports.entry = function (app, router, opts) { app.fs.logger.log('info', '[FS-AUTH]', 'Inject auth and api mv into router.'); @@ -14,6 +15,7 @@ module.exports.entry = function (app, router, opts) { app.fs.api = app.fs.api || {}; app.fs.api.authAttr = app.fs.api.authAttr || {}; app.fs.api.logAttr = app.fs.api.logAttr || {}; + app.fs.utils = app.fs.utils || {}; router.use(authenticator(app, opts)); // router.use(apiLog(app, opts)); @@ -21,6 +23,9 @@ module.exports.entry = function (app, router, opts) { paasRequest(app, opts) // 定时任务 schedule(app, opts) + + // 工具类函数 + utils(app, opts) router = routes(app, router, opts); }; diff --git a/api/app/lib/utils/index.js b/api/app/lib/utils/index.js new file mode 100644 index 00000000..19d9745a --- /dev/null +++ b/api/app/lib/utils/index.js @@ -0,0 +1,17 @@ +'use strict'; + +const path = require('path'); +const fs = require('fs'); + +module.exports = async function (app, opts) { + fs.readdirSync(__dirname).forEach((filename) => { + if (!['index.js'].some(f => filename == f)) { + const utils = require(`./${filename}`)(app, opts) + console.log(`载入 ${filename} 工具集成功`); + app.fs.utils = { + ...app.fs.utils, + ...utils, + } + } + }); +}; diff --git a/api/app/lib/utils/push.js b/api/app/lib/utils/push.js new file mode 100644 index 00000000..aa044de8 --- /dev/null +++ b/api/app/lib/utils/push.js @@ -0,0 +1,36 @@ +'use strict'; + +const moment = require('moment') +const Core = require('@alicloud/pop-core'); + + +module.exports = function (app, opts) { + const pushBySms = async ({ phone = [], templateCode, templateParam } = {}) => { + try { + if (phone.length) { + const client = new Core({ + accessKeyId: opts.sms.accessKey, + accessKeySecret: opts.sms.accessSecret, + endpoint: 'http://dysmsapi.aliyuncs.com',//固定 + apiVersion: '2017-05-25'//固定 + }); + const SendSmsRes = await client.request('SendSms', { + "PhoneNumbers": phone.join(','),//接收短信的手机号码。 + "SignName": "四好农村路",//短信签名名称。必须是已添加、并通过审核的短信签名。 + "TemplateCode": templateCode,//短信模板ID。必须是已添加、并通过审核的短信签名;且发送国际/港澳台消息时,请使用国际/港澳台短信模版。 + "TemplateParam": JSON.stringify(templateParam)//短信模板变量对应的实际值,JSON格式。 + }, { + method: 'POST' + }); + return SendSmsRes + } + } catch (error) { + throw error + } + } + + return { + + pushBySms, + } +} \ No newline at end of file