package com.ematou.wxservice.mp.handler;

import com.alibaba.fastjson.JSON;
import com.ematou.wxservice.api.WeChatApi;
import com.ematou.wxservice.api.WeChatApiRestTemplate;
import com.ematou.wxservice.mp.message.WeChatMessage;
import com.ematou.wxservice.entity.pojo.UserInfo;
import com.ematou.wxservice.mp.message.WeChatMpXmlOutMessage;
import com.ematou.wxservice.mp.message.WeChatMpXmlOutTextMessage;
import com.ematou.wxservice.service.WeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 文本信息处理器
 * @author lhm
 * @version 1.0
 * 2021-05-11 16:27
 */
@Component
public class WeChatTextMessageHandler implements WeChatMessageHandler {

    @Autowired
    WeChatApiRestTemplate weChatApiRestTemplate;

    @Autowired
    WeChatService weChatService;

    @Override
    public WeChatMpXmlOutTextMessage handlerMessage(WeChatMessage weChatMessage) {

        String openId = weChatMessage.getFromUser();

        String response = weChatApiRestTemplate.getForOther(String.format(WeChatApi.GET_USER_INFO.getUrl(), weChatService.getAccessToken().getAccessToken(), openId));
        // TODO 构建响应数据，需要修改响应数据，目前只是测试
        UserInfo userInfo = JSON.parseObject(response, UserInfo.class);
        System.out.println(userInfo.toString());
        return WeChatMpXmlOutMessage.TEXT().content(response).toUser(weChatMessage.getFromUser()).fromUser(weChatMessage.getToUser()).build();
    }
}
