89 lines
3.4 KiB
Java
89 lines
3.4 KiB
Java
package com.xkrs.controller;
|
||
|
||
import com.xkrs.common.encapsulation.PromptMessageEnum;
|
||
import com.xkrs.dao.SysUserDao;
|
||
import com.xkrs.model.entity.SysUserEntity;
|
||
import com.xkrs.model.qo.SysUserPushAccountQo;
|
||
import com.xkrs.sms.PushHelper;
|
||
import com.xkrs.utils.ListUtils;
|
||
import org.apache.hc.core5.util.TextUtils;
|
||
import org.springframework.context.i18n.LocaleContextHolder;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import javax.annotation.Resource;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Locale;
|
||
import java.util.Optional;
|
||
|
||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||
|
||
@RestController
|
||
@RequestMapping(value = "/push")
|
||
public class PushController {
|
||
|
||
private final Locale locale = LocaleContextHolder.getLocale();
|
||
|
||
@Resource
|
||
private SysUserDao sysUserDao;
|
||
|
||
@Resource
|
||
private PushHelper pushHelper;
|
||
|
||
@PostMapping("/bindUserPushInfo")
|
||
public String bindUserPushInfo(@RequestBody SysUserPushAccountQo sysUserPushAccountQo) {
|
||
Integer id = sysUserPushAccountQo.getId();
|
||
String userAccount = sysUserPushAccountQo.getUserAccount();
|
||
String regID = sysUserPushAccountQo.getRegID();
|
||
if (null == id) {
|
||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,id == null", locale);
|
||
}
|
||
Optional<SysUserEntity> targetSysUserOptional = sysUserDao.findById(id);
|
||
if (targetSysUserOptional.isEmpty()) {
|
||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,id不存在", locale);
|
||
}
|
||
if (TextUtils.isEmpty(userAccount)) {
|
||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,userAccount == null", locale);
|
||
}
|
||
if (TextUtils.isEmpty(regID)) {
|
||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "绑定失败,regID == null", locale);
|
||
}
|
||
SysUserEntity targetSysUser = targetSysUserOptional.get();
|
||
// 更新 userAccount
|
||
targetSysUser.setPushUserAccount(userAccount);
|
||
// 更新 regID
|
||
String pushRegIDs = targetSysUser.getPushRegID();
|
||
if (TextUtils.isEmpty(pushRegIDs)) {
|
||
targetSysUser.setPushRegID(regID);
|
||
} else {
|
||
List<String> pushRegIDList = new ArrayList<>();
|
||
if (pushRegIDs.contains(",")) {
|
||
pushRegIDList.addAll(ListUtils.toStringList(pushRegIDs, ","));
|
||
} else {
|
||
pushRegIDList.add(pushRegIDs);
|
||
}
|
||
if (!pushRegIDList.contains(regID)) {
|
||
pushRegIDList.add(regID);
|
||
}
|
||
String newPushRegIDs = ListUtils.fromStringList(pushRegIDList, ",");
|
||
targetSysUser.setPushRegID(newPushRegIDs);
|
||
}
|
||
sysUserDao.save(targetSysUser);
|
||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "绑定成功", locale);
|
||
}
|
||
|
||
@GetMapping("/testPush")
|
||
public String testPush(@RequestParam(value = "userAccount") String userAccount) {
|
||
//推送
|
||
try {
|
||
List<String> userAccountList = new ArrayList<>();
|
||
userAccountList.add(userAccount);
|
||
pushHelper.dispatchPushMessage(userAccountList, null);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "OKK", locale);
|
||
}
|
||
|
||
}
|