Administrator
2023-04-19 40ec16bbb7c9d23df625aa31ae42ac36e901749d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.mandi.common;
 
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.Date;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.apache.commons.lang3.StringUtils;
 
import com.mandi.system.persist.Login;
import com.mandi.system.persist.LoginSide;
 
/**
 * 封装了servlet相关接口
 * @author Administrator
 *
 */
public class SessionMethod {
    public static String getloginid(HttpSession session)
    {
        Login login=(Login)session.getAttribute("login");
        if(login!=null)
            return login.getUserid();
        return "";
    }
    public static Login getlogin(HttpSession session)
    {
        Login login=(Login)session.getAttribute("login");
        return login;
    }
    public static boolean setlogin(HttpSession session,Login login)
    {
        session.setAttribute("login", login);
        session.setAttribute("login_sess_id", login.getId());//login id
        session.setAttribute("login_sess_userid", login.getUserid());//user表 userid
        session.setAttribute("login_sess_companyno", login.getDepartno());//user 表 companyNo
        session.setAttribute("login_sess_username", login.getUsername());//user 表username
        session.setAttribute("login_sess_loginside", login.getLoginside());//暂时没用
        session.setAttribute("login_sess_companyname", login.getDepartname());//company表 name
        session.setAttribute("login_sess_workername", login.getWorkername());//user表name
        session.setAttribute("login_sess_utype", login.getWorkerno());//admin/worker 判断是否是管理员
        session.setAttribute("login_sess_fd_utype", login.isFdAdmin());//admin/worker 判断是否是管理员
        String sc=(new Date().getTime())+"";
        session.setAttribute("login_page_vesion", sc);
        return true;
    }
    public static boolean setGateslogin(HttpSession session,String gatesNo)
    {
        session.setAttribute("login_sess_gatesNo", gatesNo);
        return true;
    }
    public static String getGateslogin(HttpSession session,String gatesNo)
    {
        String sessiongatesNo= (String) session.getAttribute("login_sess_gatesNo");
        return sessiongatesNo;
    }
    public static void writeresp(HttpServletResponse resp,String content)
    {
        if(resp==null||content==null)
            return;
        PrintWriter w=null;
        try {
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json; charset=utf-8");
            w=resp.getWriter();
            w.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writerespstr(HttpServletResponse resp,String content)
    {
        if(resp==null||content==null)
            return;
        PrintWriter w=null;
        try {
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html; charset=utf-8");
            w=resp.getWriter();
            w.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writeImg(HttpServletResponse resp,String filepath)
    {
        if(resp==null||filepath==null)
            return;
        File f=new File(filepath);
        if(!f.exists())
            return;
        try {
            BufferedImage img=null;
            img=ImageIO.read(new FileInputStream(new File(filepath)));
            ImageIO.write(img, "jpeg", resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writerespstream(HttpServletResponse resp,String content)
    {
        if(resp==null||content==null)
            return;
        OutputStream os=null;
        try {
            resp.setCharacterEncoding("utf-8");
            os=resp.getOutputStream();
            os.write(content.getBytes("utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void setlogin(HttpSession session,String id,String username,long depart,LoginSide ls)
    {
        Login l=new Login();
        l.setUserid(id);
        l.setDepart(depart);
        l.setUsername(username);
        l.setLoginside(ls);
        session.setAttribute("login", l);
        session.setAttribute("login_sess_id", id);
        session.setAttribute("login_sess_depart", depart);
        session.setAttribute("login_sess_username", username);
        session.setAttribute("login_sess_loginside", ls);
        String sc=(new Date().getTime())+"";
        session.setAttribute("login_page_vesion", sc);
 
    }
 
    public static void forward(ServletResponse resp,HttpServletRequest r,String url)
    {
        if(resp==null||r==null||url==null)
            return;
        System.out.println(url);
        try {
            HttpServletRequest hr=(HttpServletRequest)r;
            hr.getRequestDispatcher(url).forward(r, resp);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    public static void sendRedirect(HttpServletResponse resp,HttpServletRequest r,String url)
    {
        if(resp==null||r==null||url==null)
            return;
        System.out.println(url);
        url=r.getServletContext().getContextPath()+url;
        try {
            resp.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static String postBody(HttpServletRequest r)
    {
        if(r==null)
            return null;
        StringBuffer sb=new StringBuffer();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(r.getInputStream(),Charset.forName("utf-8")));
            String line=null;
            while((line=reader.readLine())!=null)
            {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
 
    public static boolean checkvalicode(String code,HttpSession session){
        if(StringUtils.isBlank(code))
            return false;
        String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        if(StringUtils.isNotBlank(scode)){
            if(code.trim().equals(scode.trim())){
                return true;
            }
        }
//        int a=KaptchaTextutil.calculateNum(scode);
//        if(code.trim().equals(a+""))
//            return true;
        return false;
    }
    public static boolean checkvalicodetogetphonecode(String code,HttpSession session){
        if(StringUtils.isEmpty(code))
            return false;
        String scode=(String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        if(code.trim().equals(scode.trim())){
            return true;
        }
//        int a=KaptchaTextutil.calculateNum(scode);
//        if(code.trim().equals(a+""))
//        {
//            session.removeAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
//            return true;
//        }
        return false;
    }
}