NutzCN Logo
问答 用户名登陆 手机号登陆 这个怎么做呢
发布于 3003天前 作者 Hamming 2254 次浏览 复制 上一个帖子 下一个帖子
标签:

用户名和手机登了 这个地方 应该怎么做呢 如果用query 查出来的是多条了

package nuoshang.bluejay.modules;
import nuoshang.bluejay.bean.Address;
import nuoshang.bluejay.services.UserAddService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 用户收货地址
 * Created by Administrator on 2016/8/22.
 */
@IocBean
@At("/address")
@Fail("http:500")
public class UserAddModule {
    private static final Log log = Logs.get();

    @Inject
    private UserAddService userAddService;

    @At("/add")
    @Ok("json")
    public Object add(@Param("..") Address address){
        Address addr = userAddService.save(address);
        Integer id = addr.getUser_address_id();
        NutMap re = new NutMap();
        if( id != null || id != 0){
            return re.setv("result", "01").addv("msg", "保存成功!");
        }else {
            return re.setv("result", "00").addv("msg", "保存失败!");
        }
    }

    @At("/find")
    @Ok("json")
    public Object find(@Param("id") Integer id){
        NutMap re = new NutMap();
        if( id == null || id == 0){
            return re.setv("result", "03").addv("msg", "Id不能为空!");
        }

        Address address = userAddService.find(id);
        if( address != null){
            return re.setv("result", "01").addv("data",address);
        }else {
            return re.setv("result", "02").addv("msg", "未查到数据!");
        }
    }

    @At("/rm")
    @Ok("json")
    public Object rm(@Param("id") Integer id){
        NutMap re = new NutMap();
        if(id <= 0){
            return re.setv("result", "03").addv("msg", "Id不能为空!");
        }
        userAddService.delete(id);
        return re.setv("result", "01").addv("msg", "删除成功!");
    }

    @At("/update")
    @Ok("json")
    public Object updateAdd(@Param("..") Address address){
        NutMap re = new NutMap();
        if(address.getUser_address_id() <= 0 ||address.getAdd_user_id() <= 0){
            return re.setv("result", "03").addv("msg", "Id不能为空!");
        }
        userAddService.update(address);
        return re.setv("result", "01").addv("msg", "保存成功!");
    }

    @At("/list")
    @Ok("json")
    public Object list(@Param("uid") Integer uid){
        NutMap re = new NutMap();
        if(uid ==null || uid ==0){
            return re.setv("result", "03").addv("msg", "用户Id不能为空!");
        }
        return userAddService.list(uid);
    }
}

3 回复

没看到login

来自炫酷的 NutzCN

@wendal 不好意思 拷贝错了

package nuoshang.bluejay.services;

import nuoshang.bluejay.base.AppConst;
import nuoshang.bluejay.bean.User;
import nuoshang.bluejay.base.exception.UnAuthorizedException;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.service.IdNameEntityService;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;



/**
 * Created by Administrator on 2016/8/11.
 */
@IocBean(args = {"refer:dao"})
public class UserService extends IdNameEntityService<User>  {

    public UserService(Dao dao) {
        super(dao);
    }
    /**
     * login
     *
     * @param name name
     * @param password password
     * @return id of user. return -1 if invalid.
     */
    public User login(String name, String password) throws UnAuthorizedException {
        User user = fetch(name);
        if (user == null) {
            throw new UnAuthorizedException("用户名或密码不正确");
        }

        String _pass = new Sha256Hash(password, user.getSalt()).toHex();
        if(!_pass.equalsIgnoreCase(user.getPassword())) {
            throw new UnAuthorizedException("用户名或密码不正确");
        }
        return user;
    }

    public void updatePassword(long userId, String password) {
        User user = fetch(userId);
        if (user == null) {
            return;
        }
        user.setSalt(R.UU16());
        user.setPassword(new Sha256Hash(password, user.getSalt()).toHex());
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        dao().update(user, "^(password|salt|updateTime)$");
//        dao.updateIgnoreNull(user);
    }

    public User save(User u) {
        User user = new User();
        user.setUser_name(u.getUser_name().trim());
        user.setPhone(u.getPhone().trim());
        user.setSalt(R.UU16());
        user.setPassword(new Sha256Hash(u.getPassword(), user.getSalt()).toHex());
        user.setPay_code(new Sha256Hash(u.getPay_code(), user.getSalt()).toHex());
        user.setCreate_time(new Date());
        user.setUpdate_time(new Date());
        user.setState(User.STATE_OK);
        return dao().insert(user);
    }

    public void rm(long userId) {
        User user = fetch(userId);
        if (user == null) {
            return;
        }
        user.setState(User.STATE_RM);
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        dao().update(user);
    }

    public QueryResult list(String name, Pager pager) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("name", "like", "%"+name+"%");
        List<User> list = dao().query(User.class, cnd, pager);
        int count = dao().count(User.class, cnd);

        QueryResult qr = new QueryResult();
        qr.setList(list);
        pager.setRecordCount(count);
        pager.setPageSize(AppConst.PAGE_SIZE);
        qr.setPager(pager);

        return qr;
    }


}

@tonyyule fetch(name)改一下不就好了

dao().fetch(User.class,Cnd.where(...).or(.....).or(.....));

来自炫酷的 NutzCN

添加回复
请先登陆
回到顶部