UserController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Events\BlackUser;
  4. use App\Exceptions\AlertException;
  5. use App\Http\Resources\Admin\User\BlackCollection;
  6. use App\Models\BlackListModel;
  7. use App\Models\PartnerModel;
  8. use App\Models\RiskListModel;
  9. use App\Models\User\UserModel;
  10. use App\Http\Controllers\Controller;
  11. use App\Models\User\AuthKey;
  12. use App\Models\Wxkf\UserInfoModel;
  13. use App\Services\Partner\NoticeService;
  14. use App\Services\User\PartnerService;
  15. use App\Services\User\ProfileService;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\Redis;
  19. class UserController extends Controller
  20. {
  21. /**
  22. * 搜索用户
  23. * @param Request $request
  24. * @return array
  25. */
  26. public function search(Request $request)
  27. {
  28. $this->validate($request, [
  29. 'content' => 'required',
  30. ]);
  31. $content = $request->input('content');
  32. if (is_int(strpos($content, 'uid'))) {
  33. $uid = substr($content, 4);
  34. $where = ['uid' => $uid];
  35. } elseif (is_int(strpos($content, 'phone'))) {
  36. $phone = substr($content, 6);
  37. $where = ['phone' => $phone];
  38. } elseif (is_int(strpos($content, 'weixin'))) {
  39. $weixin = substr($content, 7);
  40. $where = ['weixin' => $weixin];
  41. } elseif (is_int(strpos($content, 'cid'))) {
  42. $partner_id = substr($content, 4);
  43. $where = ['partner_id' => $partner_id];
  44. } else {
  45. return response([
  46. 'code' => 422,
  47. 'message' => '搜索格式:uid=xxx,phone=xx,weixin=xxx'
  48. ]);
  49. }
  50. $users = UserModel::where($where)->limit(10)->get();
  51. return response([
  52. 'code' => 200,
  53. 'message' => 'success',
  54. 'data' => $users
  55. ]);
  56. }
  57. /**
  58. * 人工微信认证
  59. * @param Request $request
  60. * @return array
  61. * @throws AlertException
  62. */
  63. public function wxverify(Request $request)
  64. {
  65. $this->validate($request, [
  66. 'user_id' => 'required',
  67. 'wx_alias' => 'required',
  68. 'kf' => 'required'
  69. ]);
  70. $user_id = $request->post('user_id');
  71. $wx_alias = $request->post('wx_alias');
  72. $auth = AuthKey::where([['uid', $user_id], ['auth_type', 'wxid']])->first();
  73. if (!collect($auth)->isEmpty()) {
  74. UserInfoModel::where('wxid', $auth->auth_key)->whereNull('wx_alias')->update(['wx_alias' => $wx_alias]);
  75. } else {
  76. throw new AlertException("该用户未绑定wxid", 101);
  77. }
  78. $user = UserModel::find($user_id);
  79. $before_auth = 0;
  80. $after_auth = 0;
  81. if ($user->identity_auth && $user->wx_auth == 1) {
  82. $before_auth = 1;
  83. }
  84. if ($user->wx_auth == 1) {
  85. return array(
  86. 'code' => 200,
  87. 'message' => 'success',
  88. );
  89. }
  90. $user->wx_auth = 1;
  91. $user->weixin = $wx_alias;
  92. $ps = new PartnerService();
  93. $ps->updatePartner($user->partner_id, ['weixin' => $wx_alias]);
  94. // 发送审核通过通知
  95. $wxkf = $request->post('kf');
  96. $kfmsg = array(
  97. 'task_type' => 1,
  98. 'task_dict' => array(
  99. 'wxid_to' => $auth->auth_key,
  100. 'at_list' => "",
  101. 'msg_list' => array(
  102. [
  103. 'msg_type' => 1,
  104. 'msg' => "恭喜你已完成微信认证,现在你可以回到小程序向喜欢的ta发起“心动邀请”啦。补全信息会帮助认识彼此,同时将获得更高的曝光度。\n\n小遇祝你早日脱单哦!加油~\n\n👇👇👇👇👇"
  105. ],
  106. [
  107. 'msg_type' => 3,
  108. 'msg' => "https://oss.pocketuniversity.cn/media/2019-01-09/5c35d017d6771.png"
  109. ]
  110. )
  111. )
  112. );
  113. Redis::sadd("wehub:task:list:{$wxkf}", [json_encode($kfmsg)]);
  114. if ($user->identity_auth && $user->wx_auth == 1) {
  115. $after_auth = 1;
  116. }
  117. // 发送认证通知
  118. try {
  119. if ($before_auth == 0 && $after_auth == 1) {
  120. $ns = new NoticeService();
  121. $ns->authenticationSuccess($user->uid);
  122. }
  123. } catch (\Exception $e) {
  124. }
  125. return response([
  126. 'code' => 200,
  127. 'message' => 'success',
  128. ]);
  129. }
  130. /**
  131. * 修改用户信息
  132. * @param int $uid
  133. * @param Request $request
  134. * @return array
  135. * @throws AlertException
  136. */
  137. public function update(int $uid, Request $request)
  138. {
  139. /** @var UserModel $user */
  140. $user = UserModel::findOrFail($uid);
  141. $data = $request->input();
  142. $psf = new ProfileService();
  143. $psf->updateUser($user, $data);
  144. return response([
  145. 'code' => 200,
  146. 'message' => 'success'
  147. ]);
  148. }
  149. /**
  150. * 获取用户信息
  151. * @param int $uid
  152. * @return array
  153. */
  154. public function show(int $uid)
  155. {
  156. $user = UserModel::findOrFail($uid);
  157. return response([
  158. 'code' => 200,
  159. 'message' => 'success',
  160. 'data' => $user
  161. ]);
  162. }
  163. /**
  164. *
  165. * @param Request $request
  166. * @return BlackCollection
  167. */
  168. public function blackList(Request $request)
  169. {
  170. $blacks = BlackListModel::when($request->filled('uid'), function ($query) use ($request) {
  171. /** @var Builder $query */
  172. return $query->where('account', $request->uid);
  173. })->when($request->filled('type'), function ($query) use ($request) {
  174. /** @var Builder $query */
  175. return $query->where('type', $request->type);
  176. })->when($request->filled('state'), function ($query) use ($request) {
  177. /** @var Builder $query */
  178. return $query->where('state', $request->state);
  179. })->paginate($request->get('pre_page', 20));
  180. foreach ($blacks as $black) {
  181. $black->user;
  182. }
  183. return new BlackCollection($blacks);
  184. }
  185. /**
  186. * 拉入黑名单
  187. * @param Request $request
  188. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  189. */
  190. public function createBlack(Request $request)
  191. {
  192. $this->validate($request, [
  193. 'type' => 'required|in:msy,fpdx,all,goodnight,chat,game,home,account',
  194. 'state' => 'required|integer',
  195. ]);
  196. switch ($request->state) {
  197. case "1":
  198. $end_time = mktime(24, 0, 0) + 7 * 24 * 3600;
  199. break;
  200. case "2":
  201. $end_time = mktime(24, 0, 0) + 30 * 24 * 3600;
  202. break;
  203. case "3":
  204. $end_time = 1893427200;
  205. break;
  206. }
  207. $account_type = $request->input('account_type', 'uid');
  208. $account = $request->input('account', $request->input('uid'));
  209. $data = $request->all();
  210. $data['end_at'] = $end_time;
  211. $data['account_type'] = $account_type;
  212. $data['account'] = $account;
  213. $blacks = BlackListModel::create($data);
  214. // 永久封禁
  215. if ($account_type == 'uid' && $request->type == 'all') {
  216. $user = UserModel::find($account);
  217. $user->headimgurl = 'https://oss.pocketuniversity.cn/media/2019-08-14/5d53e27a8d81e.png';
  218. $user->save();
  219. // 封禁卡片
  220. if ($user->partner_id) {
  221. $partner = PartnerModel::find($user->partner_id);
  222. $partner->is_sell = -1;
  223. $partner->black_at = $end_time;
  224. $partner->save();
  225. }
  226. // 加入风险帐号列表
  227. RiskListModel::create([
  228. 'uid' => $user->uid,
  229. 'weixin' => $user->weixin,
  230. 'qq' => $user->qq,
  231. 'phone' => $user->phone,
  232. ]);
  233. // 手机号码封禁
  234. if ($request->state == 3) {
  235. if ($user->phone) {
  236. BlackListModel::create([
  237. 'type' => 'all',
  238. 'account_type' => 'phone',
  239. 'account' => $user->phone,
  240. 'end_at' => 1893427200,
  241. 'state' => 3
  242. ]);
  243. }
  244. }
  245. }
  246. if ($account_type == 'uid') {
  247. // 封禁事件
  248. event(new BlackUser($account, $end_time, $data['type']));
  249. }
  250. return response([
  251. 'code' => 200,
  252. 'message' => 'OK',
  253. 'blacks' => $blacks
  254. ]);
  255. }
  256. public function deleteBlack($id)
  257. {
  258. $black = BlackListModel::findOrFail($id);
  259. $black->delete();
  260. return response([
  261. 'code' => 200,
  262. 'message' => 'OK',
  263. ]);
  264. }
  265. }