zhuisu2.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. const app = getApp()
  2. //buffer转16进制,解析nfc扫描结果的id
  3. function ab2hex(buffer) {
  4. var hexArr = Array.prototype.map.call(
  5. new Uint8Array(buffer),
  6. function (bit) {
  7. return ('00' + bit.toString(16)).slice(-2)
  8. }
  9. )
  10. return hexArr.join('').toUpperCase();
  11. }
  12. Page({
  13. data: {
  14. filePath: app.globalData.$filePath,
  15. modalHidden:true,
  16. region: [],
  17. isIOS:false,
  18. seleteType:[{transportStatus:'00',transportStatusName:'入库'},{transportStatus:'10',transportStatusName:'出库'},{transportStatus:'20',transportStatusName:'结束'}],
  19. showDialog: false,// 详情弹窗
  20. },
  21. onUnload() {
  22. this.onHide()
  23. },
  24. onHide() {
  25. var nfc = this.nfc
  26. if (nfc) {
  27. nfc.offDiscovered(this.discoverHandler)
  28. }
  29. },
  30. onShow() {
  31. const that = this;
  32. if(that.data.isIOS){
  33. that.goApp()
  34. return;
  35. }
  36. const nfc = wx.getNFCAdapter();
  37. that.nfc = nfc;
  38. const type = that.data.type;
  39. const {path,addLabel,loginRole} = that.data;
  40. that.discoverHandler = function (res) {
  41. var message = res.messages && res.messages[0].records[0].payload;
  42. message = message && ab2hex(message);
  43. if(type != 'X104'){
  44. if (message && message.length >= 58) {
  45. if(path == 'factorycheck'){
  46. that.getInfo(message);
  47. }
  48. message = message.substring(6, 80);
  49. if(message.indexOf('72') == 0){
  50. message = message.substring(0,52);
  51. }
  52. if(path == 'iodep'){
  53. that.iodep(message);
  54. }else if(addLabel == 20){
  55. that.chainBind(message);
  56. }else if(path == 'find' && loginRole != 'doctor'){
  57. // that.goDetail(message);
  58. that.goDetailInfo(message);
  59. }else if(path == 'find' && loginRole == 'doctor'){
  60. that.goPdf(message);
  61. that.writeMsg();
  62. }
  63. }else{
  64. app.showToptip(that,'error','请换一个'+type+'标签');
  65. return;
  66. }
  67. }else if(type == 'X104'){
  68. var id = res.id && ab2hex(res.id);
  69. if(message && message.length >= 58){
  70. message = message.substring(6, 80);
  71. if(message.indexOf('72') == 0){
  72. app.showToptip(that,'error','请换一个'+type+'标签');
  73. return;
  74. }
  75. app.showToptip(that,'error','请换一个'+type+'标签');
  76. return;
  77. }
  78. if(path == 'iodep'){
  79. that.iodep(id);
  80. }else if(addLabel == 20){
  81. that.chainBind(id)
  82. }else if(path == 'find' && loginRole !='doctor'){
  83. // that.goDetail(id);
  84. that.goDetailInfo(id);
  85. }else if(path == 'find' && loginRole == 'doctor'){
  86. that.writeMsg();
  87. that.goPdf(id);
  88. }
  89. }
  90. }
  91. nfc.onDiscovered(that.discoverHandler)
  92. nfc.startDiscovery({
  93. fail(err) {
  94. if (err.errCode == 13000) {
  95. that.goApp();
  96. app.showToptip(that, "worning", "设备不支持NFC,请扫码下载app");
  97. }
  98. if (err.errCode == 13001) {
  99. app.showToptip(that, "worning", "系统NFC开关未打开")
  100. }
  101. if (err.errCode == 13016) {
  102. app.showToptip(that, "worning", "连接失败")
  103. }
  104. }
  105. })
  106. },
  107. getInfo(message){
  108. var that = this;
  109. app.requestP({
  110. url: "/query/scanChip1",
  111. method:"post",
  112. needToken:false,
  113. contentType:' application/json',
  114. data:{
  115. payload:message
  116. }
  117. }).then(res=>{
  118. wx.navigateTo({
  119. url: '/pages/nfc/outNfcTempInfo/outNfcTempInfo?data='+JSON.stringify(res.data.data),
  120. })
  121. }).catch(err=>{
  122. app.showToptip(that,'error',err.data.msg)
  123. })
  124. },
  125. iodep(code){
  126. var that =this;
  127. app.requestP({
  128. url:'/chainBound/checkBind',
  129. method:'post',
  130. // contentType:' application/json',
  131. data:{
  132. deviceCode:code,
  133. }
  134. }).then(res=>{
  135. if(res.data.data!=1){
  136. app.showToptip(that,'error',res.data.msg);
  137. return;
  138. }
  139. app.requestP({
  140. url:'/chain/scan',
  141. method:'post',
  142. contentType:' application/json',
  143. data:{
  144. inoculatorId:'',
  145. payload:code,
  146. type:that.data.type,
  147. }
  148. }).then(res=>{
  149. const {transportStatus,id,code} = res.data.chain;
  150. that.setData({
  151. transportStatus,
  152. iodepId:id,
  153. modalHidden:!that.data.modalHidden,
  154. seleteType:that.setSeleteType(transportStatus),
  155. });
  156. }).catch(err=>{
  157. app.showToptip(that,'error',err.data.msg)
  158. })
  159. }).catch(err=>{
  160. app.showToptip(that,'error',err.data.msg);
  161. })
  162. },
  163. setTransportStatus(transportStatus){
  164. var seleteType = this.data.seleteType;
  165. var deptType;
  166. for(var i=0;i<seleteType.length;i++){
  167. if(seleteType[i].transportStatus == transportStatus){
  168. deptType = i;
  169. break;
  170. }
  171. }
  172. return deptType;
  173. },
  174. setSeleteType(transportStatus){
  175. var seleteType = this.data.seleteType;
  176. var type;
  177. for(var i=0;i<seleteType.length;i++){
  178. if(seleteType[i].transportStatus == transportStatus){
  179. seleteType.splice(i,1);
  180. break;
  181. }
  182. }
  183. this.setTransportStatus(transportStatus);
  184. return seleteType;
  185. },
  186. goDetailInfo(message){
  187. var that = this;
  188. app.requestP({
  189. url:'/chainBound/checkBind',
  190. method:'post',
  191. // contentType:' application/json',
  192. data:{
  193. deviceCode:message,
  194. }
  195. }).then(res=>{
  196. if(res.data.data !=1){
  197. app.showToptip(that,'error',res.data.msg);
  198. return;
  199. }
  200. app.requestP({
  201. url:'/chain/scan',
  202. method:'post',
  203. contentType:' application/json',
  204. data:{
  205. inoculatorId:'',
  206. payload:message,
  207. type:that.data.type
  208. }
  209. }).then(res=>{
  210. var id = res.data.msg;
  211. that.setData({
  212. showDialog: !this.data.showDialog,
  213. detailId:id,
  214. deviceCode:message
  215. });
  216. }).catch(err=>{
  217. app.showToptip(that,'error',err.data.msg)
  218. })
  219. }).catch(err=>{
  220. app.showToptip(that,'error',err.data.msg);
  221. })
  222. },
  223. goPdf(message){
  224. var that = this;
  225. app.requestP({
  226. url:'/chainBound/checkBind',
  227. method:'post',
  228. // contentType:' application/json',
  229. data:{
  230. deviceCode:message,
  231. }
  232. }).then(res=>{
  233. if(res.data.data != 1){
  234. app.showToptip(that,'error',res.data.msg);
  235. return;
  236. }
  237. app.requestP({
  238. url:'/chain/scan',
  239. method:'post',
  240. contentType:' application/json',
  241. data:{
  242. inoculatorId:that.data.inoculatorId,
  243. payload:message,
  244. type:that.data.type
  245. }
  246. }).then(res=>{
  247. const {id} = res.data.chain;
  248. wx.navigateTo({
  249. url: '/pages/workbench/scanningType/reportPdf/reportPdf?id='+id,
  250. })
  251. }).catch(err=>{
  252. app.showToptip(that,'error',err.data.msg);
  253. })
  254. })
  255. },
  256. setModalVal(e){
  257. this.setData({
  258. modalVal:e.detail.value
  259. })
  260. },
  261. onLoad:function(option) {
  262. const that = this;
  263. //获取定位信息
  264. wx.getLocation({
  265. altitude: 'altitude',
  266. success(res) {
  267. that.setData({
  268. latitude: res.latitude,
  269. longitude: res.longitude,
  270. })
  271. },
  272. error() {
  273. app.showToptip(that, "worning", "获取位置信息失败")
  274. }
  275. })
  276. //判断是否是iOS
  277. wx.getSystemInfo({
  278. success:function(res){
  279. if(res.platform=="ios"){
  280. that.isIOS = true;
  281. }
  282. }
  283. })
  284. this.setData({
  285. path:option.path,
  286. type:option.type,
  287. id:option.id,// 绑定标签ID
  288. addLabel:option.addLabel,//判断是否是添加 20
  289. loginRole:option.loginRole,//医生身份
  290. inoculatorId:option.inoculatorId // 接种者 id
  291. })
  292. // this.iodep('02656E721801110001000800000000000011111111000000000009672900000000000967290000000000000000000000000000000041000000C4000000000002EFFFF9ECC7616C4183306610C598D31B624C498D316620C3981B09604C057E2FD5ECBF57D2F45F2BD97BAF45F0BE579AF35ECBE17B2F35DEBD9792F55EEBC181B0861EC2D8AB24628C3187316630C718F31A648C858AB0C622C1D85302616C25802FF5FAC01802FC5F2BF17CAF85F2BD97D2F85F6BED7B2F65ECBE17AB015FCC018530F626C6588314636C798F32463CC858FB21640C5D8930F616C4185B0B610C0982306606C198230260AC1582B025FEC0580B015FEC0D7FB0160AC198330B63EC8991B2063EC6D8FB2866ECAD9532765ACC99131E62CC5986B0D616C2D8430A60CC2583308610C3184B09602C0982B0960EC0D83B055F6C1D832FC5F4C358831863AC658B313640C7190B4069ACA195B430')
  293. // this.iodep('027A686174')
  294. },
  295. chainBind(deviceCode){
  296. var that = this;
  297. const {id,type,latitude,longitude} = that.data;
  298. app.requestP({
  299. url:'/chainBound/checkBind',
  300. method:'post',
  301. data:{
  302. deviceCode,
  303. }
  304. }).then(res=>{
  305. if(res.data.data == 0){
  306. app.requestP({
  307. url:'/chainBound/bind',
  308. method:'post',
  309. contentType:' application/json',
  310. data:{
  311. chainId:id,
  312. deviceCode,
  313. type,
  314. lat:latitude,
  315. lng:longitude
  316. }
  317. }).then(response=>{
  318. app.showToptip(that,'success',response.data.msg)
  319. that.writeMsg(deviceCode);
  320. }).catch(err=>{
  321. app.showToptip(that,'error',err.data.msg)
  322. })
  323. }else{
  324. app.showToptip(that,'error',res.data.msg);
  325. }
  326. }).catch(err=>{
  327. app.showToptip(that,'error',err.data.msg)
  328. })
  329. },
  330. goApp(e) {
  331. this.setData({
  332. modalName: "goApp"
  333. })
  334. },
  335. modalBindaconfirm(){ //一型保存地址
  336. var that = this;
  337. var {region,modalVal,transportStatus,iodepId,seleteType,deptType} = that.data;
  338. var addr = region.join('')+modalVal;
  339. if(region.join('') && modalVal && seleteType[deptType].transportStatus){
  340. app.requestP({
  341. url:'/chain/editTransport',
  342. // contentType:' application/json',
  343. method:'post',
  344. data:{
  345. addr,
  346. chainId:iodepId, // 00 在库 //10 运输 // 20 已结束
  347. // transportStatus:transportStatus == '00' ? '10':transportStatus == '10' ?'00':'',
  348. transportStatus:seleteType[deptType].transportStatus,
  349. }
  350. }).then(res=>{
  351. that.setData({
  352. modalHidden:!that.data.modalHidden,
  353. region:[],
  354. modalVal:'',
  355. iodepId:'',
  356. })
  357. app.showToptip(that,'success',res.data.msg);
  358. }).catch(err=>{
  359. app.showToptip(that,'error',err.data.msg);
  360. })
  361. }else{
  362. app.showToptip(that,'error','地址不能为空');
  363. }
  364. },
  365. //取消按钮点击事件
  366. modalBindcancel:function(){
  367. this.setData({
  368. modalHidden:!this.data.modalHidden,
  369. region:[],
  370. modalVal:'',
  371. iodepId:'',
  372. })
  373. },
  374. regionChange: function(e) {
  375. this.setData({
  376. region: e.detail.value
  377. })
  378. },
  379. writeMsg(){
  380. const that = this
  381. const nfc = that.nfc
  382. const ndef = nfc.getNdef()
  383. ndef.connect({
  384. fail(){
  385. // app.showToptip(that, "worning", "标签连接失败,请贴近不要离开")
  386. },
  387. success(){
  388. console.log("连接成功")
  389. let records = [{
  390. id: new ArrayBuffer(0),
  391. payload: ab2hex("at"),
  392. type: new ArrayBuffer(0),
  393. tnf: 1
  394. }]
  395. ndef.writeNdefMessage({
  396. records,
  397. success(){
  398. console.log("写入成功")
  399. //指令发送成功,请求绑定接口
  400. // that.proBindSend()
  401. },
  402. fail(){
  403. // app.showToptip(that, "worning", "指令发送失败")
  404. },
  405. complete(res) {
  406. ndef.close()
  407. }
  408. })
  409. },
  410. complete(){
  411. //that.nfc.stopDiscovery(that.discoverHandler)
  412. }
  413. })
  414. },
  415. saveCode(e) {
  416. var that = this
  417. wx.downloadFile({
  418. url:'http://oss.zhongyihz.cn/group156/images/06619e9e4bfc54382290d931bf9814c2.png',
  419. success: function (res) {
  420. wx.saveImageToPhotosAlbum({
  421. filePath: res.tempFilePath, // 此为图片路径
  422. success: (res) => {
  423. wx.showToast({
  424. title: '保存成功,请到相册中查看'
  425. });
  426. },
  427. fail: (err) => {
  428. wx.showToast({
  429. icon: 'none',
  430. title: '保存失败,请稍后重试'
  431. });
  432. }
  433. })
  434. },
  435. fail() {
  436. wx.showToast({
  437. icon: 'none',
  438. title: '保存失败,请稍后重试'
  439. });
  440. }
  441. })
  442. },
  443. hideModal(e) {
  444. this.setData({
  445. modalName: null
  446. })
  447. },
  448. setType(e){
  449. var value = e.detail.value;
  450. var that = this;
  451. that.setData({
  452. deptType:value
  453. })
  454. },
  455. toggleDialog() { //详情弹窗
  456. this.setData({
  457. showDialog: !this.data.showDialog
  458. });
  459. },
  460. goDetail(){
  461. var detailId = this.data.detailId;
  462. var deviceCode = this.data.deviceCode || '';
  463. wx.navigateTo({
  464. url: '/pages/query/details?id='+detailId+'&deviceCode='+deviceCode,
  465. })
  466. this.setData({
  467. showDialog:!this.data.showDialog,
  468. })
  469. },
  470. clearBind(){
  471. var that = this;
  472. var detailId = that.data.detailId || '';
  473. var deviceCode = that.data.deviceCode || '';
  474. app.requestP({
  475. url:'/chainBound/unbind',
  476. method:'post',
  477. contentType:' application/json',
  478. data:{
  479. chainId:detailId,
  480. deviceCode,
  481. }
  482. }).then(res=>{
  483. that.setData({
  484. detailId:'',
  485. showDialog:!that.data.showDialog,
  486. })
  487. app.showToptip(that,'success',res.data.msg)
  488. }).catch(err=>{
  489. app.showToptip(that,'error',err.data.msg)
  490. })
  491. },
  492. })