|
@@ -0,0 +1,271 @@
|
|
|
+package org.brynhild.graduation.activity.common
|
|
|
+
|
|
|
+import android.app.Activity
|
|
|
+import android.content.Intent
|
|
|
+import android.os.Bundle
|
|
|
+import android.os.Handler
|
|
|
+import android.os.Message
|
|
|
+import android.util.Log
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.widget.Toast
|
|
|
+import androidx.appcompat.app.AlertDialog
|
|
|
+import com.bumptech.glide.Glide
|
|
|
+import com.example.devicemanager.view.ConfirmPopupWindow
|
|
|
+import com.google.gson.Gson
|
|
|
+import org.brynhild.graduation.R
|
|
|
+import org.brynhild.graduation.activity.ActivityCollector
|
|
|
+import org.brynhild.graduation.common.config.LoginConfiguration
|
|
|
+import org.brynhild.graduation.common.config.LoginConfiguration.saveLoginInfo
|
|
|
+import org.brynhild.graduation.common.config.MyApplication
|
|
|
+import org.brynhild.graduation.common.constant.BroadcastConstant
|
|
|
+import org.brynhild.graduation.common.constant.FileStorageConstant
|
|
|
+import org.brynhild.graduation.common.transfer.Result
|
|
|
+import org.brynhild.graduation.common.transfer.dto.AccountBasicInfo
|
|
|
+import org.brynhild.graduation.common.transfer.dto.FileInfo
|
|
|
+import org.brynhild.graduation.common.transfer.handler.ResponseHandler
|
|
|
+import org.brynhild.graduation.common.transfer.utils.ServiceCreator
|
|
|
+import org.brynhild.graduation.common.transfer.vo.AccountToken
|
|
|
+import org.brynhild.graduation.common.transfer.vo.ModifyAccountInfo
|
|
|
+import org.brynhild.graduation.common.utils.FileUploadUtil
|
|
|
+import org.brynhild.graduation.common.utils.JsonUtils
|
|
|
+import org.brynhild.graduation.common.utils.Uri2PathUtil
|
|
|
+import org.brynhild.graduation.databinding.ActivityAccountInfoBinding
|
|
|
+import org.brynhild.graduation.databinding.ModifyAccountBasicInfoDialogBinding
|
|
|
+import org.brynhild.graduation.databinding.ModifyAccountPasswordInfoDialogBinding
|
|
|
+import org.brynhild.graduation.service.http.AccountService
|
|
|
+import retrofit2.Call
|
|
|
+import retrofit2.Callback
|
|
|
+import retrofit2.Response
|
|
|
+import java.io.File
|
|
|
+import java.io.IOException
|
|
|
+
|
|
|
+class AccountInfoActivity : BaseActivity() {
|
|
|
+ private val refreshBasicInfo = 1
|
|
|
+ private val updateAccountAvatar = 2
|
|
|
+ private lateinit var accountBasicInfo: AccountBasicInfo
|
|
|
+ private lateinit var activityAccountInfoBinding: ActivityAccountInfoBinding
|
|
|
+ private lateinit var modifyAccountBasicInfoDialogBinding: ModifyAccountBasicInfoDialogBinding
|
|
|
+ private lateinit var modifyAccountPasswordInfoDialogBinding: ModifyAccountPasswordInfoDialogBinding
|
|
|
+
|
|
|
+ val handler: Handler = Handler {
|
|
|
+ when (it.what) {
|
|
|
+ refreshBasicInfo -> {
|
|
|
+ activityAccountInfoBinding.email.text=LoginConfiguration.userInfo!!.user.email
|
|
|
+ }
|
|
|
+ updateAccountAvatar -> {
|
|
|
+ val path = it.obj as String
|
|
|
+ Glide.with(this).load(path).into(activityAccountInfoBinding.iconImage)
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data = ModifyAccountInfo(null,null,path,null)
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token,data).enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(call: Call<Result>, response: Response<Result>) {
|
|
|
+ ResponseHandler.handle(response, { ret->
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ ret.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ LoginConfiguration.userInfo!!.user.avatar=path
|
|
|
+ }, {ret->
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ ret.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ false
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+
|
|
|
+ activityAccountInfoBinding=ActivityAccountInfoBinding.inflate(LayoutInflater.from(this))
|
|
|
+ setContentView(activityAccountInfoBinding.root)
|
|
|
+ activityAccountInfoBinding.mainTitle.text = "账户信息"
|
|
|
+ val user = LoginConfiguration.userInfo!!.user
|
|
|
+ activityAccountInfoBinding.username.text=user.username
|
|
|
+ activityAccountInfoBinding.email.text=user.email
|
|
|
+ activityAccountInfoBinding.lastLogin.text=user.lastLogin
|
|
|
+ activityAccountInfoBinding.name.text=user.name
|
|
|
+ activityAccountInfoBinding.sex.text=user.sex
|
|
|
+
|
|
|
+ Glide.with(this@AccountInfoActivity)
|
|
|
+ .load(user.avatar ?: R.drawable.nav_icon_default)
|
|
|
+ .into(activityAccountInfoBinding.iconImage)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ bindObserver()
|
|
|
+ bindListener()
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun bindObserver() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun bindListener() {
|
|
|
+ activityAccountInfoBinding.backButton.setOnClickListener {
|
|
|
+ finish()
|
|
|
+ }
|
|
|
+
|
|
|
+ //有问题
|
|
|
+ activityAccountInfoBinding.modifyBtn.setOnClickListener {
|
|
|
+ modifyAccountBasicInfoDialogBinding =
|
|
|
+ ModifyAccountBasicInfoDialogBinding.inflate(LayoutInflater.from(this))
|
|
|
+ val user = LoginConfiguration.userInfo!!.user
|
|
|
+ modifyAccountBasicInfoDialogBinding.email.setText(user.email)
|
|
|
+ modifyAccountBasicInfoDialogBinding.tel.setText(user.tel)
|
|
|
+ AlertDialog.Builder(this)
|
|
|
+ .setTitle("修改联系方式")
|
|
|
+ .setView(modifyAccountBasicInfoDialogBinding.root)
|
|
|
+ .setPositiveButton("提交修改") { _, _ ->
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data=ModifyAccountInfo(null,modifyAccountBasicInfoDialogBinding.email.text.toString(),
|
|
|
+ null,modifyAccountBasicInfoDialogBinding.tel.text.toString())
|
|
|
+
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token,data).enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(call: Call<Result>, response: Response<Result>) {
|
|
|
+ ResponseHandler.handle(response, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ LoginConfiguration.userInfo!!.user.tel=modifyAccountBasicInfoDialogBinding.tel.text.toString()
|
|
|
+ LoginConfiguration.userInfo!!.user.email=modifyAccountBasicInfoDialogBinding.email.text.toString()
|
|
|
+ handler.sendEmptyMessage(refreshBasicInfo)
|
|
|
+ }, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ .setNegativeButton("取消修改") { _, _ ->
|
|
|
+
|
|
|
+ }
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ activityAccountInfoBinding.modifyPassword.setOnClickListener {
|
|
|
+ modifyAccountPasswordInfoDialogBinding =
|
|
|
+ ModifyAccountPasswordInfoDialogBinding.inflate(LayoutInflater.from(this))
|
|
|
+ AlertDialog.Builder(this)
|
|
|
+ .setTitle("修改密码")
|
|
|
+ .setView(modifyAccountPasswordInfoDialogBinding.root)
|
|
|
+ .setPositiveButton("提交修改") { _, _ ->
|
|
|
+ val password =
|
|
|
+ modifyAccountPasswordInfoDialogBinding.newPassword.text.toString()
|
|
|
+ val passwordConfirm =
|
|
|
+ modifyAccountPasswordInfoDialogBinding.newPasswordConfirm.text.toString()
|
|
|
+ if (password != passwordConfirm) {
|
|
|
+ Toast.makeText(this, "两次密码不匹配", Toast.LENGTH_SHORT).show()
|
|
|
+ return@setPositiveButton
|
|
|
+ }
|
|
|
+ val service = ServiceCreator.create(AccountService::class.java)
|
|
|
+ val data = ModifyAccountInfo(
|
|
|
+ password,null,
|
|
|
+ null,null
|
|
|
+ )
|
|
|
+ service.modifyAccountInfo(LoginConfiguration.userInfo!!.token,data).enqueue(object : Callback<Result> {
|
|
|
+ override fun onResponse(call: Call<Result>, response: Response<Result>) {
|
|
|
+ ResponseHandler.handle(response, {
|
|
|
+ Toast.makeText(
|
|
|
+ MyApplication.context,
|
|
|
+ it.message,
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ LoginConfiguration.login_password = ""
|
|
|
+ LoginConfiguration.userInfo = null
|
|
|
+ saveLoginInfo()
|
|
|
+
|
|
|
+ val intent = Intent(BroadcastConstant.OFFLINE)
|
|
|
+ MyApplication.context.sendBroadcast(intent)
|
|
|
+ }, {
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
+ Toast.makeText(MyApplication.context, "请求失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ .setNegativeButton("取消修改") { _, _ ->
|
|
|
+
|
|
|
+ }
|
|
|
+ .show()
|
|
|
+ }
|
|
|
+
|
|
|
+ activityAccountInfoBinding.iconImage.setOnClickListener {
|
|
|
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
|
|
+ intent.addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
+ intent.type = "image/*"
|
|
|
+ startActivityForResult(intent, updateAccountAvatar)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data)
|
|
|
+ when (requestCode) {
|
|
|
+ updateAccountAvatar -> {
|
|
|
+ if (resultCode == Activity.RESULT_OK && data != null) {
|
|
|
+ val uri = data.data
|
|
|
+ if (Uri2PathUtil.getRealPathFromUri(this, uri) != null) {
|
|
|
+ //从uri得到绝对路径,并获取到file文件
|
|
|
+ val file = File(Uri2PathUtil.getRealPathFromUri(this, uri))
|
|
|
+ FileUploadUtil.uploadFile(file, object : okhttp3.Callback {
|
|
|
+ override fun onFailure(call: okhttp3.Call, e: IOException) {
|
|
|
+ println("failed")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResponse(
|
|
|
+ call: okhttp3.Call,
|
|
|
+ response: okhttp3.Response
|
|
|
+ ) {
|
|
|
+ if (response.isSuccessful) {
|
|
|
+ val body = response.body()?.string()
|
|
|
+ val gson = Gson()
|
|
|
+ val result = gson.fromJson(body, Result::class.java)
|
|
|
+ println(result)
|
|
|
+ var json = gson.toJson(result.data)
|
|
|
+ val fileInfoList = JsonUtils.fromJson2List<FileInfo>(json)
|
|
|
+ if(fileInfoList?.size==1) {
|
|
|
+ val message = Message()
|
|
|
+ message.what = updateAccountAvatar
|
|
|
+ json=gson.toJson(fileInfoList[0])
|
|
|
+ val info=JsonUtils.fromJson<FileInfo>(json)
|
|
|
+ println(info)
|
|
|
+ if(info!=null){
|
|
|
+ message.obj=info.path
|
|
|
+ handler.sendMessage(message)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ Toast.makeText(this, "获取文件失败", Toast.LENGTH_SHORT).show()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|