From 14a69741a22b964c5e64a574422f230aae2e39f3 Mon Sep 17 00:00:00 2001 From: tanc <2512769611@qq.com> Date: Sun, 24 Nov 2024 03:18:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E4=B8=8E=E8=8B=A5=E4=BE=9D=E5=90=8E=E7=AB=AF=E4=B8=8E?= =?UTF-8?q?=E5=AE=89=E5=8D=93=E7=AB=AF=E5=89=8D=E7=AB=AF=E7=9B=B8=E8=BF=9E?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=BF=9B=E8=A1=8Ctoken=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liyueling_final/EnrollActivity.java | 70 +++++++++++--- .../example/liyueling_final/Expert1Api.java | 1 + .../liyueling_final/LoginMainActivity.java | 71 ++++++++++---- .../example/liyueling_final/LoginResult.java | 13 +++ .../liyueling_final/LoginResultApi.java | 12 +++ .../example/liyueling_final/LoginUser.java | 92 ++++++++++++++++--- .../example/liyueling_final/LoginUserApi.java | 20 ++-- .../liyueling_final/MainexpertFragment.java | 22 +++-- .../com/example/liyueling_final/Result.java | 11 +++ .../liyueling_final/StudyFragment.java | 1 - .../com/example/liyueling_final/Welcome.java | 40 +++++++- 11 files changed, 289 insertions(+), 64 deletions(-) create mode 100644 src/main/java/com/example/liyueling_final/LoginResult.java create mode 100644 src/main/java/com/example/liyueling_final/LoginResultApi.java create mode 100644 src/main/java/com/example/liyueling_final/Result.java diff --git a/src/main/java/com/example/liyueling_final/EnrollActivity.java b/src/main/java/com/example/liyueling_final/EnrollActivity.java index f43a106..422d37d 100644 --- a/src/main/java/com/example/liyueling_final/EnrollActivity.java +++ b/src/main/java/com/example/liyueling_final/EnrollActivity.java @@ -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 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 posterCall = + RetrofitUtils.getRetrofit("http://10.138.9.158:8080/") + .create(LoginUserApi.class) + .register("Bearer " + token,loginUser); + posterCall.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response 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 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(); } }); diff --git a/src/main/java/com/example/liyueling_final/Expert1Api.java b/src/main/java/com/example/liyueling_final/Expert1Api.java index 76701fb..b57dbc3 100644 --- a/src/main/java/com/example/liyueling_final/Expert1Api.java +++ b/src/main/java/com/example/liyueling_final/Expert1Api.java @@ -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 { diff --git a/src/main/java/com/example/liyueling_final/LoginMainActivity.java b/src/main/java/com/example/liyueling_final/LoginMainActivity.java index c5a44b2..157bbb7 100644 --- a/src/main/java/com/example/liyueling_final/LoginMainActivity.java +++ b/src/main/java/com/example/liyueling_final/LoginMainActivity.java @@ -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 posterCall = + RetrofitUtils.getRetrofit("http://10.138.9.158:8080/") + .create(LoginUserApi.class) + .login("Bearer " + token,username,userPassword); + posterCall.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response 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 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(); +// } +// } }); diff --git a/src/main/java/com/example/liyueling_final/LoginResult.java b/src/main/java/com/example/liyueling_final/LoginResult.java new file mode 100644 index 0000000..c7fc3b6 --- /dev/null +++ b/src/main/java/com/example/liyueling_final/LoginResult.java @@ -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; +} diff --git a/src/main/java/com/example/liyueling_final/LoginResultApi.java b/src/main/java/com/example/liyueling_final/LoginResultApi.java new file mode 100644 index 0000000..666b1ad --- /dev/null +++ b/src/main/java/com/example/liyueling_final/LoginResultApi.java @@ -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 login(@Body Map map); +} diff --git a/src/main/java/com/example/liyueling_final/LoginUser.java b/src/main/java/com/example/liyueling_final/LoginUser.java index a786731..abaef4f 100644 --- a/src/main/java/com/example/liyueling_final/LoginUser.java +++ b/src/main/java/com/example/liyueling_final/LoginUser.java @@ -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 data; + @SerializedName("msg") + public String msg; + @SerializedName("rows") + public List 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; + } } } diff --git a/src/main/java/com/example/liyueling_final/LoginUserApi.java b/src/main/java/com/example/liyueling_final/LoginUserApi.java index 3da3a69..3260b77 100644 --- a/src/main/java/com/example/liyueling_final/LoginUserApi.java +++ b/src/main/java/com/example/liyueling_final/LoginUserApi.java @@ -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 login(@Field("id") Integer id); - @POST("regist/register") - Call register(@Body LoginUser loginUser); + @GET("/system/users/byUsername/{username}/{password}") + Call login(@Header ("Authorization")String token, + @Path("username") String username, + @Path("password") String password); + + @POST("system/users") + Call register(@Header ("Authorization")String token, @Body LoginUser.RowsDTO loginUser); } diff --git a/src/main/java/com/example/liyueling_final/MainexpertFragment.java b/src/main/java/com/example/liyueling_final/MainexpertFragment.java index 4210ff7..81ef704 100644 --- a/src/main/java/com/example/liyueling_final/MainexpertFragment.java +++ b/src/main/java/com/example/liyueling_final/MainexpertFragment.java @@ -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 call = expert1Api.people(0); - Log.i("123456", "onFailure: "); + Call call = expert1Api.people(0); + Log.i("123456", "onFailure: "); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response 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 diff --git a/src/main/java/com/example/liyueling_final/Result.java b/src/main/java/com/example/liyueling_final/Result.java new file mode 100644 index 0000000..2e84c41 --- /dev/null +++ b/src/main/java/com/example/liyueling_final/Result.java @@ -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; +} diff --git a/src/main/java/com/example/liyueling_final/StudyFragment.java b/src/main/java/com/example/liyueling_final/StudyFragment.java index ab3446f..286bb9c 100644 --- a/src/main/java/com/example/liyueling_final/StudyFragment.java +++ b/src/main/java/com/example/liyueling_final/StudyFragment.java @@ -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) // 使用收到的消息作为图片地址 diff --git a/src/main/java/com/example/liyueling_final/Welcome.java b/src/main/java/com/example/liyueling_final/Welcome.java index 7fc13d1..02e74b2 100644 --- a/src/main/java/com/example/liyueling_final/Welcome.java +++ b/src/main/java/com/example/liyueling_final/Welcome.java @@ -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 map = new HashMap<>(); + map.put("username","admin"); + map.put("password","admin123"); - Intent intent = new Intent(Welcome.this, LoginMainActivity.class); - startActivity(intent); + Call loginUserCall = RetrofitUtils.getRetrofit("http://10.138.9.158:8080/").create(LoginResultApi.class).login(map); + loginUserCall.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response 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 call, Throwable t) { + + } + }); } });