MainActivity.kt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. package com.example.chemical.ui
  2. import android.app.AlertDialog
  3. import android.content.Intent
  4. import android.content.IntentFilter
  5. import android.graphics.Color
  6. import android.graphics.drawable.GradientDrawable
  7. import android.hardware.usb.UsbManager
  8. import android.os.Bundle
  9. import android.os.Handler
  10. import android.os.Looper
  11. import android.os.Message
  12. import android.view.KeyEvent
  13. import android.view.LayoutInflater
  14. import android.view.View
  15. import androidx.recyclerview.widget.LinearLayoutManager
  16. import androidx.recyclerview.widget.RecyclerView
  17. import com.bumptech.glide.Glide
  18. import com.bumptech.glide.load.engine.DiskCacheStrategy
  19. import com.bumptech.glide.request.RequestOptions
  20. import com.example.chemical.ChemicalApp
  21. import com.example.chemical.R
  22. import com.example.chemical.databinding.ActivityMainBinding
  23. import com.example.chemical.receiver.UsbReceiver
  24. import com.example.chemical.ui.adapter.CabinetAdapter
  25. import com.example.chemical.ui.common.BaseCountDownActivity
  26. import com.example.chemical.ui.discard.LedgerActivity
  27. import com.example.chemical.ui.discard.WasteChemicalsActivity
  28. import com.example.chemical.ui.inquiry.InquiryActivity
  29. import com.example.chemical.ui.login.FacialCardActivity
  30. import com.example.chemical.ui.login.FacialLoginActivity
  31. import com.example.chemical.ui.login.ScanLoginActivity
  32. import com.example.chemical.ui.login.SwipeActivity
  33. import com.example.chemical.ui.newly.AddedChemicalsActivity
  34. import com.example.chemical.ui.still.ChemicalsAlsoActivity
  35. import com.example.chemical.ui.still.MsdsActivity
  36. import com.example.chemical.ui.uses.UseActivity
  37. import com.example.chemical.ui.uses.WarningEventsActivity
  38. import com.example.chemical.ui.warehousing.ElectronicLedgerActivity
  39. import com.example.chemical.ui.warehousing.WarehousingActivity
  40. import com.example.chemical.weidith.CustomDialog
  41. import com.lztek.toolkit.Lztek
  42. import com.rc.core.log.RcLog
  43. import com.rc.httpcore.HttpClient
  44. import com.rc.httpcore.HttpConfig
  45. import com.rc.httpcore.client.ApiRepository
  46. import com.rc.httpcore.exception.NetException
  47. import retrofit2.HttpException
  48. import java.net.ConnectException
  49. import java.net.SocketTimeoutException
  50. import android.util.Log
  51. import android.widget.EditText
  52. import com.blankj.utilcode.util.ActivityUtils
  53. import com.blankj.utilcode.util.LogUtils
  54. import com.example.chemical.ui.warehousing.ChemicalLabelingActivity
  55. import com.example.chemical.utils.*
  56. import com.rc.core.ui.activity.RcBaseActivity
  57. import com.rc.core.util.DeviceUtils
  58. //首页 - 登录or 未登录
  59. class MainActivity : RcBaseActivity<ActivityMainBinding>() {
  60. private lateinit var timeUpdater: TimeUpdater
  61. private val mAdapter by lazy { CabinetAdapter() }
  62. private var currentPosition = 0
  63. private lateinit var recyclerView: RecyclerView
  64. private var mUsbReceiver: UsbReceiver? = null // 刷卡广播注册
  65. private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数
  66. private var mLztek: Lztek? = null
  67. private var mDeviceNum: String? = null //设备唯一标识
  68. private var handler: Handler = Handler(Looper.getMainLooper())
  69. private lateinit var runnable: Runnable
  70. private lateinit var faceList: List<String> //人员区间值
  71. private var mLoginType: String? = null //登录方式
  72. //退出账号
  73. private var mCounter = 0
  74. private var MAX_TIME = 60 //返回时间 秒
  75. private val WHAT_COUNT_DOWN = 1
  76. private lateinit var layoutManager: LinearLayoutManager
  77. override fun createViewBinding() = ActivityMainBinding.inflate(LayoutInflater.from(this))
  78. override fun onResume() {
  79. super.onResume()
  80. if (ChemicalApp.userData != null) {
  81. //已登录
  82. viewBinding.inc.userLogin.visibility = View.GONE
  83. viewBinding.inc.loggedIn.visibility = View.VISIBLE
  84. viewBinding.inc.tvName.text = ChemicalApp.userData!!.userName
  85. val imageView = viewBinding.inc.imageName
  86. // 使用 Glide 加载网络图片
  87. Glide.with(this)
  88. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
  89. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  90. .into(imageView)
  91. viewBinding.inc.imgE.setBackgroundResource(R.mipmap.cshrk_dl_tc)
  92. MAX_TIME = ChemicalApp.confs!!.signOutTime
  93. mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
  94. mCountDownHandler.sendEmptyMessage(WHAT_COUNT_DOWN)
  95. } else {
  96. viewBinding.inc.loggedIn.visibility = View.GONE
  97. viewBinding.inc.userLogin.visibility = View.VISIBLE
  98. viewBinding.inc.imgE.setBackgroundResource(R.mipmap.icon_login_img)
  99. }
  100. if (mAdapter.data != null && mAdapter.data.size > 1) {
  101. // startAutoScroll()
  102. startUpdating()
  103. }
  104. timeUpdater.startUpdating()
  105. }
  106. //必须在创建时注册信息
  107. override fun onCreate(savedInstanceState: Bundle?) {
  108. super.onCreate(savedInstanceState)
  109. // hPrints = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1)
  110. //注册广播
  111. // EventBus.getDefault().register(this)
  112. viewBinding.versionName.text = "版本号:${DeviceUtils.getVersionName(this)}"
  113. // 创建定时任务
  114. runnable = object : Runnable {
  115. override fun run() {
  116. // 每隔一个小时执行一次操作
  117. // 这里可以添加你需要执行的代码
  118. subAddData()
  119. // 重新启动定时任务
  120. handler.postDelayed(this, 3600000) // 3600000 毫秒为一小时
  121. }
  122. }
  123. // 第一次启动定时任务
  124. handler.post(runnable)
  125. viewBinding.test.setOnClickListener {
  126. showTextInputDialog("请输入内容") { input ->
  127. if (input != null) {
  128. // 处理用户输入的逻辑
  129. if (input.isNotEmpty()) {
  130. var pwd = "123" //默认密码123
  131. try {
  132. val urlBase = SharedPreferencesHelper.getUrlBase(this)
  133. if (urlBase != null && urlBase.pwd != null) {
  134. RcLog.info("=====本地密码:${urlBase!!.pwd}")
  135. pwd = urlBase.pwd!!
  136. }
  137. } catch (e: Exception) {
  138. }
  139. if (input == pwd) {
  140. UiManager.switcher(this, SettingActivity::class.java)
  141. } else {
  142. showToast("密码错误")
  143. }
  144. }
  145. } else {
  146. // 用户点击了取消按钮
  147. showToast("用户取消了输入")
  148. }
  149. }
  150. }
  151. }
  152. override fun initData() {
  153. super.initData()
  154. //获取基础配置
  155. basicConf()
  156. }
  157. //获取刷卡信息
  158. override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
  159. // mPortScanHelper.dispatchKeyEvent(event)
  160. return super.dispatchKeyEvent(event)
  161. }
  162. override fun onStop() {
  163. super.onStop()
  164. RcLog.info("============onStop")
  165. }
  166. //停止 销毁广播传递
  167. override fun onPause() {
  168. // mPortScanHelper.onPause()
  169. RcLog.info("============onPause")
  170. try {
  171. stopUpdating()
  172. timeUpdater.stopUpdating()
  173. RcLog.info("========UI在刷新停止")
  174. mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
  175. mCountDownHandler.removeCallbacksAndMessages(null)
  176. } catch (e: Exception) {
  177. }
  178. super.onPause()
  179. }
  180. // //必须写这个方法 防止注册失败
  181. // @Subscribe(threadMode = ThreadMode.MAIN)
  182. // fun onUpdateEventEvent(event: KeyEvent) {
  183. //
  184. // }
  185. override fun onNewIntent(intent: Intent?) {
  186. super.onNewIntent(intent)
  187. // registerUsbBroadcast() //注册广播
  188. }
  189. override fun initViews(savedInstanceState: Bundle?) {
  190. super.initViews(savedInstanceState)
  191. LogUtils.d(ActivityUtils.getLauncherActivity())
  192. getUserIds()
  193. val handler = Handler(Looper.getMainLooper())
  194. timeUpdater = TimeUpdater(handler) { currentTime ->
  195. viewBinding.inc.nowTime.text = "$currentTime"
  196. }
  197. // 启动定时更新
  198. timeUpdater.startUpdating()
  199. initAdapter()
  200. viewBinding.imgLeft.setOnClickListener {
  201. if (currentPosition > 0) {
  202. currentPosition--
  203. recyclerView.smoothScrollToPosition(currentPosition)
  204. }
  205. }
  206. viewBinding.imgRight.setOnClickListener {
  207. if (currentPosition < mAdapter.data.size - 1) {
  208. currentPosition++
  209. recyclerView.smoothScrollToPosition(currentPosition)
  210. }
  211. }
  212. //登录
  213. RelativeLayoutDebouncer.setDebouncedOnClickListener(viewBinding.inc.userLogin, 1000L) {
  214. basicConfLog()
  215. }
  216. // viewBinding.inc.userLogin.setOnClickListener {
  217. // basicConfLog()
  218. // }
  219. viewBinding.addChemicals.setOnClickListener {
  220. //新增
  221. if (ChemicalApp.userData != null) {
  222. if (ChemicalApp.administrators || ChemicalApp.responsibles) {
  223. UiManager.switcher(this, AddedChemicalsActivity::class.java)
  224. } else {
  225. customDialogView(2, "当前人员无权限")
  226. }
  227. } else {
  228. basicConfLog()
  229. }
  230. }
  231. //待入库
  232. viewBinding.reSto.setOnClickListener {
  233. if (ChemicalApp.userData != null) {
  234. UiManager.switcher(this, WarehousingActivity::class.java)
  235. } else {
  236. basicConfLog()
  237. }
  238. }
  239. //废弃
  240. viewBinding.reDis.setOnClickListener {
  241. if (ChemicalApp.userData != null) {
  242. UiManager.switcher(this, WasteChemicalsActivity::class.java)
  243. } else {
  244. basicConfLog()
  245. }
  246. }
  247. //查询
  248. viewBinding.reInq.setOnClickListener {
  249. val map = mutableMapOf<String, Any>()
  250. if (ChemicalApp.userData != null) {
  251. map["logIn"] = 1
  252. } else {
  253. map["logIn"] = 0
  254. }
  255. map["cabinetId"] = "a"
  256. map["doorId"] = "a"
  257. UiManager.switcher(this, map, InquiryActivity::class.java)
  258. }
  259. //领用
  260. viewBinding.reCla.setOnClickListener {
  261. if (ChemicalApp.userData != null) {
  262. UiManager.switcher(this, UseActivity::class.java)
  263. } else {
  264. basicConfLog()
  265. }
  266. }
  267. //归还
  268. viewBinding.reRet.setOnClickListener {
  269. if (ChemicalApp.userData != null) {
  270. UiManager.switcher(this, ChemicalsAlsoActivity::class.java)
  271. } else {
  272. basicConfLog()
  273. }
  274. }
  275. //电子台账
  276. viewBinding.rlLedger.setOnClickListener {
  277. if (ChemicalApp.userData != null) {
  278. UiManager.switcher(this, LedgerActivity::class.java)
  279. } else {
  280. basicConfLog()
  281. }
  282. }
  283. //标签管理
  284. viewBinding.reLedger.setOnClickListener {
  285. if (ChemicalApp.userData != null) {
  286. // UiManager.switcher(this, ElectronicLedgerActivity::class.java)
  287. //化学品标签
  288. UiManager.switcher(this, ChemicalLabelingActivity::class.java)
  289. } else {
  290. basicConfLog()
  291. }
  292. }
  293. //预警事件
  294. viewBinding.rlWarning.setOnClickListener {
  295. if (ChemicalApp.userData != null) {
  296. UiManager.switcher(this, WarningEventsActivity::class.java)
  297. } else {
  298. basicConfLog()
  299. }
  300. }
  301. //演示流程 -MSDS
  302. viewBinding.rlDem.setOnClickListener {
  303. //暂时不用演示流程 跳转MSDS
  304. // UiManager.switcher(this, DemonstrateActivity::class.java)
  305. UiManager.switcher(this, MsdsActivity::class.java)
  306. }
  307. viewBinding.inc.tvOutLogin.setOnClickListener {
  308. ChemicalApp.userData = null
  309. // ChemicalApp.subjectId = null
  310. HttpClient.token = null
  311. UiManager.switcherCashier(this, SplashActivity::class.java)
  312. }
  313. getCabinetIn()
  314. }
  315. /**
  316. * 查询柜子
  317. */
  318. private fun getCabinetIn() {
  319. showLoading("查询中...")
  320. val disposable = ApiRepository.getCabinetBySubId(ChemicalApp.subjectId!!)
  321. .subscribe({ data ->
  322. dismissLoading()
  323. mAdapter.setNewInstance(data.toMutableList())
  324. if (mAdapter.data != null && mAdapter.data.size > 1) {
  325. // 初始化 Handler 和 Runnable
  326. handlerBanner = Handler(Looper.getMainLooper())
  327. updateRunnable = object : Runnable {
  328. override fun run() {
  329. // 更新 UI 操作
  330. RcLog.info("========UI在刷新")
  331. val currentPosition = layoutManager.findFirstVisibleItemPosition()
  332. if (currentPosition != RecyclerView.NO_POSITION) {
  333. val nextPosition = (currentPosition + 1) % mAdapter.itemCount
  334. recyclerView.smoothScrollToPosition(nextPosition)
  335. }
  336. if (isUpdating) {
  337. handlerBanner.postDelayed(this, 5000) // 5 秒后再次执行
  338. }
  339. }
  340. }
  341. startUpdating()
  342. }
  343. }, { throwable ->
  344. dismissLoading()
  345. // throwableView(throwable)
  346. throwable.printStackTrace()
  347. })
  348. addDisposable(disposable)
  349. }
  350. private fun getUserIds() {
  351. if (ChemicalApp.subjectId != null) {
  352. showLoading("查询中...")
  353. val disposable = ApiRepository.getUserIds(ChemicalApp.subjectId!!)
  354. .subscribe({ data ->
  355. dismissLoading()
  356. faceList = data
  357. }, { throwable ->
  358. dismissLoading()
  359. // throwableView(throwable)
  360. })
  361. addDisposable(disposable)
  362. }
  363. }
  364. private fun initAdapter() {
  365. recyclerView = viewBinding.relView
  366. layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
  367. recyclerView.layoutManager = layoutManager
  368. recyclerView.adapter = mAdapter
  369. recyclerView.setOnTouchListener { _, _ ->
  370. true
  371. }
  372. mAdapter.setOnItemChildClickListener { adapter, view, position ->
  373. // 在这里处理子View的点击事件
  374. if (ChemicalApp.userData != null) {
  375. val map = mutableMapOf<String, Any>()
  376. map["logIn"] = 1
  377. when (view.id) {
  378. R.id.lingOne -> {
  379. map["cabinetId"] = mAdapter.data[position].cabinetId
  380. map["doorId"] = mAdapter.data[position].cabinetDoorVoList[0].doorUniqueId
  381. UiManager.switcher(this, map, InquiryActivity::class.java)
  382. }
  383. R.id.lintTwo -> {
  384. map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[1].cabinetId
  385. map["doorId"] = mAdapter.data[position].cabinetDoorVoList[1].doorUniqueId
  386. UiManager.switcher(this, map, InquiryActivity::class.java)
  387. }
  388. R.id.lintThree -> {
  389. map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[2].cabinetId
  390. map["doorId"] = mAdapter.data[position].cabinetDoorVoList[2].doorUniqueId
  391. UiManager.switcher(this, map, InquiryActivity::class.java)
  392. }
  393. R.id.lintFour -> {
  394. map["cabinetId"] = mAdapter.data[position].cabinetDoorVoList[3].cabinetId
  395. map["doorId"] = mAdapter.data[position].cabinetDoorVoList[3].doorUniqueId
  396. UiManager.switcher(this, map, InquiryActivity::class.java)
  397. }
  398. R.id.imgViews -> {
  399. map["cabinetId"] = "a"
  400. map["doorId"] = "a"
  401. UiManager.switcher(this, map, InquiryActivity::class.java)
  402. }
  403. }
  404. } else {
  405. //进行登录
  406. basicConfLog()
  407. }
  408. }
  409. }
  410. private lateinit var handlerBanner: Handler
  411. private lateinit var updateRunnable: Runnable
  412. private var isUpdating = false
  413. private fun startUpdating() {
  414. if (!isUpdating) {
  415. isUpdating = true
  416. handlerBanner.post(updateRunnable)
  417. }
  418. }
  419. private fun stopUpdating() {
  420. if (isUpdating) {
  421. isUpdating = false
  422. handlerBanner.removeCallbacks(updateRunnable)
  423. }
  424. }
  425. // private val mPortScanHelper by lazy {
  426. // PortScanHelper(this, object : OnSerialScanListener {
  427. // override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
  428. // if (!mHandleScanEvent) {
  429. // if (content.isNotBlank()) {
  430. // RcLog.info("==========当前usb返回参数$content")
  431. // mHandleScanEvent = true
  432. // handleScanEvent(content)
  433. // }
  434. //
  435. // }
  436. // }
  437. //
  438. // })
  439. // }
  440. //调用刷卡信息
  441. private fun handleScanEvent(cont: String) {
  442. // showLoading("查询中...")
  443. // val disposable = ApiRepository.querySubInfo()
  444. // .subscribe({ data ->
  445. // dismissLoading()
  446. //
  447. // }, { throwable ->
  448. // dismissLoading()
  449. // showNetError(throwable)
  450. // throwable.printStackTrace()
  451. // })
  452. // addDisposable(disposable)
  453. }
  454. //刷卡usb链接
  455. private fun registerUsbBroadcast() {
  456. if (null == mUsbReceiver) {
  457. val filter = IntentFilter().apply {
  458. addAction(UsbReceiver.ACTION_USB_PERMISSION)
  459. addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
  460. addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
  461. addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
  462. }
  463. mUsbReceiver = UsbReceiver()
  464. registerReceiver(mUsbReceiver, filter)
  465. }
  466. }
  467. override fun onDestroy() {
  468. super.onDestroy()
  469. try {
  470. // 停止定时更新
  471. timeUpdater.stopUpdating()
  472. // unregisterReceiver(mUsbReceiver)
  473. // EventBus.getDefault().unregister(this) //关闭广播
  474. // 移除未执行的定时任务
  475. handler.removeCallbacks(runnable)
  476. handler.removeCallbacksAndMessages(null)
  477. stopUpdating()
  478. mCountDownHandler.removeMessages(WHAT_COUNT_DOWN)
  479. mCountDownHandler.removeCallbacksAndMessages(null)
  480. } catch (e: Exception) {
  481. }
  482. }
  483. private fun subAddData() {
  484. //数据上报
  485. mLztek = Lztek.create(this)
  486. val toUpperCase = mLztek!!.ethMac.toUpperCase()
  487. mDeviceNum = toUpperCase.replace(":", "")
  488. val disposable = ApiRepository.monitor(mDeviceNum!!)
  489. .subscribe({ data ->
  490. }, { throwable ->
  491. showNetError(throwable)
  492. })
  493. addDisposable(disposable)
  494. }
  495. //loginType 登陆方式,1人脸 2刷卡 3扫码 4人脸+刷卡
  496. //verifyType 双人验证方式 1人脸 2刷卡 3扫码
  497. private fun basicConf() {
  498. val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
  499. .subscribe({ data ->
  500. ChemicalApp.confs = data
  501. mLoginType = data.loginType //登陆方式
  502. if (data.levelName != null) {
  503. val result = data.levelName!!.toCharArray().joinToString("\n")
  504. viewBinding.tvNoName.text = result
  505. // 创建一个GradientDrawable对象
  506. val shapes = GradientDrawable()
  507. // 设置形状为矩形
  508. shapes.shape = GradientDrawable.RECTANGLE
  509. // 设置背景色为红色(这里使用颜色资源)
  510. shapes.setColor(Color.parseColor(data.levelColor))
  511. shapes.cornerRadius = 5f
  512. // 将GradientDrawable对象设置为View的背景
  513. viewBinding.tvNoName.background = shapes
  514. } else {
  515. viewBinding.tvNoName.visibility = View.GONE
  516. }
  517. viewBinding.subName.text = data.subName
  518. viewBinding.inc.deptName.text = "${data.deptName}"
  519. Glide.with(this)
  520. .load("${HttpConfig.API_BASE_IMG_URL}${data.circularLogo}")
  521. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  522. .into(viewBinding.inc.circularLogo)
  523. }, { throwable ->
  524. showNetError(throwable)
  525. })
  526. addDisposable(disposable)
  527. }
  528. //loginType 登陆方式,1人脸 2刷卡 3扫码 4人脸+刷卡
  529. //verifyType 双人验证方式 1人脸 2刷卡 3扫码
  530. private fun basicConfLog() {
  531. val disposable = ApiRepository.basicConfig(ChemicalApp.subjectId!!)
  532. .subscribe({ data ->
  533. ChemicalApp.confs = data
  534. mLoginType = data.loginType //登陆方式
  535. if (mLoginType == null) {
  536. customDialogView(2, "登录方式配置有误")
  537. } else {
  538. if (mLoginType!!.contains("4")) {
  539. //需要刷卡+人脸
  540. UiManager.switcher(this, FacialCardActivity::class.java)
  541. } else {
  542. val map = mutableMapOf<String, String>()
  543. //1人脸 2刷卡 3扫码 4人脸+刷卡
  544. if (mLoginType!!.length == 1) {
  545. when (mLoginType) {
  546. "1" -> {
  547. //人脸
  548. map["faceList"] = faceList.toString()
  549. map["mtypes"] = "1"
  550. UiManager.switcher(this, map, FacialLoginActivity::class.java)
  551. }
  552. "2" -> {
  553. //刷卡
  554. map["mtypes"] = "1"
  555. UiManager.switcher(this, map, SwipeActivity::class.java)
  556. }
  557. else -> {
  558. //扫码
  559. map["mtypes"] = "1"
  560. UiManager.switcher(this, map, ScanLoginActivity::class.java)
  561. }
  562. }
  563. } else {
  564. val array = mLoginType!!.split(",").toTypedArray()
  565. when (array.size) {
  566. 2 -> {
  567. //1-2(人脸+刷卡) 1-3(人脸+扫码) 2-3(刷卡+扫码)
  568. if (mLoginType == "1,2" || mLoginType == "2,1") {
  569. //人脸+刷卡
  570. map["faceList"] = faceList.toString()
  571. map["mtypes"] = "4" //隐藏扫码
  572. UiManager.switcher(
  573. this,
  574. map,
  575. FacialLoginActivity::class.java
  576. )
  577. } else if (mLoginType == "1,3" || mLoginType == "3,1") {
  578. //人脸+扫码
  579. map["faceList"] = faceList.toString()
  580. map["mtypes"] = "5" //隐藏刷卡
  581. UiManager.switcher(
  582. this,
  583. map,
  584. FacialLoginActivity::class.java
  585. )
  586. } else if (mLoginType == "2,3" || mLoginType == "3,2") {
  587. //刷卡+扫码
  588. map["mtypes"] = "6" //隐藏人脸
  589. UiManager.switcher(this, map, SwipeActivity::class.java)
  590. } else {
  591. showToast("登录方式$mLoginType")
  592. }
  593. }
  594. else -> {
  595. //3条
  596. try {
  597. map["mtypes"] = "0"
  598. map["faceList"] = faceList.toString()
  599. UiManager.switcher(
  600. this,
  601. map,
  602. FacialLoginActivity::class.java
  603. )
  604. } catch (e: Exception) {
  605. map["mtypes"] = "0"
  606. UiManager.switcher(this, map, SwipeActivity::class.java)
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. }, { throwable ->
  614. showNetError(throwable)
  615. })
  616. addDisposable(disposable)
  617. }
  618. /**
  619. * 0 没有图标 1 绿色(成功) 2红色(失败)
  620. * 失败或者成功的弹框
  621. */
  622. private fun customDialogView(types: Int, msg: String) {
  623. val customDialog = CustomDialog(this, types, msg)
  624. if (!this.isFinishing && !this.isDestroyed) {
  625. customDialog.show()
  626. }
  627. }
  628. /**
  629. * 异常处理
  630. */
  631. private fun throwableView(throwable: Throwable) {
  632. when (throwable) {
  633. is NetException -> {
  634. if (throwable.message.isNullOrEmpty()) {
  635. "接口请求失败(${throwable.code})"
  636. } else {
  637. throwable.message!!
  638. }
  639. }
  640. is SocketTimeoutException -> "请求超时,请稍后重试"
  641. is ConnectException -> "无法连接服务器,请检查网络"
  642. is HttpException -> "服务器繁忙,请稍后重试"
  643. else -> null
  644. }?.let { customDialogView(2, "$it") }
  645. }
  646. //只管注销
  647. private val mCountDownHandler = object : Handler(Looper.getMainLooper()) {
  648. override fun handleMessage(msg: Message) {
  649. if (WHAT_COUNT_DOWN == msg.what) {
  650. val countDown = MAX_TIME - mCounter
  651. Log.d("======返回首页时间", "${countDown}")
  652. if (isDestroyed) return
  653. if (countDown <= 0) {
  654. ChemicalApp.confs = null
  655. ChemicalApp.subjectId = null
  656. ChemicalApp.userData = null
  657. callLogoutApis {
  658. UiManager.switcherCashier(
  659. this@MainActivity,
  660. SplashActivity::class.java
  661. )
  662. }
  663. } else {
  664. sendEmptyMessageDelayed(WHAT_COUNT_DOWN, 1000)
  665. }
  666. mCounter++
  667. }
  668. }
  669. }
  670. override fun onUserInteraction() {
  671. mCounter = 0
  672. }
  673. private fun callLogoutApis(callback: ((success: Boolean) -> Unit)? = null) {
  674. showLoading("退出中...")
  675. val disposable = ApiRepository.loginOut()
  676. .subscribe({
  677. dismissLoading()
  678. callback?.invoke(true)
  679. }, {
  680. dismissLoading()
  681. callback?.invoke(false)
  682. })
  683. addDisposable(disposable)
  684. }
  685. private fun showTextInputDialog(title: String, callback: (String?) -> Unit) {
  686. val editText = EditText(this)
  687. val dialog = AlertDialog.Builder(this)
  688. .setTitle(title)
  689. .setView(editText)
  690. .setPositiveButton("确定") { _, _ ->
  691. val inputText = editText.text.toString()
  692. callback(inputText)
  693. }
  694. .setNegativeButton("取消") { _, _ ->
  695. callback(null)
  696. }
  697. .create()
  698. dialog.show()
  699. }
  700. }