jiebangshebei.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var $deviceCategory = $('#device_category'),
  2. $deviceVersion = $('#device_version'),
  3. $deviceSn = $('#device_sn'),
  4. $unbingBtn = $('#bind_btn'),
  5. $endPointMsg = $('#end_point_msg');
  6. /**
  7. * 获取设备类型列表
  8. * */
  9. function deviceCategoryPromise() {
  10. return getReqPromise('common/device/DeviceCategory',null,'GET')
  11. .then(function(res) {
  12. if(res.status == 200) {
  13. var codes = _.pluck(res.data, 'code');
  14. var names = _.pluck(res.data, 'name');
  15. initDeviceCategory(codes, names);
  16. } else {
  17. mui.toast(res.msg);
  18. Promise.reject();
  19. }
  20. });
  21. }
  22. /**
  23. * 获取设备型号列表
  24. * */
  25. function deviceListPromise(code) {
  26. return getReqPromise('common/device/DeviceList',{
  27. category_code: code
  28. },'GET')
  29. .then(function(res) {
  30. if(res.status == 200) {
  31. var codes = _.pluck(res.data, 'id');
  32. var names = _.pluck(res.data, 'name');
  33. initDeviceVersion(codes, names);
  34. } else {
  35. mui.toast(res.msg);
  36. Promise.reject();
  37. }
  38. })
  39. }
  40. /**
  41. * 通过sn码获取设备绑定情况
  42. * */
  43. function getListByDeviceSnPromise(type,deviceSn) {
  44. return getReqPromise('doctor/device/getListByDeviceSn',{
  45. type: type,
  46. deviceSn: deviceSn
  47. },'GET')
  48. .then(function(res) {
  49. if(res.status == 200) {
  50. if(!res.data || !res.data.length) {
  51. $endPointMsg.text("该设备未绑定居民");
  52. } else {
  53. var names = _.pluck(res.data, 'userName').join('、');
  54. mui.confirm("当前设备已绑定至"+names+"账户,解绑设备后,将无法自动从设备获取相关数据,是否确认解绑?", "提示", ["立即解绑","不了,谢谢"], function(e) {
  55. if(e.index==0){
  56. unbindDevicePromise(type,deviceSn).then(function(res) {
  57. mui.toast(res.msg);
  58. });
  59. return;
  60. }else{
  61. return;
  62. }
  63. });
  64. }
  65. } else {
  66. $endPointMsg.text(res.msg);
  67. Promise.reject();
  68. }
  69. }).catch(function(e) {
  70. console&& console.error(e)
  71. })
  72. }
  73. /**
  74. * 解绑设备请求
  75. * */
  76. function unbindDevicePromise(type, deviceSn) {
  77. return getReqPromise('doctor/device/unbindDevice',{
  78. type: type,
  79. deviceSn: deviceSn
  80. },'GET')
  81. }
  82. function initMobiscroll($el,codes,names,opts) {
  83. $el.mobiscroll({
  84. theme: 'ios',
  85. lang: 'zh',
  86. customWheels: true,
  87. wheels: [
  88. [{
  89. keys: codes,
  90. values: names
  91. }]
  92. ],
  93. onSelect: function(valueText, inst) {
  94. var dd = eval("[" + valueText + "]");
  95. $(this).val(dd[0].values);
  96. $(this).attr('data-code',dd[0].keys);
  97. if(opts && opts.onselect) {
  98. opts.onselect(dd[0].keys);
  99. }
  100. $endPointMsg.text('');
  101. }
  102. });
  103. }
  104. /*
  105. * 设备类型下拉列表初始化
  106. * */
  107. function initDeviceCategory(codes,names) {
  108. initMobiscroll($deviceCategory,codes,names,{
  109. onselect: function(code) {
  110. deviceListPromise(code)
  111. }
  112. })
  113. }
  114. /*
  115. * 设备类型型号下拉列表初始化
  116. * */
  117. function initDeviceVersion(codes,names) {
  118. initMobiscroll($deviceVersion,codes,names)
  119. }
  120. /*
  121. * 必输校验
  122. * */
  123. function validRequiredPromise() {
  124. return new Promise(function(resolve, reject) {
  125. var category = $deviceCategory.attr('data-code'),
  126. version = $deviceVersion.attr('data-code'),
  127. sn = $.trim($deviceSn.val());
  128. if(!category) {
  129. reject("请选择设备类型");
  130. } else if(!version){
  131. reject("请选择设备型号");
  132. } else if(!sn) {
  133. reject("请输入SN码");
  134. } else {
  135. resolve({
  136. category: category,
  137. version: version,
  138. sn: sn
  139. });
  140. }
  141. })
  142. }
  143. // 页面业务处理流程开始
  144. new Promise(function(resolve, reject) {
  145. mui.plusReady(function() {
  146. // plus已经准备好,可以往下执行
  147. resolve(true);
  148. })
  149. }).then(function() {
  150. return deviceCategoryPromise()
  151. .then(function() {
  152. $unbingBtn.on('click',function() {
  153. validRequiredPromise().then(function(data) {
  154. return getListByDeviceSnPromise(data.category, data.sn)
  155. },function(msg) {
  156. mui.toast(msg);
  157. }
  158. ).then(function(res) {
  159. if(res.status == 200) {
  160. mui.toast(res.msg);
  161. } else if(res.status == -1){
  162. $endPointMsg.text("未查询到设备或设备未绑定居民");
  163. } else {
  164. mui.toast(res.msg);
  165. }
  166. })
  167. });
  168. $deviceSn.on('input',function() {
  169. $endPointMsg.text('');
  170. })
  171. }).catch(function(e) {
  172. console && console.error(e);
  173. })
  174. })