/** * ajax * @param url 链接地址 * @param data 请求数据 * @param callback 回调方法 * @param isAsync 是否异步 * @constructor */ function AjaxPost(url = '', data = {}, callback, isAsync = false) { let loadIndex = layer.load(2); $.ajax({ type: "POST", dataType: "json", url: url, data: data, success: function (result) { layer.close(loadIndex); if (result.code === -1) { isLoginPop(); return; } callback(result); }, async: isAsync, error: function () { layer.close(loadIndex); layer.msg('请求错误!', {icon: 7}); }, // complete: function(){ // layer.close(loadIndex); // } }); } function AjaxPostNoLode(url = '', data = {}, callback, isAsync = false) { $.ajax({ type: "POST", dataType: "json", url: url, data: data, success: function (result) { if (result.code === -1) { isLoginPop(); return; } callback(result); }, async: isAsync, error: function () { layer.msg('请求错误!', {icon: 7}); }, }); } /** * 表单ajax * @param url 链接地址 * @param data 请求数据 * @param callback 回调方法 * @param isAsync 是否异步 * @constructor */ function AjaxPostForm(url = '', data = {}, callback, isAsync = false) { let loadIndex = layer.load(2); $.ajax({ type: "POST", dataType: "json", url: url, data: data, processData: false, contentType: false, success: function (result) { layer.close(loadIndex); if (result.code === -1) { isLoginPop(); return; } callback(result); }, async: isAsync, error: function () { layer.close(loadIndex); layer.msg('请求错误!', {icon: 7}); }, // complete: function(){ // layer.close(loadIndex); // } }); } // 收藏帖子 function isCollection(dom,id = 0) { let isResult = ''; if ($(dom).hasClass('on')) { isResult = cancelCollection(id); } else { isResult = Collection(id); } return isResult; } // 收藏 function Collection(id = 0) { if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/collection/create', isResult = ''; new AjaxPost(ajaxUrl, {id: id}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 取消收藏 function cancelCollection(id = 0) { if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/collection/delete', isResult = ''; new AjaxPost(ajaxUrl, {id: id}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 关注 function isAttention(dom, uid = 0) { let isResult = '', isText = ''; if ($(dom).hasClass('on')) { isResult = cancelAttention(dom, uid); isText = '关注'; } else { isResult = Attention(dom, uid); isText = '已关注'; } if (isResult) { $(dom).toggleClass('on').text(isText); } return isResult; } // 关注 function Attention(dom, uid = 0) { if (uid === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/attention/create', isResult = ''; new AjaxPost(ajaxUrl, {id: uid}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 取消关注 function cancelAttention(dom, uid = 0) { if (uid === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/attention/delete', isResult = ''; new AjaxPost(ajaxUrl, {id: uid}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 举报帖子 function isReportQuestion(id = 0, type = 'question') { if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/report/create', isResult = ''; new AjaxPost(ajaxUrl, {id: id, type: type}, function (result) { if (result.code === 1) { // layer.msg(result.msg, {}, function () { // window.location.href = ''; // }); $('.confirm_jb').slideDown(200); setTimeout(function () { $('.confirm_jb').slideUp(200); }, 2000); isResult = true; } else { layer.msg(result.msg); isResult = false; } } ); return isResult; } // function sd() { // $('.jb_cur_a .a1').click(function () { // $('.curtain7').hide(); // $('.curtain9').hide(); // $('.confirm_jb').slideDown(200); // setTimeout(function () { // $('.confirm_jb').slideUp(200); // }, 2000); // }); // } // 举报评论 function isReportComment(dom, type = 'comment') { let id = $(dom).data('id'); if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/report/create', isResult = ''; new AjaxPost(ajaxUrl, {id: id, type: type}, function (result) { if (result.code === 1) { // layer.msg(result.msg, {}, function () { // window.location.href = ''; // }); isResult = true; } else { layer.msg(result.msg); isResult = false; } } ); return isResult; } // 采纳评论 function isAdopt(question_id = 0, best_answer_id = 0, type = 'comment') { if (question_id === 0 || best_answer_id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/question/adopt', isResult = ''; new AjaxPost(ajaxUrl, {question_id: question_id, best_answer_id: best_answer_id}, function (result) { if (result.code === 1) { $('.comment_iconn').addClass('on'); layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } } ); return isResult; } // 关注 function isVote(dom, id = 0, type = 'comment') { let isResult = '', isText = ''; if ($(dom).hasClass('on')) { isResult = cancelVoteUp(id, type); // isText = '关注'; } else { isResult = voteUp(id, type); // isText = '已关注'; } // if (isResult) { // $(dom).toggleClass('on').text(isText); // } return isResult; } // 点赞 function voteUp(id = 0, type = 'comment', value = 'up') { if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/vote/create', isResult = ''; new AjaxPost(ajaxUrl, {id: id, type: type, value: value}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 取消点赞 function cancelVoteUp(id = 0, type = 'comment', value = 'up') { if (id === 0) { layer.msg('参数错误!'); return false; } let ajaxUrl = '/addons/ask/vote/delete', isResult = ''; new AjaxPost(ajaxUrl, {id: id, type: type, value: value}, function (result) { if (result.code === 1) { layer.msg(result.msg); isResult = true; } else { layer.msg(result.msg); isResult = false; } }); return isResult; } // 评论 function isComment(formData) { new AjaxPostForm('/addons/ask/comment/post', formData, function (result) { if (result.code === 1) { layer.msg(result.msg, {}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); } // 申诉 function isAppeal(formData) { new AjaxPostForm('/addons/ask/question/appeal', formData, function (result) { if (result.code === 1) { layer.msg(result.msg, {}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); } // 设置已读 function isSetRead() { new AjaxPost('', {isread: 1}, function (result) { if (result.code === 1) { // layer.msg(result.msg, {time: 100}, function () { window.location.href = ''; // }); } else { layer.msg(result.msg); } }); } // 设置已读 function isDelete(dom, nid = 0) { if (nid === 0) { layer.msg('参数错误!'); return false; } new AjaxPost('', {isdelete: 1, nid: nid}, function (result) { if (result.code === 1) { layer.msg(result.msg, {time: 1000}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); } // 获取私信列表 function getMsgList(id = 0, listBox) { new AjaxPost('/addons/ask/message/getList', {id: id}, function (result) { let ht = ''; if (result.data) { for (let i = 0; i < result.data.length; i++) { ht += `

${result.data[i].content}

`; } } $(listBox).html(ht); }); } $('.isWechatOn').click(function () { let timeIng, _this = $('#isWechatBox'); if (_this.data('key') && _this.data('url')) { let key = _this.data('key'), url = _this.data('url'); $('#isWechatBox .wechatCancel').click(function () { clearInterval(timeIng); }); $('#isWechatBox .wechatQr').html('').qrcode(url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/user/ticket?key=' + key, '', function (result2) { if (result2.data.ticket) { clearInterval(timeIng); new AjaxPostNoLode(result2.data.url, { ticket: result2.data.ticket, __token__: result2.data.token }, function (result3) { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').find('.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } }); }, 1000); } else { new AjaxPostNoLode('/addons/mplogin/user/key', '', function (result) { if (result.data.url && result.data.key) { _this.data('key', result.data.key).data('url', result.data.url).find('.wechatMsg').html(''); $('#isWechatBox .wechatCancel').click(function () { clearInterval(timeIng); }); $('#isWechatBox .wechatQr').html('').qrcode(result.data.url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/user/ticket?key=' + result.data.key, '', function (result2) { if (result2.data.ticket) { clearInterval(timeIng); new AjaxPostNoLode(result2.data.url, { ticket: result2.data.ticket, __token__: result2.data.token }, function (result3) { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').find('.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } // clearInterval(tiem); }); }, 1000); } }); } return false; }); $('#isWechatBind').click(function () { let timeIng, _this = $('#wxBox'); if (_this.data('key') && _this.data('url')) { let key = _this.data('key'), url = _this.data('url'); $('#wxBindClose').click(function () { clearInterval(timeIng); }); $('#wxBox').html('').qrcode(url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/user/ticket?key=' + key, '', function (result2) { if (result2.data.ticket) { clearInterval(timeIng); layer.msg('绑定成功!', {time: 1000}, function () { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').next('p.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } }); }, 1000); } else { new AjaxPostNoLode('/addons/mplogin/user/key', '', function (result) { if (result.data.url && result.data.key) { _this.data('key', result.data.key).data('url', result.data.url).next('p.wechatMsg').html('扫码绑定'); $('#wxBindClose').click(function () { clearInterval(timeIng); }); $('#wxBox').html('').qrcode(result.data.url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/user/ticket?key=' + result.data.key, '', function (result2) { if (result2.state === 2) { clearInterval(timeIng); layer.msg('绑定成功!', {time: 1000}, function () { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').next('p.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } }); }, 1000); } }); } return false; }); // 微信解绑 function unBindWx() { new AjaxPost('/index/user/unbind_wx', {}, function (result) { if (result.code === 1) { layer.msg(result.msg, {time: 1000}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); } function isLogin(e) { // layer.msg('请登录后在查看!',{time: 7}); $('.curtain11').show(); $('body').css('overflow-y', 'hidden'); } // 登录弹窗 $('.nav_login').click(function () { $('.curtain2').addClass('on'); $('.curtain_box').addClass('on'); }); $('.mask,.close_tc').click(function () { $('.curtain2').removeClass('on'); $('.curtain_box').removeClass('on'); }); xxkc('.login_dl_form', '.login_dl_nav a', 'on'); function tjl(e, d) { $(e).click(function () { var thi = $(this); thi.parent().toggleClass(d); view = thi.parent().find('input').attr('type'); if (view == 'password') { thi.parent().find('input').attr("type", "text"); } else { thi.parent().find('input').attr("type", "password"); } }); } tjl('.login_icon', 'on'); // 登录 $('#login-form #login').click(function () { let url = '/index/user/login.html', data = new FormData($('#login-form')[0]); new AjaxPostForm(url, data, function (result) { if (result.code === 1) { layer.msg(result.msg, {time: 1000}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); }); // 验证码登录 $('#login-captcha-form #login-captcha').click(function () { let url = '/index/user/captcha_login.html', data = new FormData($('#login-captcha-form')[0]); new AjaxPostForm(url, data, function (result) { if (result.code === 1) { layer.msg(result.msg, {time: 1000}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); }); // 验证码登录 验证码 var iscode = true; $('#login-captcha-form .isLoginCaptcha').click(function () { if (iscode) { new AjaxPost('/api/sms/send', { event: 'login', mobile: $("#login-captcha-form .isLoginMobile").val() }, function (result) { if (result.code === 1) { layer.msg(result.msg, {icon: 1}); iscode = false; var time1 = 60; var code = $(".isLoginCaptcha"); setTimeout(function () { code.css("opacity", "0.6"); }, 1000) var set = setInterval(function () { code.html("(" + --time1 + ")秒后重新获取"); }, 1000); setTimeout(function () { code.html("重新获取验证码"); clearInterval(set); iscode = true; }, 60000); } else { layer.msg(result.msg, {icon: 7}); } }); } }); // 注册 $('#register-form #register').click(function () { let url = '/index/user/register.html', data = new FormData($('#register-form')[0]); new AjaxPostForm(url, data, function (result) { if (result.code === 1) { layer.msg(result.msg, {time: 1000}, function () { window.location.href = ''; }); } else { layer.msg(result.msg); } }); }); // 注册验证码 var iscode2 = true; $('#register-form .isRegisterCaptcha').click(function () { if (iscode2) { new AjaxPost('/api/sms/send', { event: 'register', mobile: $("#register-form .isRegisterMobile").val() }, function (result) { if (result.code === 1) { layer.msg(result.msg, {icon: 1}); iscode2 = false; var time1 = 60; var code = $(".isRegisterCaptcha"); setTimeout(function () { code.css("opacity", "0.6"); }, 1000) var set = setInterval(function () { code.html("(" + --time1 + ")秒后重新获取"); }, 1000); setTimeout(function () { code.html("重新获取验证码"); clearInterval(set); iscode2 = true; }, 60000); } else { layer.msg(result.msg, {icon: 7}); } }); } }); function isLoginPop() { $('.curtain2').addClass('on'); $('.curtain_box').addClass('on'); } // 开发平台微信登录 $('#isSettledWechatOn').click(function () { let timeIng, _this = $('#isSettledWechatBox'); if (_this.data('key') && _this.data('url')) { let key = _this.data('key'), url = _this.data('url'); $('#isSettledWechatBox .wechatCancel').click(function () { clearInterval(timeIng); }); $('#isSettledWechatBox .wechatQr').html('').qrcode(url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/settled/ticket?key=' + key, '', function (result2) { if (result2.data.ticket) { clearInterval(timeIng); new AjaxPostNoLode(result2.data.url, { ticket: result2.data.ticket, __token__: result2.data.token }, function (result3) { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').find('.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } }); }, 1000); } else { new AjaxPostNoLode('/addons/mplogin/settled/key', '', function (result) { if (result.data.url && result.data.key) { _this.data('key', result.data.key).data('url', result.data.url).find('.wechatMsg').html(''); $('#isSettledWechatBox .wechatCancel').click(function () { clearInterval(timeIng); }); $('#isSettledWechatBox .wechatQr').html('').qrcode(result.data.url); timeIng = setInterval(function () { new AjaxPostNoLode('/addons/mplogin/settled/ticket?key=' + result.data.key, '', function (result2) { if (result2.data.ticket) { clearInterval(timeIng); new AjaxPostNoLode(result2.data.url, { ticket: result2.data.ticket, __token__: result2.data.token }, function (result3) { window.location.href = ''; }); } else if (result2.state === -1) { _this.data('key','').data('url', '').find('.wechatMsg').html('二维码已过期'); clearInterval(timeIng); } // clearInterval(tiem); }); }, 1000); } }); } return false; });