diff --git a/app/api/mobile/entities/course.rb b/app/api/mobile/entities/course.rb index 925c4d7a7..fc78cc14e 100644 --- a/app/api/mobile/entities/course.rb +++ b/app/api/mobile/entities/course.rb @@ -76,6 +76,15 @@ module Mobile end can_setting end + expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options| + current_user = options[:user] + if instance[:course] + course = instance[:course] + else + course = instance + end + current_user.member_of_course?(course) + end expose :teacher, using: Mobile::Entities::User do |c, opt| if c.is_a? ::Course c.teacher diff --git a/app/api/mobile/entities/project.rb b/app/api/mobile/entities/project.rb index 073f7cd0f..8da151b42 100644 --- a/app/api/mobile/entities/project.rb +++ b/app/api/mobile/entities/project.rb @@ -23,6 +23,16 @@ module Mobile current_user.id == instance.user_id end + expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options| + current_user = options[:user] + if instance[:project] + project = instance[:project] + else + project = instance + end + current_user.member_of?(project) + end + expose :member_count, if: lambda { |instance, options| options[:user] } do |instance, options| instance.members.count diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index f14416442..d661350c6 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -84,7 +84,8 @@ class MembersController < ApplicationController # 添加成功后,申请人收到消息 AppliedMessage.create(:user_id => @applied_message.applied_user_id, :applied_type => "AppliedProject", :applied_id => applied_project.id , :status => 6, :viewed => false, :applied_user_id => @applied_message.user_id, :role => applied_project.role, :project_id => applied_project.project_id) - + ps = ProjectsService.new + ps.send_wechat_join_project_notice user,project,ap_role,0 # 添加成功后,批准人收到消息 # AppliedMessage.create(:user_id => @applied_message.user_id, :applied_type => "AppliedProject", :applied_id => applied_project.id , # :status => 7, :viewed => true, :applied_user_id => @applied_message.applied_user_id, :role => applied_project.role, :project_id => applied_project.project_id) @@ -114,6 +115,11 @@ class MembersController < ApplicationController @applied_message = AppliedMessage.find(params[:applied_message_id]) # AppliedMessage.create(:user_id => @applied_message.user_id, :applied_type => "AppliedProject", :applied_id => applied_project.id ,:status => 5, # :viewed => true, :applied_user_id => @applied_message.applied_user_id, :role => applied_project.role, :project_id => applied_project.project_id) + ps = ProjectsService.new + user = User.find(@applied_message.applied_user_id) + ap_role = applied_project.try(:role) + ps.send_wechat_join_project_notice user,project,ap_role,1 + applied_project.delete end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2f1d6a4db..f721f6b8a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -352,14 +352,14 @@ class UsersController < ApplicationController def dealwith_apply_request @msg = CourseMessage.find(params[:msg_id]) #CourseMessage content存的是role 7教辅 9 教师 + apply_user = User.find(@msg.course_message_id) + ids = @msg.content.split(",") # [@msg.content] msg content保存的是申请的职位角色 + integer_ids = [] + ids.each do |role_id| + integer_ids << role_id.to_i + end case params[:agree] when 'Y' - apply_user = User.find(@msg.course_message_id) - ids = @msg.content.split(",") # [@msg.content] msg content保存的是申请的职位角色 - integer_ids = [] - ids.each do |role_id| - integer_ids << role_id.to_i - end if apply_user.member_of_course?(Course.find(@msg.course_id)) #将角色改为老师或者教辅 member = Course.find(@msg.course_id).members.where(:user_id=>apply_user.id).all[0] @@ -382,11 +382,32 @@ class UsersController < ApplicationController Course.find(@msg.course_id).members << members CourseMessage.create(:user_id => @msg.course_message_id, :course_id => @msg.course_id, :viewed => false,:content=> @msg.content,:course_message_id=>User.current.id,:content=>@msg.content,:course_message_type=>'CourseRequestDealResult',:status=>1) @msg.update_attributes(:status=>1,:viewed=>1) + + if integer_ids.include?(9) + cs = CoursesService.new + c = Course.find(@msg.course_id) + cs.send_wechat_join_class_notice apply_user,c,9,0 + else + cs = CoursesService.new + c = Course.find(@msg.course_id) + cs.send_wechat_join_class_notice apply_user,c,7,0 + end end when 'N' CourseMessage.create(:user_id => @msg.course_message_id, :course_id => @msg.course_id, :viewed => false,:content=> @msg.content,:course_message_id=>User.current.id,:content=>@msg.content,:course_message_type=>'CourseRequestDealResult',:status=>2) @msg.update_attributes(:status=>2,:viewed=>1) + + if integer_ids.include?(9) + cs = CoursesService.new + c = Course.find(@msg.course_id) + cs.send_wechat_join_class_notice apply_user,c,9,1 + else + cs = CoursesService.new + c = Course.find(@msg.course_id) + cs.send_wechat_join_class_notice apply_user,c,7,1 + end + end respond_to do |format| format.js diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index 73ca91590..81fe9c5f4 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -108,14 +108,14 @@ class CoursesService end #双重身份 学生列表中不显示 - unless (params[:role] == '2' && role_ids.length >= 2) + # unless (params[:role] == '2' && role_ids.length >= 2) users << {:id => m.user.id, :img_url => img_url, :nickname => m.user.login, :gender => gender, :work_unit => work_unit, :mail => m.user.mail, :location => location, role_name: m.roles.first.name, name: m.user.show_name, roles_id: role_ids.include?(7) ? 7 : (role_ids.include?(9) ? 9 : 10 ), :brief_introduction => m.user.user_extensions.brief_introduction,:realname=>m.user.realname} - end + # end end users @@ -223,16 +223,20 @@ class CoursesService end if integer_ids.include?(9) message = "您已同意教师"+apply_user.show_name+",加入班级" + send_wechat_join_class_notice apply_user,c,9,0 else message = "您已同意助教"+apply_user.show_name+",加入班级" + send_wechat_join_class_notice apply_user,c,7,0 end else CourseMessage.create(:user_id => apply_user.id, :course_id => c.id, :viewed => false,:content=> messages.content,:course_message_id=>current_user.id,:course_message_type=>'CourseRequestDealResult',:status=>2) messages.update_attributes(:status=>2,:viewed=>1) if integer_ids.include?(9) message = "您已拒绝教师"+apply_user.show_name+",加入班级" + send_wechat_join_class_notice apply_user,c,9,1 else message = "您已拒绝助教"+apply_user.show_name+",加入班级" + send_wechat_join_class_notice apply_user,c,7,1 end end status = 0 @@ -528,6 +532,37 @@ class CoursesService {:state => @state,:course => course} end + #加入班级结果 + def send_wechat_join_class_notice user,course,role_id,result + count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count + if count == 0 + ws = WechatService.new + if role_id == 10 && result == 0 + title = "恭喜您加入班级成功。" + ws.join_class_notice user.id, "class", course.id,title, course.syllabus.name,course.name,course.teacher.show_name, "点击查看班级详情。" + else + + role_name = role_id == 7 ? "助教" : "教师" + result_name = result == 0 ? "通过" : "被拒绝" + content = "您以"+role_name+"身份加入班级的申请已"+result_name+"。" + + if result == 0 + title = "恭喜您加入班级成功。" + remark = "点击查看班级详情。" + uid = 0 + type = "class" + else + title = "很遗憾您未能成功加入班级。" + remark = "点击查看申请详情。" + uid = user.id + type = "review_class_member" + end + ws = WechatService.new + ws.class_notice user.id, type, course.id, title, course.name, user.show_name, format_time(Time.now), content, remark,uid + end + end + end + #多个角色加入课程 def join_course_roles params,current_user course = Course.find_by_invite_code(params[:invite_code]) if params[:invite_code] @@ -587,6 +622,7 @@ class CoursesService course.members << members StudentsForCourse.create(:student_id => current_user.id, :course_id => course.id) @state = 0 + send_wechat_join_class_notice current_user,course,10,0 else is_stu = false if role_ids.include?("10") @@ -595,6 +631,7 @@ class CoursesService course.members << members StudentsForCourse.create(:student_id => current_user.id, :course_id =>course.id) is_stu = true + send_wechat_join_class_notice current_user,course,10,0 end #如果已经发送过消息了,那么就要给个提示 if CourseMessage.where("course_message_type = 'JoinCourseRequest' and user_id = #{course.tea_id} and content = '#{role_str}' and course_message_id = #{current_user.id} and course_id = #{course.id} and status = 0").count != 0 diff --git a/app/services/projects_service.rb b/app/services/projects_service.rb index 4de9143bf..2b72fed69 100644 --- a/app/services/projects_service.rb +++ b/app/services/projects_service.rb @@ -117,6 +117,27 @@ class ProjectsService ] end + #加入项目结果 + def send_wechat_join_project_notice user,project,role_id,result + count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Project' and shield_id=#{project.id}").count + if count == 0 + result_name = result == 0 ? "已同意" : "已拒绝" + if result == 0 + title = "恭喜您加入项目成功。" + remark = "点击查看项目详情。" + uid = 0 + type = "project" + else + title = "很遗憾您未能成功加入项目。" + remark = "点击查看申请详情。" + uid = user.id + type = "review_project_member" + end + ws = WechatService.new + ws.join_project_notice user.id, "project", project.id,title, project.name,result_name, remark,uid + end + end + def join_project params,current_user status = -1 project = Project.find_by_invite_code(params[:invite_code]) if params[:invite_code] @@ -146,6 +167,7 @@ class ProjectsService project.project_infos << project_info status = 0 + send_wechat_join_project_notice current_user,project,5,0 else if !AppliedProject.where(:project_id => project.id, :user_id => current_user.id).first.nil? status = 8 @@ -271,6 +293,8 @@ class ProjectsService message = "您已同意"+rolename+user.show_name+",加入项目" + send_wechat_join_project_notice user,project,ap_role,0 + else #拒绝 AppliedMessage.create(:user_id => user.id, :applied_type => "AppliedProject", :applied_id => applied_project.id ,:status => 4, @@ -280,6 +304,8 @@ class ProjectsService applied_project.delete message = "您已拒绝"+rolename+user.show_name+",加入项目" + + send_wechat_join_project_notice user,project,ap_role,1 end status = 0 diff --git a/app/services/syllabuses_service.rb b/app/services/syllabuses_service.rb index fcb70653b..9ca7457a0 100644 --- a/app/services/syllabuses_service.rb +++ b/app/services/syllabuses_service.rb @@ -72,7 +72,7 @@ class SyllabusesService if count == 0 ws = WechatService.new title = "恭喜您创建班级成功。" - ws.create_class_notice user.id, "create_course_notice", course.id,title, course.name, user.show_name, 0, "点击查看班级详情。" + ws.create_class_notice user.id, "create_course_notice", course.id,title, course.name, user.show_name, 1, "点击查看班级详情。" end end diff --git a/app/services/wechat_service.rb b/app/services/wechat_service.rb index 9a6351391..4b120eaa0 100644 --- a/app/services/wechat_service.rb +++ b/app/services/wechat_service.rb @@ -111,11 +111,15 @@ class WechatService end end - def two_keys_template(openid, template_id, type, id, first, key1, key2, remark="") + def two_keys_template(openid, template_id, type, id, first, key1, key2, remark="",uid) + tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}/#{id}" + if uid && uid != 0 + tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + end data = { touser:openid, template_id:template_id, - url:"#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}/#{id}",#/assets/wechat/app.html#/#{type}/#{id} + url:tmpurl,#/assets/wechat/app.html#/#{type}/#{id} topcolor:"#FF0000", data:{ first: { @@ -292,6 +296,19 @@ class WechatService end end + def join_class_notice(user_id, type, id, first, key1, key2, key3,remark="") + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = three_keys_template uw.openid,Wechat.config.join_class_notice, type, id, first, key1, key2, key3, remark + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[join_class__notice] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + end + def create_class_notice(user_id, type, id, first, key1, key2, key3, remark="") uw = UserWechat.where(user_id: user_id).first unless uw.nil? @@ -383,4 +400,19 @@ class WechatService end end + def join_project_notice(user_id, type, id, first, key1, key2,remark="",uid=0) + uw = UserWechat.where(user_id: user_id).first + unless uw.nil? + data = two_keys_template uw.openid,Wechat.config.join_project_notice, type, id, first, key1, key2,remark,uid + begin + req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data) + rescue Exception => e + Rails.logger.error "[join_project_notice] ===> #{e}" + end + Rails.logger.info "send over. #{req}" + end + + + end + end \ No newline at end of file diff --git a/config/wechat.yml.template b/config/wechat.yml.template index 68e6ed62e..f55640d97 100644 --- a/config/wechat.yml.template +++ b/config/wechat.yml.template @@ -23,6 +23,8 @@ default: &default create_class_notice: "2GtJJGzzNlNy2i0UrsjEDlvfSVIUXQfSo47stpcQAVw" create_project_notice: "jYu0iimbDpgWYZaTLXioZe2lvqoWTdKnUPyphTJ1mxs" project_review_notice: "kdb-8UlMjTc3z51Qcf8g2vY4i_nE4OGKZAucdQma_2E" + join_class_notice: "0V2vhhp5co7drgoyqn3WXUUTQ9xwYrnVcMma7ika6o0" + join_project_notice: "TtXvy0XMIQyCgpnXHhoB8t-x0QIfy-78gAJXsGf9afg" production: <<: *default diff --git a/config/wechat.yml.test b/config/wechat.yml.test index 8dbd9850f..a49282c1a 100644 --- a/config/wechat.yml.test +++ b/config/wechat.yml.test @@ -23,6 +23,8 @@ default: &default create_class_notice: "9CDIvHIKiGwPEQWRw_-wieec1o50tMXQPPZIfECKu0I" create_project_notice: "R2ZaQKJfDJgujPcHWPzadKHIRkIyj2CjX2o_qIuRqig" project_review_notice: "ip192wVXTav3qchgUn9_7B6lFfTlCZjwL7A1tncTOuc" + join_class__notice: "umHZyT8n6KPf3emf4sTJ66I8OB_TJTzjYhxDbJUgUis" + join_project_notice: "3KnMQEMUCmQWkB5JvzrpmguEwnN8bvUHUdpOTudxv_M" production: <<: *default diff --git a/public/assets/wechat/activities.html b/public/assets/wechat/activities.html index fbdfb1152..a4aa9a4c7 100644 --- a/public/assets/wechat/activities.html +++ b/public/assets/wechat/activities.html @@ -26,8 +26,7 @@