|
@@ -0,0 +1,135 @@
|
|
|
|
+package com.qmth.ops.biz.service;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.qmth.boot.core.concurrent.service.ConcurrentService;
|
|
|
|
+import com.qmth.boot.core.exception.ParameterException;
|
|
|
|
+import com.qmth.boot.core.solar.model.WxappAccessToken;
|
|
|
|
+import com.qmth.ops.biz.dao.WxappDao;
|
|
|
|
+import com.qmth.ops.biz.domain.Wxapp;
|
|
|
|
+import com.qmth.ops.biz.query.WxappQuery;
|
|
|
|
+import com.qmth.ops.biz.wxapp.api.WxappApiClient;
|
|
|
|
+import com.qmth.ops.biz.wxapp.dto.AccessTokenResult;
|
|
|
|
+import com.qmth.ops.biz.wxapp.dto.Code2SessionResult;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.concurrent.locks.Lock;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class WxappService extends ServiceImpl<WxappDao, Wxapp> {
|
|
|
|
+
|
|
|
|
+ private static final String WXAPP_ACCESS_TOKEN_KEY = "wxapp:access_token:";
|
|
|
|
+
|
|
|
|
+ private static final String GRANT_TYPE_ACCESS_TOKEN = "client_credential";
|
|
|
|
+
|
|
|
|
+ private static final String GRANT_TYPE_CODE_2_SESSION = "authorization_code";
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WxappDao wxappDao;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private WxappApiClient wxappApiClient;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private ConcurrentService concurrentService;
|
|
|
|
+
|
|
|
|
+ public Wxapp findById(String id) {
|
|
|
|
+ return wxappDao.selectById(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public WxappQuery findByQuery(WxappQuery query) {
|
|
|
|
+ return wxappDao.selectPage(query, buildQuery(query));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<Wxapp> listByQuery(WxappQuery query) {
|
|
|
|
+ return wxappDao.selectList(buildQuery(query));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ public Wxapp insert(Wxapp wxapp) {
|
|
|
|
+ wxapp.setCreateTime(System.currentTimeMillis());
|
|
|
|
+ wxapp.setUpdateTime(wxapp.getCreateTime());
|
|
|
|
+ if (wxappDao.insert(wxapp) == 1) {
|
|
|
|
+ return wxapp;
|
|
|
|
+ } else {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ public Wxapp update(Wxapp wxapp) {
|
|
|
|
+ Wxapp previous = wxappDao.selectById(wxapp.getId());
|
|
|
|
+ if (previous != null) {
|
|
|
|
+ if (wxapp.getName() != null) {
|
|
|
|
+ previous.setName(wxapp.getName());
|
|
|
|
+ }
|
|
|
|
+ if (wxapp.getSecret() != null) {
|
|
|
|
+ previous.setSecret(wxapp.getSecret());
|
|
|
|
+ }
|
|
|
|
+ previous.setUpdateTime(System.currentTimeMillis());
|
|
|
|
+ if (wxappDao.updateById(previous) == 1) {
|
|
|
|
+ return previous;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private LambdaQueryWrapper<Wxapp> buildQuery(WxappQuery query) {
|
|
|
|
+ return new QueryWrapper<Wxapp>().lambda().eq(query.getId() != null, Wxapp::getId, query.getId())
|
|
|
|
+ .eq(query.getName() != null, Wxapp::getName, query.getName())
|
|
|
|
+ .likeRight(query.getNameStartWith() != null, Wxapp::getName, query.getNameStartWith());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public WxappAccessToken getAccessToken(String appId) {
|
|
|
|
+ Wxapp wxapp = findById(appId);
|
|
|
|
+ if (wxapp == null) {
|
|
|
|
+ throw new ParameterException("Invalid appId");
|
|
|
|
+ }
|
|
|
|
+ String key = WXAPP_ACCESS_TOKEN_KEY.concat(wxapp.getId());
|
|
|
|
+ WxappAccessToken obj = new WxappAccessToken();
|
|
|
|
+ if (wxapp.getAccessToken() != null && wxapp.getExpireTime() != null && wxapp.getExpireTime() > System
|
|
|
|
+ .currentTimeMillis()) {
|
|
|
|
+ obj.setAccessToken(wxapp.getAccessToken());
|
|
|
|
+ obj.setExpireTime(wxapp.getExpireTime());
|
|
|
|
+ } else {
|
|
|
|
+ Lock lock = concurrentService.getLock(key);
|
|
|
|
+ lock.lock();
|
|
|
|
+ try {
|
|
|
|
+ AccessTokenResult result = wxappApiClient
|
|
|
|
+ .getAccessToken(wxapp.getId(), wxapp.getSecret(), GRANT_TYPE_ACCESS_TOKEN);
|
|
|
|
+ if (result.success()) {
|
|
|
|
+ //强制提前60秒更新
|
|
|
|
+ result.setExpireSecond(result.getExpireSecond() - 60);
|
|
|
|
+ obj = result.output();
|
|
|
|
+ wxapp.setAccessToken(obj.getAccessToken());
|
|
|
|
+ wxapp.setExpireTime(obj.getExpireTime());
|
|
|
|
+ wxappDao.updateById(wxapp);
|
|
|
|
+ } else {
|
|
|
|
+ throw result.exception();
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return obj;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Code2SessionResult getSessionByCode(String appId, String code) {
|
|
|
|
+ Wxapp wxapp = findById(appId);
|
|
|
|
+ if (wxapp == null) {
|
|
|
|
+ throw new ParameterException("Invalid WxappId");
|
|
|
|
+ }
|
|
|
|
+ Code2SessionResult result = wxappApiClient
|
|
|
|
+ .code2session(wxapp.getId(), wxapp.getSecret(), code, GRANT_TYPE_CODE_2_SESSION);
|
|
|
|
+ if (result.success()) {
|
|
|
|
+ return result;
|
|
|
|
+ } else {
|
|
|
|
+ throw result.exception();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|