企微短剧业务系统

TestController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Log;
  4. use App\Models\AdPlacementLog;
  5. use App\Models\CustomerDetails;
  6. use App\Models\OfficialAccount;
  7. use App\Models\View\CustomerDetail;
  8. use App\RedisModel;
  9. use App\Service\AccessTokenService;
  10. use App\Service\ChatService;
  11. use App\Service\ContactService;
  12. use App\Service\CorpAuthService;
  13. use App\Service\CustomerService;
  14. use App\Service\DjOrderService;
  15. use App\Service\GroupChatJoinWayService;
  16. use App\Service\HttpService;
  17. use App\Service\MiniProgramService;
  18. use App\Service\PitcherService;
  19. use App\Service\RadarService;
  20. use App\Service\TokenService;
  21. use App\Support\EmailQueue;
  22. use App\Support\qyApi\QyCommon;
  23. use Illuminate\Http\Request;
  24. use App\Http\Controllers\Controller;
  25. use Illuminate\Support\Facades\Hash;
  26. use Illuminate\Support\Facades\Storage;
  27. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  28. class TestController extends Controller
  29. {
  30. /*
  31. * 获取分表
  32. * */
  33. public function getLabel($corpid)
  34. {
  35. $label = get_hash_code($corpid, 50);
  36. dd($label);
  37. }
  38. public function detectMobileBrand($userAgent) {
  39. // 定义常见品牌关键字
  40. $brands = [
  41. 'Apple' => 'iPhone|iPad|iPod',
  42. 'Samsung' => 'Samsung|SM-',
  43. 'Huawei' => 'Huawei|Honor',
  44. 'Xiaomi' => 'Xiaomi|Mi|Redmi',
  45. 'OPPO' => 'OPPO|CPH',
  46. 'Vivo' => 'Vivo',
  47. 'OnePlus' => 'OnePlus',
  48. 'Google' => 'Pixel',
  49. 'Sony' => 'Sony|Xperia',
  50. 'LG' => 'LG',
  51. 'Motorola' => 'Moto|Motorola',
  52. 'Nokia' => 'Nokia',
  53. 'Realme' => 'Realme',
  54. 'Meizu' => 'Meizu|MX|M9',
  55. 'ZTE' => 'ZTE',
  56. 'Lenovo' => 'Lenovo',
  57. 'Asus' => 'Asus|ZenFone',
  58. 'HTC' => 'HTC',
  59. 'BlackBerry' => 'BlackBerry|BB10',
  60. ];
  61. // 遍历品牌关键字匹配
  62. foreach ($brands as $brand => $pattern) {
  63. if (preg_match("/$pattern/i", $userAgent)) {
  64. return $brand;
  65. }
  66. }
  67. // 如果没有匹配到,返回未知
  68. return '';
  69. }
  70. /**
  71. * 测试员工搜索
  72. * */
  73. public function customer(Request $request)
  74. {
  75. try {
  76. $clickTotalRds = 'JuXing::AdClickTotal_';
  77. $promotionChannelId = $request->input('promotion_channel_id');
  78. $actionType = $request->input('action_type');
  79. $trackingId = $request->input('tracking_id');
  80. $accountId = $request->input('account_id');
  81. $campaignId = $request->input('campaign_id');
  82. $campaignName = $request->input('campaign_name');
  83. $adId = $request->input('ad_id');
  84. $creativeId = $request->input('creative_id');
  85. $idfaWithDashMd5 = $request->input('idfa_withdash_md5');
  86. $macUpperWithColon = $request->input('mac_upper_withcolon');
  87. $macUpperWithColonMd5 = $request->input('mac_upper_withcolon_md5');
  88. $macUpperNoColonMd5 = $request->input('mac_upper_nocolon_md5');
  89. $timestampMs = $request->input('timestamp_ms');
  90. $ip = $request->input('ip');
  91. $callbackUrl = $request->input('callback_url');
  92. $csite = $request->input('csite');
  93. $idfaSha1 = $request->input('idfa_sha1');
  94. $imeiMd5 = $request->input('imei_md5');
  95. $androiDidMd5 = $request->input('androidid_md5');
  96. $oaid = $request->input('oaid');
  97. $oaidMd5 = $request->input('oaid_md5');
  98. $imeiSha1 = $request->input('imei_sha1');
  99. $androididSha1 = $request->input('androidid_sha1');
  100. $ua = $request->input('ua');
  101. $brand = $this->detectMobileBrand($ua);
  102. $osCode = $request->input('os_code');
  103. $kuaiShouPhotoId = $request->input('kuaishou_photo_id');
  104. $isActivateReport = 0;
  105. if($actionType == 'impression') {
  106. $actionType = 1;
  107. } elseif($actionType == 'click') {
  108. Log::logInfo('数据回调成功', [
  109. 'data' => $request->all()
  110. ], 'JuXingCallBack');
  111. $total = RedisModel::incr($clickTotalRds . $adId);
  112. RedisModel::expire($clickTotalRds . $adId, 86400 * 30);
  113. EmailQueue::rPush('聚星回调数据收到', $actionType . $total, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  114. $actionType = 2;
  115. if($total % 2 != 0){ // 隔1传1
  116. if(!$brand) { // 非品牌机,查询上一个未上传的品牌机
  117. $callbackUrlUse = '';
  118. $clickUserWhichIsBrand = AdPlacementLog::select('callback_url', 'id')
  119. ->where('ad_id', $adId)->where('account_id', $accountId)
  120. ->where('action_type', 2)
  121. ->where('is_activate_report', 0)
  122. ->where('brand', '>', '')->orderBy('id', 'desc')
  123. ->first();
  124. Log::logInfo('非品牌机,查询上一个未上传的品牌机信息', [
  125. 'record' => $clickUserWhichIsBrand, 'ua' => $ua, 'total' => $total, 'account_id' => $accountId,
  126. 'ad_id' => $adId
  127. ], 'JuXingCallBack');
  128. if(!empty($clickUserWhichIsBrand)) {
  129. $callbackUrlUse = $clickUserWhichIsBrand->callback_url;
  130. AdPlacementLog::where('id', $clickUserWhichIsBrand->id)->update(['is_activate_report' => 1]);
  131. }
  132. } else {
  133. $callbackUrlUse = $callbackUrl;
  134. $isActivateReport = 1;
  135. }
  136. sleep(rand(3, 5));
  137. # 上传激活数据
  138. Log::logInfo('上传日志', [
  139. 'record' => $clickUserWhichIsBrand ?? $brand, 'ua' => $ua, 'total' => $total, 'account_id' => $accountId,
  140. 'ad_id' => $adId, 'url' => $callbackUrl, 'url_use' => $callbackUrlUse
  141. ], 'JuXingCallBack');
  142. $rst = HttpService::httpGet($callbackUrlUse . '&event_type=1&event_time=' . time() . rand(100, 999) );
  143. EmailQueue::rPush('聚星广告激活数据回传完成', $rst, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  144. }
  145. } else {
  146. EmailQueue::rPush('聚星回调数据收到', $actionType, ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  147. $actionType = 0;
  148. }
  149. AdPlacementLog::insert(['promotion_channel_id' => $promotionChannelId, 'action_type' => $actionType, 'tracking_id' => $trackingId, 'campaign_id' => $campaignId, 'campaign_name' => $campaignName, 'ad_id' => $adId, 'creative_id' => $creativeId, 'idfa_withdash_md5' => $idfaWithDashMd5, 'mac_upper_withcolon' => $macUpperWithColon, 'mac_upper_withcolon_md5' => $macUpperWithColonMd5, 'mac_upper_nocolon_md5' => $macUpperNoColonMd5, 'timestamp_ms' => $timestampMs, 'ip' => $ip, 'callback_url' => $callbackUrl, 'csite' => $csite, 'idfa_sha1' => $idfaSha1, 'imei_md5' => $imeiMd5, 'androidid_md5' => $androiDidMd5, 'oaid' => $oaid, 'oaid_md5' => $oaidMd5, 'imei_sha1' => $imeiSha1, 'androidid_sha1' => $androididSha1, 'ua' => $ua, 'os_code' => $osCode, 'account_id' => $accountId, 'kuaishou_photo_id' => $kuaiShouPhotoId, 'is_activate_report' => $isActivateReport, 'brand' => $brand]);
  150. } catch (\Exception $e) {
  151. Log::logError('处理回调数据过程出现异常', ['line' => $e->getLine(), 'msg' => $e->getTraceAsString()], 'JuXingCallBack');
  152. EmailQueue::rPush('处理回调数据过程出现异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  153. }
  154. // // 获取企微信息
  155. // $corpid = $request->input('corpid');
  156. // $permanentCode = $request->input('permanent_code');
  157. // $url = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info?suite_access_token=';
  158. // $component = config('qyWechat.suite_info');
  159. // $suiteId = $component['app_template_id'];
  160. // $suiteSecret = $component['app_template_secret'];
  161. // $suiteAccessToken = TokenService::getAuthToken($suiteId, $suiteSecret);
  162. // $url .= $suiteAccessToken;
  163. // var_dump($suiteAccessToken);
  164. // $params = [
  165. // 'auth_corpid' => $corpid,
  166. // 'permanent_code' => $permanentCode
  167. // ];
  168. // $response = HttpService::httpPost($url, json_encode($params), true);
  169. // var_dump($response);
  170. // // 获取应用的接口许可状态
  171. // $corpid = $request->input('corpid');
  172. // $component = config('qyWechat.suite_info');
  173. // $suiteId = $component['app_template_id'];
  174. // $suiteCorpid = $component['server_corpid'];
  175. // $suiteSecret = $component['server_provider_secret'];
  176. // $response = QyCommon::getAppLicenseInfo($corpid, $suiteId, $suiteCorpid, $suiteSecret);
  177. // var_dump($response);
  178. // $name = $request->input('name');
  179. // $avatar = $request->input('avatar');
  180. // $createtime = $request->input('createtime');
  181. //
  182. // $result = CustomerService::updateCustomerFirstAddTime($name, $avatar, $createtime);
  183. # 创建知识库分组
  184. // $response = QyCommon::addGroup('', '短剧问答库');
  185. // var_dump($response);
  186. # 添加知识库问答
  187. // $groupId = 'asezvKNwAACl_AjlEz0VWvCpE-M6k-yw';
  188. // $question = $request->input('question');
  189. // $similarQuestionList = $request->input('similar_question_list');
  190. // if(!is_array($similarQuestionList)) $similarQuestionList = explode(',', $similarQuestionList);
  191. // $similarQuestions = [];
  192. // foreach ($similarQuestionList as $item) {
  193. // $similarQuestions['items'][] = [
  194. // 'text' => [
  195. // 'content' => $item
  196. // ]
  197. // ];
  198. // }
  199. //
  200. // $answers = $request->input('answers');
  201. //
  202. // $response = QyCommon::addIntent('', $groupId, $question, $similarQuestions, $answers);
  203. // var_dump($response);
  204. // $code = $request->input('code');
  205. // $corpid = 'wpezvKNwAA9d7LlcuOOAhvlx5ikwJjHg';
  206. // $accessToken = AccessTokenService::getAccessToken($corpid);
  207. // $url = 'https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token='.$accessToken.'&js_code='.$code.'&grant_type=authorization_code';
  208. //
  209. // $response = HttpService::httpGet($url);
  210. //
  211. // return self::returnValue(json_decode($response, true));
  212. // $sysGroupId = $request->input('sys_group_id');
  213. // $adminId = $request->input('admin_id');
  214. // $isSystemAdmin = $request->input('is_system_admin');
  215. // # 获取可见的adq账号
  216. // $adqList = PitcherService::adqAccountListForUser($adminId, $sysGroupId, $isSystemAdmin);
  217. // # 获取可见的mp账号
  218. // $mpList = OfficialAccount::getAccountMpAppIdList($adminId, $sysGroupId, $isSystemAdmin);
  219. // var_dump($adqList);
  220. // var_dump($mpList);
  221. # 获取群活码配置信息
  222. // $corpid = $request->input('corpid');
  223. // $userId = $request->input('user_id');
  224. // $data = QyCommon::getUserActiveInfo($corpid, $userId, $this->suiteId, $this->suiteSecret);
  225. // var_dump($data);die;
  226. // # 获取群活码配置信息
  227. // $corpid = $request->input('corpid');
  228. // $configId = $request->input('config_id');
  229. // $qrcode = GroupChatJoinWayService::getChatGroupJoinWay($corpid, $configId);
  230. // var_dump($qrcode);
  231. # 生成二维码
  232. // $img = QrCode::size(200)->merge(public_path().'/qrcode.png',.3,true)->encoding('UTF-8')->generate('https://www.jb51.cc/laravel/2957806.html');
  233. // return $img;
  234. # 企微客服userId转openUserId
  235. // $corpid = $request->input('corpid');
  236. // $userId = $request->input('user_id');
  237. // $responseData = QyCommon::useridToOpenUserid($corpid, [$userId]);
  238. // var_dump($responseData);
  239. // # 获取累计充值人数
  240. // $accountData = array(['app_id' => 'wxc154cf1187af870c', 'start_date' => '2022-07-21', 'end_date' => '2022-07-21'], ['app_id' => 'wx303334360fe60305', 'start_date' => '2022-07-21', 'end_date' => '2022-07-21']);
  241. //
  242. // $data = DjOrderService::getChargeUserTotalUnique($accountData);
  243. //// # 获取累计充值人数
  244. //// $accountData = array(['app_id' => 'wxc154cf1187af870c', 'start_date' => '2022-07-21', 'end_date' => '2022-07-21'], ['app_id' => 'wx303334360fe60305', 'start_date' => '2022-07-21', 'end_date' => '2022-07-21']);
  245. ////
  246. //// $data = DjOrderService::getChargeUserTotalUnique($accountData);
  247. // var_dump($responseData);
  248. // $code = $request->input('code');
  249. // $corpid = $request->input('corpid');
  250. // # 根据code获取外部联系人ID
  251. // $responseData = QyCommon::getCustomerInfoByCode($corpid, $code);
  252. //
  253. // # 解析用户企微数据【1.企业成员 2.非企业成员】
  254. // $externalUserid = $responseData['external_userid'] ?? '';
  255. // $deviceId = $responseData['DeviceId'] ?? '';
  256. // $openid = $responseData['OpenId'] ?? '';
  257. // $userId = $responseData['UserId'] ?? '';
  258. // $errCode = $responseData['errcode'] ?? 9999;
  259. //
  260. // $queryParam = [
  261. // 'external_userid' => $externalUserid,
  262. // 'openid' => $openid,
  263. // 'user_id' => $userId
  264. // ];
  265. //
  266. // Log::logInfo('获取用户企微身份信息: ', [
  267. // 'corpid' => $corpid,
  268. // 'code' => $code,
  269. // 'response' => $responseData
  270. // ], 'Oauth2Authorize');
  271. //
  272. //
  273. // $appId = 'wxd16e96172ce05ddc';
  274. // $appSecret = 'fb9ac5755e38b7aebab28e4a5fd77a87';
  275. //
  276. // $urlLink = MiniProgramService::createLinkUrl($appId, $appSecret);
  277. // var_dump($urlLink);
  278. // # 检出scheme链接
  279. // $link = $urlLink['openlink'] ?? '';
  280. // return self::returnValue(['link' => $link]);
  281. // var_dump($urlLink);
  282. // $traceUri = RadarService::getTraceLink($radarId, $extra);
  283. // $corpid = 'wwc8d3e33f8e096671';
  284. // $search = array();
  285. // $page = 1;
  286. // $pageSize = 2;
  287. //
  288. // list($list, $count) = CustomerDetail::getUserInfo($corpid, $search, $page, $pageSize, 'createtime');
  289. // $requestUri = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list_v2?access_token=fzGvnmYVk-6Kk9G9Il_G2J6g-lhVDm6BWXDLXQUYXOP43Cxv5TTTCLgpGk-EuZ9WlzSlP3KGMj65TL08EJy7VJ0OgOJPIGZWEBxkFRBIZxngCs7SNbhBwnqHhZ5Krvzj8ae7xZ2nLQshws6GR8eKw_A0rdQLusY8Bh6uykZ0QHOEexpUxKhh4Dqt9CIfkthNEIN-uS_AbE_fzaItkdZMyQ';
  290. // $postData = [
  291. // 'chat_type' => 'single',
  292. // 'start_time' => 1646064000,
  293. // 'end_time' => 1648483200,
  294. // ];
  295. //
  296. // $response = HttpService::httpPost($requestUri, json_encode($postData));
  297. //
  298. // $requestUri = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=fzGvnmYVk-6Kk9G9Il_G2J6g-lhVDm6BWXDLXQUYXOP43Cxv5TTTCLgpGk-EuZ9WlzSlP3KGMj65TL08EJy7VJ0OgOJPIGZWEBxkFRBIZxngCs7SNbhBwnqHhZ5Krvzj8ae7xZ2nLQshws6GR8eKw_A0rdQLusY8Bh6uykZ0QHOEexpUxKhh4Dqt9CIfkthNEIN-uS_AbE_fzaItkdZMyQ';
  299. //
  300. // $postData = [
  301. // 'msgid' => 'msgezvKNwAAVpvDezw13yHf9_7tFr1KPQ'
  302. // ];
  303. // $response = HttpService::httpPost($requestUri, json_encode($postData));
  304. // var_dump($response);
  305. // $corpid = 'wwc8d3e33f8e096671';
  306. // $corpid = $request->input('corpid');
  307. // $userid = 'bc85094e2cfd56a8bd6cd726bd98b14b';
  308. // $response = ContactService::getContactUserDetail($corpid, $userid);
  309. // $response = CorpAuthService::getOpenCorpid($corpid, $this->suiteId, $this->suiteSecret);
  310. // var_dump($response);
  311. # 测试外部联系人搜索
  312. // $corpid = 'wpezvKNwAAAjABjCEaYNHR0wBzn1YDqQ';
  313. // $search = [
  314. // 'gender' => 0,
  315. // 'add_time_end' => '1649646180',
  316. // 'add_time_start' => '1645646180',
  317. // 'tag_screen_type' => 1,
  318. // 'tag_list' => [33,34],
  319. // 'exclude_tag_list' => [1],
  320. // ];
  321. // $fields = 'external_userid, user_id';
  322. // $sort = [
  323. // 'createtime' => 'DESC',
  324. // ];
  325. // list($externalUserList, $total) = CustomerDetails::getUserInfo(
  326. // $corpid, $search, $fields, 1, 200, $sort, 'external_userid'
  327. // );
  328. // var_dump($externalUserList);
  329. // $corpid = $request->input('corpid');
  330. // $token = $request->input('token');
  331. // $externalUserid = $request->input('external_userid');
  332. // # 测试代开发应用external_userid转第三方平台external_userid
  333. // $requestUri = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/to_service_external_userid?access_token=';
  334. // # 测试外部联系人openid转换
  335. //// $requestUri = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/convert_to_openid?access_token=';
  336. // $requestUri .= AccessTokenService::getAccessToken($corpid);
  337. // var_dump($requestUri);
  338. //
  339. // $params = array('external_userid' => $externalUserid);
  340. // var_dump($params);
  341. // $response = HttpService::httpPost($requestUri, json_encode($params));
  342. // dd($response);
  343. // $responseData = ChatService::syncChatMsg($corpid, $token, 10);
  344. // dd($responseData);
  345. }
  346. /**
  347. * 获取雷达专属链接
  348. * */
  349. public function traceLink(Request $request)
  350. {
  351. $validator = \Validator::make($request->all(),[
  352. 'radar_id' => 'required|int',
  353. 'corpid' => 'required|string',
  354. 'user_id' => 'required|string',
  355. ]);
  356. if ($validator->fails()) {
  357. return self::returnValue($validator->getMessageBag(), 1102);
  358. }
  359. $radarId = $request->input('radar_id');
  360. $params['rule_id'] = $request->input('rule_id');
  361. $params['corpid'] = $request->input('corpid');
  362. $params['user_id'] = $request->input('user_id');
  363. $params['channel'] = $request->input('channel');
  364. $params['state'] = $request->input('state', 'STATE');
  365. $errno = 0;
  366. $traceLink = RadarService::getTraceLink($radarId, $params, $errno);
  367. if($errno) return self::returnValue([], $errno);
  368. return self::returnValue($traceLink);
  369. }
  370. /**
  371. * 通过第三方主体unionid转换为第三方external_userid
  372. * */
  373. public function getExternalUserid(Request $request)
  374. {
  375. $validator = \Validator::make($request->all(),[
  376. 'unionid' => 'required|string',
  377. 'openid' => 'required|string',
  378. 'corpid' => 'required|string',
  379. ]);
  380. if ($validator->fails()) {
  381. return self::returnValue($validator->getMessageBag(), 1102);
  382. }
  383. $corpid = $request->input('corpid');
  384. $openid = $request->input('openid');
  385. $unionid = $request->input('unionid');
  386. # 获取access_token
  387. $accessToken = AccessTokenService::getAccessToken($corpid, 'Openid+unionId转换external_userid');
  388. $requestUri = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/unionid_to_external_userid?access_token='. $accessToken;
  389. $postData = [
  390. 'unionid' => $unionid,
  391. 'openid' => $openid
  392. ];
  393. $response = HttpService::httpPost($requestUri, json_encode($postData));
  394. var_dump($response);
  395. }
  396. }