Skip to content

Commit

Permalink
feat(utils): 新增 getSmsUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 26, 2023
1 parent f019f46 commit 217d733
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/utils/getSmsUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { getSmsUrl } from './getSmsUrl'

describe('getSmsUrl', () => {
test('ok', () => {
expect(
getSmsUrl({
phoneNumber: '10086',
}),
).toBe('sms:10086')
expect(
getSmsUrl({
phoneNumber: '10086',
message: 'hello',
userAgent:
'Mozilla/5.0 (Android 7.1.1; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0',
}),
).toBe('sms:10086?body=hello')
expect(
getSmsUrl({
phoneNumber: '10086',
message: 'hello',
userAgent:
'Mozilla/5.0 (iPhone 6s; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 MQQBrowser/8.3.0 Mobile/15B87 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1',
}),
).toBe('sms:10086&body=hello')
expect(
getSmsUrl({
phoneNumber: '10086',
message: '我们 ya',
userAgent:
'Mozilla/5.0 (Android 7.1.1; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0',
}),
).toBe('sms:10086?body=%E6%88%91%E4%BB%AC%20ya')
expect(
getSmsUrl({
message: 'hello',
userAgent:
'Mozilla/5.0 (Android 7.1.1; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0',
}),
).toBe('sms:?body=hello')
})

test('ua', () => {
Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (Android 7.1.1; Mobile; rv:68.0) Gecko/68.0 Firefox/68.0',
writable: true,
})
expect(
getSmsUrl({
message: 'hello',
}),
).toBe('sms:?body=hello')

Object.defineProperty(navigator, 'userAgent', {
value:
'Mozilla/5.0 (iPhone 6s; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0 MQQBrowser/8.3.0 Mobile/15B87 Safari/604.1 MttCustomUA/2 QBWebViewType/1 WKType/1',
writable: true,
})
expect(
getSmsUrl({
message: 'hello',
}),
).toBe('sms:&body=hello')
})
})
27 changes: 27 additions & 0 deletions src/utils/getSmsUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export interface GetSmsUrlOptions {
/** 手机号 */
phoneNumber?: string
/** 消息 */
message?: string
/** 用户代理 */
userAgent?: string
}

/**
* 获取短信链接。
*
* @param options 选项
* @see https://stackoverflow.com/questions/6480462/how-to-pre-populate-the-sms-body-text-via-an-html-link
*/
export function getSmsUrl(options: GetSmsUrlOptions): string {
const phoneNumber = options.phoneNumber || ''
const message = options.message || ''
let url = `sms:${phoneNumber}`
if (!message) {
return url
}
const userAgent = options.userAgent || navigator.userAgent
const separator = /iphone|ipad|ipod|macintosh/i.test(userAgent) ? '&' : '?'
url += `${separator}body=${encodeURIComponent(message)}`
return url
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export * from './formatNumber'
export * from './GeoCoord'
export * from './getCurrentScript'
export * from './getEnvironment'
export * from './getSmsUrl'
export * from './getWechatPublicAccountQrcodeUrl'
export * from './htmlToDom'
export * from './ii'
Expand Down

0 comments on commit 217d733

Please sign in to comment.