UserGroupController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace Modules\Audience\Http\Controllers;
  3. use Catch\Base\CatchController;
  4. use Illuminate\Foundation\Validation\ValidatesRequests;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Modules\Audience\Models\GzhUgMapModel;
  8. use Modules\Audience\Models\UserGroupModel;
  9. use Modules\Audience\Services\UserGroupService;
  10. use Modules\Common\Errors\Errors;
  11. use Modules\Common\Exceptions\CommonBusinessException;
  12. class UserGroupController extends CatchController
  13. {
  14. use ValidatesRequests;
  15. /**
  16. * 用户分群列表
  17. * @param Request $request
  18. */
  19. public function list(Request $request) {
  20. $name = $request->input('name');
  21. return DB::table('user_groups')
  22. ->when($name, function ($query, $name){
  23. return $query->where('name', 'like', '%'.$name.'%');
  24. })->where([
  25. 'uid' => $this->getLoginUserId(),
  26. 'is_enabled' => 1
  27. ])->orderBy('id', 'desc')
  28. ->paginate($request->input('limit', 20));
  29. }
  30. /**
  31. * 详情
  32. * @param Request $request
  33. * @throws \Illuminate\Validation\ValidationException
  34. */
  35. public function detail(Request $request) {
  36. $this->validate($request, ['id' => 'required']);
  37. $uid = $this->getLoginUserId();
  38. $obj = DB::table('user_groups')
  39. ->where([
  40. 'uid' => $uid, 'is_enabled' => 1,
  41. 'id' => $request->input('id')
  42. ])->first();
  43. if($obj) {
  44. $gzhInfo = DB::table('gzh_ug_maps as a')
  45. ->join('wechat_authorization_infos as b', function($query) use ($uid) {
  46. $query->on('a.gzh_id', '=', 'b.id')
  47. ->where([
  48. 'b.is_enabled' => 1,
  49. 'b.user_id' => $uid
  50. ]);
  51. })->where([
  52. 'a.ug_id' => $request->input('id')
  53. ])->select('b.nick_name', 'b.authorizer_appid')->get();
  54. $tags = \json_decode($obj->tags, true);
  55. $tags['gzh_appids'] = $gzhInfo->pluck('authorizer_appid')->toArray();
  56. $tags['in_48_hour'] = 1;
  57. $uids = UserGroupService::getSQL($tags)->distinct()->select('a.uid')->get()->pluck('uid')->toArray();
  58. $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags);
  59. $obj->user_num = count($uids);
  60. $tags = \json_decode($obj->tags, true);
  61. $tag_arr = $this->strTagArr($tags);
  62. $obj->tags_arr = $tag_arr;
  63. $obj->gzh_names = $gzhInfo->pluck('nick_name')->toArray();
  64. }
  65. return $obj;
  66. }
  67. private function strTagArr($tags) {
  68. $tagFields = [
  69. 't1' => [
  70. 'attention_hour' => '关注时间(小时)',
  71. 'in_48_hour' => '仅48小时内和公众号有互动的粉丝才能收到',
  72. 'interact_hour' => '上次互动时间(小时)',
  73. 'last_watch_day' => '最近观看时间(天)',
  74. 'register_day' => '用户注册日期(天)',
  75. 'total_watch_day' => '累计观看天数',
  76. 'video_watch' => '历史观看短剧',
  77. 'video_charge' => '充值过短剧',
  78. ],
  79. 't2' => [
  80. 'charge_type' => '付费类型',
  81. 'total_charge_money' => '累计充值金额',
  82. 'avg_charge_money' => '平均充值金额',
  83. 'charge_num' => '充值次数',
  84. 'remain_coin' => '看币余额',
  85. 'last_charge_day' => '上次充值日期',
  86. ]
  87. ];
  88. $t1 = [];
  89. $t2 = [];
  90. foreach ($tags as $key => $val) {
  91. if($val) {
  92. $valStr = $this->strVal($key, $val);
  93. if(in_array($key, array_keys($tagFields['t1']))) {
  94. $t1[] = $tagFields['t1'][$key] . ':'. $valStr;
  95. } else {
  96. $t2[] = $tagFields['t2'][$key]. ':'. $valStr;
  97. }
  98. }
  99. }
  100. return [
  101. 'active' => $t1, 'pay' => $t2
  102. ];
  103. }
  104. private function strVal($key, $val) {
  105. if('charge_type' == $key) {
  106. return ['1' => '未付费', '2' => '待支付', '3' => '已付费', '4' => 'VIP用户'][$val] ?? '';
  107. }
  108. if(in_array($key, ['video_watch', 'video_charge'])) {
  109. return DB::table('videos')
  110. ->whereIn('id', $val)
  111. ->select('name')->get()->pluck('name')->join(', ');
  112. }
  113. if('in_48_hour' == $key) {
  114. return ['1' => '是', '2' => '否'][$val] ?? '';
  115. }
  116. $arr = explode('-', $val);
  117. if($arr[0] == $arr[1]) {
  118. return $arr[0];
  119. }
  120. if($arr[1] == 0) {
  121. return $arr[0] . '-不限';
  122. }
  123. return $val;
  124. }
  125. /**
  126. * 添加用户分群
  127. * @param Request $request
  128. * @return string
  129. * @throws \Illuminate\Validation\ValidationException
  130. */
  131. public function add(Request $request){
  132. $this->validate($request, [
  133. 'name' => 'required|string|max:64',
  134. 'gzh_ids' => 'required|array',
  135. 'tags' => 'required|array',
  136. 'remark' => 'nullable|string|max:140'
  137. ]);
  138. $data = [
  139. 'name' => $request->input('name'), 'uid' => $this->getLoginUserId(),
  140. 'tags' => \json_encode($request->input('tags', []), JSON_UNESCAPED_UNICODE),
  141. ];
  142. if($request->input('remark')) {
  143. $data['remark'] = $request->integer('remark');
  144. }
  145. $model = UserGroupModel::create($data);
  146. foreach ($request->input('gzh_ids') as $gzh_id) {
  147. GzhUgMapModel::create([
  148. 'ug_id' => $model->id, 'gzh_id' => $gzh_id
  149. ]);
  150. }
  151. return 'ok';
  152. }
  153. /**
  154. * 批量删除
  155. * @param Request $request
  156. * @return string
  157. * @throws \Illuminate\Validation\ValidationException
  158. */
  159. public function delete(Request $request) {
  160. $this->validate($request, ['ids' => 'required|array']);
  161. foreach ($request->input('ids') as $id) {
  162. $model = UserGroupModel::where([
  163. 'uid' => $this->getLoginUserId(), 'id' => $id,
  164. 'is_enabled' => 1,
  165. ])->first();
  166. if($model) {
  167. $model->is_enabled = 0;
  168. $model->save();
  169. GzhUgMapModel::where([
  170. 'ug_id' => $model->id,
  171. 'is_enabled' => 1
  172. ])->update([
  173. 'is_enabled' => 0
  174. ]);
  175. }
  176. }
  177. return 'ok';
  178. }
  179. /**
  180. * 获取符合条件的用户分群列表
  181. * @param Request $request
  182. */
  183. public function listUser(Request $request) {
  184. $this->validate($request, [
  185. 'timestamp' => 'required',
  186. 'sign' => 'required'
  187. ]);
  188. $signKey = config('audience.ug.signKey');
  189. if($request->input('sign') != md5(
  190. $signKey.$request->input('timestamp')
  191. )) {
  192. CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_SIGN_ERROR);
  193. }
  194. if(time() - 60 > $request->integer('timestamp')) {
  195. CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_SIGN_TIMESTAMP_ERROR);
  196. }
  197. $tags = DB::table('user_groups')
  198. ->where([
  199. ['id' , '=', $request->input('ugId')],
  200. ['is_enabled', '=', 1]
  201. ])->value('tags');
  202. if(!$tags) {
  203. CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_NOT_EXISTS);
  204. }
  205. $tagsArr = \json_decode($tags, true);
  206. $authorizer_appid = DB::table('wechat_authorization_infos')
  207. ->where([
  208. ['id', '=', $request->input('gzhId')],
  209. ['is_enabled', '=', 1]
  210. ])->value('authorizer_appid');
  211. if(!$authorizer_appid) {
  212. CommonBusinessException::throwError(Errors::OPENPLATFORM_GZH_SHOUQUAN_ERROR);
  213. }
  214. $tagsArr['gzh_appids'] = [$authorizer_appid];
  215. $sql = UserGroupService::getSQL($tagsArr);
  216. $res = $sql->distinct()->select('b.uid', 'b.mp_openid')
  217. ->get();
  218. $uids = $res->pluck('uid')->toArray();
  219. if(count($uids)) {
  220. $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags);
  221. }
  222. $openid = [];
  223. foreach ($res as $item) {
  224. if(in_array($item->uid, $uids)) {
  225. $openid[] = $item->mp_openid;
  226. }
  227. }
  228. return [
  229. 'openid' => $openid
  230. ];
  231. }
  232. }