| | |
| | | import cn.binarywang.wx.miniapp.api.WxMaUserService; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| | | import cn.binarywang.wx.miniapp.config.WxMaConfig; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.google.gson.Gson; |
| | | import com.mzl.flower.base.cache.StringCacheClient; |
| | | import com.mzl.flower.config.WxMiniappProperties; |
| | | import com.mzl.flower.config.exception.ValidationException; |
| | | import com.mzl.flower.constant.Constants; |
| | | import com.mzl.flower.dto.request.CreateWechatUserDTO; |
| | | import com.mzl.flower.service.BaseService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.chanjar.weixin.common.error.WxErrorException; |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | |
| | | @Autowired |
| | | private WxMiniappProperties wxMiniappProperties; |
| | | |
| | | @Autowired |
| | | private Gson gson; |
| | | |
| | | /** |
| | | * 小程序获取会话信息-花店端 |
| | | * @param code |
| | |
| | | */ |
| | | public WxMaJscode2SessionResult getWxMaSessionInfo(String code) throws WxErrorException { |
| | | WxMaJscode2SessionResult result = maService.jsCode2SessionInfo(code); |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | throw new ValidationException(e.getMessage()); |
| | | } |
| | | } |
| | | public Map<String,Object> getuserphonenumber(CreateWechatUserDTO dto) { |
| | | if (StringUtils.isEmpty(dto.getCode())) { |
| | | throw new ValidationException("微信code为空"); |
| | | } |
| | | |
| | | String accessToken=""; |
| | | try { |
| | | accessToken=maService.getAccessToken(); |
| | | } catch (WxErrorException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | if(StringUtils.isEmpty(accessToken)){ |
| | | accessToken=getAccessToken( |
| | | wxMiniappProperties.getCustomer().getAppid(), |
| | | wxMiniappProperties.getCustomer().getSecret()); |
| | | if(StringUtils.isEmpty(accessToken)){ |
| | | throw new ValidationException("获取微信AccessToken失败"); |
| | | } |
| | | } |
| | | |
| | | |
| | | String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+ accessToken; |
| | | try{ |
| | | Map<String,Object> params=new HashMap<String,Object>(); |
| | | params.put("code",dto.getCode()); |
| | | String result = HttpUtil.post(url, gson.toJson(params)); |
| | | System.out.println(result); |
| | | Map<String,Object> resultMap=gson.fromJson(result,Map.class); |
| | | if(!resultMap.get("errmsg").equals("ok")){ |
| | | String errmsg = (String)resultMap.get("errmsg"); |
| | | log.error(resultMap.get("errcode") + ":" + errmsg); |
| | | throw new ValidationException(errmsg); |
| | | } |
| | | return resultMap; |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(), e); |
| | | throw new ValidationException(e.getMessage()); |
| | | } |
| | | } |
| | | } |