article_list.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. var d = dialog({
  2. contentType: 'load',
  3. skin: 'bk-popup'
  4. });
  5. var pagetype = 26;
  6. var $eventListWrapper = $('.main-content');
  7. var $noResultWrap = $('#building');
  8. var articleId,annoceId;
  9. var pagesize = [5,5,5],
  10. page = [1,1,0], //3个tab对应的分页的页数
  11. loaded = [true, false, false]; //3个tab对应的页面是否加载过,第一个tab默认加载过
  12. var userAgent = window.localStorage.getItem(agentName);
  13. var userInfo;
  14. var level1Type;//一键已读的类别
  15. $(function(){
  16. checkUserAgent();
  17. });
  18. function initScroller(){
  19. //阻尼系数
  20. var deceleration = mui.os.ios?0.003:0.0009;
  21. mui('.mui-scroll-wrapper').scroll({
  22. bounce: false,
  23. indicators: true, //是否显示滚动条
  24. deceleration:deceleration
  25. });
  26. mui.ready(function() {
  27. //循环初始化所有下拉刷新,上拉加载。
  28. $.each(document.querySelectorAll('.mui-slider-group .mui-scroll-wrapper'), function(index, pullRefreshEl) {
  29. if(index == 0){
  30. mui(pullRefreshEl).pullRefresh({
  31. down: {
  32. callback: function() {
  33. var self = this;
  34. setTimeout(function() {
  35. getHealthArticle(index,articleId, true,1);
  36. self.endPulldownToRefresh();
  37. }, 1000);
  38. }
  39. },
  40. up: {
  41. callback: function() {
  42. var self = this;
  43. setTimeout(function(){
  44. getHealthArticle(index,articleId, false,1);
  45. self.endPullupToRefresh();
  46. }, 1000);
  47. }
  48. }
  49. });
  50. }if(index == 1){
  51. mui(pullRefreshEl).pullRefresh({
  52. down: {
  53. callback: function() {
  54. var self = this;
  55. setTimeout(function() {
  56. getHealthArticle(index,annoceId, true,2);
  57. self.endPulldownToRefresh();
  58. }, 1000);
  59. }
  60. },
  61. up: {
  62. callback: function() {
  63. var self = this;
  64. setTimeout(function() {
  65. getHealthArticle(index,annoceId,false,2);
  66. self.endPullupToRefresh();
  67. }, 1000);
  68. }
  69. }
  70. });
  71. }if(index == 2){
  72. mui(pullRefreshEl).pullRefresh({
  73. down: {
  74. callback: function() {
  75. var self = this;
  76. setTimeout(function() {
  77. getMyCollection(index, true);
  78. self.endPulldownToRefresh();
  79. }, 1000);
  80. }
  81. },
  82. up: {
  83. callback: function() {
  84. var self = this;
  85. setTimeout(function() {
  86. getMyCollection(index);
  87. self.endPullupToRefresh();
  88. }, 1000);
  89. }
  90. }
  91. });
  92. }
  93. });
  94. });
  95. }
  96. //获取文章分类(一级标题)
  97. getCategoryList = function () {
  98. var url = "/third/jkEdu/Article/getCategoryList",
  99. params = {
  100. categoryLevel: "1"
  101. };
  102. return new Promise(function(resolve, reject) {
  103. sendPost(url, params, 'json', 'GET', queryCategoryListFailed, function(res){
  104. if(res.status == 200){
  105. d.close();
  106. var list = res.data;
  107. if(list.length == 0) {
  108. $eventListWrapper.hide();
  109. $noResultWrap.show();
  110. } else {
  111. $noResultWrap.hide();
  112. $eventListWrapper.show();
  113. var html = template('categoryList', {list:list});
  114. $("#categoryBox").html(html);
  115. articleId = $("#tab0").attr('data-type');
  116. annoceId = $("#tab1").attr('data-type');
  117. initScroller();
  118. }
  119. resolve(true)
  120. }else{
  121. queryCategoryListFailed(res);
  122. }
  123. });
  124. })
  125. }
  126. function queryCategoryListFailed(res){
  127. $eventListWrapper.hide();
  128. $noResultWrap.show();
  129. dialog({
  130. contentType: 'tipsbox',
  131. skin: 'bk-popup',
  132. content: res.msg
  133. }).show();
  134. }
  135. function getHealthArticle(index,categoryId, isInit,type){
  136. var tmpIndex = parseInt(index) + 1;
  137. if(isInit){
  138. page[index] = 1;
  139. mui("#item"+tmpIndex+" .mui-scroll-wrapper").pullRefresh().scrollTo(0, 0, 1000);
  140. }
  141. var url = "/patient/jkEdu/article/fetchArticlePushedList",
  142. params = {
  143. page: page[index],
  144. pagesize: pagesize[index],
  145. level1Type: categoryId
  146. };
  147. sendPost(url, params, 'json', 'GET', queryFailed, function(res){
  148. d.close();
  149. if(res.status == 200){
  150. $('#read_all0').show();
  151. $('#read_all1').show();
  152. loaded[index] = true;
  153. //暂时默认数据是按年份group了的 [{year: 2017, list:[]}]
  154. if(res.data.length > 0){
  155. if(type==1){
  156. var temp = 'annoce_list_temp';
  157. }else{
  158. var temp = 'article_list_temp';
  159. }
  160. // level1Type = res.data[0].level1Type;
  161. var html = template(temp, {list: res.data});
  162. if(isInit){
  163. $("#item"+tmpIndex+" .mui-scroll-wrapper").show();
  164. $("#item"+tmpIndex+" .div-no-info").hide();
  165. $("#recordList"+index).empty().append(html);
  166. }else{
  167. $("#recordList"+index).append(html);
  168. }
  169. if(res.data.length < pagesize){
  170. mui("#item"+tmpIndex+" .mui-scroll-wrapper").pullRefresh().endPullupToRefresh(true);
  171. }else{
  172. page[index] ++;
  173. }
  174. }else{
  175. if(isInit){
  176. // $('#read_all').hide();
  177. //type为1代表第一个tab栏
  178. if(type==1){
  179. //无数据
  180. $("#item1 .mui-scroll-wrapper").hide();
  181. $("#item1 .div-no-info").show();
  182. $('#read_all0').hide();
  183. }else{
  184. //无数据
  185. $("#item2 .mui-scroll-wrapper").hide();
  186. $("#item2 .div-no-info").show();
  187. $('#read_all1').hide();
  188. }
  189. }else{
  190. if(type==1){
  191. mui("#item1 .mui-scroll-wrapper").pullRefresh().endPullupToRefresh(true);
  192. }else{
  193. mui("#item2 .mui-scroll-wrapper").pullRefresh().endPullupToRefresh(true);
  194. }
  195. }
  196. }
  197. }else{
  198. queryFailed(res);
  199. }
  200. });
  201. }
  202. function getMyCollection(index, isInit){
  203. var tmpIndex = parseInt(index) + 1;
  204. if(isInit){
  205. page[index] = 0;
  206. mui("#item"+tmpIndex+" .mui-scroll-wrapper").pullRefresh().scrollTo(0, 0, 1000);
  207. }
  208. var url = "/third/jkEdu/Article/getCollectionArticalList",
  209. params = {
  210. pageIndex: page[index],
  211. pagesize: pagesize[index],
  212. articleState: "1",
  213. userType: '2'
  214. };
  215. sendPost(url, params, 'json', 'GET', queryFailed, function(res){
  216. d.close();
  217. if(res.status == 200){
  218. loaded[index] = true;
  219. //暂时默认数据是按年份group了的 [{year: 2017, list:[]}]
  220. if(res.data.length > 0){
  221. var temp = 'myCollection_list_temp';
  222. var html = template(temp, {list: res.data});
  223. if(isInit){
  224. $("#item"+tmpIndex+" .mui-scroll-wrapper").show();
  225. $("#item"+tmpIndex+" .div-no-info").hide();
  226. $("#recordList"+index).empty().append(html);
  227. }else{
  228. $("#recordList"+index).append(html);
  229. }
  230. if(res.data.length < pagesize){
  231. mui("#item"+tmpIndex+" .mui-scroll-wrapper").pullRefresh().endPullupToRefresh(true);
  232. }else{
  233. page[index] ++;
  234. }
  235. }else{
  236. if(isInit){
  237. //无数据
  238. $("#item3 .mui-scroll-wrapper").hide();
  239. $("#item3 .div-no-info").show();
  240. }else{
  241. mui("#item3 .mui-scroll-wrapper").pullRefresh().endPullupToRefresh(true);
  242. }
  243. }
  244. }else{
  245. queryFailed(res);
  246. }
  247. });
  248. }
  249. function queryFailed(res){
  250. d.close();
  251. if(res && res.msg) {
  252. dialog({
  253. contentType: 'tipsbox',
  254. skin: 'bk-popup',
  255. content: res.msg
  256. }).show();
  257. } else {
  258. dialog({
  259. contentType: 'tipsbox',
  260. skin: 'bk-popup',
  261. content: '加载失败'
  262. }).show();
  263. }
  264. }
  265. function bindEvents(){
  266. document.querySelector('.mui-slider').addEventListener('slide', function(event) {
  267. var index = event.detail.slideNumber;
  268. if(!loaded[index]){ //tab未加载过,则加载数据
  269. d.showModal();
  270. if(index == 0){
  271. getHealthArticle(index,articleId,true,1);
  272. }if(index == 1){
  273. getHealthArticle(index,annoceId,true,2);
  274. }if(index == 2){
  275. getMyCollection(index,true);
  276. }
  277. }
  278. });
  279. //tab内item的点击事件
  280. $("#recordList0").on('tap', 'li', function(){
  281. var dataId = $(this).attr("data-code");
  282. var articleId = $(this).attr("data-id");
  283. $(this).find(".new-flag").hide();
  284. window.location.href="article2.html?dataId="+dataId+'&articleId='+articleId+'&type='+'1';;
  285. });
  286. //'健康文章'tab内item对应的点击事件
  287. $("#recordList1").on('tap', 'li', function(){
  288. var dataId = $(this).attr("data-code");
  289. var articleId = $(this).attr("data-id");
  290. $(this).find(".new-flag").hide();
  291. window.location.href="article2.html?dataId="+dataId+'&articleId='+articleId+'&type='+'2';;
  292. });
  293. //'我的收藏'tab内item对应的点击事件
  294. $("#recordList2").on('tap', ' li', function(){
  295. var dataId = $(this).attr("data-code");
  296. var articleId = $(this).attr("data-id");
  297. $(this).find(".new-flag").hide();
  298. window.location.href="article2.html?dataId="+dataId+'&articleId='+articleId+'&type='+'3';
  299. });
  300. }
  301. function queryInit(){
  302. getCategoryList().then(function() {
  303. //一键已读
  304. $('#read_all0').on('click', function () {
  305. setReadTrue (articleId);
  306. })
  307. //一键已读
  308. $('#read_all1').on('click', function () {
  309. setReadTrue (annoceId);
  310. })
  311. userInfo = JSON.parse(userAgent);
  312. if(!canPrescription){
  313. $('#memberContainer').hide();
  314. $("#slider").hide();
  315. $("#building").show();
  316. }else{
  317. initFamilyData();
  318. checkPatientInfo();
  319. bindEvents();
  320. }
  321. wxGetSign();
  322. }).catch(function(e) {
  323. console && console.error(e)
  324. });
  325. }
  326. //初始化家庭成员
  327. function initFamilyData(){
  328. appendFamilyMember($('#memberContainer'),function(){
  329. loaded = [false, false, false];
  330. page = [0,1,1];
  331. checkPatientInfo();
  332. },function(){
  333. //判断有授权家人,修改样式
  334. $("#slider").css({
  335. 'top': '90px',
  336. 'height': 'calc(100% - 90px)'
  337. });
  338. });
  339. }
  340. //判断患者是否是慢病患者
  341. function checkPatientInfo(){
  342. d.showModal();
  343. //显示3个tab
  344. mui('#slider').slider().gotoItem(0);
  345. $(".mui-slider-indicator .c-33").eq(1).show();
  346. $(".mui-slider-indicator .c-33").eq(2).show();
  347. mui('#slider').slider().setStopped(false);
  348. getHealthArticle(0,articleId,true,1);
  349. }
  350. //获取微信信息,并配置微信api接口
  351. function wxGetSign(){
  352. var params = {};
  353. params.pageUrl = window.location.href;
  354. $.ajax(server + "weixin/getSign", {
  355. data: params,
  356. dataType: "json",
  357. type: "post",
  358. success: function(res){
  359. if (res.status == 200) {
  360. var t = res.data.timestamp;
  361. var noncestr = res.data.noncestr;
  362. var signature = res.data.signature;
  363. wx.config({
  364. //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  365. appId: appId, // 必填,公众号的唯一标识
  366. timestamp: t, // 必填,生成签名的时间戳
  367. nonceStr: noncestr, // 必填,生成签名的随机串
  368. signature: signature,// 必填,签名,见附录1
  369. jsApiList: [
  370. 'closeWindow'
  371. ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  372. });
  373. }
  374. }
  375. });
  376. }
  377. template.helper("setPhoto", function(p) {
  378. return getImgUrl(p);
  379. });
  380. template.helper('setTimer',function(o){
  381. return o.substr(0,19)
  382. });
  383. //设置已读
  384. function setReadTrue (id) {
  385. d.show();
  386. sendPost("patient/health/edu/readAllArticle",{firstLevelCategoryId:id},'json','get', function(res){
  387. d.close();
  388. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'加载失败'});
  389. },
  390. function(res){
  391. if(res.status == 200){
  392. //隐藏所有的newicon
  393. $(".new-flag").hide();
  394. }
  395. d.close();
  396. })
  397. }