event-profile.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. }
  199. },
  200. // 获取基础环境信息
  201. getBaseEnvPromise = function () {
  202. var env = {
  203. webview: plus&&plus.webview.currentWebview()
  204. };
  205. return Promise.resolve().then(function(res) {
  206. return env;
  207. });
  208. },
  209. getCatalogPromise = function() {
  210. return getReqPromise('doctor/archives/event/catalog',{patient: baseInfo.patiInfo.code,event: baseInfo.eventInfo.id},'GET')
  211. .then(function(res) {
  212. if(res.status==200) {
  213. catalogs = res.data
  214. } else {
  215. mui.toast('档案类型获取失败。');
  216. }
  217. },function() {
  218. mui.toast('档案类型获取失败。');
  219. })
  220. },
  221. // 初始化就诊事件列表
  222. initProfileList = function() {
  223. var list = [],
  224. list2 = [];
  225. if(catalogs) {
  226. var inspection = [],
  227. check = [],
  228. inspection2 = [],
  229. check2 = [],
  230. index = [],
  231. operation = [];
  232. _.map(catalogs,function(item){
  233. switch (item.catalog){
  234. case '0131': check.push(item); break;
  235. case '0121': inspection.push(item); break;
  236. case '0231': check2.push(item); break;
  237. case '0221': inspection2.push(item); break;
  238. case '0216': operation.push(item); break;
  239. default : index.push(item); break;
  240. }
  241. })
  242. list[0] = _.find(index,function(o){ return o.catalog == '0101'});
  243. list[1] = _.find(index,function(o){ return o.catalog == '0141'});
  244. list[2] = check;
  245. list[3] = inspection;
  246. list[4] = _.find(index,function(o){ return o.catalog == '0102'});
  247. list2[0] = _.find(index,function(o){ return o.catalog == '0201'});
  248. list2[1] = _.find(index,function(o){ return o.catalog == '0211'});
  249. list2[2] = _.find(index,function(o){ return o.catalog == '0214'});
  250. list2[3] = _.find(index,function(o){ return o.catalog == '0215'});
  251. list2[4] = _.find(index,function(o){ return o.catalog == '0241'});
  252. list2[5] = _.find(index,function(o){ return o.catalog == '0212'});
  253. list2[6] = _.find(index,function(o){ return o.catalog == '0213'});
  254. list2[7] = check2;
  255. list2[8] = inspection2;
  256. list2[9] = operation;
  257. list2[10] = _.find(index,function(o){ return o.catalog == '0202'});
  258. var html = template("profile_item_tmpl", {arr:list,arr2:list2});
  259. $profileItemList.append(html);
  260. }
  261. var $first = $profileItemList.find("li.j-hascode").eq(0),
  262. name = $.trim($first.attr("data-name")),
  263. code = $.trim($first.attr("data-code"));
  264. $first.find('a').addClass('active');
  265. if(name&&code) {
  266. $selectedProfileName.text(name);
  267. getProfileTemplateHtml(code);
  268. }
  269. },
  270. getProfileTemplateHtml = function(tempalteCode,serial){
  271. Profile.init({
  272. url: healthProfileServer+"doctor/archives/event/healthData",
  273. link: '../template/'+tempalteCode+'.html',
  274. thatMui: mui,
  275. thatPlus: plus,
  276. thatPromise: Promise,
  277. thatBaseInfo: baseInfo,
  278. thatTempalteCode: tempalteCode,
  279. thatSerial: serial,
  280. thatPageContent: $pageContent,
  281. });
  282. Profile.promiseAll();
  283. },
  284. // 绑定页面事件
  285. bindEvents = function () {
  286. var saveCode = '';
  287. var saveSerial = '';
  288. $profileItemList.on('click', 'li.j-hascode', function(){
  289. var $this = $(this);
  290. var code = $.trim($this.attr("data-code")),
  291. name = $.trim($this.attr("data-name")),
  292. serial = $.trim($this.attr("data-serial"));
  293. if(code != saveCode){
  294. getProfileTemplateHtml(code, serial);
  295. saveCode = code;
  296. $selectedProfileName.text(name);
  297. }else{
  298. if(serial != saveSerial){
  299. getProfileTemplateHtml(code, serial);
  300. saveSerial = serial;
  301. $selectedProfileName.text(name);
  302. }
  303. }
  304. })
  305. };
  306. // 页面业务处理流程开始
  307. new Promise(function(resolve, reject) {
  308. mui.plusReady(function() {
  309. // plus已经准备好,可以往下执行
  310. resolve(true);
  311. });
  312. }).then(function() {
  313. // 获取基础环境信息
  314. return getBaseEnvPromise().then(function(env) {
  315. baseEnv = env;
  316. // 获取登录医生信息
  317. baseInfo = getBaseInfo();
  318. })
  319. }).then(function() {
  320. return getCatalogPromise();
  321. }).then(function() {
  322. initProfileList();
  323. // 绑定页面事件
  324. bindEvents();
  325. }).catch(function(e) {
  326. plus.nativeUI.closeWaiting();
  327. console && console.error(e);
  328. });