var d = dialog({contentType:'load', skin:'bk-popup'});
var request = GetRequest(),
doctorCode = request["doctorCode"],
docInfo,
patientInfo = window.localStorage.getItem(agentName1);
var serverId = ""; //微信图片上传获取的图片id
$(function(){
if(patientInfo){
patientInfo = JSON.parse(patientInfo);
}
$("#lbl_patient").text(patientInfo.name);
//获取医生信息
getDoctorInfo();
bindEvents();
getWxSign();
})
function getDoctorInfo(){
d.show();
var url = "family_contract/homepage/homepage",
params = {
doctor: doctorCode
};
sendPost(url, params, 'json', 'get', queryFailed, function(res){
if(res.status == 200){
d.close();
docInfo = res.data;
var text = docInfo.name;
if(docInfo.jobName){
text += "("+docInfo.jobName+")"
}
$("#lbl_doctor").text(text);
}else{
queryFailed(res);
}
})
}
function bindEvents(){
//提交咨询
$("#commit").click(function() {
if($(this).hasClass("active")){
var txtContent = $("#txtContent").val();
if(txtContent){
txtContent = utf16toEntities(txtContent.replace(/\s+/g,""));
}
var data = {
type: 1,
symptoms: txtContent || "",
voice: ""
};
if (validate(data)) {
//验证通过执行
d.showModal();
var images = getImages();
if(images.length == 0){
$("#commit").css("pointer-events","none");
serverId = '';
doSubmit(data);
}
else{
uploadImage(data);
}
}
}
});
}
//验证信息
function validate(data) {
if (data.symptoms.length < 10) {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请至少用10个字描述您的症状'}).show();
return false;
}
if (data.symptoms.length > 500) {
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请在500字以内完成您的症状描述'}).show();
return false;
}
// if(!$(".input-group-pack > input[type=checkbox]").attr("checked")){
// dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请勾选"我已阅读咨询说明"'}).show();
// return false;
// }
return true;
}
function chooseImage(){
wx.chooseImage({
count: 9-getImages().length,
success: function (res) {
for (var i in res.localIds) {
appendFile(res.localIds[i]);
}
}
});
}
function uploadImage(data){
$("#commit").css("pointer-events","none");
var images = getImages();
if (images.length == 0) {
return;
}
var i = 0, length = images.length;
serverId = "";
function upload() {
wx.uploadImage({
localId: images[i],
isShowProgressTips: 0,
success: function (res) {
i++;
if(serverId.length == 0){
serverId = res.serverId;
}
else{
serverId =serverId + "," + res.serverId;
}
if (i < length) {
upload();
}
if(i == images.length){
doSubmit(data);
}
},
fail: function (res) {
$("#commit").css("pointer-events","");
alert(JSON.stringify(res));
}
});
}
upload();
}
// 添加文件
function appendFile(p) {
var amount = getImages().length;
if (amount >= 8) {
$("#add_img_li").hide();
}
if(amount < 9){
var $li = $('
' + ' ' + ' ' + '');
var $add_img_li = $("#add_img_li");
$add_img_li.before($li);
$("#image_tips").text(getImages().length + "/9");
}
}
//获取需要上传的图片
function getImages() {
var images = [];
$("#img_ul").find("img").each(function() {
var imgSrc = $(this).attr("data-src");
images.push(imgSrc);
});
return images;
}
//查看图片
function viewImg(dom) {
var $img = $(dom);
var thissrc = $img.attr("data-src");
var mWid = $(window).width();
var mHei = $(window).height();
var nHtml = '';
$("body").append(nHtml);
$(".delimgpop").click(function() {
$(this).remove()
});
};
//删除图片
function delImg(dom) {
var $li = $(dom).parent();
$li.remove();
$("#image_tips").text(getImages().length + "/9");
var amount = getImages().length;
if(amount <= 8){
$("#add_img_li").show();
}
return;
}
//微信sdk配置出错
wx.error(function (res) {
alert("wx.error:" + res.errMsg);
});
//提交咨询
function doSubmit(data) {
data.mediaIds = serverId;
data.doctor = doctorCode;
var url = 'patient/consult/add';//新增三师或家庭
sendPost(url, data, 'json', 'post', submitFailed, submitSuccess);
}
function submitSuccess(res) {
if (res.status == 200) {
$("#commit").css("pointer-events","");
d.close();
//页面跳转到消息页面
localStorage.setItem("signInfo_tab", 0);
var direct_url = "../../qygl/html/sign_info.html?doctor="+doctorCode+"&consult="+res.data.consult;
window.location.href = direct_url;
} else {
submitFailed(res);
}
}
function queryFailed(res){
d.close();
if (res && 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 submitFailed(res) {
$("#commit").css("pointer-events","");
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 getWxSign(){
//从后台那边获取签名等信息
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: [
'chooseImage',
'uploadImage'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
}
else{
// dialog({
// title:'提示',
// skin:"ui-dialog ax-popup pror",
// content:"获取微信签名失败",
// ok: function (){}
// }).showModal();
}
}
});
}