package com.farriver.bwf.service.wechat; import com.alibaba.fastjson2.JSON; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.options.RoleOption; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.common.utilities.EncryptUtil; import com.farriver.bwf.data.transferobject.queryobject.security.AccountMasterQueryObject; import com.farriver.bwf.data.transferobject.queryobject.vip.VIPMasterQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.security.AccountMasterViewModel; import com.farriver.bwf.data.transferobject.viewmodel.security.PasswordViewModel; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterViewModel; import com.farriver.bwf.data.transferobject.wechat.WeChatLoginInfo; import com.farriver.bwf.data.transferobject.wechat.WeChatUserModel; import com.farriver.bwf.service.extend.rbac.RBACService; import com.farriver.bwf.service.vip.VIPMasterService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import jakarta.annotation.Resource; @Service public class WeChatUserService { private static final Logger logger = LoggerFactory.getLogger(WeChatUserService.class); private String message; @Resource VIPMasterService vipService; @Resource RBACService rbacService; public VIPMasterViewModel RegisterUser(WeChatUserModel model) { try { AccountMasterViewModel userViewModel = new AccountMasterViewModel(); userViewModel.setId(model.getId()); userViewModel.setName(model.getNickname()); userViewModel.setPassword(model.getPassword()); userViewModel.setType(RoleOption.Other.ordinal()); userViewModel.setCellphone(model.getCellphone()); AccountMasterViewModel rbacUser = null; AccountMasterQueryObject queryObject = new AccountMasterQueryObject(); queryObject.setCellphone(model.getCellphone()); ApiData apiData = rbacService.GetAccountDetail(queryObject); if (apiData.getCode() == 200) { rbacUser = JSON.parseObject(JSON.toJSONString(apiData.getData()), AccountMasterViewModel.class); } if (rbacUser == null) { apiData = rbacService.RegisterAccount(userViewModel); if (apiData.getCode() == 200 || apiData.getMessage().equals(LangConstants.MSG_ERROR_CELLPHONE_EXISTED)) { rbacUser = JSON.parseObject(JSON.toJSONString(apiData.getData()), AccountMasterViewModel.class); if (rbacUser == null) { return null; } } else { return null; } } VIPMasterViewModel entity = new VIPMasterViewModel(); entity.setRemark(model.getRemark()); entity.setName(model.getName()); entity.setDiamondtype(model.getDiamondtype()); entity.setType(model.getType()); entity.setDiscountrate(model.getDiscountrate()); entity.setIdcard(model.getIdcard()); entity.setGender(model.getGender()); entity.setBirthday(model.getBirthday()); entity.setOpenid(model.getOpenid()); entity.setNickname(model.getNickname()); entity.setLanguage(model.getLanguage()); entity.setCity(model.getCity()); entity.setProvince(model.getProvince()); entity.setCountry(model.getCountry()); entity.setAvatarurl(model.getAvatarurl()); entity.setCellphone(model.getCellphone()); entity.setToken(model.getToken()); entity.setId(rbacUser.getId()); vipService.Create(entity); return entity; } catch (Exception exception) { logger.warn(exception.getMessage()); } return null; } public ApiData ResetPassword(WeChatUserModel model) { VIPMasterQueryObject queryObject = new VIPMasterQueryObject(); queryObject.setCellphone(model.getCellphone()); VIPMasterViewModel vipMasterViewModel = vipService.GetViewDetail(queryObject); if (vipMasterViewModel == null) { return null; } if (model.getPassword() != null && !model.getPassword().isEmpty()) { String encryptPassword = EncryptUtil.md5Encrypt(model.getPassword()); vipMasterViewModel.setPassword(encryptPassword); try { PasswordViewModel passwordViewModel = new PasswordViewModel(); passwordViewModel.setNewpassword(model.getPassword()); passwordViewModel.setRandomcode(model.getRandomcode()); passwordViewModel.setCellphone(model.getCellphone()); ApiData apiData = rbacService.ResetPasswordSMSCode(passwordViewModel); String m = apiData.getMessage(); logger.info("Reset password of RBAC message: " + m); } catch (Exception exception) { logger.warn(exception.getMessage()); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, vipService.Update(vipMasterViewModel)); } else { return ApiData.error(message); } } public WeChatUserModel WeChatLoginByOpenid(WeChatLoginInfo weChatLoginInfo) { if (weChatLoginInfo == null || weChatLoginInfo.getOpenid() == null || weChatLoginInfo.getOpenid().isEmpty()) { return null; } VIPMasterQueryObject queryObject = new VIPMasterQueryObject(); queryObject.setOpenid(weChatLoginInfo.getOpenid()); VIPMasterViewModel vipMasterViewModel = vipService.GetViewDetail(queryObject); if (vipMasterViewModel == null) { return null; } return BuildViewModel(vipMasterViewModel); } public WeChatUserModel WeChatLoginBySMSCode(WeChatLoginInfo weChatLoginInfo) { if (weChatLoginInfo != null && !weChatLoginInfo.getCellphone().isEmpty()) { VIPMasterQueryObject queryObject = new VIPMasterQueryObject(); queryObject.setCellphone(weChatLoginInfo.getCellphone()); VIPMasterViewModel vipMasterViewModel = vipService.GetViewDetail(queryObject); if (vipMasterViewModel == null) { return null; } return BuildViewModel(vipMasterViewModel); } return null; } public WeChatUserModel WeChatLoginByPassword(WeChatLoginInfo weChatLoginInfo) { if (weChatLoginInfo == null || weChatLoginInfo.getCellphone() == null || weChatLoginInfo.getCellphone().isEmpty()) { return null; } VIPMasterQueryObject queryObject = new VIPMasterQueryObject(); queryObject.setCellphone(weChatLoginInfo.getCellphone()); VIPMasterViewModel vipMasterViewModel = vipService.GetViewDetail(queryObject); if (vipMasterViewModel == null) { return null; } WeChatUserModel weChatUser = BuildViewModel(vipMasterViewModel); String encryptPassword = EncryptUtil.md5Encrypt(weChatLoginInfo.getPassword()); if (weChatUser.getPassword() == null || !weChatUser.getPassword().equals(encryptPassword)) { return null; } return weChatUser; } //private methods private static WeChatUserModel BuildViewModel(VIPMasterViewModel entity) { if (entity == null) return null; WeChatUserModel model = new WeChatUserModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setName(entity.getName()); model.setDiamondtype(entity.getDiamondtype()); model.setType(entity.getType()); model.setDiscountrate(entity.getDiscountrate()); model.setIdcard(entity.getIdcard()); model.setGender(entity.getGender()); model.setBirthday(entity.getBirthday()); model.setOpenid(entity.getOpenid()); model.setNickname(entity.getNickname()); model.setLanguage(entity.getLanguage()); model.setCity(entity.getCity()); model.setProvince(entity.getProvince()); model.setCountry(entity.getCountry()); model.setAvatarurl(entity.getAvatarurl()); model.setCellphone(entity.getCellphone()); model.setToken(entity.getToken()); return model; } }