增加用户登录的逻辑代码,将用户输入的信息与json中的数据进行比对,一致则登录成功,否则提示登录失败,用通知的方式获取验证码并呈现在页面中

This commit is contained in:
huangrui 2024-05-25 18:29:42 +08:00
parent f1d353d947
commit e6bff1a513
7 changed files with 294 additions and 36 deletions

View File

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
@ -74,18 +75,14 @@
<activity
android:name=".MyLogin"
android:exported="false" />
<activity
android:name=".MyNoLogin"
android:exported="false" />
<activity
android:name=".Abouterweima"
android:exported="false" />
<activity
android:name=".Tanchuang"
android:exported="false" />
<activity
android:name=".TakeSubway"
android:exported="false" />
<activity
android:name=".SubwayHistory"
android:exported="false" />

View File

@ -0,0 +1,19 @@
package com.hnucm.c25;
import java.util.List;
public class Phone_Account {
public List<PhoneDTO> phone;
public List<AccountDTO> account;
public static class PhoneDTO {
public String phonenumber;
public String yanzhengma;
}
public static class AccountDTO {
public String zhanghao;
public String password;
}
}

View File

@ -1,19 +1,75 @@
package com.hnucm.c25;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class password_enter extends AppCompatActivity {
EditText account ;
EditText password;
List<Phone_Account.AccountDTO> accountDTOs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password_enter);
account=findViewById(R.id.editText);
password=findViewById(R.id.editText2);
Request request = new Request.Builder()
.url("https://test04.usemock.com/login")
.get()
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
//创建了一个线程
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
//请求失败
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//请求成功
String result = response.body().string();
Log.i("test",result);
//所有对于UI控件的操作行为放在主线程中 解决方法切换到主线程
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Gson gson = new Gson();
Phone_Account phoneAccount = gson.fromJson(result, Phone_Account.class);
accountDTOs = phoneAccount.account;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
});
//todo 返回未登录
ImageView back = findViewById(R.id.imageView2);
back.setOnClickListener(new View.OnClickListener() {
@ -49,5 +105,39 @@ public class password_enter extends AppCompatActivity {
startActivity(intent);
}
});
//todo 点击登录
TextView enter = findViewById(R.id.textView3);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputAccount = account.getText().toString();
String inputPassword = password.getText().toString();
// 检查账号是否存在于列表中
boolean accountExists = false;
for (Phone_Account.AccountDTO accountDTO : accountDTOs) {
if (inputAccount.equals(accountDTO.zhanghao)) {
accountExists = true;
// 如果账号存在检查密码是否匹配
if (inputPassword.equals(accountDTO.password)) {
// 账号和密码都匹配
Log.i("Login", "Success");
Intent intent = new Intent(password_enter.this,MyLogin.class);
startActivity(intent);
} else {
// 密码不匹配
Log.i("Login", "Password does not match");
Toast.makeText(password_enter.this, "密码不匹配", Toast.LENGTH_LONG).show();
}
break;
}
}
if (!accountExists) {
// 账号不存在
Toast.makeText(password_enter.this, "账号不存在", Toast.LENGTH_LONG).show();
}
}
});
}
}

View File

@ -1,54 +1,190 @@
package com.hnucm.c25;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class phone_enter extends AppCompatActivity {
EditText phonenumber;
EditText yanzhengma;
List<Phone_Account.PhoneDTO> phoneDTOS;
TextView sendCode;
TextView verificationCodeMessageTextView;
TextView login;
ImageView back;
NotificationManager manager;
String CHANNEL_ID = "VerificationCodeChannel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_enter);
//todo 账号密码登录
phonenumber = findViewById(R.id.editText);
yanzhengma = findViewById(R.id.editText2);
sendCode = findViewById(R.id.textView2);
verificationCodeMessageTextView = findViewById(R.id.textView148);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Verification Code";
String description = "Channel for verification code notifications";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
// 发送验证码的网络请求
Request request = new Request.Builder()
.url("https://test04.usemock.com/login")
.get()
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
// 请求失败处理
runOnUiThread(() -> Toast.makeText(phone_enter.this, "网络请求失败", Toast.LENGTH_SHORT).show());
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
// 请求成功处理
String result = response.body().string();
Log.i("test", result);
// 切换到主线程更新UI
runOnUiThread(() -> {
try {
Gson gson = new Gson();
Phone_Account phoneAccount = gson.fromJson(result, Phone_Account.class);
phoneDTOS = phoneAccount.phone;
// // 假设我们只关心第一个 PhoneDTO
// if (!phoneDTOS.isEmpty()) {
// Phone_Account.PhoneDTO firstPhoneDTO = phoneDTOS.get(0);
// // 显示验证码消息
// showVerificationCode( firstPhoneDTO.yanzhengma);
// }
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
});
// 账号密码登录按钮
TextView zhanghao = findViewById(R.id.textView4);
zhanghao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(phone_enter.this,password_enter.class);
startActivity(intent);
}
zhanghao.setOnClickListener(v -> {
Intent intent = new Intent(phone_enter.this, password_enter.class);
startActivity(intent);
});
//todo 返回我的界面
ImageView back = findViewById(R.id.imageView2);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(phone_enter.this,MyFragment.class);
startActivity(intent);
}
// 返回我的界面按钮
back = findViewById(R.id.imageView2);
back.setOnClickListener(v -> {
Intent intent = new Intent(phone_enter.this, MyFragment.class);
startActivity(intent);
});
//todo 点击注册按钮跳转到注册页面
TextView login = findViewById(R.id.textView);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(phone_enter.this,Login.class);
startActivity(intent);
}
// 注册按钮
login = findViewById(R.id.textView);
login.setOnClickListener(v -> {
Intent intent = new Intent(phone_enter.this, Login.class);
startActivity(intent);
});
//todo 点击登录
// 登录按钮
TextView enter = findViewById(R.id.textView3);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(phone_enter.this,MyLogin.class);
enter.setOnClickListener(v -> {
String inputAccount = phonenumber.getText().toString();
String inputPassword = yanzhengma.getText().toString();
// 查找输入手机号对应的验证码
Phone_Account.PhoneDTO matchedPhoneDTO = null;
for (Phone_Account.PhoneDTO phoneDTO : phoneDTOS) {
if (inputAccount.equals(phoneDTO.phonenumber)) {
matchedPhoneDTO = phoneDTO;
break;
}
}
if (matchedPhoneDTO != null && inputPassword.equals(matchedPhoneDTO.yanzhengma)) {
// 手机号和验证码匹配
Log.i("Login", "Success");
Intent intent = new Intent(phone_enter.this, MyLogin.class);
startActivity(intent);
} else {
// 验证码错误或手机号不存在
Toast.makeText(phone_enter.this, "验证码错误或手机号不存在", Toast.LENGTH_LONG).show();
}
});
// 发送验证码按钮点击事件
sendCode.setOnClickListener(v -> {
// 查找输入手机号对应的验证码并显示
String inputAccount = phonenumber.getText().toString();
for (Phone_Account.PhoneDTO phoneDTO : phoneDTOS) {
if (inputAccount.equals(phoneDTO.phonenumber)) {
showVerificationCode(phoneDTO.yanzhengma);
break;
}
}
});
}
private void showVerificationCode(String yanzhengma) {
String verificationCodeMessage = String.format(
"【易通行】验证码:%s 。您正在使用短信验证码登录功能,验证码提供他人可能导致账号被盗,请勿转发或泄漏。",
yanzhengma);
// login.setVisibility(View.INVISIBLE);
// back.setVisibility(View.INVISIBLE);
verificationCodeMessageTextView.setText(verificationCodeMessage);
verificationCodeMessageTextView.setVisibility(View.VISIBLE);
// 5秒后隐藏验证码消息
new Handler().postDelayed(() -> verificationCodeMessageTextView.setVisibility(View.GONE), 5000);
// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.infor) // 设置通知小图标
.setContentTitle("验证码通知") // 设置通知标题
.setContentText(verificationCodeMessage) // 设置通知文本内容
.setPriority(NotificationCompat.PRIORITY_HIGH); // 设置通知优先级
// 使用 Toast 在界面上显示消息
// Toast.makeText(this, verificationCodeMessage, Toast.LENGTH_LONG).show();
// // 5秒后隐藏验证码消息
// new Handler().postDelayed(() -> verificationCodeMessageTextView.setVisibility(View.GONE), 5000);
// 发送通知
manager.notify(1, builder.build());
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 960 B

View File

@ -35,7 +35,7 @@
android:layout_marginStart="24dp"
android:layout_marginTop="128dp"
android:layout_marginEnd="24dp"
android:hint="请输入手机号"
android:hint="请输入号"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

View File

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".phone_enter">
<TextView
@ -175,4 +176,19 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/textView148"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@color/white"
android:textColor="@color/black"
android:lineSpacingExtra="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>