123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/4/25
- * Time: 15:25
- */
- namespace App\Service;
- use App\Log;
- use App\RedisModel;
- class YouZiService
- {
- const BASE_URI = 'https://api.mpyouzi.com/';
- const LOGIN_URI = 'api/dp/session/login';
- const ACCOUNT_LIST_URI = 'api/dp/user/info';
- const ORDER_LIST_URI = 'api/dp/order/query';
- const BP_ORDER_LIST_URL = 'api/bp/order/query';
- const AD_SET_LIST_URI = 'api/dp/mp/ad/set/detail';
- const CAMPAIGN_LIST_URI = 'api/dp/playletActivity/query';
- const ACCESS_TOKEN_RDS_KEY = 'Novel:YouZiAccessToken';
- const AD_ACCOUNT_RELATION_RDS_KEY = 'Novel:YouZiAdAccountRelation';
- const PLATFORM_ID = 1;
- const ACCOUNT_SERVICE_TYPE = [2, 10]; // 2: 公众账号 10: 企微号
- const ACCOUNT_LIST = [
- [
- 'loginAccount' => 'huanxiao.zhang@kuxuan-inc.com',
- 'password' => 'VVrKdPqTSiMa8WaW',
- 'adminType' => 1
- ],
- [
- 'loginAccount' => 'chuanxiao.zhang@kuxuan-inc.com',
- 'password' => 'aRBtBFDIsbXlCEP1',
- 'adminType' => 1
- ],
- [
- 'loginAccount' => '941722557@qq.com',
- 'password' => 'HZjyx@0725',
- 'adminType' => 1
- ]
- ];
- /*
- * 调用接口取得凭证
- * */
- public static function getAccessToken($accountLabel=0)
- {
- $accessToken = RedisModel::get(self::ACCESS_TOKEN_RDS_KEY . '_' .$accountLabel);
- if(empty($accessToken)) {
- $accessToken = self::login($accountLabel);
- }
- return $accessToken;
- }
- /**
- * 获取登录凭证
- * @param
- * */
- public static function login($accountLabel, $retry = 0)
- {
- $requestUri = self::BASE_URI . self::LOGIN_URI;
- $accountInfo = YouZiService::ACCOUNT_LIST;
- $loginAccount = isset($accountInfo[$accountLabel]) ? $accountInfo[$accountLabel] : '';
- if(empty($loginAccount)) return '';
- # 获取响应数据
- $accessToken = '';
- $response = HttpService::httpPost($requestUri, json_encode($loginAccount), true);
- $responseData = json_decode($response, true);
- if(isset($responseData['code']) && $responseData['code'] == 0) {
- $accessToken = isset($responseData['data']['accessToken']) ? $responseData['data']['accessToken'] : '';
- if(empty($accessToken) && $retry <5) {
- $retry++;
- return self::login($accountLabel, $retry);
- }
- if(!empty($accessToken))
- RedisModel::set(self::ACCESS_TOKEN_RDS_KEY . '_' .$accountLabel, $accessToken);
- }
- if(empty($accessToken)) {
- Log::logError('接口调用凭证获取失败', [
- 'label' => $accountLabel,
- 'retry' => $retry
- ], 'YouZiAccessToken');
- }
- return $accessToken;
- }
- }
|