Module: SmartSMS::HasSmsVerification::ClassMethods::InstanceMethods
- Defined in:
- lib/smart_sms/has_sms_verification.rb
Overview
Instance methods
Instance Method Summary collapse
-
#deliver(text = SmartSMS::VerificationCode.random) ⇒ Object
发送短信至手机.
- #deliver_fake_sms(text = SmartSMS::VerificationCode.random) ⇒ Object
-
#latest_message ⇒ Object
获取最新的一条有效短信记录.
-
#verified? ⇒ Boolean
判断是否已经验证成功.
- #verified_at ⇒ Object
-
#verify(code) ⇒ Object
安全verify方法, 用于校验短信验证码是否正确, 返回: true 或 false.
-
#verify!(code) ⇒ Object
非安全verify!方法, 验证成功后会存储成功的结果到数据表中.
Instance Method Details
#deliver(text = SmartSMS::VerificationCode.random) ⇒ Object
发送短信至手机
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/smart_sms/has_sms_verification.rb', line 117 def deliver(text = SmartSMS::VerificationCode.random) result = SmartSMS.deliver send(self.class.sms_mobile_column), text if result['code'] == 0 sms = SmartSMS.find_by_sid(result['result']['sid'])['sms'] sms, text else errors.add :deliver, result false end end |
#deliver_fake_sms(text = SmartSMS::VerificationCode.random) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/smart_sms/has_sms_verification.rb', line 128 def deliver_fake_sms(text = SmartSMS::VerificationCode.random) mobile = send(self.class.sms_mobile_column) company = SmartSMS.config.company sms = SmartSMS::FakeSMS.build_fake_sms mobile, text, company sms, text end |
#latest_message ⇒ Object
获取最新的一条有效短信记录
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/smart_sms/has_sms_verification.rb', line 97 def end_time = Time.now start_time = end_time - SmartSMS.config.expires_in if SmartSMS.config.store_sms_in_local send(self.class.) .where('send_time >= ? and send_time <= ?', start_time, end_time) .last else result = SmartSMS.find( start_time: start_time, end_time: end_time, mobile: send(self.class.sms_mobile_column), page_size: 1 ) result['sms'].first end end |
#verified? ⇒ Boolean
判断是否已经验证成功
87 88 89 |
# File 'lib/smart_sms/has_sms_verification.rb', line 87 def verified? verified_at.present? end |
#verified_at ⇒ Object
91 92 93 |
# File 'lib/smart_sms/has_sms_verification.rb', line 91 def verified_at self[self.class.sms_verification_column] end |
#verify(code) ⇒ Object
安全verify方法, 用于校验短信验证码是否正确, 返回: true 或 false
75 76 77 78 79 80 81 82 83 |
# File 'lib/smart_sms/has_sms_verification.rb', line 75 def verify(code) sms = return false if sms.blank? if SmartSMS.config.store_sms_in_local sms.code == code.to_s else sms['text'].gsub(self.class.verify_regexp, '') == code.to_s end end |
#verify!(code) ⇒ Object
非安全verify!方法, 验证成功后会存储成功的结果到数据表中
65 66 67 68 69 70 71 |
# File 'lib/smart_sms/has_sms_verification.rb', line 65 def verify!(code) result = verify code if result send("#{self.class.sms_verification_column}=", Time.now) save(validate: false) end end |