
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/


// 百度的操作
/*-------------------------------------------------------------------------------------------*/	
class index
{
	constructor(obj)
	{
		this.appid=obj?obj.appid:$ToolTparty.baidu.appid
		this.appsecret=obj?obj.appsecret:$ToolTparty.baidu.appsecret
		this.accessToken	
	}

	//得到基础AccessToken
	async getAccessToken(obj=$ToolTparty.baidu.lang)
	{ 
		if(this.accessToken)return this.accessToken
		
		let data= await $Sys.model
		(
			'/request/fetch','get',
			[
				'https://aip.baidubce.com/oauth/2.0/token',
				{
					'grant_type': 'client_credentials',
					'client_id': obj.appkey,
					'client_secret': obj.appsecret
				},
			]
		);

	

		if(!data.data.access_token)
		{
			console.error('[获取accessToken]\r\n',data.data)
			throw Error('[baidu]获取accessToken失败')
		}
                
		this.accessToken=data.data.access_token
		return this.accessToken
	}

	async like(text='',type=4)
	{   
		let gbk=$Sys.model('/tool/com/gbk','encode',[JSON.stringify({text:text,type:type})])
		let data= await $Sys.model
		(
			'/request/fetch','post',
			[
				{
					host: 'aip.baidubce.com',
					path: '/rpc/2.0/nlp/v2/comment_tag',
					method: 'POST',
					headers:{'Content-Type':'application/json'},
					data:gbk,
				},
				{access_token: await (this.getAccessToken())}
				,undefined,true		
			]
		);
		
		return data.data
	}

}
/*-------------------------------------------------------------------------------------------*/    
module.exports=index;