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 ){