package com.qy.worksheetsystem.controller;

import com.qy.worksheetsystem.model.MessageResult;
import com.qy.worksheetsystem.model.MessageResultV2;
import com.qy.worksheetsystem.service.impl.WeiXinAuthorizeServiceImpl;
import com.qy.worksheetsystem.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * description: WeiXinAuthorizeController <br>
 * date: 2022/3/7 14:30 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/timeSheet")
@Api(value = "企业微信",tags="企业微信")
public class WeiXinAuthorizeController {

    @Autowired
    private WeiXinAuthorizeServiceImpl weiXinAuthorizeService;

    @ApiOperation(value = "构造网页授权链接", notes = "构造网页授权链接")
    @RequestMapping(value ="/getRedirectUri",method = RequestMethod.GET)
    public MessageResult authorize(@RequestParam("redirect_uri") String redirect_uri) {
        return MessageResult.success().add("url", weiXinAuthorizeService.authorize(redirect_uri, ""));
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @RequestMapping(value ="/getUserInfo",method = RequestMethod.GET)
    public MessageResult getUserInfo(@RequestParam("code") String code) {
        Map<String,Object> map = weiXinAuthorizeService.getPersonInfo(code);
        MessageResult messageResult= MessageResult.success();
        messageResult.setData(map);
        return messageResult;
    }
    @ApiOperation(value = "获取Token", notes = "获取Token")
    @RequestMapping(value ="/getToken",method = RequestMethod.GET)
    public MessageResult getToken(@RequestParam("personNum")String personNum, @RequestParam("userId")String userId){
        String token = JWTUtil.createToken(personNum,userId);
        MessageResultV2 messageResult= MessageResultV2.success();
        messageResult.setData(token);
        return messageResult;
    }

}
