From 1208dfdf4035086c93ab4a38834f55b5e850a8ae Mon Sep 17 00:00:00 2001 From: Mrjing <837317210@qq.com> Date: Tue, 11 Jan 2022 13:01:54 +0800 Subject: [PATCH] fix: fix --- .../project/operation/Message/TaskCreator.tsx | 65 ++++++++++++++++--- .../cms-openapi/src/api/api.controller.ts | 14 ++-- packages/cms-openapi/src/api/api.service.ts | 6 +- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/packages/admin/src/pages/project/operation/Message/TaskCreator.tsx b/packages/admin/src/pages/project/operation/Message/TaskCreator.tsx index 3973b570..8c2e2c82 100644 --- a/packages/admin/src/pages/project/operation/Message/TaskCreator.tsx +++ b/packages/admin/src/pages/project/operation/Message/TaskCreator.tsx @@ -1,4 +1,4 @@ -import React, { MutableRefObject, useEffect, useRef } from 'react' +import React, { MutableRefObject, useEffect, useState, useRef } from 'react' import { useRequest, history } from 'umi' import ProCard from '@ant-design/pro-card' import { PageContainer } from '@ant-design/pro-layout' @@ -40,10 +40,18 @@ interface Task { } // 短信模板 -const getMessageTemplate = (miniappName = '', content = '') => `【${ - miniappName || '小程序名称' -}】${content},跳转小程序 wxaurl.cn/xxxxxxxxxxx -回T退订` +const getMessageTemplate = (miniappName = '', content = '', miniappShortname = '') => { + let finalTempStr = `【${miniappName || '小程序名称'}】${content},跳转小程序 wxaurl.cn/xxxxxxxxxxx + 回T退订` + if (miniappName.length > 12) { + if (miniappShortname) { + finalTempStr = `【${miniappShortname}】${content},跳转小程序 wxaurl.cn/xxxxxxxxxxx + 回T退订` + } + } + + return finalTempStr +} // 号码包文件最大值:30M const MAX_FILE_SIZE = 30 * 1024 * 1024 @@ -55,6 +63,7 @@ const MessageTask: React.FC = () => { const projectId = getProjectId() const globalCtx = useConcent<{}, GlobalCtx>('global') const { setting } = globalCtx.state || {} + const [shortname, setShortname] = useState('') const [{ visible, task, activityId, sendMessageType }, setState] = useSetState({ task: {}, @@ -69,6 +78,23 @@ const MessageTask: React.FC = () => { return } + let { data } = useRequest(async () => { + let shortname = '' + try { + const data = await callWxOpenAPI('getAppBasicInfo') + shortname = data.shortname + setShortname(shortname) + } catch (e) { + console.log('获取小程序简称异常', e.message) + } + + return shortname + }) + + console.log('shortname', shortname) + + const useShortname = setting?.miniappName && setting?.miniappName.length > 12 && shortname + // 创建发送任务 const { run, loading } = useRequest( async (payload: any) => { @@ -77,6 +103,7 @@ const MessageTask: React.FC = () => { try { const result = await callWxOpenAPI('sendSms', { taskId, + useShortname: !!useShortname, }) console.log(result) @@ -191,13 +218,24 @@ const MessageTask: React.FC = () => { {() => ( <> + {useShortname && ( + <> + + {`短信签名最大支持12个字,小程序名称${setting.miniappName}超过12个字,将通过小程序简称${shortname}发送`} + +
+ + )} + 短信预览: {getMessageTemplate( setting?.miniappName, - form.getFieldValue('content') + form.getFieldValue('content'), + shortname )} +
短信内调整的短链自发送后 30 天有效 )} @@ -212,6 +250,15 @@ const MessageTask: React.FC = () => { validator: (_, value) => { const template = getMessageTemplate(setting.miniappName) + // 检查小程序名称是否超过12个字符,超过则检测是否设置过简称 + if (setting.miniappName && setting.miniappName.length > 12) { + if (!shortname) { + return Promise.reject( + `短信签名最大支持12个字,小程序名称${setting.miniappName}超过12个字,请前往设置“小程序简称”以发送短信` + ) + } + } + if (template.length + (value?.length || 0) > 70) { return Promise.reject('短信超出 70 个字符,无法发送,请精简短信内容') } @@ -369,7 +416,7 @@ const MessageTask: React.FC = () => { 共计 {task?.phoneNumberList?.length || 0} 个号码,是否创建发送任务? ) : ( - + )} @@ -389,7 +436,8 @@ const SmsFileTaskModal: React.FC<{ phoneNumberFile: any activityId: string } -}> = ({ actionRef, task = {} }) => { + useShortname: boolean +}> = ({ actionRef, task = {}, useShortname = false }) => { const projectId = getProjectId() const { phoneNumberFile, activityId } = task const [{ visible, uploadPercent }, setState] = useSetState({ @@ -469,6 +517,7 @@ const SmsFileTaskModal: React.FC<{ await callWxOpenAPI('createSendSmsTaskByFile', { fileUri, taskId, + useShortname: useShortname, }) }, { diff --git a/packages/cms-openapi/src/api/api.controller.ts b/packages/cms-openapi/src/api/api.controller.ts index f1710ee8..915321c5 100644 --- a/packages/cms-openapi/src/api/api.controller.ts +++ b/packages/cms-openapi/src/api/api.controller.ts @@ -51,10 +51,10 @@ export class ApiController { */ @UseGuards(PermissionGuard('operation')) @Post('sendSms') - async createSendSmsTask(@Body() body: { taskId: string }) { + async createSendSmsTask(@Body() body: { taskId: string; useShortname: boolean }) { console.log('使用 OpenAPI 发送短信') - const { taskId } = body - return this.apiService.sendSmsByNumbers(taskId) + const { taskId, useShortname = false } = body + return this.apiService.sendSmsByNumbers(taskId, useShortname) } /** @@ -136,10 +136,12 @@ export class ApiController { */ @UseGuards(PermissionGuard('operation')) @Post('createSendSmsTaskByFile') - async createSendSmsTaskByFile(@Body() body: { fileUri: string; taskId: string }) { - const { fileUri, taskId } = body + async createSendSmsTaskByFile( + @Body() body: { fileUri: string; taskId: string; useShortname: boolean } + ) { + const { fileUri, taskId, useShortname = false } = body - return this.apiService.sendSmsByFile(fileUri, taskId) + return this.apiService.sendSmsByFile(fileUri, taskId, useShortname) } /** diff --git a/packages/cms-openapi/src/api/api.service.ts b/packages/cms-openapi/src/api/api.service.ts index 65b5b298..c726bb56 100644 --- a/packages/cms-openapi/src/api/api.service.ts +++ b/packages/cms-openapi/src/api/api.service.ts @@ -65,7 +65,7 @@ export class ApiService { * 通过号码列表,发送短信 * @param taskId */ - async sendSmsByNumbers(taskId: string) { + async sendSmsByNumbers(taskId: string, useShortname: boolean) { const wxCloudApp = getWxCloudApp() const { ENV } = wxCloudApp.getWXContext() @@ -124,6 +124,7 @@ export class ApiService { urlLink: urlLink, templateParamList: [task.content], templateId: process.env.SMS_TEMPLATE_ID || '844110', + use_short_name: !!useShortname, }) // 上报短信下发任务 @@ -203,7 +204,7 @@ export class ApiService { /** * 通过号码包文件创建发送短信的任务 */ - async sendSmsByFile(fileUri: string, taskId: string) { + async sendSmsByFile(fileUri: string, taskId: string, useShortname: boolean) { const wxCloudApp = getWxCloudApp() const envId = getEnvIdString() @@ -225,6 +226,7 @@ export class ApiService { is_url_link: true, file_url: fileUri, template_id: process.env.SMS_TEMPLATE_ID || '844110', + use_short_name: useShortname, }) console.log('发送结果', result)