企微短剧业务系统

YouZiService.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/4/25
  6. * Time: 15:25
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\RedisModel;
  11. class YouZiService
  12. {
  13. const BASE_URI = 'https://api.mpyouzi.com/';
  14. const LOGIN_URI = 'api/dp/session/login';
  15. const ACCOUNT_LIST_URI = 'api/dp/user/info';
  16. const ORDER_LIST_URI = 'api/dp/order/query';
  17. const BP_ORDER_LIST_URL = 'api/bp/order/query';
  18. const AD_SET_LIST_URI = 'api/dp/mp/ad/set/detail';
  19. const CAMPAIGN_LIST_URI = 'api/dp/playletActivity/query';
  20. const ACCESS_TOKEN_RDS_KEY = 'Novel:YouZiAccessToken';
  21. const AD_ACCOUNT_RELATION_RDS_KEY = 'Novel:YouZiAdAccountRelation';
  22. const PLATFORM_ID = 1;
  23. const ACCOUNT_SERVICE_TYPE = [2, 10]; // 2: 公众账号 10: 企微号
  24. const ACCOUNT_LIST = [
  25. [
  26. 'loginAccount' => 'huanxiao.zhang@kuxuan-inc.com',
  27. 'password' => 'VVrKdPqTSiMa8WaW',
  28. 'adminType' => 1
  29. ],
  30. [
  31. 'loginAccount' => 'chuanxiao.zhang@kuxuan-inc.com',
  32. 'password' => 'aRBtBFDIsbXlCEP1',
  33. 'adminType' => 1
  34. ],
  35. [
  36. 'loginAccount' => '941722557@qq.com',
  37. 'password' => 'HZjyx@0725',
  38. 'adminType' => 1
  39. ]
  40. ];
  41. /*
  42. * 调用接口取得凭证
  43. * */
  44. public static function getAccessToken($accountLabel=0)
  45. {
  46. $accessToken = RedisModel::get(self::ACCESS_TOKEN_RDS_KEY . '_' .$accountLabel);
  47. if(empty($accessToken)) {
  48. $accessToken = self::login($accountLabel);
  49. }
  50. return $accessToken;
  51. }
  52. /**
  53. * 获取登录凭证
  54. * @param
  55. * */
  56. public static function login($accountLabel, $retry = 0)
  57. {
  58. $requestUri = self::BASE_URI . self::LOGIN_URI;
  59. $accountInfo = YouZiService::ACCOUNT_LIST;
  60. $loginAccount = isset($accountInfo[$accountLabel]) ? $accountInfo[$accountLabel] : '';
  61. if(empty($loginAccount)) return '';
  62. # 获取响应数据
  63. $accessToken = '';
  64. $response = HttpService::httpPost($requestUri, json_encode($loginAccount), true);
  65. $responseData = json_decode($response, true);
  66. if(isset($responseData['code']) && $responseData['code'] == 0) {
  67. $accessToken = isset($responseData['data']['accessToken']) ? $responseData['data']['accessToken'] : '';
  68. if(empty($accessToken) && $retry <5) {
  69. $retry++;
  70. return self::login($accountLabel, $retry);
  71. }
  72. if(!empty($accessToken))
  73. RedisModel::set(self::ACCESS_TOKEN_RDS_KEY . '_' .$accountLabel, $accessToken);
  74. }
  75. if(empty($accessToken)) {
  76. Log::logError('接口调用凭证获取失败', [
  77. 'label' => $accountLabel,
  78. 'retry' => $retry
  79. ], 'YouZiAccessToken');
  80. }
  81. return $accessToken;
  82. }
  83. }