|
@@ -0,0 +1,155 @@
|
|
|
|
+package com.lsw.controller.front.user;
|
|
|
|
+
|
|
|
|
+import com.jfinal.kit.HashKit;
|
|
|
|
+import com.jfinal.kit.Ret;
|
|
|
|
+import com.jfinal.kit.StrKit;
|
|
|
|
+import com.jfinal.plugin.activerecord.Db;
|
|
|
|
+import com.jfinal.plugin.ehcache.CacheKit;
|
|
|
|
+import com.lsw.commons.utils.Constant;
|
|
|
|
+import com.lsw.model.work.User;
|
|
|
|
+import com.lsw.model.work.Token;
|
|
|
|
+import org.apache.shiro.crypto.hash.SimpleHash;
|
|
|
|
+import org.apache.shiro.util.SimpleByteSource;
|
|
|
|
+
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by Administrator on 2017/5/23.
|
|
|
|
+ */
|
|
|
|
+public class AppUserService {
|
|
|
|
+ private final User dao = new User().dao();
|
|
|
|
+
|
|
|
|
+ public Ret login(String account, String pass) {
|
|
|
|
+ Ret ret = Ret.create();
|
|
|
|
+ //第一次登录先保存用户信息
|
|
|
|
+ User user = dao.findFirst("select * from tb_user where account=?", account);
|
|
|
|
+ if(user==null){
|
|
|
|
+ ret.set("result", false).set("msg", "用户不存在");
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ String v_pass=new SimpleHash("MD5", new SimpleByteSource(pass), new SimpleByteSource(user.getSalt()), 2).toHex();
|
|
|
|
+ if(!v_pass.equals(user.getPassword())){
|
|
|
|
+ ret.set("result", false).set("msg", "密码错误!");
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ if(user.getMSate()==1){
|
|
|
|
+ ret.set("result", false).set("msg", "该账户被限制登录");
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ //保存token信息
|
|
|
|
+ Token token = new Token();
|
|
|
|
+ String secret = HashKit.generateSaltForSha256();
|
|
|
|
+ token.setSecret(secret);
|
|
|
|
+ //token.setPhone(account);
|
|
|
|
+ token.put("mSate",0);
|
|
|
|
+ token.save();
|
|
|
|
+ user.put("token", secret);
|
|
|
|
+ CacheKit.put(Constant.token, account, token);
|
|
|
|
+ ret.set("result", true);
|
|
|
|
+ ret.set("user", user);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 用户验证token知否有效
|
|
|
|
+ *
|
|
|
|
+ * @param secret
|
|
|
|
+ * @param phone
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Token loginBySecret(String secret, String phone) {
|
|
|
|
+ Token token = CacheKit.get(Constant.token, phone);
|
|
|
|
+ if (token == null) {
|
|
|
|
+ token = Token.dao.findFirst("SELECT u.mSate FROM tb_token t LEFT JOIN tb_app_user u ON u.account = t.phone WHERE t.secret=? AND t.phone=? ORDER BY t.id DESC", secret, phone);
|
|
|
|
+ CacheKit.put(Constant.token, phone, token);
|
|
|
|
+ }
|
|
|
|
+ return token;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 检查是否存在该用户
|
|
|
|
+ *
|
|
|
|
+ * @param account
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public boolean check(String account) {
|
|
|
|
+ User user = dao.findFirst("select * from tb_app_user where account=?", account);
|
|
|
|
+ return user == null ? true : false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 用户注册
|
|
|
|
+ *
|
|
|
|
+ * @param account 手机号
|
|
|
|
+ * @param password 密码
|
|
|
|
+ * @param newsCode 验证码
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Ret register(String account, String password, String newsCode) {
|
|
|
|
+ Ret ret = Ret.create();
|
|
|
|
+ boolean check = check(account);
|
|
|
|
+ if (check) {
|
|
|
|
+ String code = CacheKit.get(Constant.code, account);
|
|
|
|
+ if (code == null) {
|
|
|
|
+ ret.set("result", false).set("msg", "验证码超过有效期");
|
|
|
|
+ CacheKit.remove(Constant.code, account);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ if (code.equals(newsCode)) {
|
|
|
|
+ String salt = HashKit.generateSaltForSha256();
|
|
|
|
+ User user = new User();
|
|
|
|
+ user.setAccount(account);
|
|
|
|
+ user.setHeader("/upload/images/header/ls.png");
|
|
|
|
+ user.setSalt(salt);
|
|
|
|
+ //密码加密
|
|
|
|
+ user.setPassword(HashKit.sha256(password + salt));
|
|
|
|
+ user.setMTime(new Date());
|
|
|
|
+ user.save();
|
|
|
|
+ ret.set("result", true);
|
|
|
|
+ } else {
|
|
|
|
+ ret.set("result", false).set("msg", "验证码不正确");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ ret.set("result", false).set("msg", "手机号已被注册");
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 找回密码
|
|
|
|
+ *
|
|
|
|
+ * @param account 账号
|
|
|
|
+ * @param password 密码
|
|
|
|
+ * @param newsCode 验证码
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Ret findPass(String account, String password, String newsCode) {
|
|
|
|
+ Ret ret = Ret.create();
|
|
|
|
+ boolean check = check(account);
|
|
|
|
+ if (check) {
|
|
|
|
+ ret.set("result", false).set("msg", "该号码未注册");
|
|
|
|
+ } else {
|
|
|
|
+ String code = CacheKit.get(Constant.code, account);
|
|
|
|
+ if (code == null) {
|
|
|
|
+ ret.set("result", false).set("msg", "验证码超过有效期");
|
|
|
|
+ CacheKit.remove(Constant.code, account);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ if (code.equals(newsCode)) {
|
|
|
|
+ String salt = HashKit.generateSaltForSha256();
|
|
|
|
+ //密码加密
|
|
|
|
+ password = HashKit.sha256(password + salt);
|
|
|
|
+ int row = Db.update("update tb_app_user set password =?,salt=? where account=?", password, salt, account);
|
|
|
|
+ if (row > 0) {
|
|
|
|
+ ret.set("result", true);
|
|
|
|
+ } else {
|
|
|
|
+ ret.set("result", false).set("msg", "找回密码失败");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ ret.set("result", false).set("msg", "验证码不正确");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+}
|