Commit dbb1f52deb0bffb5d6b85123e68d0c1fff2e6f50
1 parent
8f79866a79
Exists in
master
录入作业
Showing
6 changed files
with
199 additions
and
7 deletions
Show diff stats
app/src/main/java/com/hjx/parent/ImageActivity.java
| ... | ... | @@ -347,10 +347,16 @@ public class ImageActivity extends BaseRxActivity<ActivityImageBinding> implemen |
| 347 | 347 | ErrorInputDialog dialog = new ErrorInputDialog(this, topicBeans); |
| 348 | 348 | dialog.show(); |
| 349 | 349 | } else { |
| 350 | + List<String> paths = new ArrayList<>(); | |
| 351 | + for (TopicBean it: topicBeans) { | |
| 352 | + paths.add(it.getPath()); | |
| 353 | + } | |
| 350 | 354 | if (addHomeworkDialog == null) { |
| 351 | 355 | addHomeworkDialog = new AddHomeworkDialog(this); |
| 352 | 356 | } |
| 353 | - addHomeworkDialog.show(); | |
| 357 | + addHomeworkDialog.show(paths, () -> { | |
| 358 | + // TODO | |
| 359 | + }); | |
| 354 | 360 | } |
| 355 | 361 | } |
| 356 | 362 | ... | ... |
app/src/main/java/com/hjx/parent/dialog/AddHomeworkDialog.java
| ... | ... | @@ -2,39 +2,47 @@ package com.hjx.parent.dialog; |
| 2 | 2 | |
| 3 | 3 | import android.annotation.SuppressLint; |
| 4 | 4 | import android.content.Context; |
| 5 | +import android.util.Pair; | |
| 5 | 6 | import android.view.View; |
| 6 | 7 | import android.widget.AdapterView; |
| 7 | 8 | import android.widget.ArrayAdapter; |
| 9 | +import android.widget.Toast; | |
| 8 | 10 | |
| 9 | 11 | import androidx.annotation.NonNull; |
| 10 | 12 | |
| 11 | 13 | import com.bigkoo.pickerview.builder.TimePickerBuilder; |
| 12 | -import com.bigkoo.pickerview.listener.OnTimeSelectListener; | |
| 13 | -import com.bigkoo.pickerview.view.TimePickerView; | |
| 14 | 14 | import com.google.gson.Gson; |
| 15 | 15 | import com.hjx.parent.R; |
| 16 | 16 | import com.hjx.parent.bean.StudentBean; |
| 17 | 17 | import com.hjx.parent.databinding.DialogAddHomeworkBinding; |
| 18 | -import com.hjx.parent.rx.ILifecycleActivity; | |
| 18 | +import com.hjx.parent.function.Function0; | |
| 19 | +import com.hjx.parent.rx.BaseRxActivity; | |
| 19 | 20 | import com.prws.common.bean.GradeAndSubject; |
| 20 | 21 | import com.prws.common.bean.ResponseResult; |
| 21 | 22 | import com.prws.common.bean.Student; |
| 22 | 23 | import com.prws.common.bean.Subject; |
| 24 | +import com.prws.common.bean.baidu.BaiduInput; | |
| 23 | 25 | import com.prws.common.net.NetWorks; |
| 24 | 26 | import com.prws.common.utils.SharedPreferencesUtil; |
| 25 | 27 | import com.trello.rxlifecycle2.android.RxLifecycleAndroid; |
| 26 | 28 | |
| 29 | +import java.io.File; | |
| 27 | 30 | import java.text.SimpleDateFormat; |
| 28 | 31 | import java.util.ArrayList; |
| 29 | 32 | import java.util.Date; |
| 33 | +import java.util.HashMap; | |
| 30 | 34 | import java.util.List; |
| 31 | 35 | import java.util.Locale; |
| 36 | +import java.util.Map; | |
| 32 | 37 | |
| 38 | +import io.reactivex.Observable; | |
| 33 | 39 | import io.reactivex.android.schedulers.AndroidSchedulers; |
| 34 | 40 | import io.reactivex.schedulers.Schedulers; |
| 35 | 41 | |
| 36 | 42 | public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ |
| 37 | - private final ILifecycleActivity activity; | |
| 43 | + private final BaseRxActivity<?> activity; | |
| 44 | + | |
| 45 | + private Function0 callback; | |
| 38 | 46 | |
| 39 | 47 | String stuId; |
| 40 | 48 | |
| ... | ... | @@ -42,17 +50,25 @@ public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ |
| 42 | 50 | List<Student> studentList; |
| 43 | 51 | List<GradeAndSubject> gradeAndSubjectList; |
| 44 | 52 | |
| 53 | + List<String> images; | |
| 54 | + | |
| 45 | 55 | Student student; |
| 46 | 56 | String grade; |
| 47 | 57 | String term; |
| 48 | 58 | String subject; |
| 49 | 59 | Date uploadTime = new Date(); |
| 50 | 60 | |
| 51 | - public AddHomeworkDialog(ILifecycleActivity context) { | |
| 61 | + public AddHomeworkDialog(BaseRxActivity<?> context) { | |
| 52 | 62 | super((Context) context); |
| 53 | 63 | activity = context; |
| 54 | 64 | } |
| 55 | 65 | |
| 66 | + public void show(List<String> paths, Function0 callback) { | |
| 67 | + images = paths; | |
| 68 | + this.callback = callback; | |
| 69 | + super.show(); | |
| 70 | + } | |
| 71 | + | |
| 56 | 72 | @SuppressLint("SetTextI18n") |
| 57 | 73 | @Override |
| 58 | 74 | public void initView() { |
| ... | ... | @@ -107,6 +123,15 @@ public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ |
| 107 | 123 | }).setType(new boolean[]{true, true, true, false, false, false}).isDialog(true).build().show(); |
| 108 | 124 | }); |
| 109 | 125 | |
| 126 | + binding.btnConfirm.setOnClickListener(v -> { | |
| 127 | + String name = binding.etName.getText().toString().trim(); | |
| 128 | + if (name.isEmpty()) { | |
| 129 | + Toast.makeText(getContext(), "请输入作业名称", Toast.LENGTH_SHORT).show(); | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + uploadImage(name); | |
| 133 | + }); | |
| 134 | + | |
| 110 | 135 | getGrade(); |
| 111 | 136 | } |
| 112 | 137 | |
| ... | ... | @@ -183,6 +208,73 @@ public class AddHomeworkDialog extends BaseDialog<DialogAddHomeworkBinding>{ |
| 183 | 208 | binding.spSubject.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_spinner, subjects)); |
| 184 | 209 | } |
| 185 | 210 | |
| 211 | + @SuppressLint("CheckResult") | |
| 212 | + public void uploadImage(String name) { | |
| 213 | + activity.showLoadingDialog("上传作业中..."); | |
| 214 | + Observable.fromIterable(images) | |
| 215 | + .map(path -> { | |
| 216 | + int index = images.indexOf(path); | |
| 217 | + return new Pair<>(index, path); | |
| 218 | + }) | |
| 219 | + .subscribeOn(Schedulers.io()) | |
| 220 | + .flatMap(pair -> { | |
| 221 | + int index = pair.first; | |
| 222 | + String path = pair.second; | |
| 223 | + File file = new File(path); | |
| 224 | + String id = file.getName().substring(0, file.getName().lastIndexOf(".")); | |
| 225 | + return Observable.just(file) | |
| 226 | + .map(var -> { | |
| 227 | + BaiduInput baiduInput = NetWorks.inputImage(path, id).blockingGet(); | |
| 228 | + if (baiduInput.getError_code() != null) { | |
| 229 | + throw new RuntimeException("图片入库失败"); | |
| 230 | + } else { | |
| 231 | + return baiduInput; | |
| 232 | + } | |
| 233 | + }) | |
| 234 | + .map(var -> NetWorks.uploadImage(path, id).blockingGet()) | |
| 235 | + .map(var -> { | |
| 236 | + Map<String, Object> map = new HashMap<>(); | |
| 237 | + map.put("brief", id); | |
| 238 | + map.put("sort", index); | |
| 239 | + return map; | |
| 240 | + }); | |
| 241 | + }, 2) | |
| 242 | + .toList() | |
| 243 | + .map(list -> { | |
| 244 | + List<String> stuIds = new ArrayList<>(); | |
| 245 | + stuIds.add(student.stuId); | |
| 246 | + String userId = NetWorks.getUserId(); | |
| 247 | + | |
| 248 | + Map<String, Object> map = new HashMap<>(); | |
| 249 | + map.put("name", name); | |
| 250 | + map.put("grade", grade); | |
| 251 | + map.put("term", term); | |
| 252 | + map.put("subject", subject); | |
| 253 | + map.put("userId", userId); | |
| 254 | + map.put("stuIds", stuIds); | |
| 255 | + map.put("briefList", list); | |
| 256 | + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); | |
| 257 | + map.put("uploadTime", format.format(uploadTime)); | |
| 258 | + return map; | |
| 259 | + }) | |
| 260 | + .flatMap(body -> { | |
| 261 | + return NetWorks.service_url.uploadHomework(NetWorks.getHeader(), body); | |
| 262 | + }) | |
| 263 | + .observeOn(AndroidSchedulers.mainThread()) | |
| 264 | + .compose(RxLifecycleAndroid.bindActivity(activity.getRxLifecycle())) | |
| 265 | + .subscribe((response, th) -> { | |
| 266 | + if (th != null) th.printStackTrace(); | |
| 267 | + if (response != null && response.getSuccess()) { | |
| 268 | + activity.cancelLoadingDialog(); | |
| 269 | + dismiss(); | |
| 270 | + if (callback != null) callback.invoke(); | |
| 271 | + } else { | |
| 272 | + activity.loadFail("上传作业失败"); | |
| 273 | + } | |
| 274 | + }) | |
| 275 | + ; | |
| 276 | + } | |
| 277 | + | |
| 186 | 278 | @NonNull |
| 187 | 279 | @Override |
| 188 | 280 | public DialogAddHomeworkBinding getBinding() { | ... | ... |
app/src/main/res/layout/dialog_add_homework.xml
| ... | ... | @@ -165,12 +165,13 @@ |
| 165 | 165 | android:maxLength="50" |
| 166 | 166 | android:paddingHorizontal="16dp" |
| 167 | 167 | android:textColor="@color/text_title" |
| 168 | - android:textSize="18sp" | |
| 168 | + android:textSize="13sp" | |
| 169 | 169 | android:inputType="text" |
| 170 | 170 | tools:ignore="Autofill,LabelFor" /> |
| 171 | 171 | |
| 172 | 172 | <Space style="@style/empty_space"/> |
| 173 | 173 | <TextView |
| 174 | + android:id="@+id/btnConfirm" | |
| 174 | 175 | android:text="确认录入" |
| 175 | 176 | android:gravity="center" |
| 176 | 177 | android:textSize="16sp" | ... | ... |
libs/common/build.gradle
| ... | ... | @@ -19,6 +19,8 @@ android { |
| 19 | 19 | |
| 20 | 20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |
| 21 | 21 | |
| 22 | + buildConfigField("String", "APIKey1", "\"eCa3YzEjTBBiNjlXcNnjnldP\"") | |
| 23 | + buildConfigField("String", "SecretKey1", "\"KnZPmjF0ZzmxWV964mzmm6yuDljnETLt\"") | |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | 26 | buildTypes { | ... | ... |
libs/common/src/main/java/com/prws/common/bean/baidu/BaiduInput.java
| ... | ... | @@ -0,0 +1,57 @@ |
| 1 | +package com.prws.common.bean.baidu; | |
| 2 | + | |
| 3 | +public class BaiduInput { | |
| 4 | + String brief; | |
| 5 | + String error_code; | |
| 6 | + String cont_sign; | |
| 7 | + String log_id; | |
| 8 | + | |
| 9 | + public void setBrief(String brief) { | |
| 10 | + this.brief = brief; | |
| 11 | + } | |
| 12 | + | |
| 13 | + public void setError_code(String error_code) { | |
| 14 | + this.error_code = error_code; | |
| 15 | + } | |
| 16 | + | |
| 17 | + public void setCont_sign(String cont_sign) { | |
| 18 | + this.cont_sign = cont_sign; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public void setLog_id(String log_id) { | |
| 22 | + this.log_id = log_id; | |
| 23 | + } | |
| 24 | + | |
| 25 | + public String getBrief() { | |
| 26 | + return brief; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public String getError_code() { | |
| 30 | + return error_code; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public String getCont_sign() { | |
| 34 | + return cont_sign; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public String getLog_id() { | |
| 38 | + return log_id; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public class Brief { | |
| 42 | + String name; | |
| 43 | + String id; | |
| 44 | + | |
| 45 | + public void setName(String name) { | |
| 46 | + this.name = name; | |
| 47 | + } | |
| 48 | + | |
| 49 | + public String getName() { | |
| 50 | + return name; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public String getId() { | |
| 54 | + return id; | |
| 55 | + } | |
| 56 | + } | |
| 57 | +} | ... | ... |
libs/common/src/main/java/com/prws/common/net/NetWorks.java
| ... | ... | @@ -13,7 +13,9 @@ import com.prws.common.bean.Student; |
| 13 | 13 | import com.prws.common.bean.Teacher; |
| 14 | 14 | import com.prws.common.bean.TopicBean; |
| 15 | 15 | import com.prws.common.bean.UpdateBean; |
| 16 | +import com.prws.common.bean.baidu.BaiduInput; | |
| 16 | 17 | import com.prws.common.bean.homework.HomeworkList; |
| 18 | +import com.prws.common.utils.BitmapUtils; | |
| 17 | 19 | import com.prws.common.utils.SharedPreferencesUtil; |
| 18 | 20 | |
| 19 | 21 | import java.io.File; |
| ... | ... | @@ -180,6 +182,16 @@ public class NetWorks extends RetrofitUtils { |
| 180 | 182 | @GET("api/v1/homework/listHomeworkByStuId") |
| 181 | 183 | Single<ResponseResult<List<HomeworkList>>> getStudentHomework(@Header("Authorization") String token, @Query("stuId") String stuId); |
| 182 | 184 | |
| 185 | + @POST | |
| 186 | + Single<BaiduInput> inputImage(@Url String url, @Body RequestBody body); | |
| 187 | + | |
| 188 | + @Multipart | |
| 189 | + @POST("api/v1/homework/uploadHomework") | |
| 190 | + Single<ResponseResult> uploadImage(@Header("Authorization") String token, @Part() MultipartBody.Part file, @Query("brief") String id); | |
| 191 | + | |
| 192 | + @POST("api/v1/homework/uploadHomeworkAction") | |
| 193 | + Single<ResponseResult> uploadHomework(@Header("Authorization") String token, @Body Object map); | |
| 194 | + | |
| 183 | 195 | } |
| 184 | 196 | |
| 185 | 197 | public static String getUserId() { |
| ... | ... | @@ -194,6 +206,24 @@ public class NetWorks extends RetrofitUtils { |
| 194 | 206 | return (String) SharedPreferencesUtil.getData("baiduToken", ""); |
| 195 | 207 | } |
| 196 | 208 | |
| 209 | + public static Single<BaiduInput> inputImage(String filePath, String id) { | |
| 210 | + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | |
| 211 | + String base64 = BitmapUtils.fileToBase64(filePath); | |
| 212 | + File file = new File(filePath); | |
| 213 | + RequestBody body = RequestBody.create(mediaType, "image=" + base64 + "&brief={\"name\":\"" + file.getName() + "\", \"id\":\"" + id + "\"}"); | |
| 214 | + return getBaiduTokenOcr().map(jsonObject -> jsonObject.get("access_token").getAsString()) | |
| 215 | + .flatMap(token -> { | |
| 216 | + return service_url.inputImage("https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/add?access_token=" + token, body); | |
| 217 | + }); | |
| 218 | + } | |
| 219 | + | |
| 220 | + public static Single<ResponseResult> uploadImage(String path, String id) { | |
| 221 | + File file = new File(path); | |
| 222 | + RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpg"), file); | |
| 223 | + MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody); | |
| 224 | + return service_url.uploadImage(getHeader(), part, id); | |
| 225 | + } | |
| 226 | + | |
| 197 | 227 | |
| 198 | 228 | public static void checkUpdate(int code, String packageName, Callback<ResponseResult<UpdateBean>> callback) { |
| 199 | 229 | service_url.checkUpdate(code, packageName, 0).enqueue(callback); |
| ... | ... | @@ -231,6 +261,10 @@ public class NetWorks extends RetrofitUtils { |
| 231 | 261 | setSubscribe(service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey + "&client_secret=" + BuildConfig.SecretKey), observer); |
| 232 | 262 | } |
| 233 | 263 | |
| 264 | + public static Single<JsonObject> getBaiduTokenOcr() { | |
| 265 | + return service_url.getBaiduToken("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + BuildConfig.APIKey1 + "&client_secret=" + BuildConfig.SecretKey1).firstOrError(); | |
| 266 | + } | |
| 267 | + | |
| 234 | 268 | public static void removeWriting(String base64, Observer<JsonObject> observer) { |
| 235 | 269 | MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
| 236 | 270 | RequestBody body = RequestBody.create(mediaType, "image=" + base64); | ... | ... |