Skip to content

Commit

Permalink
resetpwd
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbxyyx committed Jul 10, 2024
1 parent 96023be commit 9bd174a
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
android:theme="@style/Theme.Xbook"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".ForgetpwdActivity"
android:exported="false" />
<activity
android:name=".IssuesActivity"
android:exported="false" />
Expand Down
87 changes: 87 additions & 0 deletions app/src/main/java/com/github/jsbxyyx/xbook/ForgetpwdActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.github.jsbxyyx.xbook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.jsbxyyx.xbook.common.Common;
import com.github.jsbxyyx.xbook.common.DataCallback;
import com.github.jsbxyyx.xbook.common.SPUtils;
import com.github.jsbxyyx.xbook.common.SessionManager;
import com.github.jsbxyyx.xbook.data.BookNetHelper;

public class ForgetpwdActivity extends AppCompatActivity {

private BookNetHelper bookNetHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgetpwd);

bookNetHelper = new BookNetHelper();

EditText et_login_user = findViewById(R.id.et_login_user);
EditText et_login_password = findViewById(R.id.et_login_password);
EditText et_login_code = findViewById(R.id.et_login_code);
Button btn_send_code = findViewById(R.id.btn_send_code);
Button btn_resetpwd = findViewById(R.id.btn_resetpwd);
TextView tv_login = findViewById(R.id.tv_login);

btn_send_code.setOnClickListener((v) -> {
String user = et_login_user.getText().toString();
String password = et_login_password.getText().toString();
bookNetHelper.sendCodePasswordRecovery(user, new DataCallback<JsonNode>() {
@Override
public void call(JsonNode dataObject, Throwable err) {
runOnUiThread(() -> {
if (err != null) {
Toast.makeText(getBaseContext(), "err:" + err.getMessage(), Toast.LENGTH_LONG).show();
return;
}
int success = dataObject.get("success").asInt();
if (success == 1) {
Toast.makeText(getBaseContext(), "发送成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), dataObject.get("err").asText(), Toast.LENGTH_LONG).show();
}
});
}
});
});

btn_resetpwd.setOnClickListener((v) -> {
String user = et_login_user.getText().toString();
String password = et_login_password.getText().toString();
String code = et_login_code.getText().toString();
bookNetHelper.resetpwd(user, password, code, new DataCallback<JsonNode>() {
@Override
public void call(JsonNode respData, Throwable err) {
runOnUiThread(() -> {
if (err != null) {
Toast.makeText(getBaseContext(), err.getMessage(), Toast.LENGTH_LONG).show();
return;
}
if (respData.get("success").asInt() == 1) {
Toast.makeText(getBaseContext(), "重置成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), respData.get("message").asText(""), Toast.LENGTH_LONG).show();
}
});
}
});
});

tv_login.setOnClickListener((v) -> {
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
startActivity(intent);
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
Button btn_send_code = findViewById(R.id.btn_send_code);
Button btn_registration = findViewById(R.id.btn_registration);
TextView tv_login = findViewById(R.id.tv_login);
TextView tv_forgetpwd = findViewById(R.id.tv_forgetpwd);

btn_send_code.setOnClickListener((v) -> {
String user = et_login_user.getText().toString();
Expand Down Expand Up @@ -85,5 +86,10 @@ public void call(String str, Throwable err) {
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
startActivity(intent);
});

tv_forgetpwd.setOnClickListener((v) -> {
Intent intent = new Intent(getBaseContext(), ForgetpwdActivity.class);
startActivity(intent);
});
}
}
113 changes: 113 additions & 0 deletions app/src/main/java/com/github/jsbxyyx/xbook/data/BookNetHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,63 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
});
}

public void sendCodePasswordRecovery(String email, DataCallback dataCallback) {
Map<String, Object> object = new HashMap<>();
String reqUrl = zurl + "/papi/user/verification/send-code";
object.put("method", "POST");
object.put("url", reqUrl);

Map<String, Object> headers = new HashMap<>();
headers.put("User-Agent", userAgent);
headers.put(content_type_key, "multipart/form-data");
object.put("headers", headers);

Map<String, Object> data = new HashMap<>();
data.put("email", email);
data.put("action", "passwordrecovery");
object.put("data", data);

Map<String, Object> params = new HashMap<>();
object.put("params", params);

String s = JsonUtil.toJson(object);
LogUtil.d(TAG, "send-code password recovery request: %s", s);
Request request = new Request.Builder()
.url(xurl)
.post(RequestBody.create(s, MediaType.parse("application/json")))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
LogUtil.d(TAG, "onFailure: %s", LogUtil.getStackTraceString(e));
dataCallback.call(null, e);
}

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (!response.isSuccessful()) {
LogUtil.d(TAG, "onResponse: %s", response.code());
dataCallback.call(null, new HttpStatusException(response.code() + "", response.code(), reqUrl));
return;
}
String string = response.body().string();
LogUtil.d(TAG, "send-code password recovery response: %s", string);
JsonNode jsonObject = JsonUtil.readTree(string);
int status = jsonObject.get("status").asInt();
if (!Common.statusSuccessful(status)) {
LogUtil.d(TAG, "onResponse: %s", status);
dataCallback.call(null, new HttpStatusException(status + "", status, reqUrl));
return;
}
String data = jsonObject.get("data").asText();
JsonNode dataObject = JsonUtil.readTree(data);
// "data": "{\"success\":1}}" 1success 0error
int success = dataObject.get("success").asInt();
dataCallback.call(dataObject, null);
}
});
}

public void registration(String email, String password, String verifyCode, DataCallback dataCallback) {
Map<String, Object> object = new HashMap<>();
String reqUrl = zurl + "/rpc.php";
Expand Down Expand Up @@ -947,4 +1004,60 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
});
}

public void resetpwd(String email, String password, String code, DataCallback<JsonNode> dataCallback) {
Map<String, Object> object = new HashMap<>();
String reqUrl = "/zlib_resetpwd";
object.put("method", "POST");
object.put("url", reqUrl);

Map<String, Object> headers = new HashMap<>();
headers.put("User-Agent", userAgent);
headers.put(content_type_key, "application/json");
object.put("headers", headers);

Map<String, Object> params = new HashMap<>();
object.put("params", params);

Map<String, Object> data = new HashMap<>();
data.put("email", email);
data.put("password", password);
data.put("code", code);
object.put("data", data);

String s = JsonUtil.toJson(object);
LogUtil.d(TAG, "resetpwd request: %s", s);

Request request = new Request.Builder()
.url(xburl)
.post(RequestBody.create(s, MediaType.parse("application/json")))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
LogUtil.d(TAG, "onFailure: %s", LogUtil.getStackTraceString(e));
dataCallback.call(null, e);
}

@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (!response.isSuccessful()) {
LogUtil.d(TAG, "onResponse: %s", response.code());
dataCallback.call(null, new HttpStatusException(response.code() + "", response.code(), reqUrl));
return;
}
String string = response.body().string();
LogUtil.d(TAG, "resetpwd response: %s", string);
JsonNode jsonObject = JsonUtil.readTree(string);
int status = jsonObject.get("status").asInt();
if (!Common.statusSuccessful(status)) {
LogUtil.d(TAG, "onResponse: %s", status);
dataCallback.call(null, new HttpStatusException(status + "", status, reqUrl));
return;
}
JsonNode data = jsonObject.get("data");
dataCallback.call(data, null);
}
});
}

}
65 changes: 65 additions & 0 deletions app/src/main/res/layout/activity_forgetpwd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".ForgetpwdActivity">

<EditText
android:id="@+id/et_login_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:singleLine="true"
android:hint="请输入邮箱" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/et_login_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:inputType="text"
android:maxLines="1"
android:singleLine="true"
android:hint="请输入验证码" />

<Button
android:id="@+id/btn_send_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送" />

</LinearLayout>

<EditText
android:id="@+id/et_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:hint="请输入密码" />

<Button
android:id="@+id/btn_resetpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="重置密码" />

<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="登录" />

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_registration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@
android:gravity="center"
android:text="登录" />

<TextView
android:id="@+id/tv_forgetpwd"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="center"
android:gravity="center"
android:text="忘记密码" />

</LinearLayout>

0 comments on commit 9bd174a

Please sign in to comment.