微信公众号开发实现每隔五秒随机发一张图片
作者:互联网
JavaBean
@Data
public class ImageSend {
private String touser;
private String msgtype;
private Img image;
}
@Data
public class Img {
private String media_id;
}
@Component
public class SendImage {
@Autowired
private DemoService demoService;
@Scheduled(cron = "0/5 * * * * ?")
public void sendImage(){
String url="https://api.weixin.qq.com/cgi-bin/message/custom/send?" +
"access_token=44_AF5XZOwBOrI350fz4bMAutPfDMJa2y8D9ggNntOIFPxQAAnxJrW8kKC6jxgFOP7ID-ya-oYLlLshrfOCz7GL7T2sdw2qY0bhyyr3CpeGZgAzCmuk24gKtt9agd1Rtv-j6ABq6_SU_ophbK0uISWcAFAQAE";
String openid = "o1s8g6fduVqBDl9J6Z_GoqUMzJls";
ImageSend imageSend = new ImageSend();
imageSend.setTouser(openid);
imageSend.setMsgtype(MessageUtil.REQ_MESSAGE_TYPE_IMAGE);
Img img = new Img();
List<String> list = demoService.selectAll();
Random random = new Random();
int i = random.nextInt(list.size());
img.setMedia_id(list.get(i));
imageSend.setImage(img);
JSONObject jsonObject1 = JSONObject.fromObject(imageSend);
System.out.println(jsonObject1.toString());
WeixinUtil.httpRequest(url, "POST", jsonObject1.toString());
}
}
入口类加上@EnableScheduling注解
标签:String,Img,微信,img,五秒,private,imageSend,每隔,public 来源: https://blog.csdn.net/qq_40686238/article/details/116375750