完成用户登录注册功能,并且与若依后端与安卓端前端相连,增加进行token验证

This commit is contained in:
tanc 2024-11-24 03:18:42 +08:00
parent 764892f357
commit 14a69741a2
11 changed files with 289 additions and 64 deletions

View File

@ -2,6 +2,10 @@
package com.example.liyueling_final;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.animation.ValueAnimator; import android.content.ContentValues; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; 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 java.util.HashMap;
import java.util.Map;
import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;
public class EnrollActivity extends AppCompatActivity {
EditText phone;
@ -38,11 +42,11 @@ public class EnrollActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
String phoneNumber = phone.getText().toString();
String username = phone.getText().toString();
String userPassword = password.getText().toString();
String reuserPassword = repassword.getText().toString();
if (phoneNumber.isEmpty() || userPassword.isEmpty() || reuserPassword.isEmpty()) {
if (username.isEmpty() || userPassword.isEmpty() || reuserPassword.isEmpty()) {
Toast.makeText(EnrollActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
return;
}
@ -51,18 +55,58 @@ public class EnrollActivity extends AppCompatActivity {
return;
}
DBTools dbTools = new DBTools(EnrollActivity.this,"tests",null,1);//数据库的名字
SQLiteDatabase sqLiteDatabase = dbTools.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("phone",phoneNumber);
contentValues.put("password",userPassword);
contentValues.put("user_img", "");
contentValues.put("username","");
sqLiteDatabase.insert("users",null,contentValues);
// LoginUser loginUser = new LoginUser();
// ContentValues contentValues = new ContentValues();
// contentValues.put("phone",phoneNumber);
// contentValues.put("password",userPassword);
// contentValues.put("user_img", "");
// contentValues.put("username","");
// loginUser.addUser(contentValues);
Map<String,String> map = new HashMap<>();
map.put("username",username);
map.put("password",userPassword);
LoginUser.RowsDTO loginUser = new LoginUser.RowsDTO(username,userPassword," "," ");
SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
String token = sharedPreferences.getString("token","");
retrofit2.Call<Result> posterCall =
RetrofitUtils.getRetrofit("http://10.138.9.158:8080/")
.create(LoginUserApi.class)
.register("Bearer " + token,loginUser);
posterCall.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Result loginUser = response.body();
Log.i("12366",posterCall.request()+" "+loginUser.msg+"token::"+token);
if(loginUser.code==200){
Intent intent = new Intent(EnrollActivity.this, LoginMainActivity.class);
intent.putExtra("username", username);
startActivity(intent);
finish();
}else{
if(loginUser.code==401){
Intent intent = new Intent(EnrollActivity.this, Welcome.class);
startActivity(intent);
finish();
}
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(EnrollActivity.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
}
});
// DBTools dbTools = new DBTools(EnrollActivity.this,"tests",null,1);//数据库的名字
// SQLiteDatabase sqLiteDatabase = dbTools.getWritableDatabase();
// ContentValues contentValues = new ContentValues();
// contentValues.put("phone",phoneNumber);
// contentValues.put("password",userPassword);
// contentValues.put("user_img", "");
// contentValues.put("username","");
// sqLiteDatabase.insert("users",null,contentValues);
Intent intent = new Intent(EnrollActivity.this, LoginMainActivity.class);
startActivity(intent);
finish();
}
});

View File

@ -3,6 +3,7 @@ package com.example.liyueling_final;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Header;
import retrofit2.http.POST;
public interface Expert1Api {

View File

@ -24,6 +24,9 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -63,18 +66,17 @@ public class LoginMainActivity extends AppCompatActivity {
SQLiteDatabase sqLiteDatabase = dbTools.getWritableDatabase();
findViewById(R.id.enter).setOnClickListener(new View.OnClickListener() {
@SuppressLint("Range")
@Override
public void onClick(View v) {
flag = true;
String phoneNumber = phone.getText().toString();
String username = phone.getText().toString();
String userPassword = password.getText().toString();
if (phoneNumber.isEmpty() || userPassword.isEmpty()) {
if (username.isEmpty() || userPassword.isEmpty()) {
Toast.makeText(LoginMainActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
return;
}
@ -83,21 +85,56 @@ public class LoginMainActivity extends AppCompatActivity {
return;
}
Cursor cursor = sqLiteDatabase.query("users", new String[]{"id", "phone", "password","img"},
"phone=?", new String[]{phoneNumber}, null, null, null);
while (cursor.moveToNext()) {
if (cursor != null && cursor.getString(cursor.getColumnIndex("password")).equals(userPassword)) {
Intent intent = new Intent(LoginMainActivity.this, MainActivity.class);
intent.putExtra("username", phoneNumber);
startActivity(intent);
finish();
flag = false;
SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
String token = sharedPreferences.getString("token","");
retrofit2.Call<Result> posterCall =
RetrofitUtils.getRetrofit("http://10.138.9.158:8080/")
.create(LoginUserApi.class)
.login("Bearer " + token,username,userPassword);
posterCall.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Result loginUser = response.body();
Log.i("12366",loginUser.msg+' '+token);
if(loginUser.code==200){
Intent intent = new Intent(LoginMainActivity.this, MainActivity.class);
intent.putExtra("username", username);
startActivity(intent);
finish();
}else{
if(loginUser.code==401){
Intent intent = new Intent(LoginMainActivity.this, Welcome.class);
startActivity(intent);
finish();
}
}
}
}
if(flag==true){
Toast.makeText(LoginMainActivity.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(LoginMainActivity.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
}
});
// flag = true;
//
//
// Cursor cursor = sqLiteDatabase.query("users", new String[]{"id", "phone", "password","img"},
// "phone=?", new String[]{phoneNumber}, null, null, null);
// while (cursor.moveToNext()) {
// if (cursor != null && cursor.getString(cursor.getColumnIndex("password")).equals(userPassword)) {
// Intent intent = new Intent(LoginMainActivity.this, MainActivity.class);
// intent.putExtra("username", phoneNumber);
// startActivity(intent);
// finish();
// flag = false;
// }
// }
// if(flag==true){
// Toast.makeText(LoginMainActivity.this, "手机号或密码不正确", Toast.LENGTH_SHORT).show();
// }
//
}
});

View File

@ -0,0 +1,13 @@
package com.example.liyueling_final;
import com.google.gson.annotations.SerializedName;
public class LoginResult {
@SerializedName("msg")
public String msg;
@SerializedName("code")
public Integer code;
@SerializedName("token")
public String token;
}

View File

@ -0,0 +1,12 @@
package com.example.liyueling_final;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface LoginResultApi {
@POST("login")
Call<LoginResult> login(@Body Map<String,String> map);
}

View File

@ -6,25 +6,69 @@ import java.util.List;
public class LoginUser {
@SerializedName("msg")
public String msg;
@SerializedName("total")
public Integer total;
@SerializedName("code")
public Integer code;
@SerializedName("data")
public List<DataDTO> data;
@SerializedName("msg")
public String msg;
@SerializedName("rows")
public List<RowsDTO> rows;
public static class DataDTO {
@SerializedName("id")
public Integer id;
@SerializedName("username")
public String username;
@SerializedName("password")
public String password;
@SerializedName("phone")
public String phone;
@SerializedName("userImg")
public String userImg;
public static class RowsDTO {
@SerializedName("createBy")
public Object createBy;
@SerializedName("createTime")
public Object createTime;
@SerializedName("updateBy")
public Object updateBy;
@SerializedName("updateTime")
public Object updateTime;
@SerializedName("remark")
public Object remark;
public Object getCreateBy() {
return createBy;
}
public void setCreateBy(Object createBy) {
this.createBy = createBy;
}
public Object getCreateTime() {
return createTime;
}
public void setCreateTime(Object createTime) {
this.createTime = createTime;
}
public Object getUpdateBy() {
return updateBy;
}
public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}
public Object getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Object updateTime) {
this.updateTime = updateTime;
}
public Object getRemark() {
return remark;
}
public void setRemark(Object remark) {
this.remark = remark;
}
public Integer getId() {
return id;
@ -65,5 +109,23 @@ public class LoginUser {
public void setUserImg(String userImg) {
this.userImg = userImg;
}
@SerializedName("id")
public Integer id;
@SerializedName("username")
public String username;
@SerializedName("password")
public String password;
@SerializedName("phone")
public String phone;
@SerializedName("userImg")
public String userImg;
public RowsDTO(String username, String password, String phone, String user_img) {
this.username = username;
this.password = password;
this.phone = phone;
this.userImg = user_img;
}
}
}

View File

@ -1,17 +1,25 @@
package com.example.liyueling_final;
import kotlin.Result;
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
public interface LoginUserApi {
@FormUrlEncoded
@POST("login")
Call<LoginUser> login(@Field("id") Integer id);
@POST("regist/register")
Call<Result> register(@Body LoginUser loginUser);
@GET("/system/users/byUsername/{username}/{password}")
Call<Result> login(@Header ("Authorization")String token,
@Path("username") String username,
@Path("password") String password);
@POST("system/users")
Call<Result> register(@Header ("Authorization")String token, @Body LoginUser.RowsDTO loginUser);
}

View File

@ -1,5 +1,8 @@
package com.example.liyueling_final;
import static android.content.Context.MODE_PRIVATE;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
@ -80,25 +83,24 @@ public class MainexpertFragment extends Fragment {
recyclerView3.setAdapter(myAdpater3);
recyclerView3.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
Expert1Api expert1Api = RetrofitUtils.getRetrofit("http://10.0.2.2:8081/").create(Expert1Api.class);
Expert2Api expert2Api = RetrofitUtils.getRetrofit("http://10.0.2.2:8081/").create(Expert2Api.class);
Call<Expert1> call = expert1Api.people(0);
Log.i("123456", "onFailure: ");
Call<Expert1> call = expert1Api.people(0);
Log.i("123456", "onFailure: ");
call.enqueue(new Callback<Expert1>() {
@Override
public void onResponse(Call<Expert1> call, Response<Expert1> response) {
Log.i("ui123456", "onFailure: ");
Expert1 expert1 = response.body();
for (Expert1.DataDTO data : expert1.data) {
if (data.id < 7) {
expert1List.add(data);
}
for (Expert1.DataDTO data : expert1.data) {
if (data.id < 7) {
expert1List.add(data);
}
}
Collections.shuffle(expert1List);
myAdpater.notifyDataSetChanged();
}
Collections.shuffle(expert1List);
myAdpater.notifyDataSetChanged();
}
@Override

View File

@ -0,0 +1,11 @@
package com.example.liyueling_final;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("msg")
public String msg;
@SerializedName("code")
public Integer code;
}

View File

@ -68,7 +68,6 @@ public class StudyFragment extends Fragment {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage (String message){
imageView = getView().findViewById(R.id.pphoto);
Log.i("123456789",message);
if (message != null) {
Glide.with(getActivity())
.load(message) // 使用收到的消息作为图片地址

View File

@ -4,10 +4,12 @@ import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@ -16,6 +18,13 @@ import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class Welcome extends AppCompatActivity {
TextView textView;
ImageView imageView;
@ -58,9 +67,36 @@ public class Welcome extends AppCompatActivity {
animator.setDuration(1000);
animator.start();
Map<String,String> map = new HashMap<>();
map.put("username","admin");
map.put("password","admin123");
Intent intent = new Intent(Welcome.this, LoginMainActivity.class);
startActivity(intent);
Call<LoginResult> loginUserCall = RetrofitUtils.getRetrofit("http://10.138.9.158:8080/").create(LoginResultApi.class).login(map);
loginUserCall.enqueue(new Callback<LoginResult>() {
@Override
public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
if (response.isSuccessful()) {
LoginResult loginResult = response.body();
if (loginResult != null && loginResult.code == 200) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("token", loginResult.token);
editor.apply();
Intent intent = new Intent(Welcome.this, LoginMainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(Welcome.this, loginResult.msg, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Welcome.this, "服务器返回错误", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResult> call, Throwable t) {
}
});
}
});