Controller
@ApiOperation("生成邀请码")
@ApiImplicitParams({
@ApiImplicitParam(name = "level", value = "会员等级", required = true, dataType = "String")
})
@GetMapping("genInviteCode")
public InviteInfo genInviteCode(Long itemId,String type);
Service
@Override
public InviteInfo genInviteCode(Long itemId ,String type) {
AssertExt.notNull(type,"无效的type");
//Member member = this.memberMapper.selectById(memberId);
//AssertExt.notNull(member,"无效的member[%s]",memberId);
InviteInfo inviteInfo=new InviteInfo();
inviteInfo.setCreateTime(new Date());
inviteInfo.setInviteCode(UUID.randomUUID().toString().replace("-","").toUpperCase());
inviteInfo.setExpire(this.expire);
inviteInfo.setType(type);
if(itemId!=null){
inviteInfo.setItemId(itemId);
}
inviteInfo.setPicCode(this.genWechatMPCode(inviteInfo.getInviteCode(),itemId));
//inviteInfo.setRecommendName(member.getNickname());
this.redisTemplate.opsForValue().set(inviteInfo.getInviteCode(),
JSONObject.toJSONString(inviteInfo),
inviteInfo.getExpire(),
TimeUnit.HOURS);
return inviteInfo;
}
私有方法
private String genWechatMPCode(String code,Long id) {
String url = String.format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s",
this.wechatAccessToken.getWechatPartnerAccessToken());
JSONObject data = new JSONObject();
data.put("scene", code);
Content content = null;
try {
content = Request.Post(url)
.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
.body(new StringEntity(data.toJSONString(), Consts.UTF_8))
.execute().returnContent();
} catch (ClientProtocolException e) {
log.error("ClientProtocolException", e);
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
} finally {
log.info("request {},param {}, response type {}", url, data, content == null ? "null" : content.getType());
}
return this.zsycFsApi.writeByCode(code, content.asBytes());
}
zsycFsApi文档
package com.zsyc.cloud.framework.fs.api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@FeignClient("zsyc-cloud-fs")
@RequestMapping({"fs"})
public interface ZsycFsApi {
@ApiOperation("文件上传")
@PostMapping({"upload"})
@ApiImplicitParams({@ApiImplicitParam(
name = "file",
value = "file",
required = true,
dataType = "MultipartFile"
), @ApiImplicitParam(
name = "code",
value = "code",
required = true,
dataType = "String"
)})
Object upload(@RequestPart(value = "code",required = false) String code, @RequestPart("file") MultipartFile file) throws IOException;
@ApiOperation("文件读取")
@GetMapping({"read"})
@ApiImplicitParams({@ApiImplicitParam(
name = "code",
value = "code",
required = true,
dataType = "String"
)})
byte[] read(@RequestParam("code") String code);
@ApiOperation("文件写入")
@PostMapping({"write"})
@ApiImplicitParams({@ApiImplicitParam(
name = "bytes",
value = "File文件",
required = true,
dataType = "File"
)})
String write(@RequestBody byte[] bytes);
@ApiOperation("文件写入")
@PostMapping(
value = {"writeByCode"},
consumes = {"multipart/form-data"},
produces = {"multipart/form-data"}
)
@ApiImplicitParams({@ApiImplicitParam(
name = "code",
value = "code",
required = true,
dataType = "String"
), @ApiImplicitParam(
name = "bytes",
value = "byte数据",
required = true,
dataType = "byte[]"
)})
String writeByCode(@RequestPart("code") String code, @RequestPart("bytes") byte[] bytes);
@ApiOperation("code是否已经存在")
@GetMapping({"exist"})
@ApiImplicitParams({@ApiImplicitParam(
name = "code",
value = "文件code(sha256)",
required = true,
dataType = "String"
)})
Map<String, String> exist(@RequestParam("code") String code);
}