//#import Util.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_user_update_log:services/modelService.jsx
//#import $owlService:services/user/UserUtil.jsx
//#import $owlService:services/user/UserLogUtil.jsx
//#import @owl:utils/PermissionUtil.jsx
//#import DigestUtil.js

;(function () {
    var params = JSON.parse($body);
    var targetUId = params['tuid'];
    var password = params['password'];
    var passwordAgain = params['passwordAgain']
    if (password !== passwordAgain) {
        out.print(JSON.stringify({state: "err", msg: "两次密码不一致"}));
        return;
    }
    var user = owl_mall_uService.get(targetUId);
    if (!user) {
        out.print(JSON.stringify({state: "err", msg: "用户不存在"}));
        return;
    }

    var ip = $.getClientIp() + "";
    var ua = request.getHeader("user-agent") + "";

    var env = checklogin();
    var loginUserId = env.loginUserId;
    var loginUserName = UserUtil.getUserName(loginUserId);

    try {
        if (!(loginUserId == 0 || loginUserId == targetUId)) {
            $.log("changePassword.jsx...noprivilege...targetUId=" + targetUId + "...loginUserId=" + loginUserId);
            out.print(JSON.stringify({state: "err", msg: "抱歉，您没有修改密码的权限"}));
            return;
        }

        var random = Math.random();
        var passwordHash = DigestUtil.sha1(password + random);
        user.passwordhash = passwordHash;
        user.random = random;
        delete user.pwdV;

        owl_mall_uService.update(user);

        UserLogUtil.addUserLog("1", targetUId, "", "", loginUserId, loginUserName, "后台修改密码", ip, ua);

        out.print(JSON.stringify({state: "ok"}));
    } catch (e) {
        $.log("changePassword.jsx...error=" + e);
        out.print(JSON.stringify({state: "err", msg: "用户已经被修改了", error: e}));
    }

})();