|
@@ -0,0 +1,91 @@
|
|
|
+package com.hhsx.minigame.controller;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.hhsx.minigame.constant.Constant;
|
|
|
+import com.hhsx.minigame.entity.User;
|
|
|
+import com.hhsx.minigame.service.UserService;
|
|
|
+import com.hhsx.minigame.utils.HttpKit;
|
|
|
+import com.hhsx.minigame.utils.HttpRespMsg;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.NoSuchProviderException;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 前端控制器
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author 吴涛涛
|
|
|
+ * @since 2019-09-18
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/user")
|
|
|
+public class UserController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信授权登录
|
|
|
+ * 参数:
|
|
|
+ * type:授权类型,0-微信,1-微博
|
|
|
+ * code:平台返回的code值
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation("微信/微博网页授权")
|
|
|
+ @RequestMapping(value = "weiXinLogin")
|
|
|
+ @ResponseBody
|
|
|
+ public Object weiXinLogin(@RequestParam String code, Integer type,
|
|
|
+ HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ if (type == 0) {
|
|
|
+ String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.WECHAT_APPID + "&secret=" + Constant.WECHAT_APPSECRET + "&code=" + code + "&grant_type=authorization_code";
|
|
|
+ String resp = HttpKit.get(url, true);
|
|
|
+ resp = StringEscapeUtils.unescapeJava(resp);
|
|
|
+ System.out.println(resp);
|
|
|
+ JSONObject json = (JSONObject) JSON.parse(resp);
|
|
|
+ if (!json.containsKey("errcode")) {
|
|
|
+ String openId = json.getString("openid");
|
|
|
+ User user = new User();
|
|
|
+ user.setType(type);
|
|
|
+ user.setVoucherId(openId);
|
|
|
+ QueryWrapper<User> qw = new QueryWrapper<>();
|
|
|
+ qw.eq("voucher_id", openId).eq("type", type);
|
|
|
+ if (userService.count(qw) == 0) {
|
|
|
+ userService.save(user);
|
|
|
+ } else {
|
|
|
+ //列表中已包含当前用户,
|
|
|
+ user = userService.getOne(qw);
|
|
|
+ }
|
|
|
+ msg.data = user;
|
|
|
+ } else {
|
|
|
+ msg.setError(json.getString("errmsg"));
|
|
|
+ }
|
|
|
+ }else if(type == 1){
|
|
|
+// String url = "https://api.weibo.com/oauth2/authorize?client_id="+Constant.MICROBLOG_APPKEY +"&response_type=code&redirect_uri="+callbackURL;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ response.setContentType("application/json");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|