/**
   * 用户登录
   *
   * @param userName
   * @param pwd
   * @param type 用户类型:1 商户 2 顾客
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/login", produces = "text/plain;charset=UTF-8")
  public String login(
      @RequestParam(value = "name", required = false, defaultValue = "") String userName,
      @RequestParam(value = "pwd", required = false, defaultValue = "") String pwd,
      @RequestParam(value = "type", required = false, defaultValue = "2") int type)
      throws Exception {
    if (Utils.isEmpty(userName) || Utils.isEmpty(pwd))
      return JsonUtils.resultJson(-2, "用户名或密码不能为空", null);
    User user = userService.findByUserName(userName.trim());
    if (null == user) return JsonUtils.resultJson(-3, "用户尚未注册", null);
    if (Utils.isEmpty(user.getPwd()) || !user.getPwd().equalsIgnoreCase(pwd.trim()))
      return JsonUtils.resultJson(-4, "密码错误", null);

    if (type == 2) {
      if (!user.getType().equals(UserType.customer)) {
        return JsonUtils.resultJson(-5, "帐号密码错误", null);
      }
    }
    if (type == 1) {
      if (!user.getType().equals(UserType.merchants)) {
        return JsonUtils.resultJson(-5, "帐号密码错误", null);
      }
      if (user.getStatus() != 1) return JsonUtils.resultJson(-6, "帐号已被锁定", null);
    }
    initSession(user.getType(), 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", user.getUserName());
    veResult.put("nick_name", user.getNickName());
    veResult.put("phone", user.getPhone());
    veResult.put("user_type", "1");
    if (type == 1) veResult.put("merc_num", user.getMercNum() + "");
    veResult.put("addrs", resAddr);
    return JsonUtils.resultJson(1, "", veResult);
  }
  /**
   * 查询用户信息
   *
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping(value = "/userinfo", produces = "text/plain;charset=UTF-8")
  @Authorization(type = Constant.SESSION_USER)
  public String userInfo() throws Exception {
    Object obj = session.getAttribute(Constant.SESSION_USER);
    User user = userService.findOne(((User) obj).getId());

    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);
      }
    }

    // 通过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());
        }
      }
    }

    Map<String, Object> veResult = new HashMap<String, Object>();
    veResult.put("user_id", user.getId() + "");
    veResult.put("user_name", user.getUserName());
    veResult.put("nick_name", user.getNickName());
    veResult.put("phone", user.getPhone());
    if (user.getType().equals(UserType.visitor)) veResult.put("user_type", "0");
    else veResult.put("user_type", "1");
    if (user.getType().equals(UserType.merchants)) veResult.put("merc_num", user.getMercNum() + "");
    veResult.put("addrs", resAddr);
    return JsonUtils.resultJson(1, "", veResult);
  }