AyrShareController.php 5.1 KB
<?php

namespace App\Http\Controllers\Bside\AyrShare;

use App\Enums\Common\Code;
use App\Helper\AyrShare as AyrShareHelper;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\AyrShare\AyrShareLogic;
use App\Http\Requests\Bside\AyrShare\AyrShareRequest;
use App\Models\AyrShare\AyrShare as AyrShareModel;

/**
 * @name:社交绑定
 */
class AyrShareController extends BaseController
{
    //生成名称前缀
    const TITLE = 'global_so_';
    /**
     * @name   :(社交列表)lists
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 16:06
     */
    public function lists(AyrShareModel $ayrShareModel,AyrShareLogic $ayrShareLogic){
        //授权配置列表
        $share_list = $ayrShareModel->platforms;
        $lists = $ayrShareModel->lists($this->map,$this->page,$this->row,'id',['id','name','title','profile_key','bind_platforms','operator_id','created_at','updated_at']);
        foreach ($lists['list'] as $k => $v){
            if(!empty($v['profile_key'])){
                $ayrShareHelper = new AyrShareHelper();
                $share_info = $ayrShareHelper->get_profiles_users($v['profile_key']);
                if(isset($share_info['activeSocialAccounts'])){
                    $str = json_encode($share_info['activeSocialAccounts']);
                    if($str != $v['bind_platforms']){
                        $ayrShareLogic->ayr_share_edit(['bind_platforms'=>$str],$v['id']);
                    }
                }else{
                    $ayrShareLogic->ayr_share_edit(['bind_platforms'=>''],$v['id']);
                }
            }
        }
        $lists['share_list'] = $share_list;
        $this->response('列表',Code::SUCCESS,$lists);
    }

    /**
     * @name   :(定时更新)save_account
     * @author :lyh
     * @method :post
     * @time   :2023/5/9 14:39
     */
    public function save_account(AyrShareLogic $ayrShareLogic){
        $this->request->validate([
            'id'=>['required'],
        ],[
            'id.required' => 'ID不能为空',
        ]);
        $info = $ayrShareLogic->ayr_share_info();
        $ayrShareHelper = new AyrShareHelper();
        $share_info = $ayrShareHelper->get_profiles_users($info['profile_key']);
        if(isset($share_info['activeSocialAccounts'])){
            $str = json_encode($share_info['activeSocialAccounts']);
            if($str != $info['bind_platforms']){
                $ayrShareLogic->ayr_share_edit(['bind_platforms'=>$str],$this->param['id']);
                $res = true;
            }else{
                $res = false;
            }
        }else{
            $ayrShareLogic->ayr_share_edit(['bind_platforms'=>''],$this->param['id']);
            $res = true;
        }
        $this->response('success',Code::SUCCESS,['is_true'=>$res]);
    }
    /**
     * @name   :(创建ayr_share账户)create_account
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 16:44
     */
    public function create_account(AyrShareRequest $ayrShareRequest,AyrShareLogic $ayrShareLogic){
        $ayrShareRequest->validated();
        $param = [
            'title'=>self::TITLE.$this->user['project_id'].':'.$this->param['name'],
        ];
        //发送请求注册社交用户
        $ayrShareHelper = new AyrShareHelper();
        $res = $ayrShareHelper->post_create_profiles($param);
        if($res['status'] == 'fail'){
            $this->response('同步绑定失败');
        }
        //执行数据库操作
        $ayrShareLogic->ayr_share_add($res);
        $this->response('success');
    }

    /**
     * @name   :(删除用户账号并同步ayr_share账号)edit_account
     * @author :lyh
     * @method :post
     * @time   :2023/5/6 10:11
     */
    public function del_account(AyrShareLogic $ayrShareLogic){
        $this->request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $ayrShareLogic->ayr_share_info();
        $data = [
//            'title'=>$info['title'],
            'profileKey'=>$info['profile_key']
        ];
        //发送请求删除社交用户
        $ayrShareHelper = new AyrShareHelper();
        $res = $ayrShareHelper->deleted_profiles($data);
        if($res['status'] == 'fail'){
            $this->response('同步删除失败');
        }
        $ayrShareLogic->ayr_share_del();
        $this->response('success');
    }

    /**
     * @name   :(授权绑定第三方平台,生成jwt令牌)ayr_share_bind
     * @author :lyh
     * @method :post
     * @time   :2023/5/6 10:24
     */
    public function bind_account(AyrShareLogic $ayrShareLogic){
        $this->request->validate([
            'id'=>['required']
        ],[
            'id.required' => 'ID不能为空'
        ]);
        $info = $ayrShareLogic->ayr_share_info();
        //发送请求注册社交用户
        $ayrShareHelper = new AyrShareHelper();
        $data = [
            'profileKey'=>$info['profile_key']
        ];
        $res = $ayrShareHelper->post_generate_jwt($data);
        if($res['status'] == 'fail'){
            $this->response($res['message'],Code::USER_ERROR);
        }
        $this->response('success',Code::SUCCESS,$res);
    }

}