first(); if (collect($data)->isEmpty()) { throw new \ApiException("不存在的公众号", 404); } $media_key = new MediaKeyModel(); $keywords = $media_key->where([ ['media_id', $media_id], ['app', $app] ])->get(); $keys = array(); $keywords->each(function ($item, $key) use (&$keys) { array_map(function ($value) use (&$keys) { array_push($keys, $value); }, json_decode($item->keywords, true)); }); $data->keywords = $keys ?: []; return $data; } /** * 是否开启某个app * @param string 公众号id * @param string app 应用 * @param string plat 平台 * @return boolean */ public function isOpen(string $media_id, string $app, string $plat = '') { if ($app == 'msy') { $data = self::where('media_id', $media_id)->first(); if (empty($data->keywords)) { return false; } else { return true; } } $media_key = new MediaKeyModel(); $first = $media_key->where([ ['media_id', $media_id], ['app', $app] ])->when($plat, function ($query) use ($plat) { return $query->where('plat', $plat); })->first(); if (collect($first)->isEmpty()) { return false; } else { return true; } } /** * 公众号收益总计 * @param string media_id 公众号id * @return array [ * 'count' => 交易笔数, * 'msy_money' => 累计收益, * 'today_count' => 今日笔数 * ] */ public function incomeData(string $media_id) { $msy_money = self::where('media_id', $media_id)->first(['gold_flower']); $count = MediaLogModel::where(function ($query) use ($media_id) { $query->where('pay_media', $media_id)->orWhere('from_media', $media_id); })->count(); $today_count = MediaLogModel::where(function ($query) use ($media_id) { $query->where('pay_media', $media_id)->orWhere('from_media', $media_id); })->whereBetween('create_at', [mktime(0, 0, 0), mktime(0, 0, 0) + 86400])->count(); return [ 'count' => $count, 'msy_money' => isset($msy_money->gold_flower) ? $msy_money->gold_flower : 0, 'today_count' => $today_count ]; } /** * 获取开启的公众号列表 * @param string app 应用 * @return array * [ * [ * 'media_id' => 公众号id, * ‘public_name' => 公众号名称, * 'school' => 学校 * ] * ] */ public function openMedias(string $app) { $hides = Redis::lrange("{pair_hides}", 0, -1); $medias = MediaKeyModel::where('app', $app)->whereNotIn( 'media_id', $hides )->groupBy('media_id')->get(['media_id'])->pluck('media_id'); $data = $this->where('type', 0)->whereIn('media_id', $medias)->get(['media_id', 'public_name', 'school']); return $data; } }