This commit is contained in:
2017-12-02 16:17:25 +08:00
parent 35e6fa140e
commit a34539ef16
194 changed files with 6976 additions and 0 deletions

9
root/res/mods/face.js Normal file

File diff suppressed because one or more lines are too long

422
root/res/mods/index.js Normal file
View File

@@ -0,0 +1,422 @@
/**
@Name: Fly社区主入口
*/
layui.define(['layer', 'laytpl', 'form', 'upload', 'util', 'upload','face','element'], function(exports){
var $ = layui.jquery
,layer = layui.layer
,laytpl = layui.laytpl
,form = layui.form()
,util = layui.util
,device = layui.device()
//阻止IE7以下访问
if(device.ie && device.ie < 8){
layer.alert('如果您非得使用ie浏览Fly社区那么请使用ie8+');
}
layui.focusInsert = function(obj, str){
var result, val = obj.value;
obj.focus();
if(document.selection){ //ie
result = document.selection.createRange();
document.selection.empty();
result.text = str;
} else {
result = [val.substring(0, obj.selectionStart), str, val.substr(obj.selectionEnd)];
obj.focus();
obj.value = result.join('');
}
};
var gather = {
//Ajax
json: function(url, data, success, options){
var that = this;
options = options || {};
data = data || {};
return $.ajax({
type: options.type || 'post',
dataType: options.dataType || 'json',
data: data,
url: url,
success: function(res){
if(res.status === 0) {
success && success(res);
} else {
layer.msg(res.msg||res.code, {shift: 6});
}
}, error: function(e){
options.error || layer.msg('请求异常,请重试', {shift: 6});
}
});
}
//将普通对象按某个key排序
,sort: function(data, key, asc){
var obj = JSON.parse(JSON.stringify(data));
var compare = function (obj1, obj2) {
var value1 = obj1[key];
var value2 = obj2[key];
if (value2 < value1) {
return -1;
} else if (value2 > value1) {
return 1;
} else {
return 0;
}
};
obj.sort(compare);
if(asc) obj.reverse();
return obj;
}
//计算字符长度
,charLen: function(val){
var arr = val.split(''), len = 0;
for(var i = 0; i < val.length ; i++){
arr[i].charCodeAt(0) < 299 ? len++ : len += 2;
}
return len;
}
,form: {}
//简易编辑器
,layEditor: function(options){
var html = '<div class="fly-edit">'
+'<span type="face" title="插入表情"><i class="iconfont icon-biaoqing"></i>表情</span>'
+'<span type="picture" title="插入图片img[src]"><i class="iconfont icon-tupian"></i>图片</span>'
+'<span type="href" title="超链接格式a(href)[text]"><i class="iconfont icon-lianjie"></i>链接</span>'
+'<span type="code" title="插入代码"><i class="iconfont icon-daima"></i>代码</span>'
+'<span type="yulan" title="预览"><i class="iconfont icon-yulan"></i>预览</span>'
+'</div>';
var log = {}, mod = {
picture: function(editor){ //插入图片
layer.open({
type: 1
,id: 'fly-jie-upload'
,title: '插入图片'
,area: 'auto'
,shade: false
,area: '465px'
,skin: 'layui-layer-border'
,content: ['<ul class="layui-form layui-form-pane" style="margin: 20px;">'
,'<li class="layui-form-item">'
,'<label class="layui-form-label">URL</label>'
,'<div class="layui-input-inline">'
,'<input required name="image" placeholder="支持直接粘贴远程图片地址" value="" class="layui-input">'
,'</div>'
,'<input required type="file" name="file" class="layui-upload-file" value="">'
,'</li>'
,'<li class="layui-form-item" style="text-align: center;">'
,'<button type="button" lay-submit lay-filter="uploadImages" class="layui-btn">确认</button>'
,'</li>'
,'</ul>'].join('')
,success: function(layero, index){
var image = layero.find('input[name="image"]');
layui.upload({
url: '/os/file/upload'
,elem: '#fly-jie-upload .layui-upload-file'
,success: function(res){
if(res.retcode == 0){
image.val(res.retinfo);
} else {
layer.msg(res.msg, {icon: 5});
}
}
});
form.on('submit(uploadImages)', function(data){
var field = data.field;
if(!field.image) return image.focus();
layui.focusInsert(editor[0], 'img['+ field.image + '] ');
layer.close(index);
});
}
});
}
,face: function(editor, self){ //插入表情
var str = '', ul, face = gather.faces;
for(var key in face){
str += '<li title="'+ key +'"><img src="'+ face[key] +'"></li>';
}
str = '<ul id="LAY-editface" class="layui-clear">'+ str +'</ul>';
layer.tips(str, self, {
tips: 3
,time: 0
,skin: 'layui-edit-face'
});
$(document).on('click', function(){
layer.closeAll('tips');
});
$('#LAY-editface li').on('click', function(){
var title = $(this).attr('title') + ' ';
layui.focusInsert(editor[0], 'face' + title);
});
}
,href: function(editor){ //超链接
layer.prompt({
title: '请输入合法链接'
,shade: false
}, function(val, index, elem){
if(!/^http(s*):\/\/[\S]/.test(val)){
layer.tips('这根本不是个链接,不要骗我。', elem, {tips:1})
return;
}
layui.focusInsert(editor[0], ' a('+ val +')['+ val + '] ');
layer.close(index);
});
}
,code: function(editor){ //插入代码
layer.prompt({
title: '请贴入代码'
,formType: 2
,maxlength: 10000
,shade: false
,area: ['830px', '390px']
}, function(val, index, elem){
layui.focusInsert(editor[0], '[pre]\n'+ val + '\n[/pre]');
layer.close(index);
});
}
,yulan: function(editor){ //预览
var content = editor.val();
content = /^\{html\}/.test(content)
? content.replace(/^\{html\}/, '')
: gather.content(content);
layer.open({
type: 1
,title: '预览'
,area: ['100%', '100%']
,scrollbar: false
,content: '<div class="detail-body" style="margin:20px;">'+ content +'</div>'
});
}
};
layui.use('face', function(face){
options = options || {};
gather.faces = face;
$(options.elem).each(function(index){
var that = this, othis = $(that), parent = othis.parent();
parent.prepend(html);
parent.find('.fly-edit span').on('click', function(event){
var type = $(this).attr('type');
mod[type].call(that, othis, this);
if(type === 'face'){
event.stopPropagation()
}
});
});
});
}
,escape: function(html){
return String(html||'').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
}
//内容转义
,content: function(content){
//支持的html标签
var html = function(end){
return new RegExp('\\['+ (end||'') +'(pre|div|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)\\]\\n*', 'g');
};
content = gather.escape(content||'') //XSS
.replace(/img\[([^\s]+?)\]/g, function(img){ //转义图片
return '<img src="' + img.replace(/(^img\[)|(\]$)/g, '') + '">';
}).replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;" class="fly-aite">$1</a>$2') //转义@
.replace(/face\[([^\s\[\]]+?)\]/g, function(face){ //转义表情
var alt = face.replace(/^face/g, '');
return '<img alt="'+ alt +'" title="'+ alt +'" src="' + gather.faces[alt] + '">';
}).replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function(str){ //转义链接
var href = (str.match(/a\(([\s\S]+?)\)\[/)||[])[1];
var text = (str.match(/\)\[([\s\S]*?)\]/)||[])[1];
if(!href) return str;
var rel = /^(http(s)*:\/\/)\b(?!(\w+\.)*(sentsin.com|layui.com))\b/.test(href.replace(/\s/g, ''));
return '<a href="'+ href +'" target="_blank"'+ (rel ? ' rel="nofollow"' : '') +'>'+ (text||href) +'</a>';
}).replace(html(), '\<$1\>').replace(html('/'), '\</$1\>') //转移代码
.replace(/\n/g, '<br>') //转义换行
return content;
}
//新消息通知
,newmsg: function(){
if(layui.cache.user.uid !== -1){
gather.json('/message/nums/', {
_: new Date().getTime()
}, function(res){
if(res.status === 0 && res.count > 0){
var msg = $('<a class="nav-message" href="javascript:;" title="您有'+ res.count +'条未阅读的消息">'+ res.count +'</a>');
$('.nav-user').append(msg);
msg.on('click', function(){
gather.json('/message/read', {}, function(res){
if(res.status === 0){
location.href = '/user/message/';
}
});
});
}
});
}
return arguments.callee;
}
,cookie: function(e,o,t){
e=e||"";var n,i,r,a,c,p,s,d,u;if("undefined"==typeof o){if(p=null,document.cookie&&""!=document.cookie)for(s=document.cookie.split(";"),d=0;d<s.length;d++)if(u=$.trim(s[d]),u.substring(0,e.length+1)==e+"="){p=decodeURIComponent(u.substring(e.length+1));break}return p}t=t||{},null===o&&(o="",t.expires=-1),n="",t.expires&&("number"==typeof t.expires||t.expires.toUTCString)&&("number"==typeof t.expires?(i=new Date,i.setTime(i.getTime()+864e5*t.expires)):i=t.expires,n="; expires="+i.toUTCString()),r=t.path?"; path="+t.path:"",a=t.domain?"; domain="+t.domain:"",c=t.secure?"; secure":"",document.cookie=[e,"=",encodeURIComponent(o),n,r,a,c].join("");
}
};
//相册
/* layer.photos({
photos: '.photos'
,zIndex: 9999999999
,anim: -1
});*/
//相册
if($(window).width() > 750){
layer.photos({
photos: '.photos'
,zIndex: 9999999999
,anim: -1
});
} else {
$('body').on('click', '.photos img', function(){
window.open(this.src);
});
}
//搜索
$('.fly-search').submit(function(){
var input = $(this).find('input'), val = input.val();
if(val.replace(/\s/g, '') === ''){
return false;
}
input.val(/*'site:1216.top '+ */input.val());
});
$('.icon-sousuo').on('click', function(){
$('.fly-search').submit();
});
//退出登录
$(".logout").on('click', function () {
$.post("/os/user/logout",{},function (data) {
location.reload();
});
});
//新消息通知
gather.newmsg();
//发送激活邮件
gather.activate = function(email){
gather.json('/api/activate/', {}, function(res){
if(res.status === 0){
layer.alert('已成功将激活链接发送到了您的邮箱,接受可能会稍有延迟,请注意查收。', {
icon: 1
});
};
});
};
$('#LAY-activate').on('click', function(){
gather.activate($(this).attr('email'));
});
//点击@
$('body').on('click', '.fly-aite', function(){
var othis = $(this), text = othis.text();
if(othis.attr('href') !== 'javascript:;'){
return;
}
text = text.replace(/^@|[\s\S]+?/g, '');
othis.attr({
href: '/user/nick?nickname='+ text
,target: '_blank'
});
});
//表单提交
form.on('submit(*)', function(data){
var action = $(data.form).attr('action'), button = $(data.elem);
gather.json(action, data.field, function(res){
var end = function(){
if(res.action){
location.href = res.action;
} else {
gather.form[action||button.attr('key')](data.field, data.form);
}
};
if(res.status == 0){
button.attr('alert') ? layer.alert(res.msg, {
icon: 1,
time: 10*1000,
end: end
}) : end();
};
});
return false;
});
//加载特定模块
if(layui.cache.page && layui.cache.page !== 'index'){
var extend = {};
extend[layui.cache.page] = layui.cache.page;
layui.extend(extend);
layui.use(layui.cache.page);
}
//加载IM
if(!device.android && !device.ios){
//layui.use('im');
}
//加载编辑器
gather.layEditor({
elem: '.fly-editor'
});
//右下角固定Bar
util.fixbar({
bar1: false
,click: function(type){
if(type === 'bar1'){
layer.msg('bar1');
}
}
});
//手机设备的简单适配
var treeMobile = $('.site-tree-mobile')
,shadeMobile = $('.site-mobile-shade')
treeMobile.on('click', function(){
$('body').addClass('site-mobile');
});
shadeMobile.on('click', function(){
$('body').removeClass('site-mobile');
});
//图片懒加载
layui.use('flow', function(flow){
flow.lazyimg();
});
exports('fly', gather);
});

278
root/res/mods/jie.js Normal file
View File

@@ -0,0 +1,278 @@
/**
@Name: 求解板块
*/
layui.define(['laypage', 'fly', 'form'], function(exports){
var $ = layui.jquery;
var layer = layui.layer;
var util = layui.util;
var laytpl = layui.laytpl;
var form = layui.form();
var fly = layui.fly;
var laypage = layui.laypage;
var gather = {}, dom = {
jieda: $('#jieda')
,content: $('#L_content')
,jiedaCount: $('#jiedaCount')
};
//提交回答
fly.form['/jie/reply/'] = function(data, required){
var tpl = '<li>\
<div class="detail-about detail-about-reply">\
<a class="jie-user" href="/user/">\
<img src="{{= d.user.avatar}}" alt="{{= d.user.username}}">\
<cite>{{d.user.username}}</cite>\
</a>\
<div class="detail-hits">\
<span>刚刚</span>\
</div>\
</div>\
<div class="detail-body jieda-body">\
{{ d.content}}\
</div>\
</li>'
data.content = fly.content(data.content);
laytpl(tpl).render($.extend(data, {
user: layui.cache.user
}), function(html){
required[0].value = '';
dom.jieda.find('.fly-none').remove();
dom.jieda.append(html);
var count = dom.jiedaCount.text()|0;
dom.jiedaCount.html(++count);
});
};
//求解管理
gather.jieAdmin = {
//删求解
del: function(div){
layer.confirm('确认删除该求解么?', function(index){
layer.close(index);
fly.json('/api/jie-delete/', {
id: div.data('id')
}, function(res){
if(res.status === 0){
location.href = '/jie/';
} else {
layer.msg(res.msg);
}
});
});
}
//设置置顶、状态
,set: function(div){
var othis = $(this);
fly.json('/api/jie-set/', {
id: div.data('id')
,rank: othis.attr('rank')
,field: othis.attr('field')
}, function(res){
if(res.status === 0){
location.reload();
}
});
}
//收藏
,collect: function(div){
var othis = $(this), type = othis.data('type');
$.post('/os/content/collect', {
contentId: div.data('id')
,ok: type === 'add'? 1:-1
}, function(res){
res = JSON.parse(res);
console.log(res);
if(res.retcode == 0){
if(type === 'add'){
othis.data('type', 'remove').html('取消收藏').addClass('layui-btn-danger');
layer.msg("收藏成功");
} else if(type === 'remove'){
othis.data('type', 'add').html('收藏').removeClass('layui-btn-danger');
layer.msg("已取消收藏");
}
}else {
layer.msg(res.retinfo);
}
});
}
};
$('body').on('click', '.jie-admin', function(){
var othis = $(this), type = othis.attr('type');
gather.jieAdmin[type].call(this, othis.parent());
});
//异步渲染
var asyncRender = function(){
var div = $('.fly-detail-hint'), jieAdmin = $('#LAY_jieAdmin');
//查询帖子是否收藏
if(jieAdmin[0] && layui.cache.user.uid != -1){
fly.json('/collection/find/', {
cid: div.data('id')
}, function(res){
jieAdmin.append('<span class="layui-btn layui-btn-mini jie-admin '+ (res.data.collection ? 'layui-btn-danger' : '') +'" type="collect" data-type="'+ (res.data.collection ? 'remove' : 'add') +'">'+ (res.data.collection ? '取消收藏' : '收藏') +'</span>');
});
}
}();
//解答操作
gather.jiedaActive = {
zan: function(li){ //赞
var othis = $(this), ok = othis.hasClass('zanok');
$.post('/os/comment/support', {
ok: ok?-1:1
,commentId: li.data('id')
}, function(res){
res = JSON.parse(res);
console.log(res);
if(res.retcode === 0){
var zans = othis.find('em').html()|0;
othis[ok ? 'removeClass' : 'addClass']('zanok');
othis.find('em').html(ok ? (--zans) : (++zans));
} else {
layer.msg(res.retinfo);
}
});
}
,reply: function(li){ //回复
var val = dom.content.val();
var aite = '@'+ li.find('.jie-user cite i').text().replace(/\s/g, '');
dom.content.focus()
if(val.indexOf(aite) !== -1) return;
dom.content.val(aite +' ' + val);
$("input[name='pid']").val(li.data('id'));
}
,accept: function(li){ //采纳
var othis = $(this);
layer.confirm('是否采纳该回答为最佳答案?', function(index){
layer.close(index);
fly.json('/api/jieda-accept/', {
id: li.data('id')
}, function(res){
if(res.status === 0){
$('.jieda-accept').remove();
li.addClass('jieda-daan');
li.find('.detail-about').append('<i class="iconfont icon-caina" title="最佳答案"></i>');
} else {
layer.msg(res.msg);
}
});
});
}
,edit: function(li){ //编辑
fly.json('/jie/getDa/', {
id: li.data('id')
}, function(res){
var data = res.rows;
layer.prompt({
formType: 2
,value: data.content
,maxlength: 100000
}, function(value, index){
fly.json('/jie/updateDa/', {
id: li.data('id')
,content: value
}, function(res){
layer.close(index);
li.find('.detail-body').html(fly.content(value));
});
});
});
}
,del: function(li){ //删除
layer.confirm('确认删除该回答么?', function(index){
layer.close(index);
fly.json('/api/jieda-delete/', {
id: li.data('id')
}, function(res){
if(res.status === 0){
var count = dom.jiedaCount.text()|0;
dom.jiedaCount.html(--count);
li.remove();
//如果删除了最佳答案
if(li.hasClass('jieda-daan')){
$('.jie-status').removeClass('jie-status-ok').text('求解中');
}
} else {
layer.msg(res.msg);
}
});
});
}
};
$('.jieda-reply span').on('click', function(){
var othis = $(this), type = othis.attr('type');
gather.jiedaActive[type].call(this, othis.parents('li'));
});
form.on('submit(jie-add)', function(data){
var bean = {};
["contentId","title", "content", "cate"].forEach(function (value) {
bean[value] = data.field[value];
});
console.log(bean);
$.post("/os/content/save",{
bean:JSON.stringify(bean)
},function (res) {
res = JSON.parse(res);
if(res.retcode != 0){
layer.msg(res.retinfo);
return false;
}
layer.msg("发布成功",{time:2000},function () {
location.href = "/";
});
});
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
form.on('submit(jie-reply)', function(data){
var bean = {};
["contentId","pid", "content"].forEach(function (value) {
bean[value] = data.field[value];
});
console.log(bean);
$.post("/os/comment/save",{
bean:JSON.stringify(bean)
},function (res) {
res = JSON.parse(res);
if(res.retcode != 0){
layer.msg(res.retinfo);
return false;
}
layer.msg("回复成功",{time:2000},function () {
//location.href = "/";
location.reload();
});
});
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
$("."+ layui.cache.actived).addClass("tab-this");
if(layui.cache.curr){
layui.laypage({
cont:"jie-laypage"
,curr:layui.cache.curr
,pages: parseInt(layui.cache.total/15) + (layui.cache.total%15 > 0 ? 1:0)
,jump: function(obj, first){
var curr = obj.curr;
if(!first)
location.href=layui.cache.url+"?curr="+curr;
}
});
}
exports('jie', null);
});

388
root/res/mods/user.js Normal file
View File

@@ -0,0 +1,388 @@
/**
@Name: 用户模块
*/
layui.define(['laypage', 'fly','form', 'element'], function(exports){
var $ = layui.jquery;
var layer = layui.layer;
var util = layui.util;
var laytpl = layui.laytpl;
var form = layui.form();
var laypage = layui.laypage;
var fly = layui.fly;
var element = layui.element();
var gather = {}, dom = {
mine: $('#LAY_mine')
,mineview: $('.mine-view')
,minemsg: $('#LAY_minemsg')
,infobtn: $('#LAY_btninfo')
};
//我的相关数据
var elemUC = $('#LAY_uc'), elemUCM = $('#LAY_ucm');
gather.minelog = {};
gather.mine = function(index, type, url){
var tpl = [
//求解
'{{# for(var i = 0; i < d.rows.length; i++){ }}\
<li>\
{{# if(d.rows[i].collection_time){ }}\
<a class="jie-title" href="/jie/{{d.rows[i].id}}.html" target="_blank">{{= d.rows[i].title}}</a>\
<i>收藏于{{ d.rows[i].collection_time }}</i>\
{{# } else { }}\
{{# if(d.rows[i].status == 1){ }}\
<span class="fly-jing">精</span>\
{{# } }}\
{{# if(d.rows[i].accept >= 0){ }}\
<span class="jie-status jie-status-ok">已解决</span>\
{{# } }}\
<a class="jie-title" href="/jie/{{d.rows[i].id}}.html" target="_blank">{{= d.rows[i].title}}</a>\
<i>{{new Date(d.rows[i].time).toLocaleString()}}</i>\
{{# if(d.rows[i].accept == -1){ }}\
<a class="mine-edit" href="/jie/edit/{{d.rows[i].id}}">编辑</a>\
{{# } }}\
<em>{{d.rows[i].hits}}阅/{{d.rows[i].comment}}答</em>\
{{# } }}\
</li>\
{{# } }}'
];
var view = function(res){
var html = laytpl(tpl[0]).render(res);
dom.mine.children().eq(index).find('span').html(res.count);
elemUCM.children().eq(index).find('ul').html(res.rows.length === 0 ? '<div class="fly-msg">没有相关数据</div>' : html);
};
var page = function(now){
var curr = now || 1;
if(gather.minelog[type + '-page-' + curr]){
view(gather.minelog[type + '-page-' + curr]);
} else {
//我收藏的帖
if(type === 'collection'){
var nums = 10; //每页出现的数据量
fly.json(url, {}, function(res){
res.count = res.rows.length;
var rows = fly.sort(res.rows, 'collection_timestamp')
,render = function(curr){
var data = []
,start = curr*nums - nums
,last = start + nums - 1;
if(last >= rows.length){
last = curr > 1 ? rows.length - (last - rows.length) : rows.length - 1;
}
for(var i = start; i <= last; i++){
data.push(rows[i]);
}
res.rows = data;
view(res);
};
render(curr)
gather.minelog['collect-page-' + curr] = res;
now || laypage({
cont: 'LAY_page1'
,pages: Math.ceil(rows.length/nums) //得到总页数
,skin: 'fly'
,curr: curr
,jump: function(e, first){
if(!first){
render(e.curr);
}
}
});
});
} else {
fly.json('/api/'+ type +'/', {
page: curr
}, function(res){
view(res);
gather.minelog['mine-jie-page-' + curr] = res;
now || laypage({
cont: 'LAY_page'
,pages: res.pages
,skin: 'fly'
,curr: curr
,jump: function(e, first){
if(!first){
page(e.curr);
}
}
});
});
}
}
};
if(!gather.minelog[type]){
page();
}
};
if(elemUC[0]){
layui.each(dom.mine.children(), function(index, item){
var othis = $(item)
gather.mine(index, othis.data('type'), othis.data('url'));
});
}
//Hash地址的定位
var layid = location.hash.replace(/^#/, '');
element.tabChange('user', layid);
element.on('tab(user)', function(elem){
location.hash = ''+ $(this).attr('lay-id');
});
//根据ip获取城市
if($('#L_city').val() === ''){
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js', function(){
$('#L_city').val(remote_ip_info.city);
});
}
//上传图片
if($('.upload-img')[0]){
layui.use('upload', function(upload){
var avatarAdd = $('.avatar-add');
layui.upload({
elem: '.upload-img input'
,method: 'post'
,url: '/os/file/upload'
,before: function(){
avatarAdd.find('.loading').show();
}
,success: function(res){
//res = JSON.parse(res);
if(res.retcode == 0){
$.post('/os/user/update', {
bean:JSON.stringify({avatar: res.retinfo})
,columns:JSON.stringify(["avatar"])
}, function(res){
res = JSON.parse(res);
if(res.retcode != 0){
layer.msg(res.retinfo);
return false;
}
location.reload();
});
} else {
layer.msg(res.msg, {icon: 5});
}
avatarAdd.find('.loading').hide();
}
,error: function(){
avatarAdd.find('.loading').hide();
}
});
});
}
//提交成功后刷新
fly.form['set-mine'] = function(data, required){
layer.msg('修改成功', {
icon: 1
,time: 1000
,shade: 0.1
}, function(){
location.reload();
});
};
//表单验证
form.verify({
repass: function(value, item){ //value表单的值、item表单的DOM对象
var pass = $("input[name='pass']").val();
if (value != pass){
return "重复密码不一致";
}
}
});
//登录
form.on('submit(login)', function(data){
var fdata = data.field;
$.post("/os/user/login",{
bean:JSON.stringify({username:fdata.username, password:fdata.password})
,vercode:fdata.vercode
},function (data) {
data = JSON.parse(data);
if(data.retcode != 0){
layer.msg(data.retinfo);
return false;
}
location.href = "/";
});
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
form.on('submit(reg)', function(data){
data = data.field;
var bean = {
email:data.email
,password:data.pass
,nickname:data.nickname
};
$.post("/os/user/register",{
bean:JSON.stringify(bean)
},function (data) {
data = JSON.parse(data);
if(data.retcode != 0){
layer.msg(data.retinfo);
return false;
}
layer.msg("注册成功",{icon:16, shade: 0.1, time:0});
location.href = "/user/login";
});
return false; //阻止表单跳转。如果需要表单跳转,去掉这段即可。
});
form.on('submit(set-info)', function (data) {
var bean = {},columns = ['email', 'nickname', 'sex', 'city', 'sign'];
columns.forEach(function (value) {
bean[value] = data.field[value]
});
$.post("/os/user/update",{
bean:JSON.stringify(bean)
,columns:JSON.stringify(columns)
},function (res) {
res = JSON.parse(res);
if(res.retcode != 0){
layer.msg(res.retinfo);
return false;
}
location.reload();
});
return false;
});
form.on('submit(set-changepwd)', function (data) {
$.post("/os/user/changepwd",{
nowpass:data.field.nowpass
,pass:data.field.pass
},function (res) {
res = JSON.parse(res);
if(res.retcode != 0){
layer.msg(res.retinfo);
return false;
}
layer.msg('密码修改成功,重新登录', {
time: 2000 //2秒关闭如果不配置默认是3秒
}, function(){
$.post('/os/user/logout',{},function () {
location.reload();
});
});
});
return false;
});
//帐号绑定
$('.acc-unbind').on('click', function(){
var othis = $(this), type = othis.attr('type');
layer.confirm('整的要解绑'+ ({
qq_id: 'QQ'
,weibo_id: '微博'
})[type] + '吗?', {icon: 5}, function(){
fly.json('/api/unbind', {
type: type
}, function(res){
if(res.status === 0){
layer.alert('已成功解绑。', {
icon: 1
,end: function(){
location.reload();
}
});
} else {
layer.msg(res.msg);
}
});
});
});
//我的消息
gather.minemsg = function(){
var delAll = $('#LAY_delallmsg')
,tpl = '{{# var len = d.rows.length;\
if(len === 0){ }}\
<div class="fly-none">您暂时没有最新消息</div>\
{{# } else { }}\
<ul class="mine-msg">\
{{# for(var i = 0; i < len; i++){ }}\
<li data-id="{{d.rows[i].id}}">\
<blockquote class="layui-elem-quote">{{ d.rows[i].content}}</blockquote>\
<p><span>{{d.rows[i].time}}</span><a href="javascript:;" class="layui-btn layui-btn-small layui-btn-danger fly-delete">删除</a></p>\
</li>\
{{# } }}\
</ul>\
{{# } }}'
,delEnd = function(clear){
if(clear || dom.minemsg.find('.mine-msg li').length === 0){
dom.minemsg.html('<div class="fly-none">您暂时没有最新消息</div>');
}
}
// fly.json('/message/find/', {}, function(res){
// var html = laytpl(tpl).render(res);
// dom.minemsg.html(html);
// if(res.rows.length > 0){
// delAll.removeClass('layui-hide');
// }
// });
//阅读后删除
dom.minemsg.on('click', '.mine-msg li .fly-delete', function(){
var othis = $(this).parents('li'), id = othis.data('id');
layer.confirm('确定删除该消息吗?', function(index){
fly.json('/message/remove/', {
id: id
}, function(res){
if(res.status === 0){
othis.remove();
delEnd();
}
});
});
});
//删除全部
$('#LAY_delallmsg').on('click', function(){
var othis = $(this);
layer.confirm('确定清空吗?', function(index){
fly.json('/message/remove/', {
all: true
}, function(res){
if(res.status === 0){
layer.close(index);
othis.addClass('layui-hide');
delEnd(true);
}
});
});
});
};
dom.minemsg[0] && gather.minemsg();
exports('user', null);
});