|
@@ -0,0 +1,87 @@
|
|
|
|
|
+package com.jpsoft.employment.modules.wechat.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.jpsoft.employment.modules.common.dto.MessageResult;
|
|
|
|
|
+import com.jpsoft.employment.modules.common.utils.WeixinUtil;
|
|
|
|
|
+import com.jpsoft.employment.modules.pay.properties.WxCustom;
|
|
|
|
|
+import com.jpsoft.employment.modules.pay.properties.WxMini;
|
|
|
|
|
+import com.jpsoft.employment.modules.wechat.entity.AccessToken;
|
|
|
|
|
+import com.jpsoft.employment.modules.wechat.entity.SessionKey;
|
|
|
|
|
+import io.swagger.annotations.Api;
|
|
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@RequestMapping("/wechatCustom")
|
|
|
|
|
+@Api(description = "小程序")
|
|
|
|
|
+public class WxCustomController {
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WxCustom wxCustom;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ValueOperations<String, Object> valueOperations;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @ApiOperation(value = "获取公众号用户信息")
|
|
|
|
|
+ @GetMapping(value = "findAppletUserInfoz/{code}")
|
|
|
|
|
+ public MessageResult findAppletUserInfo(@PathVariable String code) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.warn("code=" + code);
|
|
|
|
|
+ log.warn("appId=" + wxCustom.getAppId());
|
|
|
|
|
+ log.warn("appSecret=" + wxCustom.getAppSecret());
|
|
|
|
|
+
|
|
|
|
|
+ SessionKey sessionKey = WeixinUtil.getSessionKey(wxCustom.getAppId(), wxCustom.getAppSecret(), code);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if (sessionKey != null && StringUtils.isNotBlank(sessionKey.getOpenid())) {
|
|
|
|
|
+ String openId = sessionKey.getOpenid();
|
|
|
|
|
+ System.out.println("openId:" + openId);
|
|
|
|
|
+ HashMap<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
|
|
+ dataMap.put("openId", openId);
|
|
|
|
|
+
|
|
|
|
|
+// UserInfo userInfo = WeixinUtil.getUserInfo(openId, sessionKey.getSessionKey());
|
|
|
|
|
+//
|
|
|
|
|
+// HashMap<String, Object> dataMap = new HashMap<String, Object>();
|
|
|
|
|
+//
|
|
|
|
|
+// dataMap.put("userInfo", userInfo);
|
|
|
|
|
+
|
|
|
|
|
+ return new MessageResult(true, "获取信息成功", openId, 200);
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return new MessageResult(false, "获取信息失败", "", 400);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
|
+ return new MessageResult(false, "系统错误", "", 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ApiOperation(value = "获取小程序用户手机号信息")
|
|
|
|
|
+ @GetMapping(value = "findUserPhoneNumber/{code}")
|
|
|
|
|
+ public MessageResult findUserPhoneNumber(@PathVariable String code) {
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+ String accessToken = (String)valueOperations.get("accessToken");
|
|
|
|
|
+ if(StringUtils.isEmpty(accessToken)){
|
|
|
|
|
+ AccessToken token = WeixinUtil.getAccessToken(wxCustom.getAppId(), wxCustom.getAppSecret());
|
|
|
|
|
+ accessToken = token.getToken();
|
|
|
|
|
+ valueOperations.set("accessToken",token.getToken(),2, TimeUnit.HOURS);
|
|
|
|
|
+ }
|
|
|
|
|
+ String phone = WeixinUtil.getUserPhoneNumber(accessToken,code);
|
|
|
|
|
+
|
|
|
|
|
+ return new MessageResult(true, "成功", phone, 200);
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
|
+ return new MessageResult(false, "系统错误", "", 500);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|