package ink.wgink.login.wechat.controller.wechat.sign;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.exceptions.PropertiesException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.properties.wechat.miniapp.MiniappProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MiniSignWechatController
 * @Description: 小程序登录
 * @Author: wanggeng
 * @Date: 2021/4/8 5:08 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_WECHAT_MINI_APP_PREFIX + "小程序登录")
@RestController
@RequestMapping(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/sign")
public class SignMiniappController {

    @Autowired
    private MiniappProperties miniAppProperties;
    @Autowired
    private IMiniappSignService miniAppSignService;

    @ApiOperation(value = "默认登录", notes = "通过jsCode完成用户与微信openid绑定并返回token")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("default")
    @CheckRequestBodyAnnotation
    public SuccessResultData<String> defaultSign(@RequestBody MiniappLoginVO miniAppLoginVO) throws Exception {
        if (!miniAppProperties.getActive()) {
            throw new PropertiesException("小程序配置未激活");
        }
        return new SuccessResultData<>(miniAppSignService.defaultSign(miniAppLoginVO));
    }

}
