zy:modify activity signup and quit

This commit is contained in:
lovelyzhang 2016-12-27 16:49:34 +08:00
parent 4a24c82a0e
commit af26c41a7d
7 changed files with 89 additions and 174 deletions

View File

@ -12,7 +12,7 @@ class MainController < ApplicationController
page_num = 1
end
if !post_type
post_type=[1,2,3]
post_type=[1, 2, 3]
end
@posts = MicroPost.where(post_type: post_type).paginate(:page => page_num, :per_page => 10).order(post_time: :desc)
@ -22,18 +22,17 @@ class MainController < ApplicationController
end
def activity
@user = current_user
micro_post_id = params[:micropost_id]
join = params[:join].to_s
micro_post = MicroPost.find_by(id: micro_post_id)
if join == "true"
micro_post.engage_people += 1
num, names = new_engaged_people(micro_post_id, current_user.name)
else
micro_post.engage_people -= 1
end
if micro_post.save
render json: {total_num: micro_post.engage_people}
num, names = delete_engaged_people(micro_post_id, current_user.name)
end
render json: {total_num: num, total_names: names}
end
end

View File

@ -27,7 +27,8 @@ class MicroPostsController < ApplicationController
if post_type >= 1 and post_type <= 3 and !title.empty? and !content.empty?
micropost = @user.micro_posts.new(title: title, content: content, pic: pic_path,
post_time: DateTime.now, post_type: post_type, engage_people: 0)
post_time: DateTime.now, post_type: post_type,
engage_people: 1, engaged_people_names: current_user.name)
micropost.save
end
redirect_to microposts_path

View File

@ -1,35 +1,5 @@
module MainHelper
def read_newest_post offset_num
micro_posts_array = []
micro_posts = MicroPost.all.order(post_time: :desc).limit(20).offset(offset_num)
if !micro_posts.empty?
micro_posts.each do |micro_post|
x = Hash.new()
x["username"] = micro_post.user.name
x["userpic"] = micro_post.user.picurl
x["postid"] = micro_post.id
x["title"] = micro_post.title
x["content"] = micro_post.content
case micro_post.post_type
when 1
x["type"] = "新鲜事"
when 2
x["type"] = "组团信息"
when 3
x["type"] = "失物招领"
else
x["type"] = "新鲜事"
end
x["time"] = get_strftime(micro_post.post_time)
x["pics"] = micro_post.pic.split(',') if micro_post.pic
x["peo_num"] = micro_post.engage_people
micro_posts_array << x
end
end
return micro_posts_array
end
def get_post posts
micro_posts_array = []
if !posts.empty?
@ -60,7 +30,65 @@ module MainHelper
end
def get_engaged_people micropost_id
# 获得参加活动的人数和姓名
MicroPost.transaction do
micropost = MicroPost.lock.find_by(id: micropost_id)
engage_people = micropost.engage_people
engage_people_name = micropost.engaged_people_names
return engage_people, engage_people_name
end
end
end
def save_engaged_people(micropost_id, num, names)
# 将参加活动的人数和姓名存入数据库中
MicroPost.transaction do
micropost = MicroPost.lock.find_by(id: micropost_id)
micropost.engage_people = num
micropost.engaged_people_names = names
micropost.save
end
end
def new_engaged_people(micropost_id, name)
# 新参加活动登记
engage_people, engage_people_names = get_engaged_people micropost_id
if engage_people_names.nil?
engage_people += 1
engage_people_names = name
else
engage_people_names = engage_people_names.split(',')
if engage_people_names.include?(name) != true
engage_people_names << name
engage_people += 1
end
engage_people_names = engage_people_names.join(',')
end
save_engaged_people(micropost_id, engage_people, engage_people_names)
return engage_people, engage_people_names
end
def delete_engaged_people(micropost_id, name)
# 退出活动登记
engage_people, engage_people_names = get_engaged_people micropost_id
engage_people_names = engage_people_names.split(',')
if engage_people_names.delete(name)
engage_people_names = engage_people_names.join(',')
engage_people -= 1
save_engaged_people(micropost_id, engage_people, engage_people_names)
end
return engage_people,engage_people_names
end
def joinded_activity(micropost_id,name)
engage_people, engage_people_names = get_engaged_people micropost_id
engage_people_names = engage_people_names.split(',')
return engage_people_names.include?(name)
end
def micro_post_belong_to_user(micropost_id,user)
find_result = user.micro_posts.where(id: micropost_id)
return !find_result.empty?
end
end

View File

@ -32,19 +32,24 @@
</div>
<% end %>
</div>
<% if micro_post["type"] == "组团信息" %>
<div class="social-button">
<button class="btn btn-lg btn-success-outline activity">报名参加
</button>
<div class="activity-detail text-center">
</div>
</div>
<% end %>
<% if micro_post["type"] == "失物招领" %>
<div class="social-button">
<button class="btn btn-success-outline lost">跟ta联系
</button>
</div>
<% if micro_post_belong_to_user(micro_post["postid"], @user) == false %>
<% if micro_post["type"] == "组团信息" %>
<div class="social-button">
<% if joinded_activity(micro_post["postid"], @user.name) %>
<button class="btn btn-lg btn-success-outline activity">取消报名</button>
<div class="activity-detail text-center"></div>
<% else %>
<button class="btn btn-lg btn-success-outline activity">报名参加</button>
<div class="activity-detail text-center"></div>
<% end %>
</div>
<% end %>
<% if micro_post["type"] == "失物招领" %>
<div class="social-button">
<button class="btn btn-success-outline lost">跟ta联系
</button>
</div>
<% end %>
<% end %>
</div>
<div class="comment-button">
@ -185,7 +190,7 @@
url: '/activity',
success: function (data, textStatus, jqXHR) {
activity.text("报名参加");
micropost.find(".activity-detail").text("");
micropost.find(".activity-detail").text("成功取消报名");
}
});
}

Binary file not shown.

View File

@ -1,120 +0,0 @@
<HTML>
<HEAD>
<script type="text/javascript">
var i = 0; // 用来动态生成span,upfile的id
function addAttachmentToList()
{
if (findAttachment(event.srcElement.value)) return; //如果此文档已在图片列表中则不再添加
// 动态创建图片信息栏并添加到图片列表中
var span = document.createElement('span');
span.id = '_attachment' + i;
span.innerHTML = extractFileName(event.srcElement.value) + '&nbsp;<a href="javascript:delAttachment(' + (i++) + ')"><font color="blue">删除</font></a><br/>';
span.title = event.srcElement.value;
G('attachmentList').appendChild(span);
// 显示图片列表并变换添加图片按钮文本
if (G('attachmentList').style.display == 'none')
{
G('btnAdd').value = '继续添加';
G('attachmentList').style.display = '';
G('btnClear').style.display = '';
}
G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个图片';
}
function selectAttachment()
{
// 先清除无效动态生成的多余upfile
cleanInvalidUpfile();
// 动态创建上传控件并与span对应
var upfile = '<input type="file" style="display:none" onchange="addAttachmentToList();" id="_upfile'+i+'">';
document.body.insertAdjacentHTML('beforeEnd', upfile);
G('_upfile'+i).click();
}
function extractFileName(fn)
{
return fn.substr(fn.lastIndexOf('\\')+1);
}
function findAttachment(fn)
{
var o = G('attachmentList').getElementsByTagName('span');
for(var i=0;i<o.length;i++)
if (o[i].title == fn) return true;
return false;
}
function delAttachment(id)
{
G('attachmentList').removeChild(G('_attachment'+id));
document.body.removeChild(G('_upfile'+id));
// 当图片列表为空则不显示并且变化添加图片按钮文本
if (G('attachmentList').childNodes.length == 0)
{
G('btnAdd').value = '添加图片';
G('attachmentList').style.display = 'none';
G('btnClear').style.display = 'none';
}
G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个图片';
}
function cleanInvalidUpfile()
{
var o = document.body.getElementsByTagName('input');
for(var i=o.length-1;i>=0;i--)
if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
{
if (!G('_attachment'+o[i].id.substr(7)))
document.body.removeChild(o[i]);
}
}
function clearAttachment()
{
var o = G('attachmentList').childNodes;
for(var i=o.length-1;i>=0;i--)
G('attachmentList').removeChild(o[i]);
o = document.body.getElementsByTagName('input');
for(var i=o.length-1;i>=0;i--)
if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
{
document.body.removeChild(o[i]);
}
G('btnAdd').value = '添加图片';
G('attachmentList').style.display = 'none';
G('btnClear').style.display = 'none';
G('total').innerText = '当前选择上传0个图片';
}
function getAttachmentInfo()
{
// 已知的js获取本地文件大小的三种方式
}
function G(id)
{
return document.getElementById(id);
}
</script>
</HEAD>
<BODY>
<form action="#">
<input type="button" value="添加图片" id="btnAdd" onclick="selectAttachment();">&nbsp;
<input type="button" value="清空图片" id="btnClear" style="display:none" onclick="clearAttachment();">
<div id="attachmentList" style="margin:3px 0px 0px 0px;padding:4px 3px 4px 3px;display:none;border:1px solid "></div>
<div id="total" style="margin:3px 0px 0px 0px;">当前选择上传0个图片</div>
<div style="padding10px 3px"><input type="submit" value="上传"></div>
</form>
</BODY>
</HTML>

View File

@ -12,6 +12,8 @@
# 朱京乔
------------------------
多图上传 朱京乔
多图上传bug
传同样的照片 虽然链接上不显示 但是传了2次
# 尹吉宪
------------------------