集成云片的短信发送功能

This commit is contained in:
guange 2017-03-12 16:53:47 +08:00
parent 828dce5d07
commit 606e431396
4 changed files with 57 additions and 74 deletions

View File

@ -521,13 +521,6 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
add_index "contest_messages", ["contest_id"], :name => "index_contest_messages_on_contest_id"
add_index "contest_messages", ["user_id"], :name => "index_contest_messages_on_user_id"
create_table "contest_notifications", :force => true do |t|
t.text "title"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "contestant_for_contests", :force => true do |t|
t.integer "student_id"
t.integer "contest_id"
@ -595,37 +588,6 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
add_index "contestant_works", ["user_id"], :name => "index_contestant_works_on_user_id"
add_index "contestant_works", ["work_id"], :name => "index_contestant_works_on_work_id"
create_table "contesting_projects", :force => true do |t|
t.integer "project_id"
t.string "contest_id"
t.integer "user_id"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "reward"
end
create_table "contesting_softapplications", :force => true do |t|
t.integer "softapplication_id"
t.integer "contest_id"
t.integer "user_id"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "reward"
end
create_table "contestnotifications", :force => true do |t|
t.integer "contest_id"
t.string "title"
t.string "summary"
t.text "description"
t.integer "author_id"
t.integer "notificationcomments_count"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "contests", :force => true do |t|
t.integer "user_id"
t.string "name"
@ -858,21 +820,6 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
create_table "delayed_jobs_20161218", :id => false, :force => true do |t|
t.integer "id", :default => 0, :null => false
t.integer "priority", :default => 0, :null => false
t.integer "attempts", :default => 0, :null => false
t.text "handler", :null => false
t.text "last_error"
t.datetime "run_at"
t.datetime "locked_at"
t.datetime "failed_at"
t.string "locked_by"
t.string "queue"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "discuss_demos", :force => true do |t|
t.string "title"
t.text "body"
@ -1143,8 +1090,8 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
t.integer "homework_type", :default => 1
t.string "late_penalty"
t.integer "course_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "teacher_priority", :default => 1
t.integer "anonymous_comment", :default => 0
t.integer "quotes", :default => 0
@ -1152,8 +1099,6 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
t.datetime "simi_time"
t.integer "score_open", :default => 1
t.integer "anonymous_appeal", :default => 0
t.integer "homework_bank_id"
t.boolean "is_update", :default => false
end
add_index "homework_commons", ["course_id", "id"], :name => "index_homework_commons_on_course_id_and_id"
@ -1456,23 +1401,6 @@ ActiveRecord::Schema.define(:version => 20170309024921) do
add_index "memos", ["root_id"], :name => "index_memos_on_root_id"
create_table "mess", :id => false, :force => true do |t|
t.string "课程名"
t.integer "课程ID", :default => 0, :null => false
t.string "教师姓", :default => "", :null => false
t.string "教师名", :limit => 30, :default => "", :null => false
t.string "主贴名", :default => "", :null => false
t.integer "主贴或回帖ID", :default => 0, :null => false
t.integer "回帖对应主贴ID"
t.integer "帖子点赞数"
t.integer "主贴回复数", :default => 0, :null => false
t.text "主贴或回帖内容"
t.datetime "发帖时间", :null => false
t.integer "发帖或回帖用户ID", :default => 0, :null => false
t.string "发帖或回帖用户姓", :default => "", :null => false
t.string "发帖或回帖用户名", :limit => 30, :default => "", :null => false
end
create_table "message_alls", :force => true do |t|
t.integer "user_id"
t.integer "message_id"

View File

@ -3,3 +3,4 @@ require 'trustie/utils/image'
require 'trustie/gitlab/api'
require 'trustie/grack/grack'
require 'trustie/at/at'
require 'trustie/sms/sms'

41
lib/trustie/sms/sms.rb Normal file
View File

@ -0,0 +1,41 @@
#coding=utf-8
require 'net/http'
require 'uri'
module Trustie
module Sms
def self.send(opt={})
Rails.logger.info "#{opt[:mobile]} - #{opt[:code]}"
begin
o = sendYunpian(opt[:mobile], opt[:code])
if o["code"] != 0
Rails.logger.error "发送短信出错: #{o['msg']}"
end
return o["code"] == 0
rescue => e
Rails.logger.error "发送短信出错: #{e}"
return false
end
end
def self.sendYunpian(mobile, code)
#修改为您的apikey.可在官网http://www.yunpian.com)登录后用户中心首页看到
apikey = '21fe1680cb994616fd86f678a7022402'
#指定模板发送接口HTTP地址
send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/tpl_single_send.json')
params = {}
params['apikey'] = apikey
params['mobile'] = mobile
#指定模板发送
#设置模板ID如使用1号模板:【#company#】您的验证码是#code#
#设置对应的模板变量值
params['tpl_id'] = 1733172
params['tpl_value'] = URI::escape('#code#')+'='+URI::escape(code)+'&'+URI::escape('#app#')+'='+URI::escape('确实')
response = Net::HTTP.post_form(send_tpl_sms_uri,params)
ActiveSupport::JSON.decode(response.body)
end
end
end

13
spec/utils/sms_spec.rb Normal file
View File

@ -0,0 +1,13 @@
#coding=utf-8
require 'rails_helper'
describe "短信发送" do
it "可以成功发送,当参数正确时" do
status = Trustie::Sms.send(mobile: "15607313800", code: '1234')
expect(status).to be true
end
end