From 606e43139677a28c956e6260873ca0388876e468 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sun, 12 Mar 2017 16:53:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=E4=BA=91=E7=89=87=E7=9A=84?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/schema.rb | 76 ++---------------------------------------- lib/trustie.rb | 1 + lib/trustie/sms/sms.rb | 41 +++++++++++++++++++++++ spec/utils/sms_spec.rb | 13 ++++++++ 4 files changed, 57 insertions(+), 74 deletions(-) create mode 100644 lib/trustie/sms/sms.rb create mode 100644 spec/utils/sms_spec.rb diff --git a/db/schema.rb b/db/schema.rb index 990bb4f00..5249b70b4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/trustie.rb b/lib/trustie.rb index b7bd97de9..46554f4c9 100644 --- a/lib/trustie.rb +++ b/lib/trustie.rb @@ -3,3 +3,4 @@ require 'trustie/utils/image' require 'trustie/gitlab/api' require 'trustie/grack/grack' require 'trustie/at/at' +require 'trustie/sms/sms' diff --git a/lib/trustie/sms/sms.rb b/lib/trustie/sms/sms.rb new file mode 100644 index 000000000..bd530ec2d --- /dev/null +++ b/lib/trustie/sms/sms.rb @@ -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 \ No newline at end of file diff --git a/spec/utils/sms_spec.rb b/spec/utils/sms_spec.rb new file mode 100644 index 000000000..3a30bec29 --- /dev/null +++ b/spec/utils/sms_spec.rb @@ -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 \ No newline at end of file