项目地址: https://github.com/lyfeyaj/smart_sms
从公司项目中抽出来的gem, 方便自己以后的项目使用, 也希望能帮到大家.
使用方法
结合 Rails 使用
在 Gemfile
里面添加:
然后
1
2
3
4
5
6
| bundle
rails g smart_sms:config
# 配置一下api_key, `config/initializers/smart_sms_config.rb`
SmartSMS.configure do |config|
config.api_key = 'your_api_key'
end
|
最后
1
2
3
4
5
| # 在现有的 model 中声明 `has_sms_verification`
class User < ActiveRecord::Base
has_sms_verification # 默认使用字段 :phone, :verified_at, 可自定义指定, 例如, :mobile, :confirmed_at
end
|
于是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 发送短信验证码
user.deliver # 将会生成一个随机的验证码发送至手机
# 查询历史短信记录
user.messages
# 查询是否已经验证
user.verified?
# 校验验证码
user.verify! '123456'
# 查询最新的一条有效短信记录
user.latest_message
|
基本用法(不结合Rails)
设置api_key
1
| SmartSMS.configure { |c| c.api_key = 'your_api_key' }
|
发送短信
1
| SmartSMS.deliver 13522948742, 'SmartSMS WOW!'
|
详细文档
https://github.com/lyfeyaj/smart_sms
注
比较了很多家短信服务商, 云片网络的服务比较完善, 可以自定义短信模板, 查询信息等, 而且短信延迟很小, 基本上5s左右能发送到手机. 所以这个gem封装了云片网的所有短信功能, 有兴趣的童鞋可以看下他们的API文档
目前这个gem已经在公司项目的生产环境中, 一切运行良好, 放心使用 ^_^