WeChatToolsController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Http\Controllers\Manage\OfficialAccount;
  3. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  4. use App\Modules\OfficialAccount\Models\OfficialAccount;
  5. use App\Modules\OfficialAccount\Models\UntieOfficialAccount;
  6. use App\Http\Controllers\Channel\BaseController;
  7. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  8. use App\Http\Controllers\Manage\OfficialAccount\Transformers\OfficialAccountTransformer;
  9. use Illuminate\Http\Request;
  10. use GuzzleHttp\Client;
  11. use Redis;
  12. /**
  13. * 微信工具修改类
  14. * @author zhoulingjie
  15. *
  16. */
  17. class WeChatToolsController extends BaseController
  18. {
  19. /**
  20. * @apiVersion 1.0.0
  21. * @api {GET} OfficialAccount/add_public_templates 根据公共模板id添加模板
  22. * @apiGroup OfficialAccount
  23. * @apiName add_public_templates
  24. * @apiParam {String} common_template_id 公共模板id.
  25. */
  26. function add_public_templates(Request $request){
  27. $common_template_id = $request->has('common_template_id') ? $request->input('common_template_id') : '';
  28. $test_appid = $request->has('test_appid') ? $request->input('test_appid') : '';
  29. \Log::info('add_public_templates:'.$common_template_id);
  30. if(empty($common_template_id)) {
  31. return response()->error("PARAM_EMPTY");
  32. }
  33. $officail_accounts = OfficialAccountService::officialAuthAccounts(1,1);
  34. // \Log::info($officail_accounts);
  35. foreach ($officail_accounts as $officail_account){
  36. try {
  37. // if($officail_account->appid != 'wxdbc486f1b4f6a8c3') {
  38. // \Log::info('not_in:'.$officail_account->appid);
  39. // continue;
  40. // }
  41. $timestamp = time();
  42. $params = array("common_template_id"=>$common_template_id,'gzh_app_id'=>$officail_account->appid,"timestamp"=>$timestamp);
  43. $sign = get_sign($params);
  44. \Log::info('add_public_template_start,appid:'.$officail_account->appid);
  45. $mediaCustomerJson = $this->getUserClient()->request("GET","add_public_template",
  46. ['query'=>['common_template_id'=>$common_template_id,'gzh_app_id'=>$officail_account->appid,'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 10]
  47. )->getBody()->getContents();
  48. //\Log::info($mediaCustomerJson);
  49. \Log::info('add_public_template_end,appid:'.$officail_account->appid.' res:'.json_encode($mediaCustomerJson));
  50. } catch (\Exception $e) {
  51. \Log::info($e->getMessage());
  52. }
  53. }
  54. return response()->success(['status'=>'1']);
  55. }
  56. /**
  57. * @apiVersion 1.0.0
  58. * @api {GET} OfficialAccount/add_public_templates 根据公共模板id添加模板
  59. * @apiGroup OfficialAccount
  60. * @apiName add_public_templates
  61. * @apiParam {String} common_template_id 公共模板id.
  62. */
  63. public function check_official_account_templates(){
  64. $officail_accounts = OfficialAccountService::officialAuthAccounts(1,1);
  65. foreach ($officail_accounts as $officail_account){
  66. try {
  67. // if($officail_account->appid != 'wxdbc486f1b4f6a8c3') {
  68. // \Log::info('not_in:'.$officail_account->appid);
  69. // continue;
  70. // }
  71. $timestamp = time();
  72. $params = array("gzh_app_id"=>$officail_account->appid,"timestamp"=>$timestamp);
  73. $sign = get_sign($params);
  74. \Log::info('check_official_account_templates_start,appid:'.$officail_account->appid);
  75. $dataJson = $this->getUserClient()->request("GET","check_official_account_templates",
  76. ['query'=>['gzh_app_id'=>$officail_account->appid,'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 10]
  77. )->getBody()->getContents();
  78. //\Log::info($dataJson);
  79. \Log::info('check_official_account_templates_end,appid:'.$officail_account->appid.' res:'.json_encode($dataJson));
  80. }
  81. catch( \Exception $e){
  82. \Log::info($e->getMessage());
  83. }
  84. }
  85. return response()->success(['status'=>'1']);
  86. }
  87. /**
  88. * @apiVersion 1.0.0
  89. * @api {GET} OfficialAccount/update_encrypt_menus 更新所有菜单->改为加密
  90. * @apiGroup OfficialAccount
  91. * @apiName update_encrypt_menus
  92. */
  93. public function update_encrypt_menus(){
  94. $officail_accounts = OfficialAccountService::officialAuthSubscribeAccounts(1,1);
  95. foreach ($officail_accounts as $officail_account){
  96. try {
  97. // if($officail_account->appid != 'wxdbc486f1b4f6a8c3') {
  98. // \Log::info('not_in:'.$officail_account->appid);
  99. // continue;
  100. // }
  101. $timestamp = time();
  102. $params = array("set_type"=>"menu","authorizer_appid"=>$officail_account->appid,"timestamp"=>$timestamp);
  103. $sign = get_sign($params);
  104. // http://zydy/oauth/auto_set_menu_and_template_test?set_type=menu&authorizer_appid=wxdbc486f1b4f6a8c3&timestamp=1511509543&sign=98dccc7b29e3c05e4b4367f488ff1966
  105. \Log::info('update_encrypt_menus_start,appid:'.$officail_account->appid);
  106. $dataJson = $this->getAuthClient()->request("GET","auto_set_menu_and_template_test",
  107. ['query'=>["set_type"=>"menu",'authorizer_appid'=>$officail_account->appid,'timestamp'=>$timestamp,'sign'=>$sign],'connect_timeout' => 10]
  108. )->getBody()->getContents();
  109. //\Log::info($dataJson);
  110. \Log::info('update_encrypt_menus_end,appid:'.$officail_account->appid.' res:'.json_encode($dataJson));
  111. }
  112. catch( \Exception $e){
  113. \Log::info($e->getMessage());
  114. }
  115. }
  116. return response()->success(['status'=>'1']);
  117. }
  118. /**
  119. * @apiVersion 1.0.0
  120. * @api {GET} OfficialAccount/untie_distribution_official_account 解绑公众号
  121. * @apiGroup OfficialAccount
  122. * @apiName untie_distribution_official_account
  123. * @apiParam {String} appid 渠道id.
  124. */
  125. public function untie_distribution_official_account(Request $request){
  126. $appid = $request->has('appid') ? $request->input('appid') : '';
  127. $force_untie = $request->has('force_untie') ? $request->input('force_untie') : '0';
  128. \Log::info('untie_distribution_official_account_start:'.$appid);
  129. if(empty($appid)) {
  130. return response()->error("PARAM_EMPTY");
  131. }
  132. // 找到公众号
  133. $official_account = OfficialAccount::officialAccountArrByAppid($appid);
  134. if(empty($official_account)) {
  135. return response()->error("NOT_FOUND_OFFICIAL_ACCOUNT");
  136. }
  137. // 判断公众号的注册用户数,大于10个默认是老号不给删除,否则用户数据会异常
  138. $force_subscribe_count = ForceSubscribeUsers::forceSubscribeUsersCountByAppid($appid);
  139. \Log::info('untie_distribution_official_account_appid:'.$appid.' force_subscribe_count:'.$force_subscribe_count.' $force_untie:'.$force_untie);
  140. if($force_untie){
  141. // 强制解绑的话,大号也不行
  142. if($force_subscribe_count >=10000){
  143. return response()->error("OLD_APP_NOT_ALLOW_DELETE");
  144. }
  145. }else{
  146. // 小权限限制下500人
  147. if($force_subscribe_count >=500){
  148. return response()->error("OLD_APP_NOT_ALLOW_DELETE");
  149. }
  150. }
  151. // 备份
  152. $untie_offcial_account = object_to_array($official_account);
  153. UntieOfficialAccount::firstOrCreate($untie_offcial_account);
  154. // 删除OfficialAccount
  155. \Log::info('del_official_account:'.json_encode($official_account));
  156. OfficialAccount::deleteOfficialAccount($official_account['id']);
  157. // 删除redis中OfficialAccount缓存
  158. \Log::info('del_redis:'.'official_account_info:app_id:'.$appid);
  159. Redis::del('official_account_info:app_id:'.$appid);
  160. \Log::info('untie_distribution_official_account_end:'.$appid);
  161. return response()->success(['status'=>'1']);
  162. }
  163. public function getUserClient(){
  164. return new Client(['base_uri' => env('MEDIA_API_BASE_URI')]);
  165. }
  166. public function getAuthClient(){
  167. return new Client(['base_uri' => env('AUTH_API_BASE_URI')]);
  168. }
  169. }