xhxu 5 лет назад
Родитель
Сommit
7e6cd2f150
4 измененных файлов: 144 добавлений и 0 удалений
  1. +35
    -0
      mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtMaQrcode.java
  2. +31
    -0
      mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtMaQrcodeService.java
  3. +26
    -0
      mallinkService/src/main/java/com/iformall/douyin/miniapp/api/impl/TtMaQrcodeServiceImpl.java
  4. +52
    -0
      mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java

+ 35
- 0
mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtMaQrcode.java Просмотреть файл

@@ -0,0 +1,35 @@
package com.iformall.douyin.miniapp.api;

import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class TtMaQrcode extends AbstractWxMaQrcodeWrapper implements Serializable {
private static final long serialVersionUID = 5777119669111011584L;
private String appname;
private String path;
private int width = 430;

public TtMaQrcode(String appname, String path, int width) {
this.appname = appname;
this.path = path;
this.width = width;
}

public static TtMaQrcode fromJson(String json) {
return WxMaGsonBuilder.create().fromJson(json, TtMaQrcode.class);
}

@Override
public String toString() {
return WxMaGsonBuilder.create().toJson(this);
}
}

+ 31
- 0
mallinkService/src/main/java/com/iformall/douyin/miniapp/api/TtMaQrcodeService.java Просмотреть файл

@@ -0,0 +1,31 @@
package com.iformall.douyin.miniapp.api;

import me.chanjar.weixin.common.error.WxErrorException;

import java.io.File;

/**
* <pre>
* 二维码相关操作接口.
*
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface TtMaQrcodeService {

String GET_TTACODE_URL = "https://developer.toutiao.com/api/apps/qrcode";


/**
* 接口A: 获取小程序码.
*
* @param path 不能为空,最大长度 128 字节
* @param width 默认430 二维码的宽度
* @return 文件对象
* @throws WxErrorException 异常
*/
File createTtaCode(String appname, String path, int width) throws WxErrorException;


}

+ 26
- 0
mallinkService/src/main/java/com/iformall/douyin/miniapp/api/impl/TtMaQrcodeServiceImpl.java Просмотреть файл

@@ -0,0 +1,26 @@
package com.iformall.douyin.miniapp.api.impl;

import cn.binarywang.wx.miniapp.util.QrcodeRequestExecutor;
import com.iformall.douyin.miniapp.api.TtMaQrcode;
import com.iformall.douyin.miniapp.api.TtMaQrcodeService;
import com.iformall.douyin.miniapp.api.TtMaService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;

import java.io.File;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class TtMaQrcodeServiceImpl implements TtMaQrcodeService {

private TtMaService ttMaService;

@Override
public File createTtaCode(String appname, String path, int width) throws WxErrorException {
final QrcodeRequestExecutor executor = new QrcodeRequestExecutor(this.ttMaService.getRequestHttp());
return this.ttMaService.execute(executor, GET_TTACODE_URL, new TtMaQrcode(appname,path, width));
}

}

+ 52
- 0
mallinkTTAdmin/src/main/java/com/iformall/controller/base/BaseController.java Просмотреть файл

@@ -6,12 +6,19 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;


import com.iformall.domain.po.MallUserInfo; import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxAppinfo;
import com.iformall.domain.po.base.TenantEntity; import com.iformall.domain.po.base.TenantEntity;
import com.iformall.douyin.miniapp.api.TtMaService;
import com.iformall.service.WxAppinfoService;
import com.iformall.shiro.UserSession; import com.iformall.shiro.UserSession;
import com.iformall.utils.Constant;
import com.iformall.utils.IPUtil; import com.iformall.utils.IPUtil;
import com.iformall.utils.MaUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session; import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -23,6 +30,9 @@ import javax.servlet.http.HttpServletRequest;
@RestController @RestController
public class BaseController { public class BaseController {


@Autowired
private WxAppinfoService wxAppinfoService;

@InitBinder @InitBinder
public void InitBinder(WebDataBinder dataBinder) { public void InitBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@@ -60,6 +70,22 @@ public class BaseController {
// return tenantId; // return tenantId;
// } // }


public TtMaService getWeappServiceByAppInfo(WxAppinfo appinfo) {
TtMaService ttMaService = MaUtil.getTtappService(appinfo);
if (StringUtils.isBlank(appinfo.getAccessToken())) {
// 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用
updateAppAccessToken(appinfo, ttMaService);

} else {
// 检查token是否已过期, 1小时就重新获取
Date curDate = new Date();
if (curDate.getTime() > appinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) {
updateAppAccessToken(appinfo, ttMaService);
}
}
return ttMaService;
}

/** /**
* 返回租户信息 * 返回租户信息
* 集团版帐号tenantId为空,parentTenantId为集团tenantId * 集团版帐号tenantId为空,parentTenantId为集团tenantId
@@ -106,6 +132,32 @@ public class BaseController {
return null; return null;
} }


/**
* WxAppInfo更新accessToken
*
* @param wxAppinfo
* @param TtMaService
*/
private void updateAppAccessToken(WxAppinfo wxAppinfo, TtMaService TtMaService) {
try {
String accessToken = TtMaService.getAccessToken(true);
WxAppinfo updateApp = new WxAppinfo();
updateApp.setId(wxAppinfo.getId());
updateApp.setAccessToken(accessToken);
updateApp.setLastTokenTime(new Date());
updateApp.setExpiresIn(7200);
wxAppinfo.setAccessToken(updateApp.getAccessToken());
wxAppinfo.setLastTokenTime(updateApp.getLastTokenTime());
wxAppinfo.setExpiresIn(updateApp.getExpiresIn());
wxAppinfoService.saveOrUpdate(updateApp);

} catch (WxErrorException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public String getIpAddr() { public String getIpAddr() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ipaddress = IPUtil.getIpAddr(request); String ipaddress = IPUtil.getIpAddr(request);


Загрузка…
Отмена
Сохранить