From 921bbc3f95b496c54670fec9e18a1b82727dc636 Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Sun, 1 Jan 2017 12:24:38 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E5=92=8C=E5=88=97=E8=A1=A8=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= =?UTF-8?q?=EF=BC=9B=E9=A1=B9=E7=9B=AE=E8=AF=A6=E6=83=85=E5=92=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=9B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=A1=B9=E7=9B=AE=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 14 +++- app/models/project.rb | 5 -- config/routes.rb | 1 + public/index.html | 2 +- public/pages/project_detail.html | 83 +++++++++---------- .../{alter_info.html => user_detail.html} | 9 +- 6 files changed, 58 insertions(+), 56 deletions(-) rename public/pages/{alter_info.html => user_detail.html} (96%) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b324444..44854cb 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,7 +1,7 @@ class ProjectsController < ApplicationController before_action :authenticate - skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail] + skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail,:update] #=============================================>获取我的项目列表,user_id保存在session中. def index @@ -107,8 +107,18 @@ class ProjectsController < ApplicationController } } end + + #=============================================>项目修改 + def update + project = Project.find_by(:id => params[:project][:id]) + if project.update(project_params) + render :json => {:code => 0} + else + render json:current_user.errors, status: :unprocessable_entity + end + end private def project_params - params.require(:project).permit(:name,:content) + params.require(:project).permit(:name,:content,:id) end end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index 74f7f30..24e0dc1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,12 +1,7 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :users -<<<<<<< HEAD - validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户 - -======= validate :require_at_least_on_user,on: :update # 项目中最少要有一个用户 ->>>>>>> 27ebee595014926419b6ccba922ad39450b73f6b validates :name, presence: true, length: { minimum: 1, maximum: 50 }, uniqueness: true private diff --git a/config/routes.rb b/config/routes.rb index 0c11c60..740ee37 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,6 +68,7 @@ Rails.application.routes.draw do get 'projects' => 'projects#index' post 'projects' => 'projects#create' + patch 'projects' => 'projects#update' post 'projects/users' => 'projects#addUsers' get 'projects/detail' => 'projects#detail' diff --git a/public/index.html b/public/index.html index 070f9bd..a1e7f1a 100644 --- a/public/index.html +++ b/public/index.html @@ -37,7 +37,7 @@ //3:通知 //4:修改个人信息 //5:项目详情 edited by cy - var src_array=["project_select","task","share","inform","alter_info","project_detail"]; + var src_array=["project_select","task","share","inform","user_detail","project_detail"]; var src="pages/"+src_array[seq]+".html"; if(seq == 5){ $("#cy_close").click(); diff --git a/public/pages/project_detail.html b/public/pages/project_detail.html index 09d598b..15b521f 100644 --- a/public/pages/project_detail.html +++ b/public/pages/project_detail.html @@ -62,14 +62,14 @@
- +
- +
@@ -104,14 +104,6 @@ - - 杨诏 - - - - 杨诏 - - @@ -158,32 +150,32 @@ $.ajax({ type:"get", dataType:"json", - url:"/projects.json", + url:"/projects/detail.json", data:{ "id":parent.LoginUserInfo.selectProject }, statusCode: { 200: function(rpstr) {// 请求成功 - var robj = eval('('+rpstr+')'); - if(robj["code"]=="0"){ - $("#name").val(robj["name"]); - $("#address").val(robj["content"]); - var users = robj["users"]; + var data=rpstr.data; + if(rpstr.code=="0"){ + $("#project_name").val(data.name); + $("#project_content").val(data.content); + var users = data.users; for(var i = 0; i < users.length; ++i){ - var tduser = $("").text(users[i]["nickname"]); + var tduser = $("").text(users[i].nickname); var btn = $("").text("删除"); btn.attr("class","btn btn-primary btn-danger"); - btn.attr("onclick","cy_deleteUser("+users[i]['id']+")"); + btn.attr("onclick","cy_deleteUser("+users[i].id+")"); var tdbtn = $("").append(btn); $("#cy_user_lists").children("tbody").append($("").append(tduser,tdbtn)); } } else{ - //todo + alert("服务器内部出错啦!"); } }, 401:function(){// 未授权 - + // TODO } } }); @@ -241,31 +233,34 @@ }); } function cy_submit(){ - $.ajax({ - type:"patch", - dataType:"json", - url:"/projects.json", - data:{ - "project_id":parent.LoginUserInfo.selectProject, - "name":$("#name").val(), - "content":$("#address").val() - //todo (write todo for find, need to be edited) - }, - statusCode: { - 200: function(rpstr) {// 请求成功 - var robj = eval('('+rpstr+')'); - if(robj["code"]==0){ - window.location.reload(); + var project_content=$("#project_content").val(); + var project_name=$("#project_name").val(); + if( project_name.length>0 && project_content.length>0 ){ + $.ajax({ + type:"patch", + dataType:"json", + url:"/projects.json", + data:{ + "project[id]":parent.LoginUserInfo.selectProject, + "project[name]":project_name, + "project[content]":project_content + }, + statusCode: { + 200: function(rpstr) {// 请求成功 + var robj = rpstr; + if(robj["code"]==0){ + alert("修改成功!"); + window.location.reload(); + } + }, + 422:function(){ + alert("输入不符合要求,请重新输入!"); } - else{ - //todo - } - }, - 401:function(){// 未授权 - - } - } - }); + } + }); + }else{ + alert("请输入完整!"); + } } diff --git a/public/pages/alter_info.html b/public/pages/user_detail.html similarity index 96% rename from public/pages/alter_info.html rename to public/pages/user_detail.html index 5fb57d9..253c5ed 100644 --- a/public/pages/alter_info.html +++ b/public/pages/user_detail.html @@ -183,11 +183,12 @@ statusCode: { 200: function(data) {// 请求成功 alert("更新成功!"); - window.location.reload(); // alter - LoginUserInfo.name=name; - LoginUserInfo.email=email; - LoginUserInfo.phone=phone; + parent.LoginUserInfo.name=name; + parent.LoginUserInfo.email=email; + parent.LoginUserInfo.phone=phone; + window.location.reload(); + }, 401:function(){// 未授权 alert("未登录!"); From b206bfa9b2a2c4966b1478c16b4288d182bc0f08 Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Sun, 1 Jan 2017 12:51:00 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE#=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=88=E5=A2=9E=E5=8A=A0=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=80=A7=E9=AA=8C=E8=AF=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 38 +++++------ public/pages/project_detail.html | 88 +++++++++++++++++--------- 2 files changed, 78 insertions(+), 48 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 44854cb..5a56ea4 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -52,25 +52,24 @@ class ProjectsController < ApplicationController #=============================================>项目添加用户 def addUsers - #Todo - uid = params[:user_id] - register = session[:user_id] - #username = params[:username] - pid = params[:project_id] - params_pu = {project_id: pid,user_id: uid} - pu = Projects_user.new(params_pu) - pu.save - content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name - puts content - params_note = {user_id: uid,content: content,category: 2,project_id: pid} - note = Note.new(params_note) - note.save! - #puts Note.find_by(id: note.id).inspect - # now = Datetime.now - # note.created_at = now - # note.updated_at = now - note.save! - render :json => {:code => 0} + uid = User.find_by(email:params[:email]).id# 需要添加的用户 + register = session[:user_id]# 当前用户 + pid = params[:project_id] + # 检查该项目中是否已经有要添加的用户 + pu_exist=Projects_user.find_by(project_id:pid,user_id:uid) + if pu_exist.nil?# 不存在 + params_pu = {project_id: pid,user_id: uid} + pu = Projects_user.new(params_pu) + pu.save + content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name + puts content + params_note = {user_id: uid,content: content,category: 2,project_id: pid} + note = Note.new(params_note) + note.save! + render :json => {:code => 0} + else# 存在 + render json:current_user.errors, status: :unprocessable_entity + end end #=============================================>项目详情 @@ -117,6 +116,7 @@ class ProjectsController < ApplicationController render json:current_user.errors, status: :unprocessable_entity end end + private def project_params params.require(:project).permit(:name,:content,:id) diff --git a/public/pages/project_detail.html b/public/pages/project_detail.html index 15b521f..44b76d3 100644 --- a/public/pages/project_detail.html +++ b/public/pages/project_detail.html @@ -77,7 +77,7 @@ @@ -92,7 +92,6 @@ -
@@ -112,16 +111,15 @@
- +
- +
-
@@ -180,32 +178,64 @@ } }); }); - function cy_addUser(){ - $.ajax({ - type:"post", - dataType:"json", - url:"/projects.json", - data:{ - "user_id":$("#cy_new_user"), - "project_id":parent.LoginUserInfo.selectProject - }, - statusCode: { - 200: function(rpstr) {// 请求成功 - var robj = eval('('+rpstr+')'); - if(robj["code"]==0){ - window.location.reload(); - } - else{ - //todo - } + function addUser(){ + var new_user_email=$("#new_user_email").val(); + if( new_user_email.length>0 ){ + // 邮箱对应的用户是否存在 + var exist; + $.ajax({ + type: "get", + dataType: "json", + url:"/users/emailExist.json", + async:false,// 强制同步 + data: { + "email":new_user_email }, - 401:function(){// 未授权 - + statusCode: { + 200: function(data) {// 请求成功 + //alert(data); + if( data=='1' ) + exist=true; + else + exist=false; + }, + 422:function(){ + alert("输入不符合要求,请重新输入!"); + }, + 401:function(){// 未授权 + alert("用户名或密码错误!"); + } } - } - }); + }); + if( exist ){// 邮箱存在 + $.ajax({ + type:"post", + dataType:"json", + async:false,// 强制同步 + url:"/projects/users.json", + data:{ + "email":new_user_email, + "project_id":parent.LoginUserInfo.selectProject + }, + statusCode: { + 200: function(response) {// 请求成功 + alert("添加成功!"); + window.location.reload(); + }, + 422:function(){ + alert("用户已经在项目中!") + } + } + }); + }else{// 邮箱不存在 + alert("该邮箱对应的用户不存在!") + } + }else{ + alert("请输入完整!"); + } + // 添加用户 } - function cy_deleteUser(id){ + function deleteUser(id){ return; $.ajax({ type:"delete", @@ -232,7 +262,7 @@ } }); } - function cy_submit(){ + function basic_alter(){ var project_content=$("#project_content").val(); var project_name=$("#project_name").val(); if( project_name.length>0 && project_content.length>0 ){ From 273234ddaa1024725a3a25d7cc29678052903bb6 Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Sun, 1 Jan 2017 15:07:16 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90=EF=BC=9B?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A1=B9=E7=9B=AE=E5=88=A0=E9=99=A4=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8E=A5=E5=8F=A3=EF=BC=9B=E4=BF=AE=E6=94=B9=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E9=A1=B9=E7=9B=AE=E6=8E=A5=E5=8F=A3bug=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A1=B9=E7=9B=AE=E5=88=97=E8=A1=A8bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/projects_controller.rb | 116 +++++++++++++++---------- config/routes.rb | 1 + public/pages/project_detail.html | 26 +++--- public/pages/project_select.html | 3 +- 4 files changed, 84 insertions(+), 62 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 5a56ea4..2546700 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,7 +1,14 @@ class ProjectsController < ApplicationController before_action :authenticate - skip_before_action :verify_authenticity_token, :only => [:index,:create,:addUsers,:detail,:update] + skip_before_action :verify_authenticity_token, :only => [ + :index, + :create, + :addUsers, + :detail, + :update, + :deleteUsers + ] #=============================================>获取我的项目列表,user_id保存在session中. def index @@ -32,21 +39,15 @@ class ProjectsController < ApplicationController #=============================================>创建项目 def create - #Todo - user_id = session[:user_id] - user = User.find_by(id: user_id) - params_project = {content: params[:content],name: params[:name]} - project = Project.new(params_project) - project.users << user - project.save - if project.save - Projects_user.create(project_id:project.id,user_id:uid) - render status: :created, nothing: true - else - render json: project.errors, status: :unprocessable_entity - end - #render :json => {:code => 0} - end + uid = session[:user_id] + project = Project.new(project_params) + if project.save + Projects_user.create(project_id:project.id,user_id:uid) + render status: :created, nothing: true + else + render json: project.errors, status: :unprocessable_entity + end + end #=============================================>项目添加用户 @@ -61,6 +62,7 @@ class ProjectsController < ApplicationController params_pu = {project_id: pid,user_id: uid} pu = Projects_user.new(params_pu) pu.save + content = User.find_by(id: register).name + "邀请您进入项目" + Project.find_by(id: pid).name puts content params_note = {user_id: uid,content: content,category: 2,project_id: pid} @@ -72,39 +74,63 @@ class ProjectsController < ApplicationController end end + #=============================================>项目删除用户 + def deleteUsers + uid = params[:user_id]# 需要添加的用户 + register = session[:user_id]# 当前用户 + pid = params[:project_id] + # 检查该项目中是否已经有要添加的用户 + projects_user=Projects_user.find_by(project_id:pid,user_id:uid) + if projects_user.nil?# 不存在 + render json:current_user.errors, status: :unprocessable_entity + end + # 存在 + puts projects_user.user_id + puts projects_user.project_id + Projects_user.where(project_id:pid,user_id:uid).delete_all + + content = User.find_by(id: register).name + "把您从项目" + Project.find_by(id: pid).name+"中删除" + puts content + params_note = {user_id: uid,content: content,category: 2,project_id: pid} + note = Note.new(params_note) + note.save! + + render :json => {:code => 0} + end + #=============================================>项目详情 def detail - #Todo - pid = params[:id] - project = Project.find_by(id: pid) - #uid = Projects_user.where(:project_id => pid) - users = [] - projects_users = Projects_user.where("project_id = ?",pid).all - - projects_users.each do |projects_user| - uid = projects_user.user_id - i = User.find_by(id: uid) - users += [{ - :nickname => i.name, - :id => i.id - }] - end - #puts users.inspect - # Users.find(uid).each do |i| - # users += [{ - # :nickname => i.name, - # :id => i.id - # }] - # end - render :json => { - :code => 0, - :data => { - :name => project.name, - :content => project.content, - :users => users + #Todo + pid = params[:id] + project = Project.find_by(id: pid) + #uid = Projects_user.where(:project_id => pid) + users = [] + projects_users = Projects_user.where("project_id = ?",pid).all + + projects_users.each do |projects_user| + uid = projects_user.user_id + i = User.find_by(id: uid) + users += [{ + :nickname => i.name, + :id => i.id + }] + end + #puts users.inspect + # Users.find(uid).each do |i| + # users += [{ + # :nickname => i.name, + # :id => i.id + # }] + # end + render :json => { + :code => 0, + :data => { + :name => project.name, + :content => project.content, + :users => users + } } - } end #=============================================>项目修改 diff --git a/config/routes.rb b/config/routes.rb index 740ee37..e7be66c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -70,6 +70,7 @@ Rails.application.routes.draw do post 'projects' => 'projects#create' patch 'projects' => 'projects#update' post 'projects/users' => 'projects#addUsers' + delete 'projects/users' => 'projects#deleteUsers' get 'projects/detail' => 'projects#detail' get 'missions/project/status' => 'missions#getlist' diff --git a/public/pages/project_detail.html b/public/pages/project_detail.html index 44b76d3..e90c312 100644 --- a/public/pages/project_detail.html +++ b/public/pages/project_detail.html @@ -163,8 +163,10 @@ var tduser = $("").text(users[i].nickname); var btn = $("").text("删除"); btn.attr("class","btn btn-primary btn-danger"); - btn.attr("onclick","cy_deleteUser("+users[i].id+")"); - var tdbtn = $("").append(btn); + btn.attr("onclick","deleteUser("+users[i].id+")"); + if( users[i].id!=parent.LoginUserInfo.id ){// 不能将自己从项目中删除 + var tdbtn = $("").append(btn); + } $("#cy_user_lists").children("tbody").append($("").append(tduser,tdbtn)); } } @@ -233,31 +235,23 @@ }else{ alert("请输入完整!"); } - // 添加用户 } function deleteUser(id){ - return; $.ajax({ type:"delete", dataType:"json", - url:"/projects.json", + url:"/projects/users.json", data:{ "project_id":parent.LoginUserInfo.selectProject, "user_id":id - //todo (write todo for find, need to be edited) }, statusCode: { - 200: function(rpstr) {// 请求成功 - var robj = eval('('+rpstr+')'); - if(robj["code"]==0){ - window.location.reload(); - } - else{ - //todo - } + 200: function(response) {// 请求成功 + alert("删除成功!"); + window.location.reload(); }, - 401:function(){// 未授权 - + 422:function(){ + alert("用户不在项目中!") } } }); diff --git a/public/pages/project_select.html b/public/pages/project_select.html index 24fd144..6702d99 100644 --- a/public/pages/project_select.html +++ b/public/pages/project_select.html @@ -188,6 +188,7 @@ var jstr = htmlobj.responseText; var robj = eval('('+jstr+')'); console.log(jstr); + var count=0; if(robj["code"]==0){ var data = robj["data"]; for(var i=data.length-1;i>=0;--i){ @@ -208,7 +209,7 @@ tp.find("#cy_template_shares_count").attr("id","cy_shares_count_"+id); tp.find("#cy_template_enterbtn").attr("id","cy_enter_"+id); - tp.find("#cy_template_bg").attr("class","widget-user-header "+bg_color[Math.floor(Math.random()*4)]); + tp.find("#cy_template_bg").attr("class","widget-user-header "+bg_color[(count++)%4]); tp.find("#cy_template_bg").attr("id","cy_bg_"+id);