#coding=utf-8 require 'net/https' require 'uri' module Trustie module Sms def self.send(opt={}) Rails.logger.info "#{opt[:mobile]} - #{opt[:code]}" begin o = sendYunpian(opt[:mobile], opt[:code], opt[:send_type], opt[:name], opt[:user_name], opt[:result]) if o["code"] != 0 Rails.logger.error "发送短信出错: #{o['code']}--#{o['msg']}" end return o["code"] rescue => e Rails.logger.error "发送短信出错: #{e}" return false end end def self.sendYunpian(mobile, code, send_type, name, user_name, result) #修改为您的apikey.可在官网(http://www.yunpian.com)登录后用户中心首页看到 apikey = Redmine::Configuration['sms_apikey'] #指定模板发送接口HTTP地址 send_tpl_sms_uri = URI.parse('https://sms.yunpian.com/v2/sms/single_send.json') params = {} params['apikey'] = apikey params['mobile'] = mobile params['text'] = "" if send_type.nil? params['text'] = "【Edu实训】" + code + "(手机验证码)。如非本人操作,请忽略。" elsif send_type == "teacher_register" params['text'] = "【Edu实训】亲爱的#{user_name},有新的老师#{name}注册啦,请尽快处理" elsif send_type == 'competition_start' params['text'] = "【Edu实训】亲爱的#{user_name},你参与的#{name}将于#{result}开始,请及时参赛" Rails.logger.info "#{params['text']}" elsif send_type == 'subject_authorization' || send_type == 'shixun_authorization' params['text'] = "【Edu实训】亲爱的#{user_name},您提交的#{name}#{send_type=='subject_authorization'?'实训课程':'实训'}发布申请#{result},请登录平台查看详情" Rails.logger.info "#{params['text']}" elsif send_type == 'authentication_pro' || send_type == 'authentication'|| send_type == 'trial_authorization' || send_type == 'project_info' params['text'] = "【Edu实训】亲爱的#{user_name},您提交的#{send_type == 'authentication_pro'?'职业认证':(send_type == 'authentication'? '实名认证' : (send_type == 'project_info'?'加入申请':'试用申请' ))}#{result},请登录平台查看详情" Rails.logger.info "#{params['text']}" elsif send_type == "apply_pro_certification" || send_type == "apply_auth" params['text'] = "【Edu实训】亲爱的#{name},有新的#{send_type == 'apply_pro_certification'?'职业':'实名'}认证申请,请尽快处理" Rails.logger.info "#{params['text']}" elsif send_type == "publish_subject" ||send_type == "publish_shixun"|| send_type == "user_apply_auth" || send_type == "discuss" params['text'] = "【Edu实训】亲爱的#{name},有新的#{send_type == 'publish_subject'?'实训课程':(send_type == 'publish_shixun' ? '实训' : (send_type == 'discuss' ? '实训评论':'试用'))}申请发布,请尽快处理" Rails.logger.info "#{params['text']}" elsif send_type == 'join_course_multi_role' params['text'] = "【Edu实训】亲爱的#{user_name},您的课堂#{name}有助教或者教师申请加入,请尽快审核" Rails.logger.info "#{params['text']}" elsif send_type == 'applied_project_info' params['text'] = "【Edu实训】亲爱的#{user_name},您的项目#{name}有成员申请加入,请尽快审核" Rails.logger.info "#{params['text']}" elsif send_type == "training_pay" params['text'] = "【计算机实践教学】亲爱的#{user_name}老师,您已经成功报名参与了11月24日--25日在深圳大学举办的全国软件工程实践教学案例与应用研讨会,请准时参加。(如有任何参会问题,请致电咨询会务联系人汤老师13099740868)" Rails.logger.info "#{params['text']}" elsif send_type == 'publish_library' params['text'] = "【Edu实训】亲爱的#{name},有新的教学案例发布申请,请尽快处理" Rails.logger.info "#{params['text']}" end http = Net::HTTP.new(send_tpl_sms_uri.host, send_tpl_sms_uri.port) http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = true begin request = Net::HTTP::Post.new(send_tpl_sms_uri.request_uri) request.set_form_data(params) request['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8' response = http.start { |http| http.request(request) } ActiveSupport::JSON.decode(response.body) rescue =>err return nil end end end end