cardDetail.js 16.3 KB
Newer Older
hanpeng committed
1 2 3 4 5 6 7 8
define(['zepto', 'medtap'], function($, medtap) {
	var card = {
		cardId: medtap.getRequest('cardId'),
		limit: 10,
		offset: 1,
		hasMore: false,
		firstLoad: false,
		upCount: 0,
hanpeng committed
9 10
		agencyId: '',
		shareId: medtap.getRequest('agencyId'),
hanpeng committed
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
		init: function() {
			card.getShareInfoByToken();
			card.getCardDetail();
			card.getCommentList();
			card.bindEve();
			card.getWechatTicket();
			card.previewImage();
			//滚动加载
			$(window).scroll(function() {
				var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度
				var scrollHeight = $(document).height(); //当前页面的总高度
				var clientHeight = $(this).height(); //当前可视的页面高度
				if (scrollTop + clientHeight >= scrollHeight) { //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 
					//滚动条到达底部
					if (card.hasMore == true) {
						card.offset += 1;
						card.getCommentList();
					} else {

					}
				} else if (scrollTop <= 0) {
					//滚动条到达顶部
					return
				}
			});
			$('input').on('blur', function() {
				$('body').scrollTop(0);
			});

			$('.btn_do_comment').on('click', function() {
				$('.content_mod').show();
				$('.comment_btn').show();
				$('.reply_input').focus();
				$('.content_mod').on('click', function() {
					$(this).hide();
					$('.comment_btn').hide();
				});
				$('.reply_btn').on('click', function() {
					card.replyPublish();
				})
			});

			//share
			$('.share').on('click', function() {
				$('.share_function').show();
				$('.close_share').on('click', function() {
					$('.share_function').hide();
				})
hanpeng committed
59 60 61 62 63 64 65
			});
			window.onload = function(){
				if(localStorage.needReload && localStorage.needReload == 'yes'){
					localStorage.needReload = 'no';
					window.location.reload();
				}
			}
hanpeng committed
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
		},
		bindEve: function() {
			//为TA加油
			$('.fabulous').on('click', function() {
				var status = card.publishUp(card.cardId);
				if (status == 1) {
					$(this).hide();
					$('.unFabulous').show();
					card.getCardDetail();
					var newNum = parseInt(card.upCount);
					$('.unFabulous').children('.fabulous_desc').html(newNum);
				}
			});
			//取消点赞
			$('.unFabulous').on('click', function() {
				var status = card.publishUp(card.cardId);
				if (status == 0) {
					$(this).hide();
					$('.fabulous').show();
					$('.fabulous').children('.fabulous_desc').html('为TA加油');
				}
			});
			//收藏
			$('.collect').on('click', function() {
				var rs = card.publishCollect();
				var _this = this;
				if (rs == 0) {
					$(this).children('img').attr('src', '../images/post_like_buttn@2x.png');
				} else if (rs == 1) {
					setTimeout(function() {
						medtap.toast({
							message: '收藏成功',
							time: 2000
						});
						$(_this).children('img').attr('src', '../images/post_like red_buttn@2x.png');
					}, 1000)
				}
			})
		},
		getWechatTicket: function() {
			medtap.submitAjax({
hanpeng committed
107
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
hanpeng committed
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
				type: 'GET',
				async: false,
				data: {
					url: window.location.href
				},
				success: function(res) {
					var data = res.content;
					wx.config({
						debug: false,
						appId: data.appId, // 必填,公众号的唯一标识
						timestamp: data.timestamp, // 必填,生成签名的时间戳
						nonceStr: data.nonceStr, // 必填,生成签名的随机串
						signature: data.signature, // 必填,签名,见附录1
						jsApiList: [
							'previewImage',
							'onMenuShareTimeline',
							'onMenuShareAppMessage'
						]
					})
					card.share();
				}
			})
		},
		share: function() {
			//微信分享
			wx.ready(function() {
				//分享给朋友
				wx.onMenuShareAppMessage({
					title: productDetail.name,
					desc: '', // 分享描述
hanpeng committed
138 139
					link: 'https://testdevgw.medtap.cn/wechat/lung/oauth_stepEnd?type=BBS_LUNG_SHARE&cardId=' + card.cardId +
						'&agencyId=' + card.shareId,
hanpeng committed
140 141 142 143
					imgUrl: '', // 分享图标
					type: 'link', // 分享类型,music、video或link,不填默认为link
					dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
					success: function() {
hanpeng committed
144

hanpeng committed
145 146
					},
					cancel: function() {
hanpeng committed
147

hanpeng committed
148 149 150 151 152
					}
				});
				//分享到朋友圈
				wx.onMenuShareTimeline({
					title: productDetail.name, // 分享标题
hanpeng committed
153 154 155
					link: 'https://testdevgw.medtap.cn/wechat/lung/oauth_stepEnd?type=BBS_LUNG_SHARE&cardId=' + card.cardId +
						'&agencyId=' + card.shareId,
					imgUrl: '', // 分享图标
hanpeng committed
156
					success: function() {
hanpeng committed
157

hanpeng committed
158 159
					},
					cancel: function() {
hanpeng committed
160

hanpeng committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
					}
				});
			});
		},
		previewImage: function() {
			$('.cardImage').on('click', 'img', function() {
				var imgArray = [];
				var curImageSrc = $(this).attr('src');
				if (curImageSrc) {
					$('.cardImage img').each(function(index, el) {
						var itemSrc = $(this).attr('src');
						imgArray.push(itemSrc);
					});
					wx.previewImage({
						current: curImageSrc,
						urls: imgArray
					});
				}
			});
		},
		getCardDetail: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/pubbbs/publish/' + card.cardId,
				type: 'GET',
				async: false,
				data: {

				},
				success: function(res) {
191
					// console.log(res)
hanpeng committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
					medtap.loading(0);
					if (res.success == true) {
						var detailInfo = res.content.publish;
						$('.user_name').html(detailInfo.nickname);
						$('.card_time').html(detailInfo.updateTime.substring(5, 16));
						$('.user_profile').attr('src', detailInfo.profile);
						$('.cardDetail').html(detailInfo.content);
						$('.seenNum').html(detailInfo.viewCount);
						$('.replyNum').html(detailInfo.commentCount);
						$('.btn_icon_commentNum').html(detailInfo.commentCount)
						card.upCount = detailInfo.upCount;
						if (detailInfo.picUrls.length != 0) {
							var imgHtml = ''
							for (var i = 0; i < detailInfo.picUrls.length; i++) {
								imgHtml += '<img src="' + detailInfo.picUrls[i] + '" >'
							}
							$('.cardImage').html(imgHtml);
						}
						//点赞状态
						if (detailInfo.upStatus == 0) {
							$('.fabulous').show();
							$('.fabulous_desc').html('为TA加油');
							$('.unFabulous').hide();
						} else if (detailInfo.upStatus == 1) {
							$('.fabulous').hide();
							$('.unFabulous').show();
							var newNum = parseInt(card.upCount);
							$('.fabulous_desc').html(newNum);
						}
						//收藏状态
						if (detailInfo.collectStatus == 0) {
							$('.collect').children('img').attr('src', '../images/post_like_buttn@2x.png');
						} else if (detailInfo.collectStatus == 1) {
							$('.collect').children('img').attr('src', '../images/post_like red_buttn@2x.png');
						}
227 228
					}else{
						medtap.winPop('该贴已被删除')
hanpeng committed
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 260 261 262 263 264 265 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 463 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
					}
				}
			})
		},
		//获取评论列表
		getCommentList: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/pubbbs/publishComment',
				type: 'GET',
				async: false,
				data: {
					offset: card.offset,
					limit: card.limit,
					publishId: card.cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var replyHtml = '';
						$('.comment_header_num').html(res.content.count + '条');
						var replyList = res.content.list;
						if (replyList.length == 0) {
							card.hasMore = false;
							if (card.offset != 1) {
								setTimeout(function() {
									medtap.toast({
										message: '没有更多了',
										time: 2000
									})
								}, 1000);
							} else {

							}
						} else {
							card.hasMore = true;
							for (var i = 0; i < replyList.length; i++) {
								if (replyList[i].hasOwnProperty('commentList')) {
									var secReplyNum = parseInt(replyList[i].commentList.length);
									var commentList = replyList[i].commentList;
									if (replyList[i].upStatus == 0) {
										replyHtml += '<div class="comment_item hasReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_unlike_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'<div class="comment_reply"  data-commentId="' + commentList[0].id + '">' +
											'<span class="comment_reply_name">' + commentList[0].nickname + ':</span>' +
											'<span class="comment_reply_content">' + commentList[0].content + '</span>' +
											'<span class="comment_reply_time">' + commentList[0].updateTime.substring(5, 16) + '</span>' +
											'<div class="comment_reply_more">查看全部' +
											secReplyNum +
											'条回复</div>' +
											'</div>' +
											'</div>';
									} else if (replyList[i].upStatus == 1) {
										replyHtml += '<div class="comment_item hasReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_like_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'<div class="comment_reply"  data-commentId="' + commentList[i].id + '">' +
											'<span class="comment_reply_name">' + commentList[i].nickname + '</span>' +
											'<span class="comment_reply_content">' + commentList[i].content + '</span>' +
											'<span class="comment_reply_time">' + commentList[i].updateTime.substring(5, 16) + '</span>' +
											'<div class="comment_reply_more">查看全部' +
											secReplyNum +
											'条回复</div>' +
											'</div>' +
											'</div>';
									}
								} else {
									if (replyList[i].upStatus == 0) {
										replyHtml += '<div class="comment_item noReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_unlike_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'</div>';
									} else if (replyList[i].upStatus == 1) {
										replyHtml += '<div class="comment_item noReply" data-id="' + replyList[i].id + '">' +
											'<div class="comment_item_header clearfix">' +
											'<img src="' + replyList[i].profile + '" >' +
											'<div class="comment_user_name">' +
											replyList[i].nickname +
											'</div>' +
											'<div class="comment_tools_fabulous clearfix">' +
											'<img src="../images/post_like_png@2x.png" >' +
											'<span class="fabulous_num">' + replyList[i].upCount + '</span>' +
											'</div>' +
											'</div>' +
											'<div class="comment_content">' +
											replyList[i].content +
											'</div>' +
											'<div class="comment_info">' +
											'<span class="comment_time txt_gray">' + replyList[i].updateTime.substring(5, 16) + '</span>' +
											'<span class="comment_reply_btn txt_blue">回复</span>' +
											'</div>' +
											'</div>';
									}
								}
							}
						}
						if (card.offset > 1) {
							$('.comment_list').append(replyHtml);
						} else {
							$('.comment_list').html(replyHtml);
						}
						//评论点赞
						$('.comment_item .comment_tools_fabulous').on('click', function(event) {
							event.stopPropagation();
							var id = $(this).parents('.comment_item').attr('data-id');
							var num = $(this).children('.fabulous_num').html();
							var commentResult = card.commentUp(id);
							if (commentResult == 0) {
								var newNum = parseInt(num) - 1;
								$(this).children('img').attr('src', '../images/post_unlike_png@2x.png');
								$(this).children('.fabulous_num').html(newNum);
							} else if (commentResult == 1) {
								var newNum = parseInt(num) + 1;
								$(this).children('img').attr('src', '../images/post_like_png@2x.png');
								$(this).children('.fabulous_num').html(newNum);
							}
						});
						//跳转至评论详情
						$('.comment_item').on('click', function() {
							var id = $(this).attr('data-id');
							medtap.pushWindow('replyDetail.html?commentId=' + id);
						})
					}
				}
			})
		},
		//为TA加油
		publishUp: function(cardId) {
			var status;
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/pubbbs/publish/up',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					publishId: cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						status = res.content.upStatus;
					}
				}
			})
			return status;
		},
		//评论点赞
		commentUp: function(id) {
			var result;
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/pubbbs/publishComment/up',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					commentId: id
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						result = res.content.upStatus;
					}
				}
			});
			return result;
		},
		//收藏帖子
		publishCollect: function() {
			var collectResult;
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/pubbbs/publishCollect',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					publishId: card.cardId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						collectResult = res.content.collectStatus;
					}
				}
			})
			return collectResult;
		},
		//回复帖子
		replyPublish: function() {
			if (!$('.reply_input').val()) {
				medtap.toast({
					message: '请输入评论内容',
					time: 2000
				})
				return
			} else {
				medtap.loading(1);
				medtap.submitAjax({
					url: 'https://testdevgw.medtap.cn/operation/pubbbs/publishComment',
					type: 'POST',
					async: false,
					contentType: 'application/json',
					data: {
						content: $('.reply_input').val(),
						publishId: card.cardId
					},
					success: function(res) {
						medtap.loading(0);
						if (res.success == true) {
							setTimeout(function() {
								medtap.toast({
									message: '回复成功~',
									time: 1800
								});
								setTimeout(function() {
									window.location.reload();
								}, 2000)
							}, 2000)
						} else {

						}
					}
				})
			}
		},
hanpeng committed
502
		getShareInfoByToken: function() {
hanpeng committed
503 504 505 506 507 508
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/common/share/getSharerByToken',
				type: 'post',
				contentType: "application/json",
				async: false,
hanpeng committed
509 510
				data: {

hanpeng committed
511 512 513 514 515
				},
				success: function(res) {
					if (res.success == true) {
						if (res.content.clientType == 'USER') {
							return;
hanpeng committed
516
						} else if (res.content.clientType == 'AGENCY') {
hanpeng committed
517
							card.agencyId = res.content.clientId;
hanpeng committed
518
						} else {
hanpeng committed
519 520
							return;
						}
hanpeng committed
521

hanpeng committed
522 523 524 525 526 527 528
					}
				}
			})
		}
	}
	card.init();
})