var userAgent = window.localStorage.getItem(agentName);
var d = dialog({contentType:'load', skin:'bk-popup'});
var Request = GetRequest();
var openid = Request["openid"];
var dataId=null,scroller1=null,articleId='',type="";
var pagetype=50;
var toUser = Request["toUser"];
var toName = decodeURIComponent(Request["toName"]);
var represented = Request["represented"];
var doctorCode;
localStorage.setItem("toUser",toUser);
$(function(){
dataId = Request["dataId"];
articleId = Request.articleId || dataId;
type = Request.type;
window.localStorage.setItem("article",dataId);
article.closeWindow();
if(!userAgent) {
window.localStorage.setItem("toUser",toUser);
window.location.href = "../../home/html/login.html?openid=" + openid+"&type="+pagetype;
}else{
queryInit();
}
})
function queryInit(){
article.initPage();
}
var article={
initPage:function(){
article.initScroll();
var newUaObj = JSON.parse(window.localStorage.getItem(agentName))
//判断关系
function hasFamilyRelation(a,b){
return new Promise(function(resolve, reject) {
var data={
patient:a,
familyMember:b
}
sendPost("patient/family/is_authorize", data, "json", "get", function(res){
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求家庭成员关系失败'}).show();
}, function(res){
if(res.status == 200) {
window.localStorage.setItem('nowPatientName',res.data.name);
resolve(res)
}else if(res.status == 100) {
dialog({
content:'对不起,'+res.data.name+'未授权给您,如需查看,请切换'+res.data.name+'账号登录',
okValue:'切换账号',
ok: function() {
window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
window.location.href = "../../grzx/html/change-login.html?type=0&openid="+ Request["openid"];
},
cancelValue: '我不看了',
cancel: function () {
wx.closeWindow();
}
}).showModal();
}else{
relogin();
}
})
})
}
//重新登录操作
function relogin(){
dialog({
content:'对不起,该消息是发给'+toName+',如需查看,请切换'+toName+'账号登录',
okValue:'切换账号',
ok: function() {
window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
window.location.href = "../../grzx/html/change-login.html?type=0&openid="+ Request["openid"];
},
cancelValue: '我不看了',
cancel: function () {
wx.closeWindow();
}
}).showModal();
}
//保存信息
function saveUserInfo(a,b){
Promise.all([hasFamilyRelation(a,b)]).then(function () {
newUaObj.represented = represented;
window.localStorage.setItem(agentName,JSON.stringify(newUaObj));
userAgent = window.localStorage.getItem(agentName);
article.initData();
article.bindEvents();
})
}
function initFamilyData () {
//从微信模板消息进入
if(represented && userAgent && toUser){
if(toUser == represented){
saveUserInfo(represented,newUaObj.uid)
}else if(toUser != represented && represented == newUaObj.uid){
//本人代理
saveUserInfo(represented,newUaObj.uid)
}else if(toUser != represented && toUser == newUaObj.uid){
//需要判定关系 200有授权 100家人 1无关系
saveUserInfo(represented,newUaObj.uid)
}else{
//重新登录
relogin();
}
}else{
article.initData();
article.bindEvents();
}
}
initFamilyData();
},
initData:function(){
d.show();
var params = {};
params.articleId = articleId;
params.userType = '2';
sendPost("/third/jkEdu/Article/getArticalById",params,"JSON","GET",article.queryFailed,article.querySuccess);
// 更新浏览量
sendPost("patient/jkEdu/article/saveBehavior",{
articleId: articleId,
behaviorAction: 4
},"JSON","POST",function fail() {
dialog({contentType:'tipsbox', skin:'bk-popup' , content: "浏览量更新失败",bottom:true}).show();
},function success(res) {
if(res.status !=200){
dialog({contentType:'tipsbox', skin:'bk-popup' , content: res.msg || "浏览量更新失败",bottom:true}).show();
}
});
},
initScroll:function(){
scroller1 = new IScrollPullUpDown('wrapper1',{
probeType:2,
bounceTime: 250,
bounceEasing: 'quadratic',
mouseWheel:false,
scrollbars:true,
fadeScrollbars:true,
click:true,
interactiveScrollbars:false
},null,null);
function pullDownAction(theScrollerTemp) {
$(".pullDown").show();
$(".pullDownLabel").html("数据刷新中....");
setTimeout(function () {
article.initData();
}, 1000);
}
},queryFailed:function(res){
$(".pullUp,.pullDown").hide();
d.close();
if(res.msg){
dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
}else{
dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据加载失败!',bottom:true}).show();
}
},querySuccess:function(res){
$(".pullUp,.pullDown").hide();
d.close();
if(res.status==200){
var content = res.data.articleContent;
var reg=/group1\//g;
content = content.replace(reg,imgUrlDomain+ 'group1/');
var data = res.data;
var html = template("article_temp",{ it: res.data, content: content});
$(".iScroller").html(html);
// if(type==1||type==2){
var html2 = template("article_temp2",data);
$(".handle-bar").html(html2);
// }
setTimeout(function(){
scroller1.myScroll.refresh();
},1000);
}else{
if(res.status==-1){
// dialog({
// content:'查询失败',
// ok: function() {
// wx.closeWindow();
// }
// }).showModal();
}
article.queryFailed(res);
}
},closeWindow:function(){
var params = {};
params.pageUrl = window.location.href;
$.ajax(server + "weixin/getSign", {
data: params,
dataType: "json",
type: "post",
success: function(res) {
if(res.status == 200) {
var t = res.data.timestamp;
var noncestr = res.data.noncestr;
var signature = res.data.signature;
wx.config({
//debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: appId, // 必填,公众号的唯一标识
timestamp: t, // 必填,生成签名的时间戳
nonceStr: noncestr, // 必填,生成签名的随机串
signature: signature, // 必填,签名,见附录1
jsApiList: [
'closeWindow'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
}
}
});
},
bindEvents: function() {
// $("#zixun").on("click", function () {
// article.querySignType();
// });
$(".handle-bar").on("click",".collection",function(){
var status = $(this).attr("data-status");
var id = $(this).attr("article-id");
if(status==0){
collectionArticle(article,$(this),id)
}else{
unCollectionArticle(article,$(this),id)
}
return;
});
},
querySignType: function() {
d.show();
sendPost('patient/sign_doctors', {}, 'json', 'post', article.querySignTypeFailed, article.querySignTypeSuccess);
},
querySignTypeFailed: function(res) {
d.close();
if (res && res.msg) {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();
} else {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'获取咨询类型失败'}).show();
}
},
querySignTypeSuccess: function(data) {
if (data.status == 200) {
var doctors = data.familyDoctors;
type = 2;
var docInfo,docQkInfo;
if(doctors.length == 0){
doctors = data.teamDoctors;
type = 1;
}
$.each(doctors, function(i, v) {
if(v.teamlevel){
if(v.teamlevel == 3){
docInfo = v;
}
if(v.teamlevel == 2){
docQkInfo = v;
}
}else{
if(v.level == 3){
docInfo = v;
}
if(v.level == 2){
docQkInfo = v;
}
}
});
if(!docInfo){
docInfo = docQkInfo;
}
sendPost('patient/consult/is_consult_unfinished', {doctor: docInfo ? docInfo.code : docQkInfo.code}, 'json', 'post',
function(res){
d.close();
if (res && res.msg) {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();
} else {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'获取是否存在咨询失败'}).show();
}
}, function(res){
if(res.status == 200){
if(res.data == ""){
checkDocInWork(0, docInfo.code, articleId);
} else {
d.close();
dialog({
content: '十分抱歉,您与'+ docInfo.name +'医生还有
未结束咨询,无法发起新的咨询',
okValue:'前往查看',
ok: function (){
var userInfo = JSON.parse(userAgent);
var rep = userInfo.represented?userInfo.represented:userInfo.uid;
window.location.href = "../../yszx/html/consulting-doctor.html?consult=" + res.data +"&toUser="+rep+"&type="+type+"&doctor="+docInfo.code;
},
cancelValue: '我知道了',
cancel: function () {
return;
}
}).showModal();
}
} else {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:res.msg}).show();
}
}
);
} else {
article.querySignTypeFailed(res);
}
}
}
function queryFailed(res){
d.close();
if(res.msg){
dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
}else{
dialog({contentType:'tipsbox', skin:'bk-popup' , content:'操作失败!',bottom:true}).show();
}
}
function checkDocInWork(type, doctor, id){
sendPost('patient/consult/is_doctor_working', {doctor:doctor}, 'json', 'get', queryFailed, function(res){
if(res.status==200){
d.close();
if(res.data==2){
dialog({
content: '您好,由于您的签约医生工作繁忙,设置了每日回复咨询的时间段,所以,在该时间段外的时间,您的咨询将不会马上获得医生的回复。',
okValue:'查看医生工作时间',
ok: function (){
window.location.href = "../../yszx/html/doctor-work-hours.html?doctor=" + doctor;
},
cancelValue: '继续新增咨询',
cancel: function () {
// window.location.href = "../../yszx/html/add-consult.html?id=" + id + "&type="+ type +"&doctorCode=" + doctor;
window.location.href = "../../yszd/html/add-consult.html?id=" + id + "&type="+ type +"&doctorCode=" + doctor+'&fromPage=jkzx';
}
}).showModal();
}else if(res.data==0){//医生不接受咨询
dialog({
content: '对不起,该医生暂时关闭了咨询功能,
暂时无法咨询该医生',
cancelValue: '我知道了',
cancel: function () {
wx.closeWindow();
}
}).showModal();
}else{
//三师咨询或家庭签约咨询,跳转到新增咨询页面
// window.location.href = "add-consult.html?id=" + id + "&type="+ type +"&doctorCode=" + doctor;
window.location.href = "../../yszd/html/add-consult.html?id=" + id + "&type="+ type +"&doctorCode=" + doctor+'&fromPage=jkzx';
}
}else{
queryFailed(res);
}
})
}
template.helper('setTimer',function(o){
return o.substr(0,19)
})
template.helper("setPhoto", function(p) {
return getImgUrl(p);
});
var collectionArticle=function(code,$this,id){
d.showModal()
var url = "/third/jkEdu/Article/saveArticleCollection",
params = {
articleId: articleId,
userType: "2"
};
sendPost(url, params, 'json', 'POST', collecFailed, function(res){
d.close();
if(res.status == 200){
$this.attr("data-status", 1);
$this.find("img").attr("src", "../images/shoucang_pre.png");
var span = $this.find("span");
span.html("已收藏");
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: '收藏成功'
}).show();
}else{
collecFailed(res);
}
})
},
collecFailed = function(res){
d.close();
if(res && res.msg) {
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: res.msg
}).show();
} else {
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: '收藏失败'
}).show();
}
},
unCollectionArticle=function(code,$this,id){
d.showModal()
var url = "/third/jkEdu/Article/cancelArticleCollection",
params = {
articleId: articleId,
userType:2
};
sendPost(url, params, 'json', 'POST', unCollecFailed, function(res){
d.close();
if(res.status == 200){
$this.attr("data-status", 0);
$this.find("img").attr("src", "../images/shoucang_button.png");
var span = $this.find("span");
span.html("收藏");
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: '取消收藏成功'
}).show();
}else{
unCollecFailed(res);
}
})
},
unCollecFailed = function(res){
d.close();
if(res && res.msg) {
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: res.msg
}).show();
} else {
dialog({
contentType: 'tipsbox',
skin: 'bk-popup',
content: '取消收藏失败'
}).show();
}
};