WallpaperController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Models\WxWallpapersComment;
  4. use App\Wen\Utils\BaiduUtils;
  5. use App\Wen\Utils\FieldUtils;
  6. use App\Wen\Utils\Settings;
  7. use App\Wen\Utils\StrUtils;
  8. use App\Wen\Utils\UserUtils;
  9. use App\Wen\Utils\Utils;
  10. use App\Models\User\WxUser;
  11. use App\Models\WxWallpaper;
  12. use Illuminate\Http\Request;
  13. class WallpaperController extends BaseController
  14. {
  15. public function detail(Request $request)
  16. {
  17. $id = $request->id;
  18. if(_empty_($id)){
  19. return $this->fail(200001);
  20. }
  21. $WxWallpaper = WxWallpaper::leftJoin('wx_wallpapers_subject', 'wx_wallpapers_subject.id', 'wx_wallpapers.subject_id')->where('wx_wallpapers.id', $id)->orderBy('wx_wallpapers.id', 'desc')->first();
  22. if(_empty_($WxWallpaper)){
  23. return $this->fail(200003);
  24. }
  25. $uid = $request->uid;
  26. $WxWallpaper->comment_count = WxWallpapersComment::where('wallpaper_id', $WxWallpaper->id)->where('comment_state', 1)->count();
  27. $collected_wallpaper = get_user_meta($uid, 'collected_wallpaper', 'j');
  28. $this->userAndImgsProcess($WxWallpaper, $collected_wallpaper);
  29. return $this->success($WxWallpaper);
  30. }
  31. public function comment_process(Request $request){
  32. $action = _empty_default_($request->action, '');
  33. $uid = $request->uid;
  34. if(_empty_($action)){
  35. return $this->fail(200001);
  36. }
  37. if(!in_array($action, ['add', 'delete'])){
  38. return $this->fail(200004);
  39. }
  40. if(!UserUtils::is_user_can_speak($uid)){
  41. return $this->fail(200043, [
  42. 'title' => '禁言中,无法言论',
  43. 'content' => '是否前往解除限制',
  44. 'confirmText' => '去解除',
  45. 'target_type' => 6,
  46. 'target_id' => '/pagesA/mine/unlock/unlock?user_id='.$uid
  47. ], '您当前处于禁言期间,无法言论');
  48. }
  49. if($action == 'add'){
  50. $wallpaper_id = _empty_default_($request->wallpaper_id, 0);
  51. if(_empty_($wallpaper_id)){
  52. return $this->fail(200001);
  53. }
  54. if(!WxWallpaper::where('id', $wallpaper_id)->exists()){
  55. return $this->fail(200000, [], '该图集不存在');
  56. }
  57. if(mb_strlen( str_replace( [Settings::get('default_user_name', '微信用户'), '微信用户', '普通用户'], '', UserUtils::get_cached_user_name($uid) )) != mb_strlen(UserUtils::get_cached_user_name($uid))){
  58. return $this->fail(200041);
  59. }
  60. $comment_id = _empty_default_($request->comment_id, 0);
  61. if(_empty_($comment_id)){
  62. $comment_id = 0;
  63. }
  64. if($comment_id > 0){
  65. if(!WxWallpapersComment::where('id', $comment_id)->where('comment_state', 1)->exists()){
  66. return $this->fail(200004, [], '该父级评论不存在');
  67. }
  68. }
  69. $comment_agent_id = _empty_default_($request->comment_agent_id, 0);
  70. if(_empty_($comment_agent_id)){
  71. $comment_agent_id = 0;
  72. }
  73. if($comment_agent_id > 0){
  74. $comment_agent_name = UserUtils::get_cached_user($comment_agent_id)['user_name'];
  75. }
  76. $comment_content = _empty_default_($request->comment_content, '');
  77. $comment_img_url = _empty_default_($request->comment_img_url, '');
  78. if(_empty_($comment_content)){
  79. return $this->fail(200001);
  80. }
  81. $comment_content = strip_tags($comment_content);
  82. if(mb_strlen($comment_content) <= 0 || mb_strlen($comment_content) > 240){
  83. return $this->fail(200004, [], '评论内容过长或过短');
  84. }
  85. global $__MINI_GLOBAL_IP__;
  86. // 百度审核
  87. if(UserUtils::user_permissions_check_by_config($uid, 'audit_used_comment_white_list')){
  88. $need_manual_review = false;
  89. }else{
  90. $need_manual_review = true;
  91. $is_audit_used_comment = Settings::get('is_audit_used_comment', 0);
  92. if ($is_audit_used_comment == 2) {
  93. $filter_result = BaiduUtils::text_filter($comment_content);
  94. if($filter_result){
  95. if($filter_result['hit_level'] == 2){
  96. // 不合规
  97. return $this->fail(200016, ['tip'=>$filter_result['tip_list'], 'hit_word'=>$filter_result['hit_word']]);
  98. }else if($filter_result['hit_level'] == 0){
  99. // 审核通过
  100. $need_manual_review = false;
  101. }
  102. }
  103. }else if($is_audit_used_comment == 0){
  104. $need_manual_review = true;
  105. }else{
  106. $need_manual_review = false;
  107. }
  108. }
  109. // 0:审核中,1:审核通过,2:驳回
  110. $comment_state = 1;
  111. if($need_manual_review){
  112. $comment_state = 0;
  113. }
  114. $model = new WxWallpapersComment();
  115. $model->user_id = $uid;
  116. $model->wallpaper_id = $wallpaper_id;
  117. if($comment_id > 0){
  118. $model->comment_id = $comment_id;
  119. }else{
  120. $model->comment_id = 0;
  121. }
  122. if($comment_agent_id > 0 && $comment_agent_name){
  123. $model->comment_agent_id = $comment_agent_id;
  124. $model->comment_agent_name = $comment_agent_name;
  125. }else{
  126. $model->comment_agent_id = 0;
  127. }
  128. $model->comment_content = $comment_content;
  129. if(!_empty_($comment_img_url)){
  130. $model->comment_img_url = $comment_img_url;
  131. }
  132. if($__MINI_GLOBAL_IP__){
  133. $model->ip = $__MINI_GLOBAL_IP__;
  134. }
  135. $model->comment_state = $comment_state;
  136. $r = $model->save();
  137. if($r){
  138. return $this->success();
  139. }
  140. return $this->fail(200002);
  141. }else if($action == 'delete'){
  142. }
  143. }
  144. public function comment_list(Request $request){
  145. $limit = _between_(_empty_default_($request->limit, 10), 1, 100);
  146. $wallpaper_id = _empty_default_($request->wallpaper_id, 0);
  147. if(_empty_($wallpaper_id)){
  148. $this->fail(200001);
  149. }
  150. $data = WxWallpapersComment::where('wallpaper_id', $wallpaper_id)->where('comment_id', 0)->where('comment_state', 1)->orderBy('id', 'desc')->simplePaginate($limit);
  151. if($data){
  152. $data->map(function ($v, $k){
  153. $v->user = UserUtils::get_cached_user($v->user_id);
  154. if(_empty_($v->comment_id)){
  155. $childData = WxWallpapersComment::where('comment_id', $v->id)->limit(100)->get();
  156. if($childData){
  157. $childData->map(function ($v2, $k2){
  158. $v2->user = UserUtils::get_cached_user($v2->user_id);
  159. $v2->format_time = format_datetime($v2->created_at);
  160. return $v2;
  161. });
  162. }
  163. $v->child = $childData;
  164. }else{
  165. $v->child = [];
  166. }
  167. $v->format_time = format_datetime($v->created_at);
  168. return $v;
  169. });
  170. return $this->success($data);
  171. }else{
  172. return $this->fail(200003);
  173. }
  174. }
  175. public function list(Request $request){
  176. $type = _empty_default_($request->type, 'subject');
  177. $uid = $request->uid;
  178. if(_empty_($type)){
  179. $type = 'subject';
  180. }
  181. if(!in_array($type, ['subject'])){
  182. return $this->fail(200004);
  183. }
  184. $collected = _empty_default_($request->collected, 0);
  185. $query = WxWallpaper::where('id', '>', 0);
  186. if($collected == 1){
  187. if(_empty_($uid)){
  188. return $this->fail(503002);
  189. }
  190. $collected_wallpaper = get_user_meta($uid, 'collected_wallpaper', 'j');
  191. if($collected_wallpaper){
  192. $query = $query->whereIn('id', array_reverse($collected_wallpaper));
  193. }
  194. }
  195. $WxWallpapers = $query->orderBy('id', 'desc')->simplePaginate();
  196. if(_empty_($WxWallpapers)){
  197. return $this->fail(200003);
  198. }
  199. $uid = Utils::uid($request);
  200. $collected_wallpaper = get_user_meta($uid, 'collected_wallpaper', 'j');
  201. $WxWallpapers->map( function ($item) use(&$collected_wallpaper) {
  202. $this->userAndImgsProcess($item, $collected_wallpaper);
  203. $item->comment_count = WxWallpapersComment::where('wallpaper_id', $item->id)->where('comment_state', 1)->count();
  204. return $item;
  205. } );
  206. return $this->success($WxWallpapers);
  207. }
  208. private function userAndImgsProcess(&$WxWallpaper, &$collected_wallpaper){
  209. $WxWallpaper->imgs = json_decode($WxWallpaper->imgs ?: '', true);
  210. $WxWallpaper->author = WxUser::where('id', $WxWallpaper->user_id)->get(FieldUtils::userInfoColums());
  211. $WxWallpaper->iscollect = $collected_wallpaper ? in_array($WxWallpaper->id, $collected_wallpaper) : 0;
  212. $WxWallpaper->firstImg = $WxWallpaper->imgs ? $WxWallpaper->imgs[0] : '';
  213. $WxWallpaper->imgNum = $WxWallpaper->imgs ? count($WxWallpaper->imgs) : 0;
  214. }
  215. public function wallpaper_process(Request $request) {
  216. $type = _empty_default_($request->type, '');
  217. if ($type == 'collect') {
  218. return $this->collect($request);
  219. }else if ($type == 'add') {
  220. return $this->add($request);
  221. }
  222. }
  223. private function add(Request $request){
  224. $uid = $request->uid;
  225. $id = _empty_default_($request->id, 0);
  226. $title = _empty_default_($request->title, '');
  227. $imgs = _empty_default_($request->imgs, '');
  228. $latitude = _empty_default_($request->latitude, 0);
  229. $longitude = _empty_default_($request->longitude, 0);
  230. $address_name = _empty_default_($request->address_name, '');
  231. $address_detailed = _empty_default_($request->address_detailed, '');
  232. global $__MINI_GLOBAL_IP__;
  233. if(_empty_($imgs)){
  234. $imgs = '';
  235. }
  236. if(is_string($imgs)){
  237. $imgs = explode(',', $imgs);
  238. }
  239. if(!is_array($imgs)){
  240. $imgs = [];
  241. }
  242. if(_empty_($imgs)){
  243. return $this->fail(200001, [], '请上传图片');
  244. }
  245. if(_empty_($title)){
  246. return $this->fail(200001, [], '请填写描述');
  247. }
  248. if($id > 0){
  249. return $this->fail(200004, [], '暂时不能处理修改图集');
  250. }
  251. $img_state_change = [];
  252. foreach ($imgs as $img_url){
  253. if(!StrUtils::startsWith($img_url, 'http')){
  254. return $this->fail(200004, [], '图片检验出错');
  255. }
  256. $img_state_change[] = $img_url;
  257. }
  258. $wallpaper = new WxWallpaper();
  259. $wallpaper->title = $title;
  260. $wallpaper->imgs = json_encode($imgs);
  261. $wallpaper->user_id = $uid;
  262. $wallpaper->ip = $__MINI_GLOBAL_IP__;
  263. if(!_empty_($address_detailed) && _empty_($address_name)){
  264. $wallpaper->latitude = $latitude;
  265. $wallpaper->longitude = $longitude;
  266. $wallpaper->address_name = $address_name;
  267. $wallpaper->address_detailed = $address_detailed;
  268. }
  269. $r = $wallpaper->save();
  270. if($r){
  271. Utils::image_state_change($img_state_change);
  272. return $this->success($wallpaper->id);
  273. }
  274. return $this->fail(200002);
  275. }
  276. private function collect(Request $request){
  277. $uid = $request->uid;
  278. $id = $request->id;
  279. if(_empty_($id)){
  280. return $this->fail(200001);
  281. }
  282. $collected_wallpaper = get_user_meta($uid, 'collected_wallpaper', 'j');
  283. if(_empty_($collected_wallpaper)){
  284. $collected_wallpaper = [];
  285. }
  286. if(in_array($id, $collected_wallpaper)){
  287. foreach($collected_wallpaper as $k=>$v){
  288. if($v == $id){
  289. unset($collected_wallpaper[$k]);
  290. break;
  291. }
  292. }
  293. if(update_user_meta($uid, 'collected_wallpaper', $collected_wallpaper, 'j')){
  294. return $this->success(['isCollect'=>0, 'msg'=>'已取消']);
  295. }else{
  296. return $this->fail(200002);
  297. }
  298. }else{
  299. $collected_wallpaper[] = $id;
  300. if(update_user_meta($uid, 'collected_wallpaper', $collected_wallpaper, 'j')){
  301. return $this->success(['isCollect'=>1, 'msg'=>'收藏成功']);
  302. }else{
  303. return $this->fail(200002);
  304. }
  305. }
  306. }
  307. }