Zhiqim Account(前端账户管理台)是在ZhiqimManager基础上改造成用于前端注册登录的管理系统,增加组织结构和余额,开放注册组织和组织管理员,可以添加组织内的部门和角色和操作员。并增加该管理台上的一些组件,如充值支付等组件。适用于二级代理管理或该大型组织机构

森中灵 最后提交于8月前 整理为组织方式和替换新的jar
ZmrProfilePresenter.java16KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_account.htm
 *
 * Zhiqim Account is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.presenter;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.httpd.context.ZmlContexts;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.validate.ones.IsByteLen;
import org.zhiqim.httpd.validate.ones.IsLen;
import org.zhiqim.httpd.validate.ones.IsNumericLen;
import org.zhiqim.httpd.validate.onex.IsAccountPass;
import org.zhiqim.httpd.validate.onex.IsMobile11;
import org.zhiqim.httpd.validate.two.IsEqual;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.model.codes.Base64;
import org.zhiqim.kernel.model.results.RS;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.Bytes;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.kernel.util.Randoms;
import org.zhiqim.kernel.util.Sqls;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.ZmrPassworder;
import org.zhiqim.manager.ZmrSessionUser;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dao.ZmrPassportDao;
import org.zhiqim.manager.dao.ZmrSmsDao;
import org.zhiqim.manager.dbo.ZmrAvatar;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 个人中心控制器
 *
 * @version v1.0.0 @author zouzhigang 2017-6-5 新建与整理
 */
@AnAlias("ZmrProfilePresenter")
@AnIntercept("chkZmrLogin")
public class ZmrProfilePresenter implements ZmlContextConstants, ZmrConstants
{
    /** 关闭超级管理员权限 */
    public static void doCloseSuperAdmin(HttpRequest request) throws Exception
    {
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        if (!sessionUser.isSuperAdmin())
        {
            request.setResponseError("你没有关闭超级管理员权限");
            return;
        }
        
        if (ORM.table().count(ZmrOperator.class, new Selector("operatorType", 1)) == 0)
        {
            request.setResponseError("还没有设置管理员,不能关闭超级管理员");
            return;
        }
        
        Updater updater = new Updater();
        updater.addField("operatorValid", false);
        updater.addMust("operatorType", 0);
        
        ORM.table().update(ZmrOperator.class, updater);
    }
    
    /** 查询系统头像 */
    public static String doQuerySysAvatar(HttpRequest request) throws Exception
    {
        int page = request.getParameterInt(_PAGE_, 1);
        int pageSize = 18;
        
        PageResult<ZmrAvatar> result = ORM.table().page(ZmrAvatar.class, page, pageSize, new Selector("avatarType", 0).addOrderbyAsc("avatarId"));
        result.addConditionMap(request.getParameterMap());
        
        return ZmlContexts.parseZmlPath(request, "/zview/zhiqim_manager/presenter/selSysAvatarInfo.zml", "result", result);
    }
    
    /** 修改为系统头像 */
    public static void doUpdateSysAvatar(HttpRequest request, long avatarId) throws Exception
    {
        if(ORM.table().count(ZmrAvatar.class, avatarId) == 0)
        {
            request.setResponseError("选择的头像不存在,请重新选择");
            return;
        }
        
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, sessionUser.getOperatorCode());
        if (operator.getOperatorAvatar() != 0)
        {//不为0,检查是否用户自定义的
            ZmrAvatar avatar = ORM.table().item(ZmrAvatar.class, operator.getOperatorAvatar());
            if (avatar.getAvatarType() == 1)
            {//如果是,要删除自定义的数据
                ORM.table().delete(ZmrAvatar.class, operator.getOperatorAvatar());
            }
        }
        
        Updater updater = new Updater();
        updater.addMust("operatorCode", sessionUser.getOperatorCode());
        updater.addField("operatorAvatar", avatarId);
        ORM.table().update(ZmrOperator.class, updater);
        
        sessionUser.setOperator(ORM.table().item(ZmrOperator.class, sessionUser.getOperatorCode()));
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "操作员修改头像", Jsons.toString("operatorCode", sessionUser.getOperatorCode(), "avatarId", avatarId));
    }
    

    /** 修改为自定义头像*/
    public static void doUpdateSelfAvatar(HttpRequest request, String data) throws Exception
    {
        //1.读取头像图片
        int idx = data.indexOf(",");
        byte[] imageData = Base64.decode(data.substring(idx+1));
        
        byte[]  bytes50 = Bytes.resizePNG(imageData, 50, 50);
        if(bytes50 == null)
        {
            request.setResponseError("图片错误");
            return;
        }
        
        byte[]  bytes100 = Bytes.resizePNG(imageData, 100, 100);
        if(bytes100 == null)
        {
            request.setResponseError("图片错误");
            return;
        }
        
        byte[]  bytes150 = Bytes.resizePNG(imageData, 150, 150);
        if(bytes150 == null)
        {
            request.setResponseError("图片错误");
            return;
        }
        
        //2.以数据库数据为准,查操作员信息,防止账号多浏览器登录
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, sessionUser.getOperatorCode());
        
        if (operator.getOperatorAvatar() != 0)
        {
            ZmrAvatar avatar = ORM.table().item(ZmrAvatar.class, operator.getOperatorAvatar());
            if (avatar.getAvatarType() == 1)
            {//操作员自定义的,要删除头像图片
                ORM.table().delete(ZmrAvatar.class, operator.getOperatorAvatar());
            }
        }
        
        //3.新的头像插入到头像表
        long avatarId = Ids.longId13();
        ZmrAvatar avatar = new ZmrAvatar();
        avatar.setAvatarId(avatarId);
        avatar.setAvatarType(1);//自定义头像
        avatar.setAvatarTime(System.currentTimeMillis());
        avatar.setAvatar50(bytes50);
        avatar.setAvatar100(bytes100);
        avatar.setAvatar150(bytes150);
        ORM.table().insert(avatar);

        //4.更新操作员头像Id号
        Updater updater = new Updater();
        updater.addMust("operatorCode", sessionUser.getOperatorCode());
        updater.addField("operatorAvatar", avatarId);
        ORM.table().update(ZmrOperator.class, updater);
        
        //5.更新会话为新的头像Id号
        operator.setOperatorAvatar(avatarId);
        sessionUser.setOperator(operator);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "操作员上传头像", Jsons.toString("operatorCode", sessionUser.getOperatorCode(), "avatarId", avatarId));
    }
    

    /**修改密码*/
    public static void doUpdatePassword(HttpRequest request) throws Exception
    {
        request.addValidate(new IsByteLen("oldPassword", "密码为6-16位,请输入正确的旧密码", 6, 16));
        request.addValidate(new IsAccountPass("newPassword", "新密码不合法,要求6-16位(大小写字母数字和特殊字符必须四选三)"));
        request.addValidate(new IsAccountPass("newPassword2", "新密码确认不合法,要求6-16位(大小写字母数字和特殊字符必须四选三)"));
        request.addValidate(new IsEqual("newPassword", "newPassword2", "新密码和新密码确认不一致"));
        
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        String operatorCode = sessionUser.getOperatorCode();
        
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if(operator == null || !ZmrOperatorDao.validatePassword(operator, oldPassword))
        {
            request.setResponseError("旧密码不正确");
            return;
        }
        
        String newPassSalt = Randoms.lettersDigitsSecure(64);
        newPassword = ZmrPassworder.encode(operatorCode, newPassword, newPassSalt);
        
        Updater updater = new Updater();
        updater.addMaybe("operatorCode", operatorCode);
        updater.addField("operatorPass", newPassword);
        updater.addField("operatorPassSalt", newPassSalt);
        updater.addField("operatorModified", Sqls.nowTimestamp());
        ORM.table().update(ZmrOperator.class, updater);
        
        sessionUser.setOperator(ZmrOperatorDao.item(sessionUser.getOperatorCode()));
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "操作员修改密码", Jsons.toString("operatorCode", operatorCode));
    }
    

    /**修改手机号*/
    public static void doUpdateMobile(HttpRequest request) throws Exception
    {
        request.addValidate(new IsMobile11("operatorMobile", "请输入正确格式的手机号"));
        request.addValidate(new IsNumericLen("vcode", "请输入正确的验证码", 6, 6));
        if (!request.chkValidate())
        {//验证参数失败
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        String operatorMobile = request.getParameter("operatorMobile");
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        if(operatorMobile.equals(sessionUser.getOperatorMobile()))
        {//无需修改
            request.setResponseError("手机号未改变,不需要修改");
            return;
        }
        
        String vcode = request.getParameter("vcode");
        if (!ZmrPassportDao.chkVcode(request, operatorMobile, ZMR_VCODE_TYPE_MODIFY.code(), vcode))
        {//验证验证码失败
            request.setResponseError("验证码不正确或已失效");
            return;
        }

        if (sessionUser.getOperator().isOperatorMobileLogin())
        {//新手机号没有被作为登录名的
            if (ZmrPassportDao.chkMobileRegister(operatorMobile))
            {//验证手机号作为登录的唯一性
                request.setResponseError("该手机号已是其他账号的登录名,请先解除,如以前注册的账号不是你的账号,请与客服联系");
                return;
            }
        }
        else
        {//新手机号不是登录名的,最多支持绑定5个
            if (ZmrPassportDao.getMobileBind(operatorMobile) >= 5)
            {//手机号最多绑定5个账号
                request.setResponseError("一个手机号最多只能绑定5个账号");
                return;
            }
        }
        
        ORM.table().update(ZmrOperator.class, new Updater()
            .addMust("operatorCode", sessionUser.getOperatorCode())
            .addField("operatorMobile", operatorMobile)
            .addField("operatorModified", Sqls.nowTimestamp()));
        
        //更新最新会话
        sessionUser.setOperator(ZmrOperatorDao.item(sessionUser.getOperatorCode()));
    }
    
     /** 修改用户名称 */
    public static void doRenameName(HttpRequest request) throws Exception
    {
        request.addValidate(new IsLen("operatorName", "请输入2-20个字符的用户名", 1, 32));
        if (!request.chkValidate())
        {
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        
        Updater updater = new Updater();
        updater.addMust("operatorCode", request.getSessionName());
        updater.addField("operatorName", request.getParameter("operatorName"));
        updater.addField("operatorModified", Sqls.nowTimestamp());
        ORM.table().update(ZmrOperator.class, updater);
        
        //刷新会话
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        sessionUser.setOperator(ZmrOperatorDao.item(sessionUser.getOperatorCode()));
    }
    
    /** 修改手机号作为登录名 */
   public static void doSetMobileLogin(HttpRequest request) throws Exception
   {
       ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
       String operatorCode = sessionUser.getOperatorCode();

       ZmrOperator item = ORM.table().item(ZmrOperator.class, operatorCode);
       if (item.isOperatorMobileLogin())
       {//当前为手机号登录名的,修改为不是登录名
           Updater updater = new Updater();
           updater.addMust("operatorCode", operatorCode);
           updater.addField("operatorMobileLogin", false);
           updater.addField("operatorModified", Sqls.nowTimestamp());
           ORM.table().update(ZmrOperator.class, updater);
       }
       else
       {//不是登录名的
           if (Validates.isEmpty(item.getOperatorMobile()))
           {
               request.setResponseError("您未绑定手机号,不支持设置手机号为登录名");
               return;
           }
           
           if (ZmrPassportDao.chkMobileRegister(item.getOperatorMobile()))
           {
               request.setResponseError("您绑定的手机号已是其他账号的登录名,请先解除");
               return;
           }
           
           Updater updater = new Updater();
           updater.addMust("operatorCode", operatorCode);
           updater.addField("operatorMobileLogin", true);
           updater.addField("operatorModified", Sqls.nowTimestamp());
           ORM.table().update(ZmrOperator.class, updater);
       }

       //刷新会话
       sessionUser.setOperator(ZmrOperatorDao.item(operatorCode));
   }

    /** 发送修改手机号验证码 */
    public static void doSendVcodeByModifyMobile(HttpRequest request) throws Exception
    {
        request.addValidate(new IsMobile11("operatorMobile", "请输入正确的手机号"));
        if (!request.chkValidate())
        {//验证参数失败
            request.setResponseError(request.getAlertMsg());
            return;
        }
        
        String operatorMobile = request.getParameter("operatorMobile");
        ZmrSessionUser sessionUser = request.getSessionUser(ZmrSessionUser.class);
        if(operatorMobile.equals(sessionUser.getOperatorMobile()))
        {//无需修改
            request.setResponseError("手机号未改变,不需要修改");
            return;
        }
        
        if (sessionUser.getOperator().isOperatorMobileLogin())
        {//新手机号没有被作为登录名的
            if (ZmrPassportDao.chkMobileRegister(operatorMobile))
            {//验证手机号作为登录的唯一性
                request.setResponseError("该手机号已是其他账号的登录名,请先解除,如以前注册的账号不是你的账号,请与客服联系");
                return;
            }
        }
        else
        {//新手机号不是登录名的,最多支持绑定5个
            if (ZmrPassportDao.getMobileBind(operatorMobile) >= 5)
            {//手机号最多绑定5个账号
                request.setResponseError("一个手机号最多只能绑定5个账号");
                return;
            }
        }
        
        RS<String> sResult = ZmrSmsDao.sendVcode(operatorMobile, ZMR_VCODE_TYPE_MODIFY.code());
        if (sResult.failure())
        {//短信发送失败
            request.setResponseError("修改手机号验证码短信发送失败,请重新填写");
            return;
        }
        
        //保存验证码到数据库
        ZmrPassportDao.saveVcode(operatorMobile, ZMR_VCODE_TYPE_MODIFY.code(), sResult.value());
    }
}