zhs 2 tahun lalu
induk
melakukan
f4dab185cb

+ 2 - 1
build.gradle

@@ -34,7 +34,8 @@ dependencies {
     implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.5.4'
     //excel导入导出
     implementation 'org.apache.poi:poi-ooxml:3.16'
-
+    //接口调用
+    implementation 'org.apache.httpcomponents:httpclient:4.5.2'
     implementation fileTree(dir:'lib',includes:['*jar'])
     annotationProcessor 'org.projectlombok:lombok'
     developmentOnly 'org.springframework.boot:spring-boot-devtools'

+ 16 - 0
src/main/java/com/tld/controller/UserController.java

@@ -2,11 +2,15 @@ package com.tld.controller;
 
 import com.tld.model.User;
 import com.tld.service.UserService;
+import com.tld.util.HttpClientUtil;
 import com.tld.util.PassToken;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -79,4 +83,16 @@ public class UserController {
     public void userExport(User user, HttpServletResponse response){
         userService.userExport(user, response);
     }
+
+
+    public static void main(String[] args) {
+        String url = "http://localhost:9520/user/test";
+        Map<String, String> map = new HashMap<String, String>();
+        map.put("name", "xiaohuang");
+        map.put("sex", "female");
+        String str = HttpClientUtil.doGet(url, map);
+        System.out.println("------------------------------");
+        System.out.println(str);
+        System.out.println("------------------------------");
+    }
 }

+ 9 - 0
src/main/java/com/tld/model/Menu.java

@@ -41,4 +41,13 @@ public class Menu implements Serializable {
      * 条数
      */
     private int limit;
+    /**
+     * 排序
+     */
+    private String orderBy;
+    /**
+     * 菜单
+     */
+    private String menu;
+
 }

+ 4 - 0
src/main/java/com/tld/model/Role.java

@@ -36,6 +36,10 @@ public class Role implements Serializable {
      * 菜单
      */
     private String menu;
+    /**
+     * 前段用菜单名称
+     */
+    private String menuBefore;
     /**
      * 页数
      */

+ 4 - 0
src/main/java/com/tld/model/User.java

@@ -60,6 +60,10 @@ public class User implements Serializable {
      * token
      */
     private String token;
+    /**
+     * 菜单
+     */
+    private String menu;
     /**
      * 页数
      */

+ 165 - 0
src/main/java/com/tld/util/HttpClientUtil.java

@@ -0,0 +1,165 @@
+package com.tld.util;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import org.apache.http.HttpStatus;
+
+public class HttpClientUtil {
+
+    public static String doGet(String url, Map<String, String> param) {
+
+        // 创建Httpclient对象
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+        //设置请求超时时间(各项超时参数具体含义链接)
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(10000)
+                .setConnectionRequestTimeout(10000)
+
+                .setSocketTimeout(10000)
+                .build();
+
+        String resultString = "";
+        CloseableHttpResponse response = null;
+        try {
+            // 创建uri
+            URIBuilder builder = new URIBuilder(url);
+            if (param != null) {
+                for (String key : param.keySet()) {
+                    builder.addParameter(key, param.get(key));
+                }
+            }
+            URI uri = builder.build();
+            // 创建http GET请求
+            HttpGet httpGet = new HttpGet(uri);
+
+            //给这个请求设置请求配置
+            httpGet.setConfig(requestConfig);
+            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
+
+            // 执行请求
+            response = httpclient.execute(httpGet);
+            // 判断返回状态是否为200
+            if (response.getStatusLine().getStatusCode() == 200) {
+                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                httpclient.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return resultString;
+    }
+
+
+    public static String doPost(String url, Map<String, String> param) {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+
+        //设置请求超时时间
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(10000)
+                .setConnectionRequestTimeout(10000)
+
+                .setSocketTimeout(10000)
+                .build();
+
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpPost httpPost = new HttpPost(url);
+            httpPost.setConfig(requestConfig);
+            // 创建参数列表
+            if (param != null) {
+                List<NameValuePair> paramList = new ArrayList<>();
+                for (String key : param.keySet()) {
+                    paramList.add(new BasicNameValuePair(key, param.get(key)));
+                }
+                // 模拟表单
+                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
+                httpPost.setEntity(entity);
+            }
+            // 执行http请求
+            response = httpClient.execute(httpPost);
+
+            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
+                resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+
+        return resultString;
+    }
+
+    public static String doPostJson(String url, String json) {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+
+        //设置请求超时时间
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(10000)
+                .setConnectionRequestTimeout(10000)
+                .setSocketTimeout(10000)
+                .build();
+
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpPost httpPost = new HttpPost(url);
+
+            httpPost.setConfig(requestConfig);
+            // 创建请求内容 ,发送json数据需要设置contentType
+            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
+            httpPost.setEntity(entity);
+            // 执行http请求
+            response = httpClient.execute(httpPost);
+            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
+                resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+
+        return resultString;
+    }
+}
+

+ 14 - 11
src/main/resources/mapper/MenuMapper.xml

@@ -4,7 +4,7 @@
 <mapper namespace="com.tld.mapper.MenuMapper">
 
     <sql id="field">
-        id,name,url,p_id
+        id,name,url,p_id,order_by
     </sql>
     <!-- 查询菜单 -->
     <select id="getMenu" resultType="com.tld.model.Menu">
@@ -16,6 +16,7 @@
                 and p_id = #{pId}
             </if>
         </trim>
+        order by order_by
     </select>
     <!-- 查询列表 -->
     <select id="getPage" resultType="com.tld.model.Menu">
@@ -30,10 +31,11 @@
                 and name = #{name}
             </if>
         </trim>
+        order by order_by
     </select>
     <!-- 新增菜单 -->
     <insert id="addMenu">
-        insert into tld_menu(name,url,p_id) values(#{name},#{url},#{pId})
+        insert into tld_menu(name,url,p_id,order_by) values(#{name},#{url},#{pId},#{orderBy})
     </insert>
     <!-- 修改菜单 -->
     <update id="updateMenu">
@@ -49,6 +51,9 @@
                 <if test="pId != null and pId != ''">
                     p_id = #{pId},
                 </if>
+                <if test="orderBy != null and orderBy != ''">
+                    order_by = #{orderBy},
+                </if>
             </trim>
         </set>
             where id = #{id}
@@ -59,17 +64,15 @@
     </delete>
     <!-- 根据角色查询菜单内容 -->
     <select id="getUserMenu" resultType="com.tld.model.Menu">
-        SELECT
-               a.id,
+        SELECT a.id,
                a.name,
                a.url,
                a.p_id,
-               a.id_code
-        FROM
-             tld_menu a
-             join tld_user b on b.id = #{userId}
-             join tld_role c on b.role = c.id
-        WHERE
-              c.menu LIKE CONCAT('%',CONCAT(a.id, ','),'%')
+               c.menu
+        FROM tld_menu a
+                 JOIN tld_user b ON b.id = #{userId}
+                 JOIN tld_role c ON b.role = c.id
+        WHERE c.menu LIKE CONCAT('%', CONCAT(a.id, ','), '%')
+        ORDER BY a.order_by
     </select>
 </mapper>

+ 4 - 1
src/main/resources/mapper/RoleMapper.xml

@@ -4,7 +4,7 @@
 <mapper namespace="com.tld.mapper.RoleMapper">
 
     <sql id="roleField">
-        id,role_code,role_name,create_time,remarks,menu
+        id,role_code,role_name,create_time,remarks,menu,menu_before
     </sql>
     <!-- 新增角色 -->
     <insert id="addRole">
@@ -30,6 +30,9 @@
                 <if test="menu != null and menu != ''">
                     menu = #{menu},
                 </if>
+                <if test="menuBefore != null and menuBefore != ''">
+                    menu_before = #{menuBefore},
+                </if>
             </trim>
         </set>
             where id = #{id}

+ 14 - 3
src/main/resources/mapper/UserMapper.xml

@@ -9,9 +9,20 @@
     <!-- 登录 -->
     <select id="login" resultType="com.tld.model.User">
         select
-        <include refid="userField"/>
-        from tld_user
-        where user_name = #{userName} and password = #{password}
+            a.id,
+            a.code,
+            a.user_name,
+            a.password,
+            a.real_name,
+            a.email,
+            a.landline,
+            a.phone,
+            a.department,
+            a.role,
+            b.menu
+        from tld_user a
+        left join tld_role b on a.role = b.id
+        where a.user_name = #{userName} and a.password = #{password}
     </select>
     <!-- 查询所有用户 -->
     <select id="getAllUser" resultType="com.tld.model.User">