package com.platform.modules.foreign.controller;


import com.platform.common.utils.R;
import com.platform.modules.app.entity.UserEntity;
import com.platform.modules.app.service.UserService;
import com.platform.modules.app.service.UserTokenService;
import io.swagger.annotations.Api;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * APP登录授权
 */
@RestController
@RequestMapping("/foreign")
@Api("对外服务登录接口")
public class ForeignLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserTokenService userTokenService;

    /**
     * access_token获取
     */
    @GetMapping("/token")
    public R login(String appid, String secret){
        //校验
        if (StringUtils.isBlank(appid) || StringUtils.isBlank(secret)) {
            return R.error("账号信息不正确");
        }

        //用户验证
        UserEntity user = userService.queryByUsername(appid);
        if(user==null || user.getUserId()==null){
        	return R.error("账号不存在");
        }
        
        //账号不存在、密码错误
		if(!user.getPassword().equals(new Sha256Hash(secret, user.getSalt()).toHex())) {
			return R.error("账号或密码不正确");
		}
        // 生成token并保存到数据库
        R r = userTokenService.createToken(user.getUserId());

        return r;
    }
    
    public static void main(String[] args) {
//	  String p = new Sha256Hash("keenkeen", "undefined").toHex();
//	  String p1 = new Sha256Hash("TmIUEunyMhjZj18Q8UWt9OHGE6OUVvSH", "UtMsWx84Oi4JP9U").toHex();
//	  System.out.println(p);
    	String pushUrl="rtmp://aijinan.push.zhongkedongxin.com/21ZoxrLa/e06b5dcd69d248f6b3bd666c1ff8e7f1?auth_key=1741276800-0-0-76f9bbd93f7c7717d49643f004751f4d";
    	String endStr = pushUrl.substring(pushUrl.indexOf("=")+1, pushUrl.indexOf("-"));
    	Date end = new Date(Long.parseLong(endStr+"000"));
    	System.out.println(endStr);
	}

}
