rukou.js 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
define(['zepto', 'medtap'], function($, medtap) {
	var chat = {
		userId: medtap.getRequest('userId'),
		relationId: '',
		doctorId: '',
		doctorName: '',
		doctorProfile: '',
		helperName: '',
	  helperId: '',
		helperProfile: '',
		userName: '',
		userProfile: '',
		hasMore: false,
		limit: 50,
		offset: 1,
		minId: 0,
		maxId: 0,
		// 默认消息类型
		num:0,
		init: function() {
			chat.bindEve();
			chat.getPmdOrderInfo();
			chat.talkRelation();
			chat.getImgConsultNumber();
			chat.getConsultNumber();
			chat.getPmdNumber();
			/* $(window).scroll(function() {
				var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度
				var scrollHeight = $(document).height(); //当前页面的总高度
				var clientHeight = $(this).height(); //当前可视的页面高度
				if (scrollTop + clientHeight >= scrollHeight) { //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 
					//滚动条到达底部
					chat.offset = 1;
					chat.refreshRecord(0);
				} else if (scrollTop <= 0) {
					//滚动条到达顶部
					chat.offset = 1;
					chat.refreshRecord(1);
				}
			}); */
			var getTalk = setInterval(function() {
				chat.getTalkRecord();
				if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
					setTimeout(function() {
						document.documentElement.scrollTop = document.documentElement.scrollHeight;
					})
				}
			}, 5000)
			$('input').on('focus', function() {
				clearInterval(getTalk);
			});
			$('input').on('blur', function() {
				var getTalk = setInterval(function() {
					chat.getTalkRecord();
					if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
						setTimeout(function() {
							document.documentElement.scrollTop = document.documentElement.scrollHeight;
						})
					}
				}, 5000)
			});
		},
		bindEve: function() {
			$('.close_info_btn').unbind().bind('click', function() {
				$('.chat_info').hide();
				$(this).hide();
				$('.open_info_btn').show();
			});

			$('.open_info_btn').unbind().bind('click', function() {
				$('.chat_info').show();
				$(this).hide();
				$('.close_info_btn').show();
			});

			$('.send').unbind().bind('click', function() {
				if (!$('#sendMsg').val()) {
					medtap.winPop('消息不能为空')
				} else {
					chat.sendMsg(chat.num);
				}
			})
		},
		// 图片选择
		choseImage: function(){
			$('.footer_addPic').on('click', function() {
				$('.footer').toggleClass("main");
				$('.footer_choseImg').toggle()
				$('.footer_gallery').unbind().bind('click',function(){
					//alert(selectImgObj)
					var selectImgObj = {
					 crop:false,//是否裁剪
					 compress:true,//是否压缩
					 multiple:1 //图片数量
					};
					try {
					 if (!WebAPI) {
					
					 }
					 try {
						WebAPI.selectImages(selectImgObj,function(data){
						//  alert(JSON.stringify(data.keys[0]))
						 chat.sendMsg(1,data.keys[0])
						});
					 } catch (e) {
					
					 }
					} catch (e) {
					
					}
					// 隐藏图片选择模块
					$('.footer').toggleClass("main");
					$('.footer_choseImg').toggle()
				 });
				 
				 $('.footer_Photograph').unbind().bind('click',function(){
					var cameraObj = {
					 crop:false,//是否裁剪
					 compress:true//是否压缩
					}
					try {
					 if (!WebAPI) {
					
					 }
					 try {
						WebAPI.openCamera(cameraObj,function(data){
							chat.sendMsg(1,data.keys[0])
						});
					 } catch (e) {
					
					 }
					} catch (e) {
					
					}
					// 隐藏图片选择模块
					$('.footer').toggleClass("main");
					$('.footer_choseImg').toggle()
				 })
			})
		},
		// 图片预览
		previewImg: function(){
		$('.chat_list').on('click', '.chat_nopadding img',function() {
				
				var imgObject = {};
				// var imgSelectsrc = $(this).index();
				var imgIndex = 0;
				var urls = [],
				picUrl = "";
				// [].forEach.call($('.chat_list .chat_nopadding img'),function(item,index){
				//  if($(item).index() == imgSelect){
				// 	imgIndex = index;
				//  }
				 urls.push($(this).attr('src'));
				// })
				
				imgObject.imgList = urls;
				imgObject.imgIndex = imgIndex;
				try {
				 if (!WebAPI) {
				
				 }
				 try {
					WebAPI.showImages(imgObject);
				 } catch (e) {
				
				 }
				} catch (e) {
				
				}
			 });
		},
		//获取私人医生订单相关信息
		getPmdOrderInfo: function() {
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
177
				url: 'https://gateway.medtap.cn/trade/pmdConsultOrder/listValidOrder',
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
				type: 'post',
				contentType: "application/json",
				async: false,
				headers: medtap.getHeaders(),
				data: JSON.stringify({
					clientId: parseInt(chat.userId),
					clientType: 'USER'
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						var pmdOrderList = res.content.list;
						if (pmdOrderList.length == 0) {
							//console.log(1)
							$('.service_btn').addClass('service_none');
						} else {
							//console.log(2)
						}
					}
				}
			})
		},
		//绑定,获取聊天关系
		talkRelation: function() {
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
204
				url: 'https://gateway.medtap.cn/operation/talk/talkRelation',
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
				type: 'post',
				async: false,
				contentType: 'application/json',
				headers: medtap.getHeaders(),
				data: JSON.stringify({
					userId: parseInt(chat.userId)
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						chat.relationId = res.content.talkRealtion.id;
						chat.doctorId = res.content.talkRealtion.doctorId;
						chat.doctorName = res.content.talkRealtion.doctorName;
						chat.doctorProfile = res.content.talkRealtion.doctorProfile;
						chat.userName = res.content.talkRealtion.username;
						chat.userProfile = res.content.talkRealtion.userProfile;
						chat.helperId = res.content.talkRealtion.helperId;
						chat.helperName = res.content.talkRealtion.helperName;
						chat.helperProfile = res.content.talkRealtion.helperProfile;
						$('.user_profile').attr('src', chat.userProfile);
						$('.user_name').html(chat.userName);
						$('.assistant_name').html(chat.helperName);
						document.title = chat.userName;
						$('.assistant_profile').attr('src', chat.helperProfile);
						chat.getTalkRecord();
					}
				}
			})
		},
		sendMsg: function(num,content) {
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
237
				url: 'https://gateway.medtap.cn/operation/talk/talk',
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
				type: 'post',
				async: false,
				headers: medtap.getHeaders(),
				contentType: 'application/json',
				data: JSON.stringify({
					contentType: num,
					content: content? content:$('#sendMsg').val(),
					orderSn: '',
					relationId: chat.relationId
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						//window.location.reload();
						chat.getTalkRecord();
						//window.scrollTo(0, document.documentElement.clientHeight);
						$('#sendMsg').val('')
					} else {
						medtap.winPop(res.resultDesc)
					}
				}
			})
		},
		//获取消息列表
		getTalkRecord: function() {
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
265
				url: 'https://gateway.medtap.cn/operation/talk/talkRecord',
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
				type: 'get',
				async: false,
				headers: medtap.getHeaders(),
				data: {
					offset: chat.offset,
					limit: 50,
					relationId: chat.relationId,
					orderSn: '',
				},
				success: function(res) {
					medtap.jzz(0);
					var record = res.content.list;
					var html = '';
					if (record.length == 0) {
						chat.hasMore = false;
						//medtap.winPop('没有更多了')
					} else {
						chat.hasMore = true;
						chat.minId = record[0].id;
						chat.maxId = record[record.length - 1].id;
						//console.log(chat.minId + ':' + chat.maxId)
						for (var i = 0; i < record.length; i++) {
							var type = record[i].contentType;
							if (type == 0) {
								if (record[i].senderId == chat.doctorId) {
									html += '<div class="chat_item chat_item_right">' +
										'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' +
										'<div class="chat_message_warp">' +
										'<div class="chat_message chat_message_send">' +
										record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else if (record[i].senderId == chat.userId) {
									html += '<div class="chat_item chat_item_left">' +
										'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' + chat.userProfile + '" class="chat_profile from_profile">' +
										'<div class="chat_message_warp">' +
										/* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */
										'<div class="chat_message chat_message_from">' +
										record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else {
									html += '<div class="chat_item chat_item_left">' +
										'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' + chat.helperProfile + '" class="chat_profile from_profile" style="margin-top:0.2rem">' +
										'<div class="chat_message_warp">' +
										'<p class="chat_from_name">' + chat.helperName + '</p>' +
										'<div class="chat_message chat_message_from">' +
										record[i].content +
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								}
							} else if (record[i].contentType == 10) {
								var content = JSON.parse(record[i].content);
								html += '<div class="service_message img_consult clearfix" style="margin-bottom:0.2rem;" data-id="' +
									content.orderSn + '">' +
									'<div class="service_left_box sevice_img_consult">' +
									'<img src="images/consult_order_twzx_png@2x.png" >' +
									'<p class="service_title">图文咨询</p>' +
									'<p class="service_price">' + content.payFee + '</p>' +
									'</div>' +
									'<div class="service_right_box service_message_detail">' +
									'<p class="detail_patient_name">' + content.texts[0] + '</p>' +
									'</div>' +
									'</div>';
							} else if (record[i].contentType == 11) {
								var content = JSON.parse(record[i].content);
								html += '<div class="service_message consult_box clearfix" data-id="' + content.orderSn + '">' +
									'<div class="service_left_box sevice_consult">' +
									'<img src="images/consult_order_twzx_png@2x.png" >' +
									'<p class="service_title">私人医生</p>' +
									'<p class="service_price">' + content.payFee + '</p>' +
									'</div>' +
									'<div class="service_right_box service_message_detail">' +
									/* '<p class="detail_patient_name">患者:张天问</p>'+ */
									'<p class="detail_question">' + content.texts[1] + '</p>' +
									'</div>' +
									'</div>';
							} else if (type == 3) {
								var content = record[i].content;
								content.replace(/\n/g, "<br>");
								html += '<p class="system_msg">' + content.replace(/\n/g, "<br>") + '</p>';
							} else if (type == 1) {
								if (record[i].senderId == chat.userId) {
									html +=
										'<div class="chat_item chat_item_left">' +
										'<p class="message_time">' +
										record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.userProfile +
										'" class="chat_profile send_profile">' +
										'<div class="chat_message_warp">' +
										'<div class="chat_message chat_message_send chat_nopadding">' +
										// 图片消息
										'<img src="'+record[i].contentPic[0]+'" />'+
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								} else if (record[i].senderId == chat.doctorId) {
									html +=
										'<div class="chat_item chat_item_right">' +
										'<p class="message_time">' +
										record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.doctorProfile +
										'" class="chat_profile from_profile">' +
										'<div class="chat_message_warp">' +
										// '<p class="chat_from_name">' +
										// chat.doctorName +
										// '医生</p>' +
										'<div class="chat_message chat_message_from chat_nopadding">' +
										// 图片消息
										'<img src="'+record[i].contentPic[0]+'" />'+
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								}else if (record[i].senderId == chat.helperId) {
									html +=
										'<div class="chat_item chat_item_left">' +
										'<p class="message_time">' +
										record[i].createTime.substring(5, 16) +
										'</p>' +
										'<div class="chat_info_warp clearfix">' +
										'<img src="' +
										chat.helperProfile +
										'" class="chat_profile from_profile">' +
										'<div class="chat_message_warp">' +
										'<p class="chat_from_name">' +
										chat.helperName +
										'</p>' +
										'<div class="chat_message chat_message_from chat_nopadding">' +
										// 图片消息
										'<img src="'+record[i].contentPic[0]+'" />'+
										'</div>' +
										'</div>' +
										'</div>' +
										'</div>';
								}
							}
						}
					}
					if (chat.offset == 1) {
						$('.chat_list').html(html);
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							})
						}
					} else {
						$('.chat_list').prepend(html);
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							})
						}
					}
					$('.consult_box').unbind().bind('click', function() {
						var id = $(this).attr('data-id');
						//medtap.pushWindow('../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?id=' + id + '&wechatId=' + medtap.getRequest('wechatId'));
						window.location.href = 'medtapdoc://medtap.cn/order/detail?orderSn=' + id;
					});

					$('.img_consult').unbind().bind('click', function() {
						var id = $(this).attr('data-id');
						//medtap.pushWindow('../../wechat_lung_imgConsult/pages/imgConsultDetail.html?id=' + id + '&wechatId=' + medtap.getRequest('wechatId'));
						window.location.href = 'medtapdoc://medtap.cn/order/detail?orderSn=' + id;
					})
				}
			})
		},
		//刷新消息列表
		refreshRecord: function(operateType) {
			var id = 0;
			if (operateType == 0) {
				id = chat.maxId;
			} else {
				id = chat.minId;
			}
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
463
				url: 'https://gateway.medtap.cn/operation/talk/talkRecord/refresh',
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
				type: 'get',
				async: false,
				headers: medtap.getHeaders(),
				data: {
					offset: chat.offset,
					limit: 10,
					relationId: chat.relationId,
					orderSn: '',
					operateType: operateType,
					startId: id
				},
				success: function(res) {
					medtap.jzz(0);
					chat.offset += 1;
					var record = res.content.list;
					var html = '';
					if (record.length == 0) {
						chat.hasMore = false;
						//medtap.winPop('没有更多了');
					} else {
						chat.hasMore = true;
						if (operateType == 0) {
							chat.maxId = record[record.length - 1].id;
							for (var i = 0; i < record.length; i++) {
								var type = record[i].contentType;
								if (type == 0) {
									if (record[i].senderId == chat.doctorId) {
										html += '<div class="chat_item chat_item_right">' +
											'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (record[i].senderId == chat.userId) {
										html += '<div class="chat_item chat_item_left">' +
											'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' + chat.userProfile + '" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											/* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								}
								$('.chat_list').append(html);
							}
						} else {
							chat.minId = record[0].id;
							for (var i = 0; i < record.length; i++) {
								var type = record[i].contentType;
								if (type == 0) {
									if (record[i].senderId == chat.doctorId) {
										html += '<div class="chat_item chat_item_right">' +
											'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' +
											'<div class="chat_message_warp">' +
											'<div class="chat_message chat_message_send">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									} else if (record[i].senderId == chat.userId) {
										html += '<div class="chat_item chat_item_left">' +
											'<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' +
											'<div class="chat_info_warp clearfix">' +
											'<img src="' + chat.userProfile + '" class="chat_profile from_profile">' +
											'<div class="chat_message_warp">' +
											/* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */
											'<div class="chat_message chat_message_from">' +
											record[i].content +
											'</div>' +
											'</div>' +
											'</div>' +
											'</div>';
									}
								}
							}
							$('.chat_list').prepend(html);
						}
					}

				}
			})
		},
		getImgConsultNumber:function(){
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
562
				url:'https://gateway.medtap.cn/trade/imageConsultOrder/countOrder',
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
				type:'post',
				async: false,
				contentType: 'application/json',
				headers:medtap.getHeaders(),
				data:JSON.stringify({
					condition:{
						serviceStatusList:['3','10'],
						clientId:parseInt(chat.userId),
						clientType:"USER"
					}
				}),
				success:function(res){
					medtap.jzz(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count == 0){
							$('.to_img_consult').addClass('service_none');
						}else{
							$('.to_img_consult').removeClass('service_none');
							$('.to_img_consult').unbind().bind('click',function(){
								//window.location.href = 'pages/myOrder.html?orderType=TWZX&userId=' + chat.userId;
								medtap.pushNewWindow('pages/myOrder.html?orderType=TWZX&userId=' + chat.userId);
							})
						}
					}
				}
			})
		},
		getConsultNumber:function(){
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
594
				url:'https://gateway.medtap.cn/trade/consult/countOrder',
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
				type:'post',
				async: false,
				contentType: 'application/json',
				headers:medtap.getHeaders(),
				data:JSON.stringify({
					condition:{
						serviceStatusList:['3','10'],
						userId:parseInt(chat.userId)
					}
				}),
				success:function(res){
					medtap.jzz(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count == 0){
							$('.to_consult').addClass('service_none');
						}else{
							$('.to_consult').removeClass('service_none');
							$('.to_consult').unbind().bind('click',function(){
								//window.location.href = 'pages/myOrder.html?orderType=DHZX&userId=' + chat.userId;
								medtap.pushNewWindow('pages/myOrder.html?orderType=DHZX&userId=' + chat.userId);
							})
						}
					}
				}
			})
		},
		getPmdNumber:function(){
			medtap.jzz(1);
			$.ajax({
zujiaozhang committed
625
				url:'https://gateway.medtap.cn/trade/pmdConsultOrder/countOrder',
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
				type:'post',
				async: false,
				contentType: 'application/json',
				headers:medtap.getHeaders(),
				data:JSON.stringify({
					condition:{
						serviceStatusList:['3','10'],
						clientId:parseInt(chat.userId),
						clientType:"USER"
					}
				}),
				success:function(res){
					medtap.jzz(0);
					if(res.success == true){
						var count = res.content.totalCount;
						if(count == 0){
							$('.to_pmd').addClass('service_none');
						}else{
							$('.to_pmd').removeClass('service_none');
							$('.to_pmd').unbind().bind('click',function(){
								//window.location.href = 'pages/myOrder.html?orderType=PMD&userId=' + chat.userId;
								medtap.pushNewWindow('pages/myOrder.html?orderType=PMD&userId=' + chat.userId)
							})
						}
					}
				}
			})
		}
	}
	chat.init();
	chat.choseImage();
	chat.previewImg();
})