|
@@ -0,0 +1,52 @@
|
|
|
+package com.xy.third.service.wx;
|
|
|
+
|
|
|
+import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
+import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
+import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
|
|
+import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
|
|
+import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
|
|
+import com.xy.annotate.RestMappingController;
|
|
|
+import com.xy.collections.map.JHashMap;
|
|
|
+import com.xy.third.config.ThirdLoginConfig;
|
|
|
+import com.xy.third.dto.wx.WxMaDto;
|
|
|
+import com.xy.utils.R;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 微信小程序
|
|
|
+ *
|
|
|
+ * @author lijin
|
|
|
+ */
|
|
|
+@AllArgsConstructor
|
|
|
+@Api(tags = "微信小程序")
|
|
|
+@RestMappingController("wx-ma")
|
|
|
+public class WxMaServiceImpls {
|
|
|
+
|
|
|
+ private ThirdLoginConfig thirdLoginConfig;
|
|
|
+
|
|
|
+ @PostMapping("oenId")
|
|
|
+ @ApiOperation("获取openId")
|
|
|
+ public R<String> openId(@RequestBody @Validated WxMaDto.OpenId openId) {
|
|
|
+ ThirdLoginConfig.Wx wx = thirdLoginConfig.getWx();
|
|
|
+ WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
|
|
+ config.setAppid(wx.getAppid());
|
|
|
+ config.setSecret(wx.getSecret());
|
|
|
+ WxMaService wxMaService = new WxMaServiceImpl();
|
|
|
+ wxMaService.setMultiConfigs(new JHashMap<String, WxMaConfig>().set("config", config));
|
|
|
+ try {
|
|
|
+ WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(openId.getCode());
|
|
|
+ return R.ok(session.getOpenid());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return R.fail("code无效");
|
|
|
+ } finally {
|
|
|
+ WxMaConfigHolder.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|