rukou.js 14.6 KB
Newer Older
hanpeng committed
1
/* define(['zepto','medtap'],function($,medtap){
hanpeng committed
2 3 4
	var main = {
		userId:medtap.getRequest('userId'),
		init:function(){
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
			$('#content').append(main.userId);
			main.bindEve()
		},
		bindEve:function(){
			$('#content img').unbind().bind('click',function(){
				var imgObeject = {};//webApi对象参数
				var imgSelect = $(this).index();//选中图片
				var imgIndex = 0;
				var urls = [],
					picUrl = "";
				[].forEach.call($('#content img'), function(item,index) {
					if($(item).index() == imgSelect){
						imgIndex = index;
					}
					urls.push($(item).attr('src'));
				});
				imgObeject.imgList = urls;
				imgObeject.imgIndex = imgIndex;
				console.log(imgObeject);
				//调用webApi展示大图
				try {
					if (!WebAPI) {
				
					}
					try {
						WebAPI.showImages(imgObeject);
					} catch (e) {
				
					}
				} catch (e) {
				
				}
			})
hanpeng committed
38 39 40
		}
	}
	main.init();
hanpeng committed
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
}) */
define(['zepto', 'medtap'], function($, medtap) {
	var chat = {
		userId: medtap.getRequest('userId'),
		relationId: '',
		doctorId: '',
		doctorName: '',
		doctorProfile: '',
		helperName: '',
		helperProfile: '',
		userName: '',
		userProfile: '',
		hasMore: false,
		limit: 50,
		offset: 1,
		minId: 0,
		maxId: 0,
		init: function() {
			chat.bindEve();
			chat.getPmdOrderInfo();
			chat.talkRelation();
			/* $(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);
				}
			}); */
hanpeng committed
76
			var getTalk = setInterval(function() {
hanpeng committed
77
				chat.getTalkRecord();
hanpeng committed
78 79 80 81 82
				if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
					setTimeout(function() {
						document.documentElement.scrollTop = document.documentElement.scrollHeight;
					})
				}
hanpeng committed
83
			}, 5000)
hanpeng committed
84
			$('input').on('focus', function() {
hanpeng committed
85 86
				clearInterval(getTalk);
			});
hanpeng committed
87
			$('input').on('blur', function() {
hanpeng committed
88 89
				var getTalk = setInterval(function() {
					chat.getTalkRecord();
hanpeng committed
90 91 92 93 94
					if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
						setTimeout(function() {
							document.documentElement.scrollTop = document.documentElement.scrollHeight;
						})
					}
hanpeng committed
95 96
				}, 5000)
			});
hanpeng committed
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
		},
		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();
				}
			})
		},
		//获取私人医生订单相关信息
		getPmdOrderInfo: function() {
			medtap.jzz(1);
			$.ajax({
123
				url: 'https://gateway.medtap.cn/trade/pmdConsultOrder/listValidOrder',
hanpeng committed
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
				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({
150
				url: 'https://gateway.medtap.cn/operation/talk/talkRelation',
hanpeng committed
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 177 178 179 180 181
				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.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() {
			medtap.jzz(1);
			$.ajax({
182
				url: 'https://gateway.medtap.cn/operation/talk/talk',
hanpeng committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
				type: 'post',
				async: false,
				headers: medtap.getHeaders(),
				contentType: 'application/json',
				data: JSON.stringify({
					contentType: 0,
					content: $('#sendMsg').val(),
					orderSn: '',
					relationId: chat.relationId
				}),
				success: function(res) {
					medtap.jzz(0);
					if (res.success == true) {
						//window.location.reload();
						chat.getTalkRecord();
hanpeng committed
198
						//window.scrollTo(0, document.documentElement.clientHeight);
hanpeng committed
199
						$('#sendMsg').val('')
hanpeng committed
200
					} else {
hanpeng committed
201
						medtap.winPop(res.resultDesc)
hanpeng committed
202 203 204 205 206 207 208 209
					}
				}
			})
		},
		//获取消息列表
		getTalkRecord: function() {
			medtap.jzz(1);
			$.ajax({
210
				url: 'https://gateway.medtap.cn/operation/talk/talkRecord',
hanpeng committed
211 212 213 214 215
				type: 'get',
				async: false,
				headers: medtap.getHeaders(),
				data: {
					offset: chat.offset,
hanpeng committed
216
					limit: 50,
hanpeng committed
217 218 219 220 221 222 223 224 225
					relationId: chat.relationId,
					orderSn: '',
				},
				success: function(res) {
					medtap.jzz(0);
					var record = res.content.list;
					var html = '';
					if (record.length == 0) {
						chat.hasMore = false;
hanpeng committed
226
						//medtap.winPop('没有更多了')
hanpeng committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
					} 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>';
hanpeng committed
260
								} else {
hanpeng committed
261 262 263 264 265 266 267 268 269 270 271 272
									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>';
hanpeng committed
273
								}
hanpeng committed
274
							} else if (record[i].contentType == 10) {
hanpeng committed
275
								var content = JSON.parse(record[i].content);
hanpeng committed
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
								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) {
hanpeng committed
301
								var content = record[i].content;
hanpeng committed
302 303
								content.replace(/\n/g, "<br>");
								html += '<p class="system_msg">' + content.replace(/\n/g, "<br>") + '</p>';
hanpeng committed
304 305 306 307 308
							}
						}
					}
					if (chat.offset == 1) {
						$('.chat_list').html(html);
hanpeng committed
309 310 311 312 313
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							})
						}
hanpeng committed
314 315
					} else {
						$('.chat_list').prepend(html);
hanpeng committed
316 317 318 319 320
						if (document.documentElement.scrollHeight > document.documentElement.clientHeight) {
							setTimeout(function() {
								document.documentElement.scrollTop = document.documentElement.scrollHeight;
							})
						}
hanpeng committed
321
					}
hanpeng committed
322
					$('.consult_box').unbind().bind('click', function() {
hanpeng committed
323 324 325 326
						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;
					});
hanpeng committed
327 328

					$('.img_consult').unbind().bind('click', function() {
hanpeng committed
329 330
						var id = $(this).attr('data-id');
						//medtap.pushWindow('../../wechat_lung_imgConsult/pages/imgConsultDetail.html?id=' + id + '&wechatId=' + medtap.getRequest('wechatId'));
hanpeng committed
331
						window.location.href = 'medtapdoc://medtap.cn/order/detail?orderSn=' + id;
hanpeng committed
332
					})
hanpeng committed
333 334 335 336 337 338 339 340 341 342 343 344 345
				}
			})
		},
		//刷新消息列表
		refreshRecord: function(operateType) {
			var id = 0;
			if (operateType == 0) {
				id = chat.maxId;
			} else {
				id = chat.minId;
			}
			medtap.jzz(1);
			$.ajax({
346
				url: 'https://gateway.medtap.cn/operation/talk/talkRecord/refresh',
hanpeng committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
				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;
hanpeng committed
365
						//medtap.winPop('没有更多了');
hanpeng committed
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
					} 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);
						}
					}

				}
			})
		}
	}
	chat.init();
})