package com.fangxinxing.xxfxx.ui.mine import android.app.Activity import android.content.Intent import android.os.CountDownTimer import android.os.Environment import android.util.Log import com.fangxinxing.xxfxx.utils.isValidIdCard import com.fangxinxing.xxfxx.utils.isValidPhone import com.fangxinxing.xxfxx.utils.visible import com.fangxinxing.xxfxx.IdCardDateRangeDialog import com.fangxinxing.xxfxx.R import com.fangxinxing.xxfxx.SelectPhotoDialogKt import com.fangxinxing.xxfxx.base.MyApplication import com.fangxinxing.xxfxx.base.MyBaseActivity import com.fangxinxing.xxfxx.bean.StringBean import com.fangxinxing.xxfxx.netUtls.Api import com.fangxinxing.xxfxx.netUtls.HttpManager import com.fangxinxing.xxfxx.netUtls.MyObserver import com.fangxinxing.xxfxx.netUtls.callNet import com.fangxinxing.xxfxx.netUtls.getMapByAny import com.fangxinxing.xxfxx.netUtls.upload import com.fangxinxing.xxfxx.bean.UserBankInfo import com.fangxinxing.xxfxx.netUtls.* import com.fangxinxing.xxfxx.utils.Cache.CacheKey import com.fangxinxing.xxfxx.utils.glide.GlideUtil import kotlinx.android.synthetic.main.activity_mine.* import kotlinx.android.synthetic.main.activity_withdraw_account.* import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File class WithdrawAccountActivity: MyBaseActivity() { override fun setContentView() { setContentView(R.layout.activity_withdraw_account) } private val data:UserBankInfo? by lazy { intent.getParcelableExtra("data") } private var frontImg = "" //正面身份证图片链接 private var frontToken = "" //正面加密字符串 private var backImg = "" //背面图片链接 private var backToken = "" //背面加密字符串 private var bankBranchCode = "" //行联号 private var bankCode = "" //银行编号 private val timer by lazy { object : CountDownTimer(60000, 1000) { override fun onFinish() { tv_get_code.text = "获取验证码" tv_get_code.isEnabled = true } override fun onTick(millisUntilFinished: Long) { tv_get_code.text = "已发送${millisUntilFinished / 1000}s" } } } override fun initView() { setTitleText("提现账户设置") if (data?.applicationStatus == "succeeded"){ data?.also { et_name.setText(it.certificateName) et_name.isEnabled = false et_id_card.setText(it.certificateCode) et_id_card.isEnabled = false tv_date.text = it.certificateExpireTime tv_date.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,0,0) et_provider.setText(it.licenceIssuingAuthority) et_provider.isEnabled = false tv_bank.text = it.bankName bankBranchCode = it.bankBranchCode?:"" bankCode = it.bankCode?:"" et_bank_num.setText(it.bankCardCode) et_bank_name.setText(it.openAccountName) et_bank_name.isEnabled = false et_pwd.setText("123456") et_pwd.isEnabled = false et_phone.setText(it.phone) frontToken = it.idCardFrontPicture?:"" frontImg = it.imgUrl?:"" GlideUtil.load(MyApplication.getInstance(), frontImg, iv_front,0) backToken = it.idCardReversePicture?:"" backImg = it.imgUrlOne?:"" GlideUtil.load(MyApplication.getInstance(), backImg, iv_back,0) tv_success.visible() tv_action.text = "重新提交" } }else et_phone.setText(CacheKey.getUserInfo().phone) } override fun setOnclick() { tv_date.setOnClickListener { val idCardDateRangeDialog = IdCardDateRangeDialog() idCardDateRangeDialog.setCallback(object : IdCardDateRangeDialog.Callback{ override fun onOk(s: String) { tv_date.text = s } }) idCardDateRangeDialog.show(supportFragmentManager,"date") } tv_bank.setOnClickListener { startActivityForResult(1) } tv_get_code.setOnClickListener { val phone = et_phone.text.toString() if (!phone.isValidPhone()){ toast("请输入正确手机号") return@setOnClickListener } tv_get_code.isEnabled = false val map = getMapByAny() map["phone"] = phone callNet(Api.getCode,map,{ timer.start() toast("发送成功") }){ tv_get_code.isEnabled = true } } iv_front.setOnClickListener { MyApplication.isPlayVoice = false startActivityForResult(2) } iv_back.setOnClickListener { MyApplication.isPlayVoice = false startActivityForResult(3) } tv_action.setOnClickListener { if (data?.applicationStatus == "succeeded"){ updateInfo() }else{ commitInfo() } } } private fun commitInfo() { val name = et_name.text.toString() if (name.isEmpty()){ toast("请输入证件人姓名") return } val idCard = et_id_card.text.toString() if (!idCard.isValidIdCard()){ toast("请输入正确的身份证号码") return } val date = tv_date.text.toString() if (date.isEmpty()){ toast("请选择证件有效期") return } val provider = et_provider.text.toString() if (provider.isEmpty()){ toast("请填写发证机关") return } val bankName = tv_bank.text.toString() if (bankName.isEmpty()){ toast("请选择银行") return } val bankNum = et_bank_num.text.toString() if (bankNum.isEmpty()){ toast("请输入银行卡号") return } val bankCardName = et_bank_name.text.toString() if (bankCardName.isEmpty()){ toast("请输入开户名称") return } if (bankCardName!= name){ toast("开户名称和证件人姓名不一致") return } val pwd = et_pwd.text.toString() if (pwd.length!=6){ toast("请填写6位交易授权码") return } val phone = et_phone.text.toString() if (phone.isEmpty()){ toast("请输入手机号码") return } if (!phone.isValidPhone()){ toast("手机号格式有误") return } val code = et_code.text.toString() if (code.length!=6){ toast("请输入6位验证码") return } if (frontToken.isEmpty()){ toast("请上传身份证正面照") return } if (backToken.isEmpty()){ toast("请上传身份证反面照") return } tv_action.isEnabled = false val map = getMapByAny() map["bankBranchCode"] = bankBranchCode map["bankCardCode"] = bankNum map["bankCode"] = bankCode map["bankName"] = bankName map["certificateCode"] = idCard map["certificateExpireTime"] = date map["certificateName"] = name map["certificateType"] = 1 map["code"] = code map["copy1"] = frontToken map["copy2"] = backToken map["idCardFrontPicture"] = frontImg map["idCardReversePicture"] = backImg map["licenceIssuingAuthority"] = provider map["openAccountName"] = bankCardName map["phone"] = phone map["transactionAuthorizationCode"] = pwd map["userType"] = 2 callNet(Api.addElectronicLedger,map,{ startActivity() finish() }){ tv_action.isEnabled = true } } private fun updateInfo(){ val bankNum = et_bank_num.text.toString() if (bankNum.isEmpty()){ toast("请输入银行卡号") return } val phone = et_phone.text.toString() if (phone.isEmpty()){ toast("请输入手机号码") return } if (!phone.isValidPhone()){ toast("手机号格式有误") return } val code = et_code.text.toString() if (code.length!=6){ toast("请输入6位验证码") return } tv_action.isEnabled = false val map = getMapByAny() map["bankBranchCode"] = bankBranchCode map["bankCardCode"] = bankNum map["bankCode"] = bankCode map["bankName"] = tv_bank.text.toString() map["certificateCode"] = et_id_card.text.toString() map["certificateExpireTime"] = tv_date.text.toString() map["certificateName"] = et_name.text.toString() map["code"] = code map["copy1"] = frontToken map["copy2"] = backToken map["idCardFrontPicture"] = frontImg map["idCardReversePicture"] = backImg map["openAccountName"] = et_bank_name.text.toString() map["phone"] = phone map["userType"] = 2 callNet(Api.updateElectronicLedgerInfo,map,{ startActivity() finish() }){ tv_action.isEnabled = true } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK&&data!=null){ if (requestCode == 1){ tv_bank.text = data.getStringExtra("name") bankBranchCode = data.getStringExtra("code")?:"" bankCode = data.getStringExtra("bankCode")?:"" }else{ MyApplication.isPlayVoice = true Luban.with(this) .load(File(data.getStringExtra("path"))) .ignoreBy(100) .setTargetDir(getPath()) .filter { path -> !(path.isEmpty() || path.toLowerCase().endsWith(".gif")) } .setCompressListener(object : OnCompressListener { override fun onStart() { } override fun onSuccess(file: File) { Log.e("mmp", "压缩完成" + file.name) if (requestCode == 2){ frontToken = "" frontImg = "" } else{ backToken = "" backImg = "" } showDialog() HttpManager.getInstance().post( Api.upImage, file, object : MyUploadObserver(this@WithdrawAccountActivity) { override fun success(responseString: String) { if (requestCode == 2) { frontImg = responseString GlideUtil.load( MyApplication.getInstance(), frontImg, iv_front, 0 ) } else { backImg = responseString GlideUtil.load( MyApplication.getInstance(), backImg, iv_back, 0 ) } } }) upload(true, Api.uploadImg,file){ val bean = gson.fromJson(it, StringBean::class.java) if (requestCode == 2) frontToken = bean.data?:"" else backToken = bean.data?:"" } } override fun onError(e: Throwable) { e.printStackTrace() } }).launch() } } } fun getPath(): String { val path = Environment.getExternalStorageDirectory().toString() + "/Luban/image/" val file = File(path) return if (file.mkdirs()) { path } else path } }