search_all.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview)
  4. baseEnv = null;
  5. // 搜索框
  6. var $searchbar = $('.searchbar'),
  7. // 搜索输入框
  8. $searchbarInput = $('.lin-search-ipt input'),
  9. // 搜索取消按钮
  10. $searchCancelBtn = $('.searchbar-cancel'),
  11. // 搜索框下面悬浮的搜索提示
  12. $searchSuggest = $('#search_suggest_text'),
  13. // 搜索结果展示容器
  14. $searchtResult = $('#search_result'),
  15. // 搜索无结果时显示
  16. $noResultWrap = $('#no_result_wrap'),
  17. $patiList = $('#pati_list'),
  18. $history = $('#history_search'),//历史搜索
  19. $hisSearList = $('#his_sear_list');
  20. var lastId = 1,// 分页查询最后一页
  21. search_keyword = "", //记录搜索框的内容
  22. // iscroll 滚动条实例
  23. iscroller,
  24. // 从缓存取出所选团队
  25. // 每页加载数据量
  26. pagesize = 15,
  27. myScroll,
  28. hasMoreResults = false,
  29. historyData=[],//历史数据
  30. historyStr,
  31. inputText="";
  32. var self;
  33. mui.plusReady(function() {
  34. // initKeyboardAndroid();
  35. self = plus.webview.currentWebview();
  36. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  37. historyStr = window.localStorage.getItem("qianyueSearchHis"+docInfo.code);
  38. if(historyStr){
  39. historyStr = JSON.parse(historyStr).join(',');
  40. historyData = historyStr.split(',');
  41. }else{
  42. historyData = [];
  43. }
  44. if(historyData.length){
  45. $history.show();
  46. }
  47. //阻尼系数
  48. var deceleration = mui.os.ios?0.003:0.0009;
  49. mui('.mui-scroll-wrapper').scroll({
  50. scrollX: true,
  51. bounce: false,
  52. indicators: true, //是否显示滚动条
  53. deceleration:deceleration
  54. });
  55. });
  56. window.addEventListener("initSearch", function(e){
  57. // openSoftKeyboard();
  58. baseInfo = getBaseInfo();
  59. setTimeout(function(){
  60. $("#searchPut").focus();
  61. },500)
  62. })
  63. // 获取登录相关信息
  64. var getBaseInfo = function() {
  65. // 登录的相关信息
  66. var userAgent = JSON.parse(plus.storage.getItem("userAgent"))
  67. return {
  68. userAgent: userAgent
  69. }
  70. },
  71. // 获取基础环境信息
  72. getBaseEnvPromise = function () {
  73. self = plus.webview.currentWebview();
  74. var env = {
  75. webview: plus.webview.currentWebview()
  76. };
  77. return Promise.resolve().then(function(res) {
  78. return env;
  79. });
  80. },
  81. historyList = function(){
  82. historyStr = window.localStorage.getItem("qianyueSearchHis"+docInfo.code);
  83. if(historyStr){
  84. historyStr = JSON.parse(historyStr).join(',');
  85. historyData = historyStr.split(',');
  86. }else{
  87. historyData = [];
  88. }
  89. var html = template('his_list_tmpl',{list:historyData});
  90. $hisSearList.html('');
  91. $hisSearList.append(html);
  92. },
  93. // 初始化“患者”视图列表
  94. initPatientViewList = function(data,keyword,isAppend) {
  95. data.inputText = inputText;
  96. data.patients_len = data.patients.length;
  97. var html = template('pati_list_tmpl', data)
  98. searchText = $searchbarInput.val().trim();
  99. if(isAppend === true) {
  100. $patiList.append(html)
  101. } else {
  102. $patiList.html(html);
  103. }
  104. ellipsisText($patiList.find('li'),searchText);
  105. },
  106. // $el: $('.c-content-warp')
  107. getRowProps = function ($el) {
  108. var $textEllipsis = $el,
  109. $text = $textEllipsis.eq(0),
  110. $chart = $text.text('a'),
  111. enWidth = $chart.width(),
  112. $chart = $text.text('中'),
  113. zhWidth = $chart.width(),
  114. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  115. rowHeight = $chart.height();
  116. $chart.text('');
  117. return {
  118. chartWidth: {
  119. zh: zhWidth,
  120. en: enWidth
  121. },
  122. rowHeight: Math.max(rowHeight, lineHeight),
  123. rowWidth: $el.width()
  124. };
  125. },
  126. replaceAll = function (text, arr) {
  127. var html = text;
  128. _.each(arr,function(kw) {
  129. var reg = new RegExp(kw+"(?!>)","gi");
  130. html = html.replace(reg,'<em>'+kw+'</em>');
  131. });
  132. return html;
  133. },
  134. highlineKeyword = function ($el,searchText) {
  135. var props = getRowProps($el),
  136. chartWidth = props.chartWidth,
  137. rowHeight = props.rowHeight,
  138. rowWidth = props.rowWidth,
  139. // 每行显示字符数(以中文字符为标准计算)
  140. chartNum = Math.floor(rowWidth / chartWidth.zh),
  141. // 排除指定数量字符所占宽度
  142. exceptNum = 0,
  143. // 行数
  144. rowNum = 1,
  145. // 预计显示总字符数
  146. expectedNum = chartNum * rowNum - exceptNum,
  147. $target = $el,
  148. // 目标文本
  149. text = $.trim($target.attr('data-text')),
  150. length = text.length,
  151. // 关键字数组
  152. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  153. var fidx = 0,preFidx,diff = 0;
  154. $target.html(replaceAll(text, kws));
  155. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  156. return ;
  157. }
  158. if(text.length > expectedNum) {
  159. fidx = text.indexOf(kws[0])+kws[0].length-1;
  160. diff = fidx - expectedNum + 1;
  161. preFidx = fidx;
  162. diff = (diff<0)?0:diff;
  163. var preChar = (diff>0)?"...":"";
  164. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  165. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  166. preFidx++;
  167. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  168. }
  169. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  170. diff = diff>0?(diff - 1):0;
  171. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  172. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  173. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  174. }
  175. }
  176. },
  177. ellipsisText = function ($elements, searchText) {
  178. //$el.ellipsis({ row: 2});
  179. _.each($elements,function(el) {
  180. var $textEl = $(el).find(".j-text-ellipsis");
  181. _.each($textEl, function(t){
  182. highlineKeyword($(t),searchText)
  183. })
  184. });
  185. },
  186. // 控制搜索关键字悬浮提示的显示
  187. showSearchSuggest = function(text) {
  188. var suggestText = '搜索“'+text+'”';
  189. // 如果text不为空,则显示;否则隐藏
  190. if(text&&text.trim().length) {
  191. $searchSuggest.text(suggestText);
  192. $searchSuggest.show();
  193. } else {
  194. $searchSuggest.text('');
  195. $searchSuggest.hide();
  196. }
  197. },
  198. // 分页查询列表
  199. searchByPaging = function () {
  200. search_keyword = $searchbarInput.val();
  201. if(!$.trim(search_keyword)) {
  202. return ;
  203. }
  204. plus.nativeUI.showWaiting();
  205. // page: 分页索引,filter:搜素关键字,pagesize:每页条数
  206. var url = "/doctor/patient_label_info/getPatientByNameOrIdcard",
  207. params = {str: search_keyword};
  208. getReqPromise(url,params, "GET").then(function(res){
  209. if(res.status == 200) {
  210. if(res.data.teams.length == 0 && res.data.patients.length == 0 ) {
  211. $searchtResult.hide();
  212. $noResultWrap.show();
  213. } else {
  214. $noResultWrap.hide();
  215. $searchtResult.show();
  216. initPatientViewList(res.data,search_keyword);
  217. }
  218. } else {
  219. plus.nativeUI.toast("搜索失败");
  220. }
  221. plus.nativeUI.closeWaiting();
  222. }).catch(function(e) {
  223. console && console.error(e)
  224. });
  225. },
  226. // 滚动条分页实例初始化
  227. initScroller = function($el,url,getData,pullUpAction, reqType) {
  228. var scroller;
  229. if(hasMoreResults) {
  230. scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  231. var data = getData();
  232. getReqPromise(url,data,reqType).then(function(data) {
  233. if(pullUpAction && $.isFunction(pullUpAction)) {
  234. pullUpAction(data);
  235. updatePullUpText(scroller,data.list);
  236. }
  237. })
  238. }});
  239. } else {
  240. scroller = $el.initScroll({pullDown: false,pullUp: false});
  241. }
  242. return scroller;
  243. },
  244. // 更新分页上拉加载的提示文本
  245. updatePullUpText= function(scroller,list) {
  246. var $wrap = $(scroller.wrapper),
  247. $pullupLabel = $wrap.find('.pullUpLabel');
  248. if(!list || !list.length) {
  249. $pullupLabel.text('没有更多');
  250. } else {
  251. $pullupLabel.text('上拉加载更多');
  252. }
  253. scroller.on('refresh',function() {
  254. if(!list || !list.length) {
  255. $pullupLabel.text('没有更多');
  256. } else {
  257. $pullupLabel.text('上拉加载更多');
  258. }
  259. });
  260. },
  261. // 绑定页面事件
  262. bindEvents = function () {
  263. $patiList.on('tap','li[data-patient-code]',function() {
  264. var code = $(this).attr("data-patient-code");
  265. openWebview("huanzhexinxi.html",{
  266. patiCode: code
  267. });
  268. return false;
  269. }).on('tap', 'li[data-code]', function(){
  270. var code = $(this).attr("data-code");
  271. openWebview("juminxinxi.html",{patiCode: code});
  272. });
  273. $('#clear_his_data').on('tap', function(){
  274. window.localStorage.removeItem("qianyueSearchHis"+docInfo.code);
  275. historyList();
  276. $history.hide();
  277. })
  278. $hisSearList.on('click','.his-list',function(){
  279. var $that = $(this);
  280. lastId = 1;
  281. $("#searchAll").hide();
  282. $searchSuggest.hide();
  283. $history.hide();
  284. $searchbarInput.val($that.text())
  285. searchByPaging();
  286. })
  287. $searchbarInput.on('input', function() {
  288. var text = $(this).val().trim();
  289. $searchtResult.hide();
  290. $('#no_result_wrap').hide();
  291. if(text.length > 0){
  292. $history.hide();
  293. }else{
  294. $history.show();
  295. }
  296. showSearchSuggest(text);
  297. }).on('keydown',function(e) {
  298. if (e.which === 13 || e.keycode === 13) {
  299. saveHisData();//保存历史数据20个
  300. validateInputText();
  301. }
  302. });
  303. $('.lin-search-ipt a').on('click', function(){
  304. plus.webview.currentWebview().close();
  305. })
  306. function saveHisData(){
  307. //保存历史数据20个
  308. var value = $searchbarInput.val().trim();
  309. if(value){
  310. if(historyData.length>19){
  311. historyData.pop();
  312. }
  313. if(historyData.indexOf(value)>-1){
  314. historyData = _.filter(historyData, function(o){ return o != value; });
  315. }
  316. historyData.unshift(value);
  317. var str = JSON.stringify(historyData)
  318. window.localStorage.setItem("qianyueSearchHis"+docInfo.code,str);
  319. historyList();
  320. }
  321. }
  322. $searchSuggest.on('click',function() {
  323. saveHisData();//保存历史数据20个
  324. validateInputText();
  325. });
  326. $searchCancelBtn.on('click',function() {
  327. mui.back();
  328. });
  329. //搜索全部居民的绑定事件
  330. $("#searchAll").on('click', function(){
  331. openWebview("search_all.html");
  332. });
  333. },
  334. validateInputText = function(){
  335. //校验输入的数据
  336. var text = $('#searchPut').val(),
  337. reg = /[\u4E00-\u9FA5]/i, //验证汉字
  338. pass = false,
  339. inputName = true;
  340. if(!reg.test(text)){
  341. pass = IdentityCodeValid(text);
  342. inputName = false;
  343. inputText = "idcard";
  344. }else{
  345. //校验汉字
  346. pass = (text.length > 1);
  347. inputText = "name";
  348. if(!pass){
  349. dialog({
  350. contentType: "tipsbox",
  351. skin:"popup",
  352. closeTime: 2000,
  353. content: "请输入完整的姓名"
  354. }).showModal();
  355. }
  356. }
  357. if(inputName){
  358. $noResultWrap.find("div").text("无符合条件的结果,请确认输入的名字是否正确");
  359. }else{
  360. $noResultWrap.find("div").text("无符合条件的结果,请确认输入的身份证号是否正确");
  361. }
  362. if(pass){
  363. lastId = 1;
  364. $searchSuggest.hide();
  365. searchByPaging();
  366. }
  367. },
  368. IdentityCodeValid = function(code) {
  369. var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
  370. var tip = "";
  371. var pass= true;
  372. if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
  373. tip = "身份证号格式错误";
  374. pass = false;
  375. }
  376. else if(!city[code.substr(0,2)]){
  377. tip = "地址编码错误";
  378. pass = false;
  379. }
  380. else{
  381. //18位身份证需要验证最后一位校验位
  382. if(code.length == 18){
  383. code = code.split('');
  384. //∑(ai×Wi)(mod 11)
  385. //加权因子
  386. var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
  387. //校验位
  388. var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
  389. var sum = 0;
  390. var ai = 0;
  391. var wi = 0;
  392. for (var i = 0; i < 17; i++)
  393. {
  394. ai = code[i];
  395. wi = factor[i];
  396. sum += ai * wi;
  397. }
  398. var last = parity[sum % 11];
  399. if(parity[sum % 11] != code[17]){
  400. tip = "校验位错误";
  401. pass =false;
  402. }
  403. }
  404. }
  405. if(!pass){
  406. dialog({
  407. contentType: 'tipsbox',
  408. skin: 'bk-popup',
  409. content: "请输入正确的身份证号",
  410. closeTime: 2000
  411. }).showModal();
  412. }
  413. return pass;
  414. };
  415. // 页面业务处理流程开始
  416. new Promise(function(resolve, reject) {
  417. mui.plusReady(function() {
  418. // hrefhrefplus已经准备好,可以往下执行
  419. resolve(true);
  420. });
  421. }).then(function() {
  422. // 获取基础环境信息
  423. return getBaseEnvPromise().then(function(env) {
  424. baseEnv = env;
  425. }).then(function() {
  426. // 获取登录基本信息
  427. baseInfo = getBaseInfo();
  428. lastTeamId = plus.storage.getItem("selectedTeamId");
  429. $searchbar.searchBar();
  430. historyList();
  431. if(historyData.length){
  432. $history.show();
  433. }
  434. // openSoftKeyboard();
  435. $searchbarInput.focus();
  436. // 绑定页面事件
  437. bindEvents();
  438. })
  439. }).catch(function(e) {
  440. plus.nativeUI.closeWaiting();
  441. console && console.error(e);
  442. });