LocalLoginController.java 2.07 KB
package cn.com.glsx.app.modules;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.LoginByAccountBO;
import cn.com.glsx.app.modules.service.LoginService;
import cn.com.glsx.app.modules.service.UserService;
import com.glsx.plat.core.web.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 本地登录
 *
 * @author liuyf
 */
@Slf4j
@Api(value = "登录(本地)", tags = {"登录(本地)"})
@RestController
public class LocalLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private WxMaService wxMaService;

    @GetMapping(value = "/")
    public String index() {
        return "success";
    }

    /**
     * 本地登录
     */
    @ApiOperation(value = "登录系统")
    @PostMapping(value = "/login")
    public R login(@RequestBody @Validated LoginByAccountBO loginBO) throws WxErrorException {
        //本地登录授权
        AppUser user = loginService.localAuth(loginBO);
        //关联微信账号
        loginService.linkWxAuth(loginBO.getCodeForWx(), user);
        //创建授权令牌
        String token = userService.createToken(user);

        Map<String, Object> rtnMap = new HashMap<>();
        rtnMap.put("token", token);
        rtnMap.put("user", user);
        return R.ok().data(rtnMap);
    }

    @ApiOperation(value = "登出系统")
    @GetMapping(value = "/logout")
    public R logout() {
        //todo 拉黑token
        return R.ok();
    }

}