MainActivity.kt 29 KB

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