event-profile.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. var Profile = {
  2. url: '',
  3. link: '',
  4. thatBaseInfo: '',
  5. thatPlus: null,
  6. thatMui: null,
  7. thatPromise: null,
  8. thatTempalteCode: '',
  9. thatSerial: '',
  10. thatPageContent: null,
  11. index: 0,
  12. firstCode: '',
  13. profileRes: [],
  14. dc: [],
  15. htmlStr:['<div id="no_result_wrap">',
  16. '<img class="no-result-img" src="../../../images/shujuweikong_img.png" />',
  17. '<div class="f-fs14 c-323232 c-t-center" style="text-align:center">抱歉,暂未找到符合条件的结果。</div>',
  18. '</div>'].join(''),
  19. init: function (ops) {
  20. var me = this;
  21. if (!(!!ops && typeof ops === 'object')) {
  22. alert('参数有误!');
  23. return;
  24. }
  25. ops.cb && ops.cb.call(me);
  26. me.url = ops.url;
  27. me.link = ops.link;
  28. me.thatMui = ops.thatMui;
  29. me.thatPlus = ops.thatPlus;
  30. me.thatPromise = ops.thatPromise;
  31. me.thatBaseInfo = ops.thatBaseInfo;
  32. me.thatTempalteCode = ops.thatTempalteCode;
  33. me.thatSerial = ops.thatSerial;
  34. me.thatPageContent = ops.thatPageContent;
  35. me.thatPlus.nativeUI.showWaiting();
  36. me.index = 0;
  37. me.firstCode = '';
  38. me.profileRes = [];
  39. me.dc = [];
  40. window['dataCode']=[];
  41. },
  42. //获取模板
  43. templateRequest: function () {
  44. var me = this;
  45. return new me.thatPromise(function(resolve, reject) {
  46. me.thatMui.ajax( me.link,
  47. { dataType: 'html',
  48. type:'GET',
  49. error: function(xht, type, throwErr) {
  50. me.thatMui.toast("档案模板获取失败。");
  51. me.thatPlus.nativeUI.closeWaiting();
  52. },
  53. success: function(html) {
  54. resolve(html);
  55. }
  56. })
  57. })
  58. },
  59. //根据thatTempalteCode获取数据
  60. profileDataRequest: function () {
  61. var me = this;
  62. return getReqPromise( me.url,{
  63. patient: me.thatBaseInfo.patiInfo.code,
  64. event: me.thatBaseInfo.eventInfo.id, // 事件ID
  65. catalog: me.thatTempalteCode, // 档案类型
  66. serial: me.thatSerial || "1" // 该类别顺序号,默认填1
  67. },'GET')
  68. },
  69. promiseAll: function () {
  70. var me = this,
  71. fistDc = me.thatTempalteCode;
  72. me.thatPromise.all([ me.templateRequest(), me.profileDataRequest()]).then(function (datas) {
  73. if(datas[1].data){
  74. var html = datas[0];
  75. me.profileRes.push(datas[1]);
  76. me.thatPageContent.html(html);
  77. me.firstCode = me.thatTempalteCode;
  78. me.dc = window['dataCode'];
  79. if (me.dc.length > 0) {
  80. me.reqDataMore(me);
  81. } else {
  82. me.changeData(fistDc);
  83. }
  84. }else{
  85. plus.nativeUI.closeWaiting();
  86. mui.toast('档案数据为空');
  87. }
  88. });
  89. },
  90. //获取多个数据
  91. reqDataMore: function (me) {
  92. me.thatTempalteCode = me.dc[me.index];
  93. Promise.all([ me.profileDataRequest()]).then(function (datas) {
  94. me.profileRes.push(datas[0]);
  95. me.index++;
  96. if (me.dc.length > me.index) {
  97. me.reqDataMore(me);
  98. } else {
  99. me.changeData(me.firstCode);
  100. }
  101. });
  102. },
  103. changeData: function (fistDc) {
  104. var me = this,
  105. jsonData = [];
  106. me.dc.unshift(me.firstCode);
  107. console.log(me.profileRes)
  108. $.each( me.profileRes, function (index) {
  109. jsonData.push(me.selectXmlDom(me.profileRes[index],me.dc[index]));
  110. });
  111. if (jsonData.length > 0) {
  112. if (jsonData.length === 1 && !!!jsonData[0]) {
  113. me.thatPageContent.html(me.htmlStr);
  114. } else {
  115. me.renderProfileTemplate($.extend(true,{},me.mosaicData(jsonData)));
  116. }
  117. } else {
  118. me.thatPageContent.html(me.htmlStr);
  119. }
  120. plus.nativeUI.closeWaiting();
  121. },
  122. //渲染模板
  123. renderProfileTemplate: function (data) {
  124. this.thatPageContent.removeAttr('avalonctrl').attr('ms-controller',"viewController");
  125. var vm = avalon.define($.extend({},{"$id": "viewController"},data));
  126. avalon.scan();
  127. },
  128. //获取xml数据
  129. selectXmlDom: function (profileRes,tempalteCode) {
  130. if(profileRes.status == 200){
  131. var me = this,
  132. $dom = $(profileRes.data.replace(/<\?xml .*\?>/,'')),
  133. version = $dom.find(">version").length?$dom.find(">version").attr("code"):$dom.attr('version');
  134. $.trim(version) || (function () {
  135. version = $dom.find(">ClinicalDocument").length? '2.0.0.1' : '';
  136. })();
  137. if($.trim(version)) {
  138. var verNum = version.split('.')[0];
  139. var xmlToJson = window["dsXmlToJson"+tempalteCode+'_'+verNum],
  140. jsonData = {};
  141. if(xmlToJson && $.isFunction(xmlToJson)) {
  142. if(!profileRes.data || profileRes.data=="[]") {
  143. if(version=2) {
  144. profileRes.data = '<?xml version="1.0" encoding="utf-8"?><ClinicalDocument></ClinicalDocument>';
  145. } else {
  146. profileRes.data = '<?xml version="1.0" encoding="utf-8"?><root></root>';
  147. }
  148. }
  149. // xml 转 json
  150. if((typeof profileRes.data=='string')&&profileRes.data.constructor==String) {
  151. // 去除携带的脚本内容
  152. profileRes.data = profileRes.data.replace(/<script[^>]*?>[\s\S]*?<\/script>/ig,'');
  153. console.log(profileRes.data.replace(/<\?xml .*\?>/,''))
  154. jsonData = xmlToJson(profileRes.data.replace(/<\?xml .*\?>/,''));
  155. }
  156. }
  157. }
  158. }else{
  159. jsonData = ''
  160. }
  161. return jsonData;
  162. },
  163. //数据拼接
  164. mosaicData: function (data) {
  165. for (var i = 1; i < data.length; i++) {
  166. data[i] && (function () {
  167. for (key in data[i]['data']) {
  168. data[0]['data'][key] = data[i]['data'][key];
  169. }
  170. })();
  171. }
  172. return data[0];
  173. }
  174. };
  175. var dataCode = [];
  176. // 基本信息(包括userAgent、上个页面传递的数据)
  177. var baseInfo = null,
  178. // 基础环境信息(包括当前webview)
  179. baseEnv = null;
  180. var $profileItemList = $('.lin-sel-group'),
  181. // 搜索无结果时显示
  182. $noResultWrap = $('#no_result_wrap'),
  183. $selectedProfileName = $('#selected_profile_name'),
  184. $pageContent = $('.page-content');
  185. // 分页查询当前页数
  186. var curPage = 1,
  187. // iscroll 滚动条实例
  188. iscroller,
  189. catalogs = [];
  190. // 获取基本信息(包括userAgent、上个页面传递的数据)
  191. var getBaseInfo = function() {
  192. // 登录的相关信息
  193. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  194. return {
  195. userAgent: userAgent,
  196. eventInfo: baseEnv.webview.eventInfo,
  197. patiInfo: baseEnv.webview.patiInfo,
  198. type:baseEnv.webview.type //cy
  199. }
  200. },
  201. // 获取基础环境信息
  202. getBaseEnvPromise = function () {
  203. var env = {
  204. webview: plus&&plus.webview.currentWebview()
  205. };
  206. return Promise.resolve().then(function(res) {
  207. return env;
  208. });
  209. },
  210. getCatalogPromise = function() {
  211. return getReqPromise('doctor/archives/event/catalog',{patient: baseInfo.patiInfo.code,event: baseInfo.eventInfo.id,type:baseInfo.type},'GET')
  212. .then(function(res) {
  213. if(res.status==200) {
  214. catalogs = res.data
  215. } else {
  216. mui.toast('档案类型获取失败。');
  217. }
  218. },function() {
  219. mui.toast('档案类型获取失败。');
  220. })
  221. },
  222. // 初始化就诊事件列表
  223. initProfileList = function() {
  224. var list = [],
  225. list2 = [];
  226. if(catalogs) {
  227. var inspection = [],
  228. check = [],
  229. inspection2 = [],
  230. check2 = [],
  231. index = [],
  232. operation = [];
  233. _.map(catalogs,function(item){
  234. switch (item.catalog){
  235. case '0131': check.push(item); break;
  236. case '0121': inspection.push(item); break;
  237. case '0231': check2.push(item); break;
  238. case '0221': inspection2.push(item); break;
  239. case '0216': operation.push(item); break;
  240. default : index.push(item); break;
  241. }
  242. })
  243. list[0] = _.find(index,function(o){ return o.catalog == '0101'});
  244. list[1] = _.find(index,function(o){ return o.catalog == '0141'});
  245. list[2] = check;
  246. list[3] = inspection;
  247. list[4] = _.find(index,function(o){ return o.catalog == '0102'});
  248. list2[0] = _.find(index,function(o){ return o.catalog == '0201'});
  249. list2[1] = _.find(index,function(o){ return o.catalog == '0211'});
  250. list2[2] = _.find(index,function(o){ return o.catalog == '0214'});
  251. list2[3] = _.find(index,function(o){ return o.catalog == '0215'});
  252. list2[4] = _.find(index,function(o){ return o.catalog == '0241'});
  253. list2[5] = _.find(index,function(o){ return o.catalog == '0212'});
  254. list2[6] = _.find(index,function(o){ return o.catalog == '0213'});
  255. list2[7] = check2;
  256. list2[8] = inspection2;
  257. list2[9] = operation;
  258. list2[10] = _.find(index,function(o){ return o.catalog == '0202'});
  259. var html = template("profile_item_tmpl", {arr:list,arr2:list2});
  260. $profileItemList.append(html);
  261. }
  262. var $first = $profileItemList.find("li.j-hascode").eq(0),
  263. name = $.trim($first.attr("data-name")),
  264. code = $.trim($first.attr("data-code"));
  265. $first.find('a').addClass('active');
  266. if(name&&code) {
  267. $selectedProfileName.text(name);
  268. getProfileTemplateHtml(code);
  269. }
  270. },
  271. getProfileTemplateHtml = function(tempalteCode,serial){
  272. Profile.init({
  273. url: healthProfileServer+"doctor/archives/event/healthData",
  274. link: '../template/'+tempalteCode+'.html',
  275. thatMui: mui,
  276. thatPlus: plus,
  277. thatPromise: Promise,
  278. thatBaseInfo: baseInfo,
  279. thatTempalteCode: tempalteCode,
  280. thatSerial: serial,
  281. thatPageContent: $pageContent,
  282. });
  283. Profile.promiseAll();
  284. },
  285. // 绑定页面事件
  286. bindEvents = function () {
  287. var saveCode = '';
  288. var saveSerial = '';
  289. $profileItemList.on('click', 'li.j-hascode', function(){
  290. var $this = $(this);
  291. var code = $.trim($this.attr("data-code")),
  292. name = $.trim($this.attr("data-name")),
  293. serial = $.trim($this.attr("data-serial"));
  294. if(code != saveCode){
  295. getProfileTemplateHtml(code, serial);
  296. saveCode = code;
  297. $selectedProfileName.text(name);
  298. }else{
  299. if(serial != saveSerial){
  300. getProfileTemplateHtml(code, serial);
  301. saveSerial = serial;
  302. $selectedProfileName.text(name);
  303. }
  304. }
  305. })
  306. };
  307. // 页面业务处理流程开始
  308. new Promise(function(resolve, reject) {
  309. mui.plusReady(function() {
  310. // plus已经准备好,可以往下执行
  311. resolve(true);
  312. });
  313. }).then(function() {
  314. // 获取基础环境信息
  315. return getBaseEnvPromise().then(function(env) {
  316. baseEnv = env;
  317. // 获取登录医生信息
  318. baseInfo = getBaseInfo();
  319. })
  320. }).then(function() {
  321. return getCatalogPromise();
  322. }).then(function() {
  323. initProfileList();
  324. // 绑定页面事件
  325. bindEvents();
  326. }).catch(function(e) {
  327. plus.nativeUI.closeWaiting();
  328. console && console.error(e);
  329. });