OfficialAccount.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Modules\OfficialAccount\Models;
  3. use DB;
  4. use Illuminate\Database\Eloquent\Model;
  5. class OfficialAccount extends Model
  6. {
  7. protected $connection = 'api_mysql';
  8. protected $tables = 'official_accounts';
  9. protected $fillable = ['distribution_channel_id', 'nickname', 'name', 'head_img', 'appid', 'appsecret', 'verify_txt', 'alias', 'qrcode_url', 'principal_name', 'service_type_info', 'func_info', 'authorizer_refresh_token', 'is_auth', 'cancel_auth_time', 'official_account_type', 'verify_type_info', 'subscribe_top_num', 'subscribe_day_maximum', 'sort_no', 'is_enabled'];
  10. /**
  11. * 根据appid获取公众号
  12. */
  13. static function officialAccountByAppid($appid)
  14. {
  15. return self::where('appid', isset($appid) ? $appid : '')->first();
  16. }
  17. /**
  18. * 根据appid获取公众号
  19. */
  20. static function officialAccountArrByAppid($appid)
  21. {
  22. $official_account = self::where('appid', isset($appid) ? $appid : '')->first();
  23. if (!empty($official_account)) {
  24. return $official_account->toArray();
  25. } else {
  26. return null;
  27. }
  28. }
  29. /**
  30. * 获取所有公众号
  31. */
  32. static function getAllOfficialAccountDB()
  33. {
  34. return self::select()->get();
  35. }
  36. /**
  37. * 根据id获取公众号
  38. */
  39. static function officialAccountById($id)
  40. {
  41. return self::where('id', $id)->first();
  42. }
  43. /**
  44. * 根据appid和distribution_channel_id获取公众号
  45. */
  46. static function officialAccountByAppidAndChannelId($appid, $distribution_channel_id)
  47. {
  48. return self::where(['appid' => $appid, 'distribution_channel_id' => $distribution_channel_id])->first();
  49. }
  50. /**
  51. * 获取所有可用已授权公众号
  52. */
  53. static function officialAuthAccounts($is_auth, $is_enabled)
  54. {
  55. return self::where(['is_auth' => isset($is_auth) ? $is_auth : '', 'is_enabled' => isset($is_enabled) ? $is_enabled : ''])->groupBy('appid')->get();
  56. }
  57. /**
  58. * 获取1个可用已授权公众号
  59. */
  60. static function getOneOfficialAuthAccounts($distribution_channel_id, $is_auth, $is_enabled)
  61. {
  62. return self::where(['distribution_channel_id' => $distribution_channel_id, 'is_auth' => $is_auth, 'is_enabled' => $is_enabled])->first();
  63. }
  64. /**
  65. * 获取所有可用已授权强关类型的公众号
  66. */
  67. static function officialAuthSubscribeAccounts($is_auth, $is_enabled)
  68. {
  69. return self::where(['is_auth' => isset($is_auth) ? $is_auth : '', 'is_enabled' => isset($is_enabled) ? $is_enabled : '', 'official_account_type' => 'force_subscribe'])->groupBy('appid')->get();
  70. }
  71. /**
  72. * 删除指定公众号
  73. */
  74. static function deleteOfficialAccount($id)
  75. {
  76. if (empty($id)) return false;
  77. return self::where(['id' => $id])->delete();
  78. }
  79. /**
  80. * 根据分销渠道号获取授权公众号
  81. */
  82. static function officialAuthAccountBydistributionChannelId($distribution_channel_id, $is_auth, $is_enabled)
  83. {
  84. return self::where(['distribution_channel_id' => isset($distribution_channel_id) ? $distribution_channel_id : '', 'is_auth' => isset($is_auth) ? $is_auth : '', 'is_enabled' => isset($is_enabled) ? $is_enabled : ''])->orderBy('sort_no', 'desc')->get();
  85. }
  86. /**
  87. * 根据distribution_channel_id获取公众号数量
  88. */
  89. static function officialAccountCountByChannelId($distribution_channel_id)
  90. {
  91. return self::where('distribution_channel_id', isset($distribution_channel_id) ? $distribution_channel_id : '')->count();
  92. }
  93. /**
  94. * 根据分销渠道号获取服务号列表
  95. */
  96. static function allOfficialAccountBydistributionChannelId($distribution_channel_id, $is_enabled)
  97. {
  98. return self::where(['distribution_channel_id' => isset($distribution_channel_id) ? $distribution_channel_id : '', 'is_enabled' => isset($is_enabled) ? $is_enabled : ''])->get();
  99. }
  100. /**
  101. * 根据分销渠道号获取服务号列表以及强关类型
  102. */
  103. static function allOfficialAccountBydistributionChannelIdAndGetForceType($distribution_channel_id, $is_enabled)
  104. {
  105. return self::LeftJoin('distribution_force_subscribe_setting', 'official_accounts.appid', '=', 'distribution_force_subscribe_setting.appid')
  106. ->select(DB::raw('official_accounts.*, distribution_force_subscribe_setting.force_subscribe_type,distribution_force_subscribe_setting.resource_url'))
  107. ->where(['official_accounts.distribution_channel_id' => isset($distribution_channel_id) ? $distribution_channel_id : '', 'official_accounts.is_enabled' => isset($is_enabled) ? $is_enabled : ''])->orderBy('official_accounts.sort_no', 'desc')->get();
  108. }
  109. /**
  110. * 根据托管获取服务号列表
  111. */
  112. static function getTrusteeshipOfficialAccounts()
  113. {
  114. return self::LeftJoin('custom_msg_switchs_msgs', 'official_accounts.distribution_channel_id', '=', 'custom_msg_switchs_msgs.distribution_channel_id')
  115. ->select(DB::raw('official_accounts.*, custom_msg_switchs_msgs.status,custom_msg_switchs_msgs.custom_category'))
  116. ->where('official_accounts.is_auth', 1)
  117. ->where('official_accounts.is_enabled', 1)
  118. ->where('custom_msg_switchs_msgs.status', 1)
  119. ->where('custom_msg_switchs_msgs.custom_category', 'auto_custom_trusteeship')
  120. ->groupBy('official_accounts.appid')
  121. ->get();
  122. }
  123. static function get_account_appids($account):array{
  124. return self::LeftJoin('distribution_channels', 'official_accounts.distribution_channel_id','distribution_channels.id')
  125. ->LeftJoin('channel_users', 'channel_users.id','distribution_channels.channel_user_id')
  126. ->select('official_accounts.appid')
  127. ->where('channel_users.phone', $account)
  128. ->groupBy('official_accounts.appid')
  129. ->pluck('appid')->all();
  130. }
  131. /**
  132. * 获取渠道粉丝阈值信息
  133. */
  134. static function getBusinessChannelData($channels = [], $distribution_channel_name = '', $official_account_name = '', $isAll = false)
  135. {
  136. $total_sql = "(select COUNT(1) from force_subscribe_users WHERE force_subscribe_users.appid=official_accounts.appid) as day_total_fans_num";
  137. $sql = "(select COUNT(1) from force_subscribe_users WHERE force_subscribe_users.appid=official_accounts.appid and created_at>=curdate()) as day_fans_num";
  138. $obj = self::select('official_accounts.distribution_channel_id',
  139. "distribution_channels.nickname as distribution_channel_name",
  140. 'official_accounts.nickname as service_name',
  141. 'official_accounts.subscribe_day_maximum',
  142. 'official_accounts.subscribe_top_num',
  143. DB::raw($sql),
  144. DB::raw($total_sql))
  145. ->join("distribution_channels", 'official_accounts.distribution_channel_id', "=", "distribution_channels.id");
  146. if ($channels) {
  147. $obj = $obj->whereIn('official_accounts.distribution_channel_id', $channels);
  148. }
  149. if ($distribution_channel_name) {
  150. $obj = $obj->where('distribution_channels.nickname', 'like', '%' . $distribution_channel_name . '%');
  151. }
  152. if ($official_account_name) {
  153. $obj = $obj->where('official_accounts.nickname', 'like', '%' . $official_account_name . '%');
  154. }
  155. if ($isAll) {
  156. return $obj->get();
  157. } else {
  158. return $obj->paginate();
  159. }
  160. }
  161. public static function getInfoList($params = [])
  162. {
  163. $search_object = self::select('distribution_channel_id', 'nickname','principal_name')->orderBy('created_at', 'desc');
  164. if (isset($params['start_date']) && $params['start_date']) $search_object->where('created_at', '>=', $params['start_date']);
  165. if (isset($params['end_date']) && $params['end_date']) $search_object->where('created_at', '<=', $params['end_date']);
  166. return $search_object->get();
  167. }
  168. }