SwipeActivity.kt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package com.example.chemical.ui.login
  2. import android.content.IntentFilter
  3. import android.hardware.usb.UsbManager
  4. import android.os.Bundle
  5. import android.os.Handler
  6. import android.os.Looper
  7. import android.view.KeyEvent
  8. import android.view.LayoutInflater
  9. import android.view.MotionEvent
  10. import android.view.View
  11. import android.widget.TextView
  12. import com.blankj.utilcode.util.AppUtils
  13. import com.bumptech.glide.Glide
  14. import com.bumptech.glide.load.engine.DiskCacheStrategy
  15. import com.bumptech.glide.request.RequestOptions
  16. import com.example.chemical.ChemicalApp
  17. import com.example.chemical.R
  18. import com.example.chemical.databinding.ActivitySwipeBinding
  19. import com.example.chemical.receiver.OnSerialScanListener
  20. import com.example.chemical.receiver.PortScanHelper
  21. import com.example.chemical.receiver.UsbReceiver
  22. import com.example.chemical.ui.MainActivity
  23. import com.example.chemical.ui.common.BaseCountDownActivity
  24. import com.example.chemical.utils.MediaPlayerHelper
  25. import com.example.chemical.utils.UiManager
  26. import com.example.chemical.weidith.AuthenticationDialog
  27. import com.example.chemical.weidith.CustomDialog
  28. import com.rc.core.log.RcLog
  29. import com.rc.httpcore.HttpClient
  30. import com.rc.httpcore.HttpConfig
  31. import com.rc.httpcore.client.ApiRepository
  32. import com.rc.httpcore.exception.NetException
  33. import org.greenrobot.eventbus.EventBus
  34. import org.greenrobot.eventbus.Subscribe
  35. import org.greenrobot.eventbus.ThreadMode
  36. import retrofit2.HttpException
  37. import java.net.ConnectException
  38. import java.net.SocketTimeoutException
  39. /**
  40. * 刷卡登录
  41. */
  42. class SwipeActivity : BaseCountDownActivity<ActivitySwipeBinding>() {
  43. private var mUsbReceiver: UsbReceiver? = null // 刷卡广播注册
  44. private var mHandleScanEvent = false //当前是否已经获取过 usb返回的参数
  45. override fun createViewBinding() = ActivitySwipeBinding.inflate(LayoutInflater.from(this))
  46. override fun initViews(savedInstanceState: Bundle?) {
  47. super.initViews(savedInstanceState)
  48. MediaPlayerHelper.playRawMp3(this, R.raw.login_shua_ka)
  49. //注册广播
  50. EventBus.getDefault().register(this)
  51. viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
  52. viewBinding.tvReturn.setOnClickListener {
  53. UiManager.switcherCashier(this, MainActivity::class.java)
  54. }
  55. val stringExtra = intent.getStringExtra("mtypes")
  56. when (stringExtra) {
  57. "1" -> {
  58. viewBinding.linType.visibility = View.GONE
  59. }
  60. "6" -> {
  61. viewBinding.tvFace.visibility = View.GONE
  62. }
  63. "4" -> {
  64. viewBinding.tvWx.visibility = View.GONE
  65. }
  66. }
  67. viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
  68. Glide.with(this)
  69. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
  70. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  71. .into(viewBinding.image)
  72. val map = mutableMapOf<String, String>()
  73. if (stringExtra != null) {
  74. map["mtypes"] = stringExtra
  75. }
  76. try {
  77. val face = intent.getStringExtra("faceList")
  78. if (face != null) {
  79. map["faceList"] = face
  80. }
  81. } catch (e: Exception) {
  82. }
  83. viewBinding.tvFace.setOnClickListener {
  84. UiManager.switcher(this, map, FacialLoginActivity::class.java)
  85. finish()
  86. }
  87. viewBinding.tvWx.setOnClickListener {
  88. if (stringExtra != null) {
  89. map["mtypes"] = stringExtra
  90. }
  91. UiManager.switcher(this, map, ScanLoginActivity::class.java)
  92. finish()
  93. }
  94. }
  95. override fun onResume() {
  96. super.onResume()
  97. mPortScanHelper.onResume()
  98. registerUsbBroadcast()
  99. }
  100. private val mPortScanHelper by lazy {
  101. PortScanHelper(this, object : OnSerialScanListener {
  102. override fun dispatchScanEvent(type: OnSerialScanListener.ScanType, content: String) {
  103. if (!mHandleScanEvent) {
  104. if (content.isNotBlank()) {
  105. mHandleScanEvent = true
  106. handleScanEvent(content)
  107. }
  108. }
  109. }
  110. })
  111. }
  112. //刷卡usb链接
  113. private fun registerUsbBroadcast() {
  114. if (null == mUsbReceiver) {
  115. val filter = IntentFilter().apply {
  116. addAction(UsbReceiver.ACTION_USB_PERMISSION)
  117. addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
  118. addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
  119. addAction(UsbReceiver.ACTION_USB_STATE) // usb连接状态广播
  120. }
  121. mUsbReceiver = UsbReceiver()
  122. registerReceiver(mUsbReceiver, filter)
  123. }
  124. }
  125. //调用刷卡信息
  126. private fun handleScanEvent(cont: String) {
  127. showLoading("登录中...")
  128. HttpClient.token = null
  129. val disposable = ApiRepository.cardNum(cont)
  130. .subscribe({ data ->
  131. ChemicalApp.userData = data
  132. authenticationInfo(data.userId, ChemicalApp.subjectId!!)
  133. }, { throwable ->
  134. dismissLoading()
  135. throwableView(throwable)
  136. mHandleScanEvent = false
  137. })
  138. addDisposable(disposable)
  139. }
  140. //验证当前人员身份
  141. private fun authenticationInfo(userId: String, subId: String) {
  142. showLoading("验证中...")
  143. val disposable = ApiRepository.userCardValidation(userId, subId)
  144. .subscribe({ data ->
  145. dismissLoading()
  146. val allFalse = with(data) {
  147. cabinetAdmin == false &&
  148. belongUser == false &&
  149. toipcUser == false &&
  150. safeUser == false &&
  151. collegeAdmin == false &&
  152. schoolLevelAdmin == false &&
  153. adminUser == false
  154. }
  155. ChemicalApp.responsibles = false
  156. ChemicalApp.administrators = false
  157. if (allFalse) {
  158. HttpClient.token = null
  159. ChemicalApp.userData = null
  160. customDialogView(2, "当前身份不符合")
  161. mHandleScanEvent = false
  162. } else {
  163. //校级管理员 schoolLevelAdmin
  164. //院级管理员 collegeAdmin
  165. //实验室负责人 adminUser
  166. //安全负责人 safeUser
  167. //柜锁管理员 cabinetAdmin
  168. //是否化学品归属人 belongUser
  169. //是否化学品归属课题组下成员 toipcUser
  170. if (data.schoolLevelAdmin == true || data.collegeAdmin == true) {
  171. //院级管理员 or 校级管理员
  172. ChemicalApp.administrators = true
  173. authenticationDialog(data.faceImg, data.userName)
  174. } else if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
  175. //实验室负责人 or 安全负责人 or 柜锁管理员
  176. ChemicalApp.responsibles = true
  177. authenticationDialog(data.faceImg, data.userName)
  178. } else if (data.belongUser == true || data.toipcUser == true) { //当前身份 归属人or课题组
  179. ChemicalApp.responsibles = false
  180. ChemicalApp.administrators = false
  181. authenticationDialog(data.faceImg, data.userName)
  182. } else {
  183. HttpClient.token = null
  184. ChemicalApp.userData = null
  185. customDialogView(2, "当前身份不符合")
  186. mHandleScanEvent = false
  187. }
  188. }
  189. }, { throwable ->
  190. dismissLoading()
  191. throwableView(throwable)
  192. HttpClient.token = null
  193. ChemicalApp.userData = null
  194. mHandleScanEvent = false
  195. })
  196. addDisposable(disposable)
  197. }
  198. //获取刷卡信息
  199. override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
  200. mPortScanHelper.dispatchKeyEvent(event)
  201. return super.dispatchKeyEvent(event)
  202. }
  203. //停止 销毁广播传递
  204. override fun onPause() {
  205. mPortScanHelper.onPause()
  206. super.onPause()
  207. }
  208. override fun onDestroy() {
  209. super.onDestroy()
  210. mPortScanHelper.onPause()
  211. // 移除回调,以防止内存泄漏
  212. try {
  213. handlerBack.removeCallbacks(countdownRunnable)
  214. } catch (e: Exception) {
  215. }
  216. unregisterReceiver(mUsbReceiver)
  217. // 停止定时更新
  218. EventBus.getDefault().unregister(this) //关闭广播
  219. }
  220. //必须写这个方法 防止注册失败
  221. @Subscribe(threadMode = ThreadMode.MAIN)
  222. fun onUpdateEventEvent(event: KeyEvent) {
  223. }
  224. override fun onBackPressed() {
  225. super.onBackPressed()
  226. UiManager.switcherCashier(this, MainActivity::class.java)
  227. }
  228. private val handlerBack = Handler(Looper.getMainLooper())
  229. private var timeLeftInSeconds = 2
  230. private var mTvView: TextView? = null
  231. private var mDialogsAut: AuthenticationDialog? = null
  232. //身份认证成功
  233. private fun authenticationDialog(faceImg: String?, userName: String) {
  234. MediaPlayerHelper.playRawMp3(this, R.raw.login_ren_zheng_tong_hua)
  235. mDialogsAut = AuthenticationDialog(
  236. this,
  237. faceImg,
  238. ChemicalApp.confs!!.subName,
  239. ChemicalApp.confs!!.deptName,
  240. "${ChemicalApp.confs!!.buildName}${ChemicalApp.confs!!.floorName}",
  241. userName, object : AuthenticationDialog.IClickLit {
  242. override fun onUpView(tvView: TextView) {
  243. mTvView = tvView
  244. }
  245. })
  246. mDialogsAut!!.show()
  247. // 开始倒计时
  248. handlerBack.post(countdownRunnable)
  249. // 获取对话框的 Window 对象
  250. mDialogsAut!!.window?.decorView?.setOnTouchListener { _, event ->
  251. // 判断是否点击了对话框外部空白区域
  252. if (event.action == MotionEvent.ACTION_DOWN) {
  253. val x = event.x
  254. val y = event.y
  255. val dialogView = mDialogsAut!!.window?.decorView
  256. if (dialogView != null && (x < 0 || x > dialogView.width || y < 0 || y > dialogView.height)) {
  257. // 在此处执行点击对话框外部空白区域时的操作
  258. // 例如关闭对话框
  259. // 移除回调,以防止内存泄漏
  260. mDialogsAut!!.dismiss()
  261. finish()
  262. return@setOnTouchListener true
  263. }
  264. }
  265. return@setOnTouchListener false
  266. }
  267. }
  268. private val countdownRunnable = object : Runnable {
  269. override fun run() {
  270. if (timeLeftInSeconds > 0) {
  271. mTvView!!.text = "${timeLeftInSeconds}秒后自动返回首页"
  272. timeLeftInSeconds--
  273. handlerBack.postDelayed(this, 1000)
  274. } else {
  275. mDialogsAut!!.dismiss()
  276. finish()
  277. }
  278. }
  279. }
  280. /**
  281. * 0 没有图标 1 绿色 2红色
  282. * 失败或者成功的弹框
  283. */
  284. private fun customDialogView(types: Int, msg: String) {
  285. if (!this.isFinishing && !this.isDestroyed) {
  286. val customDialog = CustomDialog(this, types, msg)
  287. customDialog.show()
  288. }
  289. }
  290. /**
  291. * 异常处理
  292. */
  293. private fun throwableView(throwable: Throwable) {
  294. when (throwable) {
  295. is NetException -> {
  296. if (throwable.message.isNullOrEmpty()) {
  297. "接口请求失败(${throwable.code})"
  298. } else {
  299. throwable.message!!
  300. }
  301. }
  302. is SocketTimeoutException -> "请求超时,请稍后重试"
  303. is ConnectException -> "无法连接服务器,请检查网络"
  304. is HttpException -> "服务器繁忙,请稍后重试"
  305. else -> null
  306. }?.let { customDialogView(2, "$it") }
  307. }
  308. }