Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

李洋 / 消息系统

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • CMS-Message
  • app
  • Http
  • Controllers
  • FavoriteController.php
FavoriteController.php 5.03 KB
李洋's avatar
Initial commit
60b5dc25
 
李洋 committed 7 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Config;
use Log;

class FavoriteController extends Controller
{
    public function add(Request $request)
    {
        $type = $request->input('type', null);
        $data_id = $request->input('data_id', null);
        $args = $request->input('data_args', '');

        if ($type === null)
            return ['errcode' => 404, 'errmsg' => 'Bad Request: type not found'];
        if ($type != 1 && $type != 2)
            return ['errcode' => 404, 'errmsg' => 'Bad Request: invaild type ' . $type];
        if ($data_id === null)
            return ['errcode' => 404, 'errmsg' => 'Bad Request: data_id not found'];
        if (!empty($args) && !json_decode($args))
            return ['errcode' => 404, 'errmsg' => 'Bad Request: invaild data_args'];

        $userId = $request->user->userId;

        $sql = "REPLACE INTO favorites(data_type,data_id,data_args,user_id,ctime) VALUES(?,?,?,?,?)";
        $data= [$type, $data_id, $args, $userId, date('Y-m-d H:i:s')];

        // $sql = "REPLACE INTO favorites(data_type,data_id,user_id,ctime) VALUES(?,?,?,?)";
        // $data= [$type, $data_id, $userId, date('Y-m-d H:i:s')];

        try {
            DB::connection('tableconfig')->insert($sql, $data);

            return ['errcode' => 0, 'errmsg' => ''];
        } catch (\Exception $e) {
            return ['errcode' => 500, 'errmsg' => 'DB::insert failed:' . $e->getMessage()];
        }
    }

    public function del(Request $request, $favorite_id)
    {
        if ($favorite_id <= 0)
            return ['errcode' => 404, 'errmsg' => 'Bad Request: invaild favorite_id' . $favorite_id];
        $userId = $request->user->userId;

        try {
            $ret = DB::connection('tableconfig')->table('favorites')
                ->where('favorite_id', $favorite_id)->where('user_id', $userId)->delete();
            if ($ret) {
                return ['errcode' => 0, 'errmsg' => ''];
            } else {
                return ['errcode' => 404, 'errmsg' => "Bad Request: not found favorite_id {$favorite_id}"];
            }
        } catch (\Exception $e) {
            return ['errcode' => 500, 'errmsg' => 'DB::insert ' . $e->getMessage()];
        }
    }

    public function getList(Request $request, $type=null)
    {
        $page = $request->input('page', 1);
        $count= $request->input('count', 20);

        $userId= $request->user->userId;

        try {
            $q = DB::connection('tableconfig')->table('favorites')
                ->where('user_id', $userId);
            if ($type !== null)
                $q->where('data_type', $type);

            $total = $q->count();
            $data = ['errcode' => 0, 'data' => ['page' => $page, 'count' => $count, 'total' => $total, 'list' => [] ] ];
            if ($total == 0)
                return $data;
            $ret = $q->skip(($page-1) * $count)->take($count)->orderBy('ctime', 'DESC')->get();
            if (!$ret)
                return $data;

            $types = [];
            foreach ($ret as $item)
                $types[$item->data_type][] = $item->data_id;

            $urls = [];
            if (isset($types[1])) {
                $table = DB::connection('tableconfig')->table('tableconfig')->whereIn('table_id', $types[1])->get();
                if ($table) {
                    foreach ($table as $item) {
                        $urls[1][$item->table_id] = ['url'=> '/database/' . $item->table_id, 'title' => $item->title];
                    }
                }
            }

            if (isset($types[2])) {
                $config = DB::connection('tableconfig')->table('config')->whereIn('config_id', $types[2])->get();
                if ($config) {
                    foreach ($config as $item) {
                        $urls[2][$item->config_id] = ['url'=> '/setting/' . $item->config_id, 'title' => $item->config_title];
                    }
                }
            }

            if (isset($types[3])) {
                $config = DB::connection('tableconfig')->table('relation_config')->whereIn('relation_id', $types[3])->get();
                if ($config) {
                    foreach ($config as $item) {
                        $urls[3][$item->config_id] = ['url'=> '/relation/' . $item->relation_id, 'title' => $item->title];
                    }
                }
            }

            $outdata = [];
            foreach ($ret as $item) {
                if (!isset($urls[$item->data_type][$item->data_id]))
                    continue;
                $arr = $urls[$item->data_type][$item->data_id];
                $item->url = $arr['url'];
                $item->title = $arr['title'];
                if (isset($item->args))
                    $item->args = json_decode($item->args);
                $outdata[] = $item;
            }

            $data['data']['list'] = $outdata;
            return $data;
        } catch (\Exception $e) {
            return ['errcode' => 500, 'errmsg' => 'DB::insert failed: ' . $e->getMessage()];
        }
    }
}