示例#1
0
  /**
   * 修改帐户信息
   *
   * @param nickName
   * @param phone
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/modify", produces = "text/plain;charset=UTF-8")
  @Authorization(type = Constant.SESSION_USER)
  public String modify(
      @RequestParam(value = "nickname", required = false, defaultValue = "") String nickName,
      @RequestParam(value = "phone", required = false, defaultValue = "") String phone)
      throws Exception {
    if (Utils.isEmpty(nickName) && Utils.isEmpty(phone))
      return JsonUtils.resultJson(-2, "修改字段不能为空", null);

    Object obj = session.getAttribute(Constant.SESSION_USER);
    User user = userService.findOne(((User) obj).getId());

    if (!Utils.isEmpty(nickName)) user.setNickName(nickName.trim());
    if (!Utils.isEmpty(phone)) user.setPhone(phone);
    userService.save(user);
    session.setAttribute(Constant.SESSION_USER, user);
    return JsonUtils.resultJson(1, "", null);
  }
示例#2
0
  /**
   * 用户注册
   *
   * @param name 用户名
   * @param nickname 昵称
   * @param pwd 密码(MD5)
   * @param phone 手机号
   * @param type 用户类型:1 商户 2 顾客
   * @param code 推广码
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/register", produces = "text/plain;charset=UTF-8")
  public String register(
      @RequestParam(value = "name", required = false, defaultValue = "") String userName,
      @RequestParam(value = "nickname", required = false, defaultValue = "") String nickName,
      @RequestParam(value = "pwd", required = false, defaultValue = "") String pwd,
      @RequestParam(value = "phone", required = false, defaultValue = "") String phone,
      @RequestParam(value = "type", required = false, defaultValue = "2") int type,
      @RequestParam(value = "promocode", required = false, defaultValue = "") String code)
      throws Exception {
    if (Utils.isEmpty(userName) || Utils.isEmpty(pwd)) {
      return JsonUtils.resultJson(-2, "用户名或密码不能为空", null);
    }
    if (userService.findByUserName(userName.trim()) != null)
      return JsonUtils.resultJson(-3, "用户名已经被注册", null);
    if (Utils.isEmpty(nickName)) {
      nickName = userName;
    }

    Object obj = session.getAttribute(Constant.SESSION_USER);
    User user = null;
    if (null != obj) user = (User) obj;
    else user = new User();
    user.setUserName(userName.trim());
    user.setNickName(nickName.trim());
    user.setPwd(pwd.trim());
    user.setPhone(phone.trim());
    user.setPromoCode(code.trim());
    user.setType(type == 1 ? UserType.merchants : UserType.customer);
    user.setImei(getImei());
    user.setRegisterVer(getVersionName());
    user.setCurrVer(getVersionName());

    // 通过IP获取位置信息
    if (Utils.isEmpty(user.getCity()) || user.getCityCode() == 0) {
      String ip = getRemoteAddr();
      if (!Utils.isEmpty(ip)) {
        AddressBean addr = Common.ipWithBaidu(ip);
        if (null != addr) {
          user.setCity(addr.getCity());
          user.setProvince(addr.getProvince());
          user.setCityCode(addr.getCityCode());
        }
      }
    }

    userService.save(user);
    session.setAttribute(Constant.SESSION_USER, user);

    if (type == 1) {
      initSession(UserType.merchants, user, false);
    } else {
      initSession(UserType.customer, user, false);
    }

    List<Address> addrs = addressService.findByUserId(user.getId());
    List<Map<String, String>> resAddr = new LinkedList<Map<String, String>>();
    if (null != addrs && !addrs.isEmpty()) {
      for (Address addr : addrs) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("addr_id", addr.getId() + "");
        map.put("address", addr.getAddress());
        map.put("phone", addr.getPhone());
        map.put("name", addr.getName());
        map.put("lng", addr.getLocation()[0] + "");
        map.put("lat", addr.getLocation()[1] + "");
        map.put("def", addr.getId() == user.getAddressId() ? "1" : "0");
        resAddr.add(map);
      }
    }

    Map<String, Object> veResult = new HashMap<String, Object>();
    veResult.put("user_id", user.getId() + "");
    veResult.put("user_name", userName);
    veResult.put("nick_name", nickName);
    veResult.put("phone", phone);
    veResult.put("user_type", "1");
    if (type == 1) veResult.put("merc_num", user.getMercNum() + "");
    veResult.put("addrs", resAddr);
    return JsonUtils.resultJson(1, "", veResult);
  }