123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- <?php
- namespace App\Modules\OfficialAccount\Services;
- use App\Libs\SMS;
- use App\Libs\AliSMS;
- use App\Modules\OfficialAccount\Models\OfficialAccount;
- use App\Modules\OfficialAccount\Models\OfficialAuthUrl;
- use App\Modules\OfficialAccount\Models\ForbiddenOfficialAccountRecord;
- use App\Modules\User\Services\UserService;
- use App\Modules\Channel\Services\ChannelService;
- use App\Modules\Channel\Models\Channel;
- use DB;
- use GuzzleHttp\Client;
- use Redis;
- class OfficialAccountService
- {
- public $secret_key = 'zhuishuyun#_2017';
-
-
- static function officialAccountByAppid($officialAccountPrams)
- {
- try {
- \Log::info('==========================通过appid获取公众号' . $officialAccountPrams['appid']);
- $officialMsg = Redis::hget('official_account_info:app_id:' . $officialAccountPrams['appid'], 'official_account_info');
- if (!$officialMsg) {
- $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);
- if (!empty($official_accounts)) {
- $officialAccountArray = $official_accounts->toArray();
- Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
- }
- $official_accounts['todayForceSubscribeUsers'] = 0;
- $official_accounts['allForceSubscribeUsers'] = 0;
- return $official_accounts;
- } else {
- $official_object = json_decode($officialMsg);
- return $official_object;
- }
- } catch (\Exception $e) {
- \Log::info('==========================通过appid获取公众号报错');
- \Log::info($e->getMessage());
- }
- }
-
- static function officialAccountById($officialAccountPrams)
- {
- $official_accounts = OfficialAccount::officialAccountById($officialAccountPrams['id']);
- return $official_accounts;
- }
-
- static function officialAuthAccounts($auth, $is_enabled)
- {
- $official_accounts = OfficialAccount::officialAuthAccounts($auth, $is_enabled);
- return $official_accounts;
- }
-
-
- static function getOneOfficialAuthAccounts($distribution_channel_id,$auth, $is_enabled)
- {
- $official_accounts = OfficialAccount::getOneOfficialAuthAccounts($distribution_channel_id,$auth, $is_enabled);
- return $official_accounts;
- }
-
-
-
-
- static function officialAuthSubscribeAccounts($auth, $is_enabled)
- {
-
- $official_accounts = OfficialAccount::officialAuthSubscribeAccounts($auth, $is_enabled);
-
- return $official_accounts;
- }
-
-
- static function canUseOfficialAccountByChannelId($officialAccountPrams)
- {
- $is_auth = 1;
- $is_enabled = 1;
- $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($officialAccountPrams['distribution_channel_id'], $is_auth, $is_enabled);
-
-
-
-
-
- \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----official_accounts');
- for ($i = 0; $i < count($official_accounts); $i++) {
-
-
- $total_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], 'total');
- $total_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:' . $official_accounts[$i]['appid'], 'total');
- $allForceSubscribeUsers = $total_fans_add_nums - $total_fans_cancel_nums;
-
- \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----allForceSubscribeUsers' . $allForceSubscribeUsers);
-
- $day_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], date("Y-m-d"));
-
- $todayForceSubscribeUsers = $day_fans_add_nums;
-
- \Log::info('wechat_log_info:canUseOfficialAccountByChannelId-----todayForceSubscribeUsers' . $todayForceSubscribeUsers);
- if (($allForceSubscribeUsers >= $official_accounts[$i]['subscribe_top_num']) || ($todayForceSubscribeUsers >= $official_accounts[$i]['subscribe_day_maximum'])) {
-
- continue;
- } else {
- $official_accounts[$i]['todayForceSubscribeUsers'] = $todayForceSubscribeUsers;
- $official_accounts[$i]['allForceSubscribeUsers'] = $allForceSubscribeUsers;
- return $official_accounts[$i];
- }
- }
- }
-
- static function officialAuthAccountBydistributionChannelId($officialAccountPrams)
- {
- $is_auth = 1;
- $is_enabled = 1;
- $official_accounts = OfficialAccount::officialAuthAccountBydistributionChannelId($officialAccountPrams['distribution_channel_id'], $is_auth, $is_enabled);
-
- for ($i = 0; $i < count($official_accounts); $i++) {
-
- $official_accounts[$i]['todayForceSubscribeUsers'] = 0;
- $official_accounts[$i]['allForceSubscribeUsers'] = 0;
- }
- return $official_accounts;
- }
-
- static function allOfficialAccountBydistributionChannelId($officialAccountPrams)
- {
- $is_enabled = 1;
-
- $official_accounts = OfficialAccount::allOfficialAccountBydistributionChannelIdAndGetForceType($officialAccountPrams['distribution_channel_id'], $is_enabled);
-
- for ($i = 0; $i < count($official_accounts); $i++) {
-
- $day_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], date("Y-m-d"));
-
- $todayForceSubscribeUsers = $day_fans_add_nums;
- $official_accounts[$i]['todayForceSubscribeUsers'] = $todayForceSubscribeUsers;
-
- $total_fans_add_nums = (int)Redis::hget('day_appid_fans_nums_appid:add:' . $official_accounts[$i]['appid'], 'total');
- $total_fans_cancel_nums = (int)Redis::hget('day_appid_fans_nums_appid:cancel:' . $official_accounts[$i]['appid'], 'total');
- $allForceSubscribeUsers = $total_fans_add_nums - $total_fans_cancel_nums;
- $official_accounts[$i]['allForceSubscribeUsers'] = $allForceSubscribeUsers;
-
- }
- return $official_accounts;
- }
-
- static function officialAccountCountByChannelId($officialAccountPrams)
- {
- $official_accounts = OfficialAccount::officialAccountCountByChannelId($officialAccountPrams['distribution_channel_id']);
- return $official_accounts;
- }
-
- static function authOfficialAccount($officialAccountPrams)
- {
- try {
- $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);
- if (empty($official_accounts)) {
- $officialAccount = OfficialAccount::create($officialAccountPrams);
- $officialAccountArray = $officialAccount->toArray();
- Redis::hset('official_account_info:app_id:' . $officialAccountPrams['appid'], 'official_account_info', json_encode($officialAccountArray));
- return 1;
- } else {
- if (!empty($officialAccountPrams['distribution_channel_id'])) {
- $official_accounts['distribution_channel_id'] = $officialAccountPrams['distribution_channel_id'];
- }
- if (!empty($officialAccountPrams['appsecret'])) {
- $official_accounts['appsecret'] = $officialAccountPrams['appsecret'];
- }
- if (!empty($officialAccountPrams['alias'])) {
- $official_accounts['alias'] = $officialAccountPrams['alias'];
- }
- if (!empty($officialAccountPrams['name'])) {
- $official_accounts['name'] = $officialAccountPrams['name'];
- }
- if (!empty($officialAccountPrams['nickname'])) {
- $official_accounts['nickname'] = $officialAccountPrams['nickname'];
- }
- if (!empty($officialAccountPrams['head_img'])) {
- $official_accounts['head_img'] = $officialAccountPrams['head_img'];
- }
- if (!empty($officialAccountPrams['qrcode_url'])) {
- $official_accounts['qrcode_url'] = $officialAccountPrams['qrcode_url'];
- }
- if (!empty($officialAccountPrams['principal_name'])) {
- $official_accounts['principal_name'] = $officialAccountPrams['principal_name'];
- }
- if (!empty($officialAccountPrams['service_type_info'])) {
- $official_accounts['service_type_info'] = $officialAccountPrams['service_type_info'];
- }
- if (!empty($officialAccountPrams['func_info'])) {
- $official_accounts['func_info'] = $officialAccountPrams['func_info'];
- }
- if (!empty($officialAccountPrams['authorizer_refresh_token'])) {
- $official_accounts['authorizer_refresh_token'] = $officialAccountPrams['authorizer_refresh_token'];
- }
- if (!empty($officialAccountPrams['official_account_type'])) {
- $official_accounts['official_account_type'] = $officialAccountPrams['official_account_type'];
- }
- if (!empty($officialAccountPrams['verify_txt'])) {
- $official_accounts['verify_txt'] = $officialAccountPrams['verify_txt'];
- }
- $official_accounts['is_auth'] = 1;
- $official_accounts->save();
- $officialAccountArray = $official_accounts->toArray();
- Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
- return 1;
- }
- } catch (\Exception $e) {
-
- return 0;
- }
- }
-
- static function updateOfficialAccount($officialAccountPrams)
- {
- try {
- $official_accounts = OfficialAccount::officialAccountByAppidAndChannelId($officialAccountPrams['appid'], $officialAccountPrams['distribution_channel_id']);
-
- if (empty($official_accounts)) {
- return 2;
- } else {
- $official_accounts['appsecret'] = $officialAccountPrams['appsecret'];
- $official_accounts['subscribe_top_num'] = $officialAccountPrams['subscribe_top_num'];
- $official_accounts['subscribe_day_maximum'] = $officialAccountPrams['subscribe_day_maximum'];
- $official_accounts->save();
- $officialAccountArray = $official_accounts->toArray();
- Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
- return 1;
- }
- } catch (\Exception $e) {
-
- return 0;
- }
- }
-
- static function updateOfficialAccountTopNum($officialAccountPrams)
- {
- try {
- $official_accounts = OfficialAccount::officialAccountById($officialAccountPrams['id']);
-
- if (empty($official_accounts)) {
- return 2;
- } else {
-
- $official_accounts['subscribe_top_num'] = $officialAccountPrams['subscribe_top_num'];
- $official_accounts['subscribe_day_maximum'] = $officialAccountPrams['subscribe_day_maximum'];
- $official_accounts['sort_no'] = $officialAccountPrams['sort_no'];
- $official_accounts->save();
- $officialAccountArray = $official_accounts->toArray();
- Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
- return 1;
- }
- } catch (\Exception $e) {
- \Log("updateOfficialAccountTopNum_ept:" . $e->getMessage());
- return 0;
- }
- }
-
- static function cancelAuthOfficialAccount($officialAccountPrams)
- {
- try {
-
- $official_accounts = OfficialAccount::officialAccountByAppid($officialAccountPrams['appid']);
- if (empty($official_accounts)) {
- return 2;
- } else {
- $official_accounts['is_auth'] = 0;
- $official_accounts['cancel_auth_time'] = date("Y-m-d H:i:s");
- $official_accounts->save();
- $officialAccountArray = $official_accounts->toArray();
- Redis::hset('official_account_info:app_id:' . $official_accounts['appid'], 'official_account_info', json_encode($officialAccountArray));
- return 1;
- }
- } catch (\Exception $e) {
-
- return 0;
- }
- }
-
- static function officialAccountAuthUrl($officialAccountPrams)
- {
- try {
- $officialAuthUrl = OfficialAuthUrl::officialAuthUrl();
- $base_url = $officialAuthUrl['base_url'];
- $official_account_type = $officialAuthUrl['official_account_type'];
- $group_nick = $officialAuthUrl['group_nick'];
- $redirect_url = $officialAuthUrl['redirect_url'];
- $timestamp = time();
- $distribution_channel_id = $officialAccountPrams['distribution_channel_id'];
- $params = compact('official_account_type', 'group_nick', 'redirect_url', 'timestamp', 'distribution_channel_id');
- $params['sign'] = get_sign($params);
- return $base_url . http_build_query($params);
- } catch (\Exception $e) {
-
- }
- }
-
-
- static function saveOfficialWechatUser($officialWechatUserPrams)
- {
- try {
- UserService::addUser($officialWechatUserPrams);
- return 1;
- } catch (\Exception $e) {
-
- return 0;
- }
- }
-
- static function getOfficialWechatUser($officialWechatUserPrams)
- {
- return UserService::getById($officialWechatUserPrams['id']);
- }
-
- static function getTrusteeshipOfficialAccounts($params=[])
- {
- return OfficialAccount::getTrusteeshipOfficialAccounts($params);
- }
-
- static function getTrusteeshipOfficialAccountsWithParams($params=[])
- {
- return OfficialAccount::getTrusteeshipOfficialAccountsWithParams($params);
- }
-
- static function check_gzh_ban()
- {
- $officail_accounts = OfficialAccountService::officialAuthAccounts(1, 1);
- foreach ($officail_accounts as $key=>$officail_account) {
- try {
- $test = false;
-
- if($test && $officail_account->appid != 'wxdbc486f1b4f6a8c3') {
- continue;
- }
- $timestamp = time();
- $params = array('gzh_app_id' => $officail_account->appid, "timestamp" => $timestamp);
- $sign = get_sign($params);
- $phone_arr = [
- '15858178353',
- '13858057394',
- '15868100210',
- '15088790066',
- '18668029091',
- '15268462210',
- '13575592290',
- '18657562939',
- '13758229484',
- '13735561774',
- '18668420256',
- '15869177764'
- ];
-
-
- $account_priv = ChannelService::check_channel_account_priv($officail_account->distribution_channel_id,'gzh_forbidden_send_phone');
- $add_phones = isset($account_priv->content)?$account_priv->content:'';
- if(!empty($add_phones)){
- $add_phone_arr = explode(',',$add_phones);
- $phone_arr = array_merge($phone_arr,$add_phone_arr);
- }
-
- $resJson = [];
- if($test){
- \Log::info('test_phone_send:'.$key);
- $phone_arr = ['18668420256'];
- $resJson = ['code'=>1,'data'=>0];
- if($key == 1){
- \Log::info('test_phone_send_end:'.$key);
- break;
- }
- }else{
- $resJson = self::getUserClient()->request("GET", "check_gzh_ban",
- ['query' => ['gzh_app_id' => $officail_account->appid, 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 10]
- )->getBody()->getContents();
- $resJson = object_to_array(json_decode($resJson));
- }
- $redis_key = 'check_gzh_ban_altert:appid:' . $officail_account->appid;
-
- if ($resJson['code'] == 1 && $resJson['data'] == 0) {
- $redis_val = Redis::get($redis_key);
-
- if ($redis_val) {
- continue;
- }
-
-
- $distribution_channel_id = $officail_account->distribution_channel_id;
- $distribution_channel_info = Channel::get_distribution_channel_info($distribution_channel_id);
- $company_name = isset($distribution_channel_info->company_name)?$distribution_channel_info->company_name:'';
- $business_name = isset($distribution_channel_info->person_in_charge_name)?$distribution_channel_info->person_in_charge_name:'';
- $company_id = isset($distribution_channel_info->company_id)?$distribution_channel_info->company_id:'';
- foreach ($phone_arr as $phone) {
-
- $param = array('official_account_name'=> $officail_account->nickname,'business_name'=>$business_name,'company_name'=>$company_name);
- $res = null;
- if(!$test){
- \Log::info('check_gzh_ban_altert_send:appid:'.$officail_account->appid.' phone:'.$phone.' param:'.json_encode($param,JSON_UNESCAPED_UNICODE));
- $res = AliSMS::send($phone,'forbidden_remind',$param);
- }
- }
-
- Redis::set($redis_key, date('Y-m-d H:i:s'));
-
- DB::table('official_accounts')->where(['appid' => $officail_account->appid])->update(['subscribe_day_maximum' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
-
- $forbidden_official_account_record = [
- 'distribution_channel_id'=>$distribution_channel_id,
- 'appid'=>$officail_account->appid,
- 'nickname'=>$officail_account->nickname,
- 'company_id'=>$company_id,
- 'company_name'=>$company_name,
- 'business_name'=>$business_name,
- 'forbbidden_time'=>date('Y-m-d H:i:s'),
- 'unforbidden_time'=>null,
- 'date'=>date('Y-m-d'),
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s'),
- ];
-
- ForbiddenOfficialAccountRecord::save_or_update($forbidden_official_account_record);
-
- } else {
-
- Redis::del($redis_key);
- }
- } catch (\Exception $e) {
-
- }
- }
- return 1;
-
- }
-
- static function get_short_url($distribution_channel_id,$url)
- {
-
- $result = ['code'=>0,'url'=>'','msg'=>''];
-
- $officail_account = OfficialAccountService::getOneOfficialAuthAccounts($distribution_channel_id,1, 1);
- $gzh_app_id = isset($officail_account->appid)?$officail_account->appid:env('WECHAT_AUTH_APPID');
- \Log::info('get_short_url_start:'.$distribution_channel_id.' $gzh_app_id:'.$gzh_app_id.' url:'.$url);
-
- $short_url = '';
- try {
- $url = urlencode($url);
- $timestamp = time();
- $params = array('gzh_app_id' => $gzh_app_id,'url'=>$url, "timestamp" => $timestamp);
- $sign = get_sign($params);
-
- $resJson = self::getUserClient()->request("GET", "get_short_url",
- ['query' => ['gzh_app_id' => $gzh_app_id,'url'=>$url, 'timestamp' => $timestamp, 'sign' => $sign], 'connect_timeout' => 10]
- )->getBody()->getContents();
- $resJson = object_to_array(json_decode($resJson));
- \Log::info('get_short_url_end,appid:'.$gzh_app_id);
- \Log::info($resJson);
- $short_url = isset($resJson['data'])?$resJson['data']:'';
- $msg = isset($resJson['msg'])?$resJson['msg']:'';
- if(!empty($short_url)){
- $result['code'] = 1;
- $result['url'] = $short_url;
- }else{
- $result['msg'] = $msg;
- }
-
- } catch (\Exception $e) {
- \Log::info($e->getMessage());
- $result['msg'] = '短链接转化异常,请稍后再试!';
- }
- \Log::info('get_short_url_end:'.json_encode($result));
- return $result;
- }
- static public function getUserClient()
- {
- return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
- }
-
- static public function getAuthClient(){
- return new Client(['base_uri' => env('AUTH_API_BASE_URI')]);
- }
-
- static public function get_account_appids($account){
- return OfficialAccount::get_account_appids($account);
- }
-
- static public function update_official_account_menu($appid){
- try {
- $timestamp = time();
- $params = array("set_type"=>"menu","authorizer_appid"=>$appid,"timestamp"=>$timestamp);
- $sign = get_sign($params);
-
- \Log::info('update_encrypt_menus_start,appid:'.$appid);
-
- $dataJson = self::getAuthClient()->request("GET","auto_set_menu_and_template_test",
- ['query'=>["set_type"=>"menu",'authorizer_appid'=>$appid,'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 10]
- )->getBody()->getContents();
-
- \Log::info('update_official_account_menu_end,appid:'.$appid.' res:'.json_encode($dataJson));
- }
- catch( \Exception $e){
- \Log::info('update_official_account_menu_ept:'.$appid.' msg:'.$e->getMessage());
- }
- }
-
- static public function getBusinessChannelData($channels, $distribution_channel_name = '', $official_account_name='',$isAll = false)
- {
- return OfficialAccount::getBusinessChannelData($channels, $distribution_channel_name, $official_account_name,$isAll);
- }
- }
|