<–start–>
需求描述:當客戶打開收到郵箱激活碼的郵件,點擊激活鏈接,正確填寫激活碼后就會完成郵箱激活的步驟。
在后台編程代碼編寫中,有以下幾個要點:
① 接收客戶的手機號碼和郵箱激活碼。
② 先判斷激活碼是否有效。如果激活碼無效,提示用戶。
③ 如果激活碼有效,接下來就要判斷用戶是否在重復綁定郵箱,customer表中
的type字段就是用來甄別郵箱是否已經激活,默認該字段的值是空值,如果type字段的值為1表示用戶已經綁定郵箱。
④ 如果用戶沒有綁定過郵箱,就完成郵箱的綁定。
編寫CustomerAction類,提供activeMail方法:
① 使用屬性驅動接收激活碼。
// 屬性驅動 private String activecode; public void setActivecode(String activecode) { this.activecode = activecode; }
② 判斷激活碼是否有效。因為先前已經將郵箱激活碼存入了redis中,所以我們
可以直接從redis中獲取激活碼,判斷用戶提交的激活碼是否為空或者與redis中存儲的不同,這兩種情況均表示激活碼無效。
③ 解決響應到客戶端的中文亂碼的問題。
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
④ 激活碼有效的話,為了避免用戶重復點擊激活地址導致重復綁定情形發生,
就需要通過webservice查詢crm系統中的客戶信息,判斷是否已經綁定。
⑤ 對於已經激活郵箱的用戶,在redis中刪除郵箱激活碼。
// 刪除redis的激活碼 redisTemplate.delete(model.getTelephone());
完整的CustomerAction代碼:
@Action("customer_activeMail") public String activeMail() throws IOException { ServletActionContext.getResponse().setContentType( "text/html;charset=utf-8"); // 判斷激活碼是否有效 String activecodeRedis = redisTemplate.opsForValue().get( model.getTelephone()); if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) { // 激活碼無效 ServletActionContext.getResponse().getWriter() .println("激活碼無效,請登錄系統,重新綁定郵箱!"); } else { // 激活碼有效 // 防止重復綁定 // 調用CRM webService 查詢客戶信息,判斷是否已經綁定 Customer customer = WebClient .create("http://localhost:9002/crm_management/services" + "/customerService/customer/telephone/" + model.getTelephone()) .accept(MediaType.APPLICATION_JSON).get(Customer.class); if (customer.getType() == null || customer.getType() != 1) { // 沒有綁定,進行綁定 WebClient.create( "http://localhost:9002/crm_management/services" + "/customerService/customer/updatetype/" + model.getTelephone()).get(); ServletActionContext.getResponse().getWriter() .println("郵箱綁定成功!"); } else { // 已經綁定過 ServletActionContext.getResponse().getWriter() .println("郵箱已經綁定過,無需重復綁定!"); } // 刪除redis的激活碼 redisTemplate.delete(model.getTelephone()); } return NONE; }
在crm_management系統中,編寫webservice服務接口findByTelephone,通過手機號碼來查詢客戶信息。
@Path("/customer/telephone/{telephone}") @GET @Consumes({ "application/xml", "application/json" }) public Customer findByTelephone(@PathParam("telephone") String telephone);
編寫updateType服務接口,當激活碼有效時,就修改customer表中的type字段的值為1。
@Path("/customer/updatetype/{telephone}") @GET public void updateType(@PathParam("telephone") String telephone);
在實現類CustomerServiceImpl中實現findByTelephone和updateType這兩個方法。
@Override public Customer findByTelephone(String telephone) { return customerRepository.findByTelephone(telephone); } @Override public void updateType(String telephone) { customerRepository.updateType(telephone); }
在CustomerRepository的dao中編寫方法,運用spring data jpa完成持久層的操作。
public Customer findByTelephone(String telephone); @Query("update Customer set type=1 where telephone= ?") @Modifying public void updateType(String telephone);
完整的CustomerService服務接口代碼:
public interface CustomerService { @Path("/customer/telephone/{telephone}") @GET @Consumes({ "application/xml", "application/json" }) public Customer findByTelephone(@PathParam("telephone") String telephone); @Path("/customer/updatetype/{telephone}") @GET public void updateType(@PathParam("telephone") String telephone); }
完整的CustomerServiceImpl實現類代碼:
@Service @Transactional public class CustomerServiceImpl implements CustomerService { // 注入DAO @Autowired private CustomerRepository customerRepository; @Override public Customer findByTelephone(String telephone) { return customerRepository.findByTelephone(telephone); } @Override public void updateType(String telephone) { customerRepository.updateType(telephone); } }
完整的CustomerRepository持久層代碼:
public interface CustomerRepository extends JpaRepository<Customer, Integer> { public Customer findByTelephone(String telephone); @Query("update Customer set type=1 where telephone= ?") @Modifying public void updateType(String telephone); }
<–end–>