gongdecai 2 år sedan
förälder
incheckning
2901fe5b61

+ 4 - 0
unimall-app-api/src/main/java/com/iotechn/unimall/app/api/user/UserService.java

@@ -1,5 +1,6 @@
 package com.iotechn.unimall.app.api.user;
 
+import com.alibaba.fastjson.JSONObject;
 import com.iotechn.unimall.core.Const;
 import com.iotechn.unimall.core.annotation.HttpMethod;
 import com.iotechn.unimall.core.annotation.HttpOpenApi;
@@ -8,6 +9,7 @@ import com.iotechn.unimall.core.annotation.HttpParamType;
 import com.iotechn.unimall.core.annotation.param.NotNull;
 import com.iotechn.unimall.core.annotation.param.TextFormat;
 import com.iotechn.unimall.core.exception.ServiceException;
+import com.iotechn.unimall.data.domain.AjaxResult;
 import com.iotechn.unimall.data.dto.UserDTO;
 
 /**
@@ -73,4 +75,6 @@ public interface UserService {
     public Object getH5Sign(
             @NotNull @HttpParam(name = "url", type = HttpParamType.COMMON, description = "url") String url) throws ServiceException;
 
+    public AjaxResult appletLogin(@NotNull @HttpParam(name = "code", type = HttpParamType.COMMON, description = "code")JSONObject code) throws Exception;
+
 }

+ 23 - 0
unimall-app-api/src/main/java/com/iotechn/unimall/app/api/user/UserServiceImpl.java

@@ -1,5 +1,6 @@
 package com.iotechn.unimall.app.api.user;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.iotechn.unimall.biz.service.user.UserBizService;
@@ -13,12 +14,15 @@ import com.iotechn.unimall.core.notify.SMSResult;
 import com.iotechn.unimall.core.util.GeneratorUtil;
 import com.iotechn.unimall.core.util.SHA1Util;
 import com.iotechn.unimall.data.component.CacheComponent;
+import com.iotechn.unimall.data.domain.AjaxResult;
 import com.iotechn.unimall.data.domain.UserDO;
 import com.iotechn.unimall.data.dto.UserDTO;
 import com.iotechn.unimall.data.enums.UserLevelType;
 import com.iotechn.unimall.data.enums.UserLoginType;
 import com.iotechn.unimall.data.mapper.UserMapper;
+import com.iotechn.unimall.data.util.HttpRequestUtil;
 import com.iotechn.unimall.data.util.SessionUtil;
+import com.iotechn.unimall.data.util.WeiXinQiYeUtil;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import org.apache.commons.codec.digest.Md5Crypt;
@@ -84,6 +88,25 @@ public class UserServiceImpl implements UserService {
     @Value("${com.iotechn.unimall.wx.h5.app-secret}")
     private String wxH5Secret;
 
+    /**
+     * 企业小程序授权登录
+     * @param code
+     * @return
+     */
+    @Override
+    public AjaxResult appletLogin(JSONObject code) throws Exception {
+
+        String accessToken = WeiXinQiYeUtil.accessToken();
+
+        String appletCode = code.getString("code");
+        String url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token="+accessToken+"&js_code="+appletCode+"&grant_type=authorization_code";
+        String msg = HttpRequestUtil.httpGet(url,null);
+//        log.info("msg: "+msg);
+        JSONObject jsonObject = JSON.parseObject(msg);
+        return AjaxResult.success(jsonObject);
+    }
+
+
     @Override
     public String sendVerifyCode(String phone) throws ServiceException {
         String verifyCode = GeneratorUtil.genSixVerifyCode();

+ 15 - 0
unimall-data/src/main/java/com/iotechn/unimall/data/domain/AccessToken.java

@@ -0,0 +1,15 @@
+package com.iotechn.unimall.data.domain;
+
+import lombok.Data;
+
+/**
+ * 企业微信生成Token凭证
+ */
+@Data
+public class AccessToken {
+
+    private String errCode;
+    private String errMsg;
+    private String accessToken;
+    private String expiresIn;
+}

+ 286 - 0
unimall-data/src/main/java/com/iotechn/unimall/data/util/HttpRequestUtil.java

@@ -0,0 +1,286 @@
+package com.iotechn.unimall.data.util;
+
+import java.io.IOException;
+
+import java.util.ArrayList;
+
+import java.util.HashMap;
+
+import java.util.Iterator;
+
+import java.util.List;
+
+import java.util.Map;
+
+import java.util.Map.Entry;
+
+import java.util.Set;
+
+
+
+import org.apache.http.Header;
+
+import org.apache.http.HttpEntity;
+
+import org.apache.http.HttpResponse;
+
+import org.apache.http.NameValuePair;
+
+import org.apache.http.client.HttpClient;
+
+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.impl.client.CloseableHttpClient;
+
+import org.apache.http.impl.client.HttpClients;
+
+import org.apache.http.message.BasicNameValuePair;
+
+import org.apache.http.util.EntityUtils;
+
+import org.slf4j.Logger;
+
+import org.slf4j.LoggerFactory;
+public class HttpRequestUtil {
+    private static final Logger log = LoggerFactory.getLogger(HttpRequestUtil.class);
+
+
+
+    public static String UTF8 = "UTF-8";
+
+
+
+    public static String httpPost(String url, Map<String, String> params, String encoding) throws Exception {
+
+        log.debug("收到HTTP POST请求");
+
+
+
+        String result = "";
+
+        // 创建默认的httpClient实例.    
+
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+
+        // 创建httppost    
+
+        HttpPost httppost = new HttpPost(url);
+
+
+
+        //参数
+
+        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
+
+        if (params != null) {
+
+            log.debug("发送post参数");
+
+            Set<String> keys = params.keySet();
+
+
+
+            for (String key : keys) {
+
+                log.debug("param:" + key);
+
+                formparams.add(new BasicNameValuePair(key, params.get(key)));
+
+            }
+
+
+
+        }
+
+
+
+        UrlEncodedFormEntity uefEntity;
+
+        try {
+
+            uefEntity = new UrlEncodedFormEntity(formparams, encoding);
+
+            httppost.setEntity(uefEntity);
+
+
+
+            log.debug("executing request " + httppost.getURI());
+
+
+
+            CloseableHttpResponse response = httpclient.execute(httppost);
+
+
+
+            try {
+
+                log.debug("返回HTTP状态:" + response.getStatusLine());
+
+
+
+                Header[] headers = response.getAllHeaders();
+
+
+
+                log.debug("返回HTTP头");
+
+                log.debug("--------------------------------------");
+
+                for (Header header : headers) {
+
+                    log.debug(header.getName() + "-->" + header.getValue());
+
+                }
+
+                log.debug("--------------------------------------");
+
+
+
+                HttpEntity entity = response.getEntity();
+
+                if (entity != null) {
+
+                    result = EntityUtils.toString(entity, encoding);
+
+                    log.debug("--------------------------------------");
+
+                    log.debug("Response content: " + result);
+
+                    log.debug("--------------------------------------");
+
+                }
+
+
+
+            } finally {
+
+                response.close();
+
+            }
+
+        } catch (IOException e) {
+
+            throw e;
+
+        } finally {
+
+            // 关闭连接,释放资源    
+
+            try {
+
+                httpclient.close();
+
+            } catch (IOException e) {
+
+            }
+
+        }
+
+
+
+        return result;
+
+    }
+
+
+
+    public static String httpGet(String url, String encoding) throws Exception {
+
+
+
+        log.debug("收到HTTP GET请求");
+
+
+
+        String result = "";
+
+
+
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+
+        try {
+
+            // 创建httpget.    
+
+            HttpGet httpget = new HttpGet(url);
+
+            log.debug("executing request " + httpget.getURI());
+
+            // 执行get请求.    
+
+            CloseableHttpResponse response = httpclient.execute(httpget);
+
+            try {
+
+
+
+                log.debug("返回HTTP状态:" + response.getStatusLine());
+
+
+
+                Header[] headers = response.getAllHeaders();
+
+
+
+                log.debug("返回HTTP头");
+
+                log.debug("--------------------------------------");
+
+                for (Header header : headers) {
+
+                    log.debug(header.getName() + "-->" + header.getValue());
+
+                }
+
+                log.debug("--------------------------------------"); // 获取响应实体    
+
+                HttpEntity entity = response.getEntity();
+
+                if (entity != null) {
+
+                    result = EntityUtils.toString(entity, encoding);
+
+                    // 打印响应内容    
+
+                    log.debug("Response content: " + result);
+
+                }
+
+                log.debug("------------------------------------");
+
+            } finally {
+
+                response.close();
+
+            }
+
+        } catch (Exception e) {
+
+            throw e;
+
+        } finally {
+
+            // 关闭连接,释放资源    
+
+            try {
+
+                httpclient.close();
+
+            } catch (IOException e) {
+
+            }
+
+        }
+
+        return result;
+
+
+
+    }
+}

+ 46 - 0
unimall-data/src/main/java/com/iotechn/unimall/data/util/WeiXinQiYeUtil.java

@@ -0,0 +1,46 @@
+package com.iotechn.unimall.data.util;
+
+import com.alibaba.fastjson.JSONObject;
+import com.iotechn.unimall.data.domain.AccessToken;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.cache.RedisCache;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+@Component
+@Slf4j
+public class WeiXinQiYeUtil {
+
+    @Autowired
+    private RedisTemplate redisCache;
+
+    /**
+     * 获取accessToken
+     *
+     * @return
+     */
+    public static String accessToken() throws Exception {
+        //缓存access_token
+        if (!redisCache.hasKey("qiyeweixin_access_token")) {
+            //获取企业Id和凭证
+            String corpId = "自己企业微信corpId";
+            String corpSecret = "自己企业微信corpSecret";
+
+            //拼接调用企业微信SDK
+            String qyAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret;
+            // 获取企业微信 access_token
+            String s = HttpRequestUtil.httpPost(qyAccessTokenUrl,null,null);
+            s = "[" + s + "]";
+            //把读出来的json文件换换为实体
+            List<AccessToken> tokens = JSONObject.parseArray(s, AccessToken.class);
+            String accessToken = tokens.get(0).getAccessToken();
+            return accessToken;
+        }
+
+      return null;
+    }
+}