package com.kym.admin.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.kym.common.R;
import com.kym.common.controller.IController;
import com.kym.entity.admin.AdminUser;
import com.kym.service.admin.AdminUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Qualifier;
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;


/**
 * 微信看板小程序
 * 登录控制器
 */
@Slf4j
@RestController
@RequestMapping("/wx")
public class WxKanbanController extends IController {

    private final WxMaService wxMaService;

    private final AdminUserService adminUserService;


    public WxKanbanController(@Qualifier("kanbanWxMaService") WxMaService wxMaService, AdminUserService adminUserService) {
        this.wxMaService = wxMaService;
        this.adminUserService = adminUserService;
    }

    @RequestMapping("getUnionId")
    public R<?> getUnionId(String code) throws WxErrorException {
        WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
        return R.success(result);
    }

    /**
     * 获取微信手机号
     * @param code
     * @return
     * @throws WxErrorException
     */
    @RequestMapping("getPhoneNumber")
    public R<?> getPhoneNumber(String code) throws WxErrorException {
        WxMaPhoneNumberInfo result = wxMaService.getUserService().getPhoneNoInfo(code);
        return R.success(result);
    }


    @PostMapping("login")
    public R<?> login(@RequestBody AdminUser  user) {
        return resp(()->adminUserService.wxLogin(user));
    }

    @PostMapping("loginByOpenId")
    public R<?> loginByUnion(@RequestBody AdminUser  user) {
        return resp(()->adminUserService.loginByOpenId(user));
    }



}
