|
|
|
@ -0,0 +1,71 @@
|
|
|
|
|
package com.kiisoo.ic.wx.controller;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.kiisoo.ic.base.entity.BasicConfigDO;
|
|
|
|
|
import com.kiisoo.ic.base.mapper.BasicConfigDOMapper;
|
|
|
|
|
import com.kiisoo.ic.common.BaseController;
|
|
|
|
|
import com.kiisoo.ic.common.utils.httpClientUtil.HttpClientUtil;
|
|
|
|
|
import com.kiisoo.ic.common.utils.httpClientUtil.HttpResult;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Controller
|
|
|
|
|
@RequestMapping("/wechat")
|
|
|
|
|
public class WechatController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private BasicConfigDOMapper basicConfigDOMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/openId")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public Map<String, Object> openId(String code) { // 小程序端获取的CODE
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
|
result.put("code", 0);
|
|
|
|
|
try {
|
|
|
|
|
QueryWrapper<BasicConfigDO> baseParams1 = new QueryWrapper<>();
|
|
|
|
|
baseParams1.eq("code", "MINIPROGRAM_CONF");
|
|
|
|
|
baseParams1.eq("conf_key", "APPID");
|
|
|
|
|
BasicConfigDO basicConfigDO1 = basicConfigDOMapper.selectOne(baseParams1);
|
|
|
|
|
QueryWrapper<BasicConfigDO> baseParams2 = new QueryWrapper<>();
|
|
|
|
|
baseParams2.eq("code", "MINIPROGRAM_CONF");
|
|
|
|
|
baseParams2.eq("conf_key", "SECRET");
|
|
|
|
|
BasicConfigDO basicConfigDO2 = basicConfigDOMapper.selectOne(baseParams2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean check = (StringUtils.isEmpty(code)) ? true : false;
|
|
|
|
|
if (check) {
|
|
|
|
|
throw new Exception("参数异常");
|
|
|
|
|
}
|
|
|
|
|
StringBuilder urlPath = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session"); // 微信提供的API,这里最好也放在配置文件
|
|
|
|
|
urlPath.append(String.format("?appid=%s", basicConfigDO1.getConfValue()));
|
|
|
|
|
urlPath.append(String.format("&secret=%s", basicConfigDO2.getConfValue()));
|
|
|
|
|
urlPath.append(String.format("&js_code=%s", code));
|
|
|
|
|
urlPath.append(String.format("&grant_type=%s", "authorization_code")); // 固定值
|
|
|
|
|
|
|
|
|
|
HttpResult data = HttpClientUtil.httpGet(urlPath.toString());
|
|
|
|
|
//http返回参数
|
|
|
|
|
if (JSONObject.parseObject(data.getMessage()).get("errcode").equals("0")) {
|
|
|
|
|
String openid = JSONObject.parseObject(data.getMessage()).get("openid").toString();
|
|
|
|
|
String sessionKey = JSONObject.parseObject(data.getMessage()).get("session_key").toString();
|
|
|
|
|
String unionid = JSONObject.parseObject(data.getMessage()).get("unionid").toString();
|
|
|
|
|
|
|
|
|
|
result.put("openId", openid);
|
|
|
|
|
result.put("sessionKey", sessionKey);
|
|
|
|
|
result.put("unionid", unionid);
|
|
|
|
|
}
|
|
|
|
|
return data(result);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return fail();
|
|
|
|
|
}
|
|
|
|
|
}
|