MainActivity.kt 29 KB

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