From b9b9a867f453eb34deffcfad349e0bb39ab94116 Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Mon, 25 Jul 2016 15:20:01 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/api.rb | 2 + app/api/mobile/apis/projects.rb | 123 +++++++++++++++ app/api/mobile/entities/project.rb | 18 +++ app/models/project.rb | 1 + app/services/projects_service.rb | 140 ++++++++++++++++++ config/menu.yml | 8 +- config/menu.yml.test | 10 +- public/assets/wechat/project.html | 60 ++++++++ public/assets/wechat/project_list.html | 36 +++++ .../javascripts/wechat/controllers/project.js | 64 ++++++++ .../wechat/controllers/project_list.js | 78 ++++++++++ public/javascripts/wechat/others/routes.js | 2 + 12 files changed, 533 insertions(+), 9 deletions(-) create mode 100644 app/api/mobile/apis/projects.rb create mode 100644 app/api/mobile/entities/project.rb create mode 100644 app/services/projects_service.rb create mode 100644 public/assets/wechat/project.html create mode 100644 public/assets/wechat/project_list.html create mode 100644 public/javascripts/wechat/controllers/project.js create mode 100644 public/javascripts/wechat/controllers/project_list.js diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 9b4bdb5d6..348f3aa0e 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -20,6 +20,7 @@ module Mobile require_relative 'apis/praise' require_relative 'apis/resources' require_relative 'apis/syllabuses' + require_relative 'apis/projects' class API < Grape::API version 'v1', using: :path @@ -75,6 +76,7 @@ module Mobile mount Apis::Praise mount Apis::Resources mount Apis::Syllabuses + mount Apis::Projects add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? diff --git a/app/api/mobile/apis/projects.rb b/app/api/mobile/apis/projects.rb new file mode 100644 index 000000000..181e5746b --- /dev/null +++ b/app/api/mobile/apis/projects.rb @@ -0,0 +1,123 @@ +#coding=utf-8 + +module Mobile + module Apis + class Projects < Grape::API + + resources :projects do + desc "获取项目列表" + params do + requires :token, type: String + end + get do + authenticate! + + ps = ProjectsService.new + projects = ps.user_projects(current_user) + present :data, projects, with: Mobile::Entities::Project,user: current_user + present :status, 0 + end + + desc "返回单个项目" + params do + requires :id, type: Integer + requires :token,type:String + end + route_param :id do + get do + # course = Course.find(params[:id]) + ps = ProjectsService.new + project = ps.show_project(params,current_user) + + if project[:status] != 9 + {status:-1, message: '该项目不存在或已被删除啦' } + else + present :data, project, with: Mobile::Entities::Project,user: current_user + present :status, 0 + end + end + end + + desc "获取项目动态" + params do + requires :token, type: String + + end + post 'auth' do + authenticate! + + auth = 0 + + if (current_user.user_extensions && current_user.user_extensions.identity == 0 && current_user.allowed_to?(:add_course, nil, :global => true)) + auth = 1 + end + + present :auth, auth + end + + desc "获取项目" + params do + requires :token, type: String + end + post 'auth' do + authenticate! + + auth = 0 + + if (current_user.user_extensions && current_user.user_extensions.identity == 0 && current_user.allowed_to?(:add_course, nil, :global => true)) + auth = 1 + end + + present :auth, auth + end + + desc "新建大纲" + params do + requires :token, type: String + requires :title, type: String, desc: '大纲标题' + requires :courses, type: Array[String], desc: '课程名' + end + post do + authenticate! + + ss = SyllabusesService.new + + sy = ss.create(current_user, params[:title], + params[:courses].map{|c| {name: c} }) + + if sy.new_record? + {status:-1, message: '创建大纲失败' } + else + present :data, sy, with: Mobile::Entities::Syllabus,user: current_user + present :status, 0 + end + + end + + desc '编辑大纲' + params do + requires :token, type: String + requires :title, type: String, desc: '大纲标题' + # requires :add_courses, type: Array[String], desc: '课程名' + # requires :modify_courses, type: Array[Integer,String], desc: '课程名' + end + post ':id/edit' do + + authenticate! + + ss = SyllabusesService.new + + #修改课程大纲 + status = ss.edit(current_user, params) + + if status == -1 + {status:status, message: '修改课程信息失败' } + else + present :status, status + end + end + end + + end + end +end \ No newline at end of file diff --git a/app/api/mobile/entities/project.rb b/app/api/mobile/entities/project.rb new file mode 100644 index 000000000..91d6a3073 --- /dev/null +++ b/app/api/mobile/entities/project.rb @@ -0,0 +1,18 @@ +module Mobile + module Entities + class Project < Grape::Entity + expose :name + expose :id + expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options| + current_user = options[:user] + can_setting = instance.user_id == current_user.id ? true : false + can_setting = false if instance.id.nil? + can_setting + end + + expose :member_count, if: lambda { |instance, options| options[:user] } do |instance, options| + instance.members.count + end + end + end +end diff --git a/app/models/project.rb b/app/models/project.rb index bbc639f3d..aa70084d2 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -165,6 +165,7 @@ class Project < ActiveRecord::Base scope :has_module, lambda {|mod| where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s) } + scope :not_deleted, lambda{where("status<>9")} scope :active, lambda { where(:status => STATUS_ACTIVE) } scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } scope :all_public, lambda { where(:is_public => true) } diff --git a/app/services/projects_service.rb b/app/services/projects_service.rb new file mode 100644 index 000000000..8384efc18 --- /dev/null +++ b/app/services/projects_service.rb @@ -0,0 +1,140 @@ +#coding=utf-8 + +class ProjectsService + + include ApplicationHelper + # include CoursesHelper + + def judge_can_setting(sy,user) + sy[:can_setting] = sy[:user_id] == user.id ? true : false + + sy[:can_setting] = false if sy[:id].nil? + + sy.courses.each do |c| + c[:can_setting] = false + + member = c.members.where("user_id=#{user.id} and course_id=#{c.id}")[0] + roleName = member.roles[0].name if member + + if roleName && (roleName == "TeachingAsistant" || roleName == "Teacher" ) + c[:can_setting] = true + end + + if c.tea_id == user.id + c[:can_setting] = true + end + end + + sy + end + #获取指定用户的项目列表 + def user_projects(user) + projects = user.projects.not_deleted.select("projects.*,(SELECT MAX(updated_at) FROM `forge_activities` WHERE forge_activities.project_id = projects.id) AS updated_at ").order("updated_at desc") + projects + end + + #显示项目 + def show_project(params,current_user) + project = Project.find(params[:id]) + # course.generate_invite_code + # course.generate_qrcode + + # unless (course.is_public == 1 || current_user.member_of_course?(course) || current_user.admin?) + # raise '403' + # end + # {:course => course,:work_unit => work_unit, :img_url => url_to_avatar(course),:current_user_is_member => current_user.nil? ? false : current_user.member_of_course?(course),:current_user_is_teacher => current_user.nil? ? false : is_course_teacher(current_user,course),:course_student_num => course ? course.student.count.to_s : 0} + project + end + + def after_create_course(course, user) + #unless User.current.admin? + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => user, :roles => [r]) + m.project_id = -1 + course_info = CourseInfos.new(:user_id => user.id, :course_id => course.id) + #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) + course.members << m + course.course_infos << course_info + end + + def send_wechat_create_class_notice user,course + 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 + title = "恭喜您创建班级成功。" + ws.create_class_notice user.id, "create_course_notice", course.id,title, course.name, user.show_name, 0, "点击查看班级详情。" + end + end + + #创建大纲 + # params {title: '大纲名称', [{course}, {course}]} + def create(user, title, courses = []) + sy = Syllabus.new(title: title, user_id: user.id) + ActiveRecord::Base.transaction() do + sy.save! + + courses.each do |course| + if ::Course === course + course.syllabus_id = sy.id + course.save! + send_wechat_create_class_notice user,course + elsif Hash === course + c = ::Course.new(course) + c.tea_id = user.id + c.syllabus_id = sy.id + c.update_default_value + c.is_public = 0 + c.save! + after_create_course(c, user) + send_wechat_create_class_notice user,c + end + end + + end + sy[:can_setting] = true + sy + end + + #修改课程大纲的名称、班级名称、新增班级 + def edit(user, option) + courses = [] + status = -1 + syllabus_id = option[:id] + + sy = Syllabus.where("id=?",option[:id]).first + + if sy && sy.user_id == user.id + syllabus_title = option[:title] + + sy.title = syllabus_title + sy.save! + #修改班级名称 + modify_courses = option[:modify_courses] + modify_courses.each do |c| + course = Course.where("id=?",c.id).first + + if course && course.tea_id == user.id + course.name = c.name + !course.save + end + end + + #新增班级 + add_courses = option[:add_courses] + add_courses.each do |c| + course = Course.new() + course.name = c + course.tea_id = user.id + course.syllabus_id = sy.id + course.update_default_value + course.is_public = 0 + course.save! + after_create_course(course, user) + send_wechat_create_class_notice user,course + end + status = 0 + end + status + end + +end diff --git a/config/menu.yml b/config/menu.yml index cc799b531..19891b911 100644 --- a/config/menu.yml +++ b/config/menu.yml @@ -30,11 +30,11 @@ button: type: "click" name: "加入项目" key: "JOIN_PROJECT" - - - type: "click" - name: "反馈" - key: "FEEDBACK" - type: "view" name: "历史推文" url: "http://mp.weixin.qq.com/mp/getmasssendmsg?__biz=MzIwOTM2NDkxMA==#wechat_webview_type=1&wechat_redirect" + - + type: "click" + name: "联系我们" + key: "FEEDBACK" diff --git a/config/menu.yml.test b/config/menu.yml.test index 521ff290a..f0a6c4e3a 100644 --- a/config/menu.yml.test +++ b/config/menu.yml.test @@ -13,7 +13,7 @@ button: - type: "click" name: "我的项目" - key: "PROJECT" + url: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d&redirect_uri=https://test.forge.trustie.net/wechat/user_activities&response_type=code&scope=snsapi_base&state=project_list#wechat_redirect" - type: "view" name: "我的宝库" @@ -30,11 +30,11 @@ button: type: "click" name: "加入项目" key: "JOIN_PROJECT" - - - type: "click" - name: "反馈" - key: "FEEDBACK" - type: "view" name: "历史推文" url: "http://mp.weixin.qq.com/mp/getmasssendmsg?__biz=MzIwOTM2NDkxMA==#wechat_webview_type=1&wechat_redirect" + - + type: "click" + name: "联系我们" + key: "FEEDBACK" diff --git a/public/assets/wechat/project.html b/public/assets/wechat/project.html new file mode 100644 index 000000000..f9618411d --- /dev/null +++ b/public/assets/wechat/project.html @@ -0,0 +1,60 @@ +
+
+
邀请码
+
+ {{menu}} +
+ + + + + + + + + + + + +
+
+ +
+
+ +
+
{{r.filename}}发送
+

暂无课件,
+ 请登录Trustie网站,在PC浏览器中上传课件。

+
+
+
授课老师
+ +
+ {{teacher.name}}{{teacher.role_name|identify}} +
+
+
我的同学
+
+ {{student.name}} +
+
+
+ +
+
{{r.homework_name}}发送
+

暂无作业,
+ 请登录Trustie网站,在PC浏览器中上传作业。

+ +
+ +
+
{{r.exercise_name}}发送
+

暂无小测验,
+ 请登录Trustie网站,在PC浏览器中上传小测验。

+
+ + + + +
diff --git a/public/assets/wechat/project_list.html b/public/assets/wechat/project_list.html new file mode 100644 index 000000000..ef0e517b7 --- /dev/null +++ b/public/assets/wechat/project_list.html @@ -0,0 +1,36 @@ +
+
+
项目列表
+ +
+
我创建的项目
+
+
+ + {{project.member_count}}人> +
+ +
+
+ + +
+
我参与的项目
+
+
+ + {{project.member_count}}人> +
+ +
+
+ + +
+ 新建项目 + 加入项目 +
+ + + +
\ No newline at end of file diff --git a/public/javascripts/wechat/controllers/project.js b/public/javascripts/wechat/controllers/project.js new file mode 100644 index 000000000..5020d040b --- /dev/null +++ b/public/javascripts/wechat/controllers/project.js @@ -0,0 +1,64 @@ +app.controller('ProjectController', ['$scope', 'config','$http', 'auth','$location','$routeParams','alertService','rms', function($scope, config, $http, auth, $location, $routeParams,alertService,rms){ + + var vm = $scope; + var projectid = $routeParams.id; + + vm.project_activities = []; + vm.project_activities_page = 0; + + + vm.project_members = []; + vm.project_members_page = 0; + + + vm.currentTab = 1; + vm.tab = function(index){ + vm.currentTab = index; + vm.searchText = ''; + + if(index == 1){ + $http.get(config.apiUrl + 'project/activities?token='+auth.token()+'&project_id='+projectid).then( + function(response) { + console.log(response.data); + vm.project_activities = response.data.data; + } + ) + } + else if(index == 2){ + $http.get(config.apiUrl + 'project/members?token='+auth.token()+'&project_id='+projectid).then( + function(response) { + console.log(response.data); + vm.project_activities = response.data.data; + } + ) + } + }; + + + $http.get(config.apiUrl+ 'project/'+projectid+"?token="+auth.token()).then( + function(response) { + console.log(response.data); + + if (response.data.status == 0){ + vm.project = response.data.data; + resetMenu(vm.project.can_setting); + vm.tab(1); + } + else{ + vm.alertService.showMessage('提示', response.data.message); + } + + } + ); + + + var resetMenu = function(can_setting){ + if(can_setting){ + vm.menus = ["项目动态", "成员管理"]; + } else { + vm.menus = ['项目动态', "我的伙伴"]; + } + + } + +}]); \ No newline at end of file diff --git a/public/javascripts/wechat/controllers/project_list.js b/public/javascripts/wechat/controllers/project_list.js new file mode 100644 index 000000000..4576a81ef --- /dev/null +++ b/public/javascripts/wechat/controllers/project_list.js @@ -0,0 +1,78 @@ +/** + * Created by guange on 16/6/27. + */ + + +app.controller('ProjectListController', ['$scope', 'config', 'auth', '$http', '$location', 'alertService','rms', + function ($scope, config, auth, $http, $location, alertService,rms) { + var vm = $scope; + vm.projects = rms.get('projects') || []; + + vm.alertService_1 = alertService.create(); + vm.alertService_3 = alertService.create(); + + var loadProjectList = function () { + $http.get(config.apiUrl + "projects?token=" + auth.token()).then( + function (response) { + console.log(response.data); + vm.projects = response.data.data; + rms.save('projects', vm.projects); + } + ); + }; + + if(vm.projects.length<=0){ + loadProjectList(); + } + + vm.goProject = function (project_id) { + console.log(project_id); + $location.path("/project").search({id: project_id}); + } + + vm.newProject = function () { + //先判断下权限 + $http.post(config.apiUrl + "projects/auth",{token: auth.token()} ).then( + function (response) { + console.log(response.data); + if (response.data.auth == 0) { + vm.alertService_1.showMessage('提示', '非教师身份不能创建课程哦~'); + } + else{ + $location.path("/new_project"); + } + } + ); + } + + vm.joinProject = function () { + vm.alertService_3.showMessage('提示', '请输入6位项目邀请码(不区分大小写)', function(){ + if (vm.alertService_3.invite && vm.alertService_3.invite.length == 5) { + $http.post(config.apiUrl + "courses/join", { + token: auth.token(), + invite_code: vm.alertService_3.invite + }).then(function (response) { + console.log(response.data); + if (response.data.status != 0) { + vm.alertService_1.showMessage('提示', response.data.message); + } else { + vm.alertService_1.showMessage('提示', '加入项目成功'); + vm.alertService_3.invite = ""; + loadClassList(); + } + }); + } else { + if(vm.alertService_3.invite){ + vm.alertService_1.showMessage('提示', '邀请码格式不正确'); + } + } + }); + }; + + vm.onSetting = function (project) { + console.log(project); + rms.save('current_edit_project', project); + $location.path("/edit_project").search({id: project.id}); + } + + }]); \ No newline at end of file diff --git a/public/javascripts/wechat/others/routes.js b/public/javascripts/wechat/others/routes.js index a40951bcc..5b131433e 100644 --- a/public/javascripts/wechat/others/routes.js +++ b/public/javascripts/wechat/others/routes.js @@ -37,6 +37,8 @@ app.config(['$routeProvider',"$httpProvider", "$locationProvider",'config', func .when('/myresource', makeRoute('myresource.html', 'MyResourceController')) .when('/invite_code', {templateUrl: rootPath + 'invite_code.html', controller: 'InviteCodeController'}) .when('/send_class_list', makeRoute('send_class_list.html', 'SendClassListController')) + .when('/project_list', makeRoute('project_list.html', 'ProjectListController')) + .when('/project', makeRoute('project.html', 'ProjectController')) .otherwise({ redirectTo: '/activites' });