diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 781008e..b11895c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -45,4 +45,9 @@ dependencies { androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") implementation ("com.geyifeng.immersionbar:immersionbar:3.2.2") implementation ("com.geyifeng.immersionbar:immersionbar-components:3.2.2") + implementation("com.squareup.okhttp3:okhttp:4.11.0") + implementation ("com.google.code.gson:gson:2.10.1") + implementation ("com.squareup.okhttp3:logging-interceptor:4.9.3") + implementation ("com.squareup.retrofit2:retrofit:2.9.0") + implementation ("com.squareup.retrofit2:converter-gson:2.9.0") } \ No newline at end of file diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index e64013f..2aa9088 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -11,12 +11,24 @@ android:supportsRtl="true" android:theme="@style/Theme.Liyueling" android:usesCleartextTraffic="true"> - - - + + + + + + diff --git a/src/main/java/com/example/liyueling_final/ChangePasswordActivity.java b/src/main/java/com/example/liyueling_final/ChangePasswordActivity.java new file mode 100644 index 0000000..820e4a7 --- /dev/null +++ b/src/main/java/com/example/liyueling_final/ChangePasswordActivity.java @@ -0,0 +1,14 @@ +package com.example.liyueling_final; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; + +public class ChangePasswordActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_change_password); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/liyueling_final/EnrollActivity.java b/src/main/java/com/example/liyueling_final/EnrollActivity.java new file mode 100644 index 0000000..d8dcc4f --- /dev/null +++ b/src/main/java/com/example/liyueling_final/EnrollActivity.java @@ -0,0 +1,85 @@ +package com.example.liyueling_final; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.EditText; +import android.widget.Toast; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class EnrollActivity extends AppCompatActivity { + + + EditText phone; + EditText password; + EditText repassword; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_enroll); + + findViewById(R.id.enter).setOnClickListener(new View.OnClickListener() { + + + @Override + public void onClick(View v) { + + String phoneNumber = phone.getText().toString(); + String userPassword = password.getText().toString(); + String reuserPassword = repassword.getText().toString(); + + if (phoneNumber.isEmpty() || userPassword.isEmpty() || reuserPassword.isEmpty()) { + Toast.makeText(EnrollActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show(); + return; + } + if (!userPassword.equals(reuserPassword)) { + Toast.makeText(EnrollActivity.this, "两次输入密码不同", Toast.LENGTH_SHORT).show(); + return; + } + userApi api2 = RetrofitUtils.getRetrofit("https://dev.usemock.com/664ac0559e857b0cdafce629/").create(userApi.class); + try { + Call call = api2.Enroll(phoneNumber,userPassword); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + User user = response.body(); + if (user != null && user.password.equals(userPassword)) { + Intent intent = new Intent(EnrollActivity.this, LoginMainActivity.class); + startActivity(intent); + finish(); + } else { + // 注册失败 + Toast.makeText(EnrollActivity.this, "注册失败", Toast.LENGTH_SHORT).show(); + } + + } + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(EnrollActivity.this, "Network error", Toast.LENGTH_SHORT).show(); + } + }); + } catch (Exception e) { + Log.e("YourTag", "Error occurred: " + e.getMessage()); // 打印异常信息 + } + } + + + + }); + findViewById(R.id.textView174).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(EnrollActivity.this,LoginMainActivity.class); + startActivity(intent); + finish(); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/liyueling_final/ForgetPasswordActivity.java b/src/main/java/com/example/liyueling_final/ForgetPasswordActivity.java new file mode 100644 index 0000000..0c1d2cb --- /dev/null +++ b/src/main/java/com/example/liyueling_final/ForgetPasswordActivity.java @@ -0,0 +1,41 @@ +package com.example.liyueling_final; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.EditText; +import android.widget.Toast; + +public class ForgetPasswordActivity extends AppCompatActivity { + + EditText yanzhen; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_forget_password); + yanzhen = findViewById(R.id.password1); + + findViewById(R.id.fasong).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(ForgetPasswordActivity.this, "239546", Toast.LENGTH_SHORT).show(); + } + }); + + findViewById(R.id.enter).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(yanzhen.getText().toString().equals("239546")){ + Intent intent = new Intent(ForgetPasswordActivity.this, ChangePasswordActivity.class); + startActivity(intent); + finish(); + }else{ + Toast.makeText(ForgetPasswordActivity.this, "验证码错误", Toast.LENGTH_SHORT).show(); + } + + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/liyueling_final/LoginMainActivity.java b/src/main/java/com/example/liyueling_final/LoginMainActivity.java index 0d92d1d..e8b1153 100644 --- a/src/main/java/com/example/liyueling_final/LoginMainActivity.java +++ b/src/main/java/com/example/liyueling_final/LoginMainActivity.java @@ -3,28 +3,99 @@ package com.example.liyueling_final; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.RadioButton; +import android.widget.TextView; +import android.widget.Toast; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; public class LoginMainActivity extends AppCompatActivity { + private RadioButton radioButton; + EditText phone; + EditText password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main); + radioButton = findViewById(R.id.radioButton); + phone = findViewById(R.id.phone); + password = findViewById(R.id.password1); + + findViewById(R.id.enter).setOnClickListener(new View.OnClickListener() { + + @Override public void onClick(View v) { - SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putBoolean("isLogin",true); - editor.commit(); - Intent intent = new Intent(LoginMainActivity.this,MainActivity.class); + String phoneNumber = phone.getText().toString(); + String userPassword = password.getText().toString(); + + if (phoneNumber.isEmpty() || userPassword.isEmpty()) { + Toast.makeText(LoginMainActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show(); + return; + } + if (!radioButton.isChecked()) { + Toast.makeText(LoginMainActivity.this, "请同意协议", Toast.LENGTH_SHORT).show(); + return; + } + userApi api2 = RetrofitUtils.getRetrofit("https://dev.usemock.com/664ac0559e857b0cdafce629/").create(userApi.class); + try { + Call call = api2.Login(phoneNumber); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + User user = response.body(); + if (user != null && user.password.equals(userPassword)) { + Intent intent = new Intent(LoginMainActivity.this, MainActivity.class); + startActivity(intent); + finish(); + } else { + // 登录失败 + Toast.makeText(LoginMainActivity.this, "Login failed", Toast.LENGTH_SHORT).show(); + } + + } + @Override + public void onFailure(Call call, Throwable t) { + Toast.makeText(LoginMainActivity.this, "Network error", Toast.LENGTH_SHORT).show(); + } + }); + } catch (Exception e) { + Log.e("YourTag", "Error occurred: " + e.getMessage()); // 打印异常信息 + } + } + + + + }); + findViewById(R.id.textView174).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(LoginMainActivity.this,EnrollActivity.class); + startActivity(intent); + finish(); + } + }); + findViewById(R.id.textView175).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(LoginMainActivity.this,ForgetPasswordActivity.class); startActivity(intent); finish(); } }); } + } \ No newline at end of file diff --git a/src/main/java/com/example/liyueling_final/RetrofitUtils.java b/src/main/java/com/example/liyueling_final/RetrofitUtils.java new file mode 100644 index 0000000..a4ec1db --- /dev/null +++ b/src/main/java/com/example/liyueling_final/RetrofitUtils.java @@ -0,0 +1,53 @@ +package com.example.liyueling_final; + +import android.util.Log; + +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +public class RetrofitUtils { + + public static Retrofit getRetrofit(String url) { + //日志显示级别 + HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY; + //新建log拦截器 + HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { + @Override + public void log(String message) { + //我们采取分段打印日志的方法:当长度超过4000时,我们就来分段截取打印 + //剩余的字符串长度如果大于4000 + if (message.length() > 4000) { + for (int i = 0; i < message.length(); i += 4000) { + //当前截取的长度<总长度则继续截取最大的长度来打印 + if (i + 4000 < message.length()) { + Log.i("RetrofitMessage" + i, message.substring(i, i + 4000)); + } else { + //当前截取的长度已经超过了总长度,则打印出剩下的全部信息 + Log.i("RetrofitMessage" + i, message.substring(i, message.length())); + } + } + } else { + //直接打印 + Log.i("RetrofitMessage", "OkHttp====Message:" + message); + } + } + }); + loggingInterceptor.setLevel(level); + //定制OkHttp + OkHttpClient.Builder httpClientBuilder = new OkHttpClient + .Builder(); + //OkHttp进行添加拦截器loggingInterceptor + httpClientBuilder.addInterceptor(loggingInterceptor); + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl(url) + .addConverterFactory(GsonConverterFactory.create()) + .client( httpClientBuilder.build()) + .build(); + + return retrofit; + } + +} \ No newline at end of file diff --git a/src/main/java/com/example/liyueling_final/User.java b/src/main/java/com/example/liyueling_final/User.java new file mode 100644 index 0000000..911a078 --- /dev/null +++ b/src/main/java/com/example/liyueling_final/User.java @@ -0,0 +1,19 @@ +package com.example.liyueling_final; + +import com.google.gson.annotations.SerializedName; + +public class User { + + @SerializedName("id") + public Integer id; + @SerializedName("username") + public String username; + @SerializedName("phone") + public String phone; + @SerializedName("password") + public String password; + @SerializedName("age") + public Integer age; + @SerializedName("school") + public String school; +} diff --git a/src/main/java/com/example/liyueling_final/userApi.java b/src/main/java/com/example/liyueling_final/userApi.java new file mode 100644 index 0000000..5ac41a8 --- /dev/null +++ b/src/main/java/com/example/liyueling_final/userApi.java @@ -0,0 +1,17 @@ +package com.example.liyueling_final; + +import retrofit2.Call; +import retrofit2.http.Field; +import retrofit2.http.FormUrlEncoded; +import retrofit2.http.GET; +import retrofit2.http.Header; +import retrofit2.http.POST; +import retrofit2.http.Query; + +public interface userApi { + @FormUrlEncoded + @POST("userlogin") + Call Login (@Field("phone") String phone); + @POST("userenroll") + Call Enroll (@Field("phone") String phone,@Field("password") String password ); +} diff --git a/src/main/res/drawable/textview_border.xml b/src/main/res/drawable/textview_border.xml new file mode 100644 index 0000000..d4c8160 --- /dev/null +++ b/src/main/res/drawable/textview_border.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/src/main/res/layout/activity_change_password.xml b/src/main/res/layout/activity_change_password.xml new file mode 100644 index 0000000..a72dac9 --- /dev/null +++ b/src/main/res/layout/activity_change_password.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/activity_enroll.xml b/src/main/res/layout/activity_enroll.xml new file mode 100644 index 0000000..4ac007d --- /dev/null +++ b/src/main/res/layout/activity_enroll.xml @@ -0,0 +1,140 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/activity_forget_password.xml b/src/main/res/layout/activity_forget_password.xml new file mode 100644 index 0000000..26e45fc --- /dev/null +++ b/src/main/res/layout/activity_forget_password.xml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/activity_login_main.xml b/src/main/res/layout/activity_login_main.xml index 02e35a0..23584fe 100644 --- a/src/main/res/layout/activity_login_main.xml +++ b/src/main/res/layout/activity_login_main.xml @@ -18,21 +18,21 @@ app:srcCompat="@drawable/img_117" /> + app:layout_constraintTop_toBottomOf="@+id/phone" /> + app:layout_constraintTop_toBottomOf="@+id/password1" /> @@ -106,6 +107,7 @@ android:layout_height="wrap_content" android:layout_marginTop="39dp" android:text="没有账号?点击注册" + android:textColor="#9C9C9C" app:layout_constraintStart_toStartOf="@+id/imageView144" app:layout_constraintTop_toBottomOf="@+id/enter" /> @@ -114,6 +116,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码" + android:textColor="#9C9C9C" app:layout_constraintBottom_toBottomOf="@+id/textView174" - app:layout_constraintEnd_toEndOf="@+id/editTextTextPassword" /> + app:layout_constraintEnd_toEndOf="@+id/password1" /> \ No newline at end of file diff --git a/src/main/res/layout/fragment_maingood.xml b/src/main/res/layout/fragment_maingood.xml index e94db54..e6133ef 100644 --- a/src/main/res/layout/fragment_maingood.xml +++ b/src/main/res/layout/fragment_maingood.xml @@ -715,7 +715,7 @@