什么是账号授权(用户)
用户授权是指开发者在开放平台申请的应用,我们用到的应用类型是网站应用,用户可授权该能力给开发者,开发者通过用户授权的合规方式获取到用户数据并提供相应服务,如账号管理,视频发布,数据分析,数据统计等。
(1)添加账号授权(抖yin。快手。小红书,B站,好看视频,西瓜,头条)
* 添加账号/刷新授权
*/
public function addAccountAction()
{
$type = $this->request->getStrParam('platform', 'bai_jia_hao');
$this->useLayout('dydqtshoppc-head.html');
switch ($type) {
case 'bai_jia_hao':
$this->displaySmarty('dydqtshoppc/account/addBjhAuth.html');
break;
}
}
##region 抖yin
/*
* 抖yin账号列表
*/
public function dyUserListAction()
{
$url = $this->accountLink('dou_yin');
$this->output['link_url'] = $url;
//应用类型输出
$this->outputOpenTypes('dou_yin');
$keyword_type = $this->request->getStrParam('keyword_type');
$keyword = $this->request->getStrParam('keyword');
$time_range = $this->request->getStrParam('time_range');
$this->output['time_range'] = $time_range;
$group_id = $this->request->getStrParam('group_id');
$store_id = $this->request->getStrParam('store_id');
$auth_status = $this->request->getIntParam('auth_status', 0);
$this->output['auth_status'] = $auth_status;
#$this->output['group_id'] = $group_id;
$where = [
['name' => 'da_s_id', 'oper' => '=', 'value' => $this->sid],
['name' => 'da_aa_id', 'oper' => '>', 'value' => 0],//企业号显示与否的判断
//['name' => 'da_account_role', 'oper' => '=', 'value' => 'normal'],
];
$group_model = new App_Model_Douyin_MysqlAccountGroupStorage();
$store_model = new App_Model_Shop_MysqlSaleStoreStorage();
if ($this->admin_role > 1) {
$role = $this->getAdminRole();
if ($role['role'] == 3) {//普通员工
$where[] = ['name' => 'da_admin_id', 'oper' => '=', 'value' => $this->admin_id];
} else {//部门管理
$where[] = ['name' => 'da_admin_id', 'oper' => 'in', 'value' => $role['list']];
}
(2)刷新账号授权到期时间:
接口说明
access_token 有效期说明
- 当 access_token 过期(过期时间 15 天)后,可以通过该接口使用 refresh_token(过期时间 30 天)进行刷新。刷新后获得一个有效期为15天的 access_token,但是 refresh_token 的有效期保持不变。
- 若 refresh_token 过期,获取 access_token 会报错(
error_code=10010
),此时需要重新引导用户授权。 - 用户可以在抖音-我-设置(右上角)-帐号与安全-授权管理 中取消对应用的授权,取消授权后原有 access_token 会立即失效。
- 开放平台会定期对用户授权进行检查,取消不合规的 access_token 授权。
代码开发展示
case 2 : //授权即将到期
$expire = $curr_time + 10 * 24 * 60 * 60;
$where[] = ['name' => 'da_auth_status', 'oper' => '=', 'value' => 0]; //正常授权
$where[] = ['name' => 'da_refresh_expire', 'oper' => '=', 'value' => 0];
$where[] = ['name' => 'da_access_expire', 'oper' => '<>', 'value' => 0];
$where[] = ['name' => 'da_access_expire', 'oper' => '<', 'value' => $expire];
break;
case 3 :
$where[] = ['name' => 'da_auth_status', 'oper' => '=', 'value' => 0]; //正常授权
$where[] = ['name' => 'da_refresh_expire', 'oper' => '=', 'value' => 0];
$where[] = ['name' => 'da_access_expire', 'oper' => '=', 'value' => 0];
break;
case 4 :
$where[] = ['name' => 'da_auth_status', 'oper' => '=', 'value' => 1]; //取消授权
break;
case 5 :
$where[] = ['name' => 'da_creator_status', 'oper' => '=', 'value' => 0]; //未授权创作者中心
break;
}
}
(3)授权账号信息批量导出
字段:账号类型,是否到期,部门管理等
开发代码
* 抖音绑定账号数据导出
*/
public function exportDyacctAction()
{
$account_model = new App_Model_Douyin_MysqlDyAccountStorage();
$keyword_type = $this->request->getStrParam('keyword_type');
$keyword = $this->request->getStrParam('keyword');
$time_range = $this->request->getStrParam('time_range');
$auth_status = $this->request->getIntParam('auth_status', 0);
$fieldsStr = $this->request->getStrParam('fields_str');
$fields = array_unique(array_filter(explode(',', trim($fieldsStr, ','))));
if (count($fields) == 0) {
$this->displayJsonError('至少选择一个属性项!');
}
$where = [
['name' => 'da_s_id', 'oper' => '=', 'value' => $this->sid],
//企业号显示与否的判断
['name' => 'da_aa_id', 'oper' => '>', 'value' => 0],
//['name' => 'da_account_role', 'oper' => '=', 'value' => 'normal'],
];
if ($this->admin_role > 1) {
$role = $this->getAdminRole();
if ($role['role'] == 3) {//普通员工
$where[] = ['name' => 'da_admin_id', 'oper' => '=', 'value' => $this->admin_id];
} else {//部门管理
$where[] = ['name' => 'da_admin_id', 'oper' => 'in', 'value' => $role['list']];
}
}
(4)账号数据删除,解绑
* 删除抖yin账号
*/
public function deleteDyAction()
{
$id = $this->request->getStrParam('id');
if (empty($id)) $this->displayJsonError('请求参数异常');
$updata = [
'da_is_delete' => 1,
'da_update_time' => time(),
];
$dy_account = new App_Model_Douyin_MysqlDyAccountStorage();
$result = $dy_account->getRowUpdateByIdSid($id, $this->sid, $updata);
$this->showAjaxResult($result, '解除授权');
}
/*
* 更新抖音授权到期时间
*/
public function updateDyAuthAction()
{
$daid = $this->request->getIntParam('daid');
$account_model = new App_Model_Douyin_MysqlDyAccountStorage();
$account = $account_model->getRowByIdSid($daid, $this->sid);
if (empty($account)) {
$this->displayJsonError('参数错误');
}
$client = new App_Plugin_Douyin_ClientPlugin($daid, 'team');
if (empty($client->refresh_token)) {
$this->displayJsonError('refresh_token已过期,无法刷新获取,请点击右侧重新授权');
}
if (empty($client->access_token)) {
$this->displayJsonError('access_token已过期,无法刷新获取,请点击右侧重新授权');
}
$this->displayJsonSuccess(null, true, '更新授权成功');
}
/*
* 更新西瓜头条授权到期时间
*/
public function updateXtAuthAction()
{
$daid = $this->request->getIntParam('daid');
$account_model = new App_Model_Shop_MysqlXiTouStorage();
$account = $account_model->getRowByIdSid($daid, $this->sid);
if (empty($account)) {
$this->displayJsonError('参数错误');
}
$client = new App_Plugin_Douyin_ClientPlugin($daid, $account['xa_platform']);
if (!$client->access_token) {
$updata = [
'xa_access_expire' => 0,
'xa_refresh_expire' => 0,
];
$account_model->updateById($updata, $daid);
$this->displayJsonError('refresh_token已过期,无法刷新获取,请点击右侧重新授权');
}
$this->displayJsonSuccess(null, true, '更新授权成功');
}
/**
* 抖yin视频发送日志
*/
public function dySendLogAction()
{
//面包屑
$breadcrumbs = [
['title' => '抖yin平台账号', 'link' => '/account/dyUserList'],
['title' => '抖yin视频发送记录', 'link' => '#'],
];
(5)发送私信消息给粉丝用户
/**
* 企业发私信给粉丝用户
*/
public function sendMsgtoFansAction()
{
$da_id = $this->request->getIntParam('da_id');
$u_openid = $this->request->getStrParam('u_openid');
$content = $this->request->getStrParam('content');
if (empty($content)) $this->displayJsonError('私信内容不能为空');
try {
$plugin = new App_Plugin_Douyin_ClientPlugin($da_id, 'team');
$res = $plugin->sendMsgToUser('', 'text', $u_openid, '', $content);
if ($res['errcode'] != 0) {
$this->displayJsonError($res['errmsg']);
}
} catch (\Exception $e) {
$this->displayJsonError($e->getMessage());
}
$this->displayJsonSuccess([], true, '私信发送成功');
}
/**
* 批量发送私信
*/
public function batchSendMsgtoFansAction()
{
$content = $this->request->getStrParam('content', '');
$da_id = $this->request->getIntParam('da_id');
$uopenids = $this->request->getArrParam('uopenids');
if (!is_array($uopenids) || count($uopenids) == 0) $this->displayJsonError('请选择要发信的用户');
if (empty($content)) $this->displayJsonError('私信内容不能为空');
try {
foreach ($uopenids as &$item) {
plum_open_backend('douyindqt', 'sendMsgToFans', ['da_id' => $da_id, 'u_openid' => $item, 'content' => $content]);
}
$this->displayJsonSuccess([], true, '私信发送成功');
} catch (\Exception $e) {
$this->displayJsonError($e->getMessage());
}
}
总结:
接入方式:
- OpenAPI:在接入授权SDK基础上,通过服务端请求调用具体的开放能力(Scope)
- Web:在抖yin端外上线的PC端网站应用,通过URL拼接完成授权调用