package cclotus.weixin.api.service

import cclotus.weixin.api.domain.GetUserPhoneNumberReq
import cclotus.weixin.api.domain.GetUserPhoneNumberRes
import cclotus.weixin.api.domain.JsCode2Session
import cclotus.weixin.api.feign.WxFeignConfiguration
import cclotus.weixin.util.WeixinError
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.RequestParam
import kotlin.jvm.Throws

@FeignClient(name = "wx-app-login", url = "\${wxapi.url}", configuration = [WxFeignConfiguration::class])
interface WxAppLoginService {
    /**
     * 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
     * @param appid 小程序 appId
     * @param js_code 登录时获取的 code，可通过wx.login获取
     * @param grant_type 授权类型，此处只需填写 authorization_code
     * @return session_key、unionid、openid等信息
     * @see JsCode2Session
     */
    @Throws(WeixinError::class)
    @GetMapping("/api.weixin.qq.com/sns/jscode2session")
    fun code2Session(
        @RequestParam("appid") appid: String,
        @RequestParam("js_code") js_code: String,
        @RequestParam("grant_type") grant_type: String = "authorization_code"
    ): JsCode2Session

    /**
     * 获取用户手机号
     * @param data 请求数据，手机号获取凭证
     * @return GetUserPhoneNumberRes
     * @see GetUserPhoneNumberRes
     */
    @Throws(WeixinError::class)
    @PostMapping("/api.weixin.qq.com/wxa/business/getuserphonenumber")
    fun getUserPhoneNumber(
        @RequestParam("appid") appid: String,
        @RequestBody data: GetUserPhoneNumberReq
    ): GetUserPhoneNumberRes
}