-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters