OfficialAccount.php 9.0 KB

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