Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrjing committed Jan 11, 2022
1 parent eaf3bbf commit 1208dfd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
65 changes: 57 additions & 8 deletions packages/admin/src/pages/project/operation/Message/TaskCreator.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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<any>({
task: {},
Expand All @@ -69,6 +78,23 @@ const MessageTask: React.FC = () => {
return <span />
}

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) => {
Expand All @@ -77,6 +103,7 @@ const MessageTask: React.FC = () => {
try {
const result = await callWxOpenAPI('sendSms', {
taskId,
useShortname: !!useShortname,
})

console.log(result)
Expand Down Expand Up @@ -191,13 +218,24 @@ const MessageTask: React.FC = () => {
<Form.Item shouldUpdate className="mb-0">
{() => (
<>
{useShortname && (
<>
<Text type="secondary">
{`短信签名最大支持12个字,小程序名称${setting.miniappName}超过12个字,将通过小程序简称${shortname}发送`}
</Text>
<br />
</>
)}

<Text type="secondary">
短信预览:
{getMessageTemplate(
setting?.miniappName,
form.getFieldValue('content')
form.getFieldValue('content'),
shortname
)}
</Text>
<br />
<Text type="secondary">短信内调整的短链自发送后 30 天有效</Text>
</>
)}
Expand All @@ -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 个字符,无法发送,请精简短信内容')
}
Expand Down Expand Up @@ -369,7 +416,7 @@ const MessageTask: React.FC = () => {
共计 {task?.phoneNumberList?.length || 0} 个号码,是否创建发送任务?
</Modal>
) : (
<SmsFileTaskModal actionRef={modalRef} task={task} />
<SmsFileTaskModal actionRef={modalRef} task={task} useShortname={!!useShortname} />
)}
</Col>
</Row>
Expand All @@ -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({
Expand Down Expand Up @@ -469,6 +517,7 @@ const SmsFileTaskModal: React.FC<{
await callWxOpenAPI('createSendSmsTaskByFile', {
fileUri,
taskId,
useShortname: useShortname,
})
},
{
Expand Down
14 changes: 8 additions & 6 deletions packages/cms-openapi/src/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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)
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/cms-openapi/src/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -124,6 +124,7 @@ export class ApiService {
urlLink: urlLink,
templateParamList: [task.content],
templateId: process.env.SMS_TEMPLATE_ID || '844110',
use_short_name: !!useShortname,
})

// 上报短信下发任务
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down

0 comments on commit 1208dfd

Please sign in to comment.