device-bind-info.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. var snCode,
  2. deviceInfo,
  3. isManage,
  4. xieyiImg,
  5. choosePatient;
  6. mui.init();
  7. mui.plusReady(function(){
  8. var self = plus.webview.currentWebview();
  9. deviceInfo = self.info;
  10. snCode = self.sn;
  11. isManage = self.isManage;
  12. templateHelper();
  13. //获取设备的基本信息
  14. fillDeviceInfo();
  15. bindEvents();
  16. });
  17. var old_back = mui.back;
  18. mui.back = function(){
  19. var opener = plus.webview.currentWebview().opener();
  20. if(opener){
  21. mui.fire(opener, "reload");
  22. }
  23. mui.later(function(){
  24. old_back();
  25. }, 300);
  26. }
  27. function fillDeviceInfo(){
  28. var keys = [];
  29. var multiUser = deviceInfo.multiUser ? JSON.parse(deviceInfo.multiUser) : {};
  30. if(deviceInfo.isMultiUser == "1"){
  31. //设备有多个键需要绑定
  32. for(key in multiUser){
  33. var bindUser = _.findWhere(deviceInfo.bindingInfo, {userType: key});
  34. keys.push({
  35. key: key,
  36. name: multiUser[key]+"键",
  37. user: bindUser
  38. });
  39. }
  40. }else{
  41. //设备只有一个键
  42. keys.push({
  43. key: "1",
  44. name: "居民",
  45. user: deviceInfo.bindingInfo[0]
  46. });
  47. }
  48. deviceInfo.bindKeys = keys;
  49. var html = template("info-tmp", {info: deviceInfo, sn: snCode});
  50. $(".info-card").empty().append(html);
  51. }
  52. function bindEvents(){
  53. $(".info-card").on('click', ".choose-patient", function(){
  54. var key = $(this).attr("data-key");
  55. var $news = $(".new"),
  56. len = $news.length,
  57. choosed = [];
  58. for(i=0; i<len; i++){
  59. var $item = $($news[i]),
  60. json = JSON.parse($item.attr("data-json"));
  61. choosed.push(json.code);
  62. }
  63. openWebview("choose-patient.html", {key: key, sn: snCode, isManage: isManage, choosed: choosed});
  64. });
  65. $(".info-card").on('click', ".edit-btn", function(){
  66. var key = $(this).attr("data-key"),
  67. jsonStr = $(this).attr("data-json"),
  68. action = $(this).attr("data-action");
  69. if(action == "unbind"){
  70. confirmUnBind(jsonStr, this);
  71. }else{
  72. var $news = $(".new"),
  73. len = $news.length,
  74. choosed = [];
  75. for(i=0; i<len; i++){
  76. var $item = $($news[i]),
  77. json = JSON.parse($item.attr("data-json"));
  78. choosed.push(json.code);
  79. }
  80. openWebview("choose-patient.html", {key: key, sn: snCode, isManage: isManage, choosed: choosed});
  81. }
  82. });
  83. window.addEventListener("setPatientInfo", function(arg){
  84. var info = arg.detail,
  85. key = info.key,
  86. obj = info.obj;
  87. var $target = $(".choose-patient[data-key="+key+"]"),
  88. $grant_parent = $target.parent().parent();
  89. var html = template("patient-info-tmp", info);
  90. if($target.length > 0){
  91. $target.parent().remove();
  92. $grant_parent.append(html);
  93. }else{
  94. //修改的情况
  95. var $target1 = $(".user-"+key),
  96. $parent1 = $target1.parent();
  97. $target1.remove();
  98. $parent1.append(html);
  99. }
  100. choosePatient = true;
  101. checkBtnEnable();
  102. });
  103. $(".confirm-btn").on('click', function(){
  104. if(!$(this).hasClass("active")){
  105. return false;
  106. }
  107. bindDeviceInfo();
  108. });
  109. $(".upload-btn").on('click', function(){
  110. //上传协议前需判断是否选择了居民
  111. var $news = $(".new");
  112. if($news.length == 0){
  113. mui.toast("请选择居民");
  114. }else{
  115. mui('#Sheet').popover('toggle');
  116. }
  117. });
  118. //重新上传
  119. $(".re-upload-btn").on('click', function(){
  120. mui('#Sheet').popover('toggle');
  121. });
  122. $("#chooseItem").on('click', "li", function(){
  123. var $this = $(this),
  124. type = $this.attr("data-val");
  125. if(type == "camera"){
  126. mui('#Sheet').popover('toggle');
  127. getCamera();
  128. }else if(type == "gallery"){
  129. mui('#Sheet').popover('toggle');
  130. getGallery();
  131. }
  132. })
  133. }
  134. //判断确定按钮是否高亮
  135. function checkBtnEnable(){
  136. if(choosePatient && xieyiImg!=""){
  137. $(".confirm-btn").addClass("active");
  138. }
  139. }
  140. //绑定设备信息
  141. function bindDeviceInfo(){
  142. var $news = $(".new");
  143. var reqs = [];
  144. for(i=0; i<$news.length; i++){
  145. var $item = $($news[i]),
  146. json = JSON.parse($item.attr("data-json")),
  147. key = $item.attr("data-key");
  148. var obj = {
  149. user: json.code,
  150. deviceId: deviceInfo.device_id,
  151. deviceName: deviceInfo.deviceName,
  152. deviceSn: snCode,
  153. categoryCode: deviceInfo.deviceType,
  154. userType: deviceInfo.isMultiUser ? key : "-1"
  155. // deviceActivityType: deviceInfo.deviceActivityType
  156. };
  157. if(deviceInfo.sim){
  158. obj.sim = deviceInfo.sim;
  159. }
  160. reqs.push({
  161. url: "doctor/device/SavePatientDeviceAndCredit",
  162. reqType: 'POST',
  163. data: {
  164. data: JSON.stringify(obj),
  165. agreementPhoto: xieyiImg
  166. }
  167. });
  168. }
  169. plus.nativeUI.showWaiting();
  170. getReqPromises(reqs, true).then(function(ress){
  171. plus.nativeUI.closeWaiting();
  172. var success = true,
  173. errorMsg = "";
  174. for(i=0; i<ress.length; i++){
  175. var rs = ress[i];
  176. if(rs.status != 200){
  177. success = false;
  178. errorMsg = rs.msg;
  179. }
  180. }
  181. if(success){
  182. openWebview("bind-success.html",{isManage: isManage});
  183. }else{
  184. mui.toast(errorMsg);
  185. }
  186. })
  187. }
  188. function confirmUnBind(jsonStr, e){
  189. var obj = jsonStr ? JSON.parse(jsonStr) : undefined;
  190. if(!obj){
  191. return false;
  192. }
  193. dialog({
  194. content: "当前设备已绑定至"+obj.userName+"账户,解绑设备后,将无法自动从设备获取相关数据,是否确认解绑?",
  195. okValue: "立即解绑",
  196. ok: function(){
  197. unbindKey(obj, e);
  198. },
  199. cancelValue: "不了,谢谢",
  200. cancel:function(){}
  201. }).showModal();
  202. }
  203. function unbindKey(obj, target){
  204. plus.nativeUI.showWaiting();
  205. var url = "/doctor/device/DeletePatientDevice",
  206. params = {
  207. id: obj.patientDeviceId//绑定设备的记录id
  208. };
  209. sendPost(url, params, null, function(res){
  210. plus.nativeUI.closeWaiting();
  211. if(res.status == 200){
  212. mui.toast("解绑成功");
  213. var $parent = $(target).parent().parent(),
  214. key = obj.userType < 0 ? 1 : obj.userType,
  215. html = '<div class="ui-col-1">'+
  216. '<div class="choose-patient" data-key="'+key+'">选择绑定居民</div>'+
  217. '</div>';
  218. $(".user-"+key).remove();
  219. $parent.append(html);
  220. }else{
  221. mui.toast(res.msg);
  222. }
  223. }, 'post', '', true);
  224. }
  225. //获取相册
  226. function getGallery() {
  227. plus.gallery.pick(function(cbFile) { //scb:SuccessCallBack
  228. handlePickSucc(cbFile);
  229. }, function(ecb) { //ecb:ErrorCallBack
  230. }, {
  231. filename: "_doc/gallery/",
  232. filter: "image"
  233. });
  234. }
  235. // 调用系统摄像头
  236. function getCamera() {
  237. var cmr = plus.camera.getCamera();
  238. cmr.captureImage(function(path) {
  239. /**
  240. * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
  241. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
  242. */
  243. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  244. /*
  245. * 将获取目录路径转换为本地路径URL地址
  246. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
  247. */
  248. fileUrl = entry.toLocalURL();
  249. handlePickSucc(fileUrl);
  250. });
  251. }, function(error) {
  252. if(error.code==11 && error.message == "null") {
  253. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  254. }
  255. }, {
  256. filename: "_doc/camera/",
  257. index: 1 //ios指定主摄像头
  258. });
  259. }
  260. var localImg = "";
  261. function handlePickSucc(srcUrl) {
  262. var lastIdx = srcUrl.lastIndexOf("/"),
  263. imgName = srcUrl;
  264. if(lastIdx>-1) {
  265. imgName = srcUrl.slice(lastIdx+1);
  266. }
  267. plus.zip.compressImage({
  268. src: srcUrl,
  269. dst: "_doc/compressImg/"+imgName,
  270. quality: 20,
  271. overwrite: true
  272. }, function(succ) {
  273. localImg = succ.target;
  274. uploader(localImg);
  275. }, function(err) {
  276. mui.toast("压缩失败: " + err.message);
  277. });
  278. }
  279. var xieyiImg = "";
  280. function uploader(url) {
  281. var uploadServerUrl = server + "upload/image";
  282. plus.nativeUI.showWaiting();
  283. var task = plus.uploader.createUpload(uploadServerUrl, {
  284. method: "post"
  285. }, function(t, status) {
  286. if (status == 200) {
  287. xieyiImg = JSON.parse(t.responseText).urls;
  288. //填充背景图片
  289. $("#xieyiImage").css("background-image", "url("+localImg+")")
  290. $(".upload-btn").parent().hide();
  291. $(".image-panel").show();
  292. $(".confirm-btn").parent().show();
  293. checkBtnEnable();
  294. plus.nativeUI.closeWaiting();
  295. } else {
  296. mui.toast("上传协议图片失败,请稍后重试");
  297. plus.nativeUI.closeWaiting();
  298. }
  299. });
  300. task.addFile(url, {})
  301. task.start();
  302. }
  303. function templateHelper(){
  304. template.helper("getJsonStr", function(obj){
  305. return JSON.stringify(obj);
  306. })
  307. }