package com.ruoyi.web.controller.api.xcxManager;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.domain.vo.XcxUserVO;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: cc
 * @Date: 2024/5/10
 * @Description:用户端登录
 */
@Api("小程序管理端/登录")
@RestController
@RequestMapping("/xcx/login")
public class ApiXcxSULoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private RedisCache redisCache;
    /**
     * 登录
     * @param loginDTO 参数
     * @return 结果
     */
    @RepeatSubmit
    @PostMapping("/xcxlogin")
    @Log(title = "登录", businessType = BusinessType.OTHER)
    public AjaxResult csUserLogin(@RequestBody LoginBody loginDTO){
        // 参数校验
        if(StringUtils.isBlank(loginDTO.getPassword())){
            return AjaxResult.error("缺少密码");
        }
        if(StringUtils.isBlank(loginDTO.getUsername())){
            return AjaxResult.error("缺少账号");
        }
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        Map<String,String> tokenObj = new HashMap<>();
        String token = loginService.xcxlogin(loginDTO.getUsername(), loginDTO.getPassword());
        tokenObj.put("uid",token);
        return AjaxResult.success(tokenObj);
    }

    /**
     * 获取用户信息
     * @return 结果
     */
    @GetMapping("/getXcxUserInfo")
    public AjaxResult getXcxUserInfo(){
        return AjaxResult.success(sysUserService.selectUserInfoForXcx());
    }


}
