store_id)) { $this->store_id = get_store_id(); } } public static function paramsClient($params, $client_user_id, $client_saas_user_id) { return array_merge($params, ['is_client' => 1, 'client_user_id' => $client_user_id, 'client_saas_user_id' => $client_saas_user_id]); } public static $confs = []; public static function conf($store_id = 0, $refresh = 0) { if (isset(self::$confs[$store_id]) && !$refresh) { return self::$confs[$store_id]; } $confDef = [ 'deepseek_apiKey' => '', 'baidu_api_appid' => '', 'baidu_api_key' => '', 'jifen_get_single_up_contents' => 0, 'jifen_get_types' => array_keys(QuanziComment::type()), 'jifen_get_single' => 0, 'jifen_max_get_day' => 0, 'style_list_page' => 0, ]; $conf = Option::get(OptionSetting::QUANZI_SETTING, $store_id, 'store')['value']; if ($conf) { $conf = array_merge($confDef, json_decode($conf, true)); } else { $conf = $confDef; } self::$confs[$store_id] = $conf; return $conf; } public static function confSave($store_id = 0, $config = []) { $oldConf = self::conf($store_id, 1); $conf = array_merge($oldConf, $config); $set = Option::set(OptionSetting::QUANZI_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store'); self::conf($store_id, 1); return $set; } public static function chatConf($store_id = 0) { $conf = self::conf($store_id); return [ 'apiKey' => $conf['deepseek_apiKey'], 'appid' => $conf['baidu_api_appid'], 'key' => $conf['baidu_api_key'], ]; } public static function chat2($store_id = 0, $question = '') { $data = BaiduAiTools::chat(self::chatConf($store_id), $question); return $data; } public static function chat($store_id = 0, $question = '') { $data = DeepSeekAiTools::chat(self::chatConf($store_id), $question, ''); return $data; } public static function afterContentsSave($contents, $insert, $changedAttributes) { if (!$insert && isset($changedAttributes['status']) && $contents->status == 1) { self::contentsUpJifen($contents); } } public static function afterCommentSave($comment, $insert, $changedAttributes) { if ($insert) { self::commentJifen($comment); } } public function upInfo($user_id) { $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]); $user = User::findOne($user_id); $saasUser = SaasUser::findOne(['mobile' => $user['binding']]); return [ 'code' => 0, 'data' => $info, 'id' => $info['id'], 'user' => $user, 'saasUser' => $saasUser, ]; } public function upHome($user_id, $up_user_id, $params = []) { $up = $this->upInfo($up_user_id); $isSelfHome = $user_id == $up_user_id; !$isSelfHome && QuanziUp::upInit($up_user_id)::updateAllCounters(['look_num' => 1], ['user_id' => $up_user_id]); $contentsCatCount = QuanziContents::find()->where(['user_id' => $up_user_id, 'is_delete' => 0, 'status' => isset($params['status']) ? $params['status'] : 1])->groupBy('cat_id')->select('cat_id, count(1) cat_count')->asArray()->all(); $contentsAllCount = array_sum(array_column($contentsCatCount, 'cat_count')); $cats = $this->catSelectList()['data']; foreach($contentsCatCount as &$item){ $item['cat_name'] = $cats[$item['cat_id']] ? $cats[$item['cat_id']]['name'] : '-'; } $client_user_follow = QuanziFollow::findOne(['up_user_id' => $up_user_id, 'user_id' => $user_id]); return array_merge($up, [ 'contentsAllCount' => $contentsAllCount, 'contentsCatCount' => $contentsCatCount, 'isSelfHome' => $isSelfHome, 'cats' => $cats, 'client_user_follow' => $client_user_follow, ]); } public function blackUpUserIdAdd($user_id, $up_user_id, $del = 0) { try { QuanziUp::blackUpUserIdAdd($user_id, $up_user_id, $del); return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function blackUpUserList($user_id) { try { $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]); $ids = $info['black_up_user_ids'] ? explode(',', $info['black_up_user_ids']) : []; $query = User::find()->where(['id' => $ids]); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $saasUser = SaasUser::findOne(['mobile' => $item['binding']]); $item['saasUser'] = $saasUser; } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, 'q' => $query->createCommand()->getRawSql(), ]; return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function contentsList($params = []) { try { $query = QuanziContents::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]); if ($params['is_client']) { $up = QuanziUp::upInit($params['client_user_id'])::findOne($params['client_user_id']); if($up && $up->black_up_user_ids){ $query->andWhere(['not in', 'user_id', explode(',', $up->black_up_user_ids)]); } } if (!empty($params['id'])) { if(!is_array($params['id']) && strstr($params['id'], ',')){ $params['id'] = explode(',', $params['id']); } $query->andWhere(['id' => $params['id']]); } if (!empty($params['user_id'])) { $query->andWhere(['user_id' => $params['user_id']]); } if (isset($params['status'])) { if ($params['status'] >= 0) { $query->andWhere(['status' => $params['status']]); } }else{ $query->andWhere(['status' => 1]); } if (!empty($params['title'])) { $query->andWhere([ 'or', ['like', 'title', trim($params['title'])], ['like', 'labels', trim($params['title'])], ['like', 'body', trim($params['title'])], ['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', trim($params['title'])])], ]); } if (!empty($params['cat_id'])) { $query->andWhere(['cat_id' => $params['cat_id']]); } if (!empty($params['label'])) { $query->andWhere('FIND_IN_SET("'. $params['label'] .'", labels)'); } if (!empty($params['start_time'])) { $query->andWhere(['>=', 'created_at', strtotime($params['start_time'])]); } if (!empty($params['end_time'])) { $query->andWhere(['<=', 'created_at', strtotime($params['end_time']) + 86400]); } if ($params['user_mobile']) { $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'mobile', $params['user_mobile']])]); } if ($params['user_name']) { $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', $params['user_name']])]); } if (!empty($params['orderByAdmin'])) { $query->addOrderBy('status ASC,id DESC'); }else{ $query->addOrderBy('id DESC'); } $pagination = pagination_make($query); $cats = $this->catSelectList()['data']; foreach ($pagination['list'] as &$item) { $item['timeAgo'] = timeAgo($item['created_at']); $item['cat_name'] = $cats[$item['cat_id']] ? $cats[$item['cat_id']]['name'] : '-'; $user = User::findOne($item['user_id']); $saasUser = SaasUser::findOne($item['saas_user_id']); $item['user'] = $user; $item['saasUser'] = $saasUser; if ($params['is_client']) { $isSelfHome = $params['client_user_id'] == $item['user_id']; $item['isSelfHome'] = $isSelfHome; $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]); $item['client_user_good'] = QuanziComment::findOne(['type' => QuanziComment::TYPE_GOOD, 'user_id' => $params['client_user_id'], 'contents_id' => $item['id'], 'comment_id' => 0, 'at_comment_id' => 0]); } if (!empty($params['id'])) { $activity_goods = \app\models\Goods::find()->where(['in', 'id', explode(',', $item['goods_id'])])->andWhere(['is_delete' => 0])->asArray()->all(); $item['activity_goods'] = $activity_goods; } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function contentsInfo($id, $params = []) { $info = $this->contentsList(array_merge($params, ['id' => $id, 'status' => -1])); if($info['code']){ return $info; } if(empty($info['data']['list'])){ return [ 'code' => 1, 'msg' => '数据不存在或参数错误', ]; } if($info['data'] && $info['data']['list']){ $info = $info['data']['list'][0]; } if($info['status'] != 1){ return [ 'code' => 1, 'msg' => '帖子未审核通过,无法查看', ]; } if ($params['is_client']) { if(!cache_lock(['QuanziContents', 'look_num', $id, $params['client_user_id']], 86400)){ QuanziContents::updateAllCounters(['look_num' => 1], ['id' => $id]); } QuanziContents::lookUserIdAdd($id, $params['client_saas_user_id']); $info['lookUser'] = QuanziContents::lookUserLogo($id); $info['body'] = nl2br($info['body']); } return [ 'code' => 0, 'data' => $info, ]; } public static function contentsUpJifen($contents) { $conf = self::conf($contents->store_id); $jifen = $conf['jifen_get_single_up_contents']; if($jifen <= 0){ return; } $contents->jifen = $jifen; $contents->save(); AccountLog::saveLog($contents->user_id, $jifen, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '发帖ID:'.$contents['id']); } /** * 修改字段(包括删除is_delete) * @param type $id * @param type $key * @param type $val * @return type * @throws \Exception */ public function contentsSaveKey($id, $key, $val) { try { if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $model = QuanziContents::findOne($item); if (!$model) { throw new \Exception('参数错误' . $item); } $model->$key = $val; if (!$model->save()) { throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors())); } } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function commentsList($params = []) { try { $query = QuanziComment::find()->where(['store_id' => $this->store_id]); if (!empty($params['id'])) { $query->andWhere(['id' => $params['id']]); }else{ if (!empty($params['contents_id'])) { $query->andWhere(['contents_id' => $params['contents_id']]); } if (isset($params['comment_id']) && $params['comment_id'] >= 0) { $query->andWhere(['comment_id' => $params['comment_id']]); } if (!empty($params['type_weigui'])) { $query->andWhere(['type_weigui' => $params['type_weigui']]); } if (empty($params['type'])) { $params['type'] = $params['comment_id'] ? QuanziComment::TYPE_REPLY : QuanziComment::TYPE_COMMENT; } if (isset($params['type']) && $params['type'] >= 0) { $query->andWhere(['type' => $params['type']]); } if ($params['jifen'] !== null) { $query->andWhere(['!=', 'jifen', 0]); } if (!empty($params['contents_title'])) { $query->andWhere(['contents_id' => QuanziContents::find()->select('id')->where(['like', 'title', trim($params['contents_title'])])]); } if (!empty($params['cat_id'])) { $query->andWhere(['contents_id' => QuanziContents::find()->select('id')->where(['cat_id' => $params['cat_id']])]); } if (!empty($params['start_time'])) { $query->andWhere(['>=', 'created_at', strtotime($params['start_time'])]); } if (!empty($params['end_time'])) { $query->andWhere(['<=', 'created_at', strtotime($params['end_time']) + 86400]); } if ($params['user_id']) { $query->andWhere(['user_id' => $params['user_id']]); } if ($params['user_mobile']) { $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'binding', $params['user_mobile']])]); } if ($params['user_name']) { $query->andWhere(['user_id' => User::find()->alias('u') ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile') ->select('u.id') ->where(['like', 'su.name', $params['user_name']]) ]); } if ($params['at_comment_user_id']) { $query->andWhere(['at_comment_user_id' => $params['at_comment_user_id']]); } if ($params['up_user_id']) { $query->andWhere(['up_user_id' => $params['up_user_id']]); } if ($params['up_mobile']) { $query->andWhere(['contents_id' => QuanziContents::find()->alias('qc') ->leftJoin(['su' => SaasUser::tableName()], 'qc.saas_user_id = su.id') ->select('qc.id') ->where(['like', 'su.mobile', $params['up_mobile']]) ]); } if ($params['up_name']) { $query->andWhere(['contents_id' => QuanziContents::find()->alias('qc') ->leftJoin(['su' => SaasUser::tableName()], 'qc.saas_user_id = su.id') ->select('u.id') ->where(['like', 'su.name', $params['user_name']]) ]); } } if (!empty($params['orderBy'])) { $query->addOrderBy($params['orderBy']); }else{ $query->addOrderBy('id DESC'); } $pagination = pagination_make($query); $cats = $this->catSelectList()['data']; foreach ($pagination['list'] as &$item) { $item['type_name'] = QuanziComment::type($item['type'], $item); $item['type_weigui_name'] = QuanziComment::typeWeigui($item['type_weigui']); $item['contents'] = QuanziContents::findOne($item['contents_id']); $item['cat_name'] = $cats[$item['contents']['cat_id']] ? $cats[$item['contents']['cat_id']]['name'] : '-'; $user = User::findOne($item['user_id']); $saasUser = SaasUser::findOne($item['saas_user_id']); $at_comment_saas_user = SaasUser::findOne($item['at_comment_saas_user_id']); $item['user'] = $user; $item['saasUser'] = $saasUser; $item['at_comment_saas_user'] = $at_comment_saas_user; if ($params['is_client']) { $isSelfHome = $params['client_user_id'] == $item['user_id']; $item['isSelfHome'] = $isSelfHome; $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]); $client_user_good_cond = ['comment_id' => $item['id'], 'at_comment_id' => 0]; if($item['type'] == QuanziComment::TYPE_REPLY){ $client_user_good_cond['comment_id'] = $item['comment_id']; $client_user_good_cond['at_comment_id'] = $item['id']; } $item['client_user_good'] = QuanziComment::findOne(['type' => QuanziComment::TYPE_GOOD, 'user_id' => $params['client_user_id'], 'contents_id' => $item['contents_id']] + $client_user_good_cond); } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, 'typeWeigui' => QuanziComment::typeWeigui(), 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public static function commentJifen($comment) { $conf = self::conf($comment->store_id); $jifen_get_types = $conf['jifen_get_types']; $jifen_get_single = $conf['jifen_get_single']; $jifen_max_get_day = $conf['jifen_max_get_day']; if(!in_array($comment->type, (array)$jifen_get_types) || $jifen_get_single <= 0){ return; } $daySum = (float)QuanziComment::find()->where(['and', ['user_id' => $comment->user_id], ['>', 'jifen', 0], ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))], ['<', 'created_at', time()]])->sum('jifen'); if($daySum + $jifen_get_single > $jifen_max_get_day){ return; } $jifen = $jifen_get_single; $comment->jifen = $jifen; $comment->save(); AccountLog::saveLog($comment->user_id, $jifen, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '评赞ID:'.$comment['id']); } public function catList($params = []) { try { $is_delete = 0; if ($params['is_delete'] == 1) { $is_delete = 1; } $query = QuanziCat::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!empty($params['id'])) { $query->andWhere(['id' => $params['id']]); } if (!is_null($params['is_show']) && $params['is_show'] > -1) { $query->andWhere(['is_show' => $params['is_show']]); } if (!empty($params['name'])) { $query->andWhere(['like', 'name', trim($params['name'])]); } $query->orderBy('sort DESC, id DESC'); $pagination = pagination_make($query); return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function catSelectList($indexBy = 1, $is_delete = 0) { $query = QuanziCat::find()->where(['store_id' => $this->store_id, 'is_delete' => $is_delete]) ->select('id,name,is_show,pic_url'); if ($indexBy) { $query->indexBy('id'); } $list = $query->orderBy('sort DESC, id DESC')->asArray()->all(); foreach ($list as &$item) { if (!$item['is_show']) { $item['name'] .= '(已隐藏)'; } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $list, ]; } public function catSave($params = []) { try { $model = $params['id'] ? QuanziCat::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziCat(); if (empty($model)) { throw new \Exception('参数错误' . $params['id']); } $model->name = $params['name']; $model->sort = $params['sort'] ?: 0; $model->is_show = $params['is_show']; $model->store_id = $this->store_id; isset($params['pic_url']) && $model->pic_url = $params['pic_url']; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('保存失败。' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 修改字段(包括删除is_delete) * @param type $id * @param type $key * @param type $val * @return type * @throws \Exception */ public function catSaveKey($id, $key, $val) { try { if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $model = QuanziCat::findOne($item); if (!$model) { throw new \Exception('参数错误' . $item); } $model->$key = $val; if (!$model->save()) { throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors())); } } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function labelList($params = []) { try { $is_delete = 0; if ($params['is_delete'] == 1) { $is_delete = 1; } $query = QuanziLabel::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!empty($params['id'])) { $query->andWhere(['id' => $params['id']]); } if (!is_null($params['is_show']) && $params['is_show'] > -1) { $query->andWhere(['is_show' => $params['is_show']]); } if (!empty($params['name'])) { $query->andWhere(['like', 'name', trim($params['name'])]); } $query->orderBy('sort DESC, id DESC'); $pagination = pagination_make($query); return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function labelSelectList($indexBy = 1, $is_delete = 0) { $query = QuanziLabel::find()->where(['store_id' => $this->store_id, 'is_delete' => $is_delete]) ->select('id,name,is_show'); if ($indexBy) { $query->indexBy('id'); } $list = $query->orderBy('sort DESC, id DESC')->asArray()->all(); foreach ($list as &$item) { if (!$item['is_show']) { $item['name'] .= '(已隐藏)'; } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $list, ]; } public function labelSave($params = []) { try { $model = $params['id'] ? QuanziLabel::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziLabel(); if (empty($model)) { throw new \Exception('参数错误' . $params['id']); } $model->name = $params['name']; $model->sort = $params['sort'] ?: 0; $model->is_show = $params['is_show']; $model->store_id = $this->store_id; isset($params['pic_url']) && $model->pic_url = $params['pic_url']; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('保存失败。' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 修改字段(包括删除is_delete) * @param type $id * @param type $key * @param type $val * @return type * @throws \Exception */ public function labelSaveKey($id, $key, $val) { try { if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $model = QuanziLabel::findOne($item); if (!$model) { throw new \Exception('参数错误' . $item); } $model->$key = $val; if (!$model->save()) { throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors())); } } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function labelUserSave($user_id, $label = '') { try { QuanziUp::labelUserSave($user_id, $label); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function bgmList($params = []) { try { $is_delete = 0; if ($params['is_delete'] == 1) { $is_delete = 1; } $query = QuanziBgm::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!empty($params['id'])) { $query->andWhere(['id' => $params['id']]); } if (!is_null($params['is_show']) && $params['is_show'] > -1) { $query->andWhere(['is_show' => $params['is_show']]); } if (!empty($params['name'])) { $query->andWhere(['like', 'name', trim($params['name'])]); } $query->orderBy('sort DESC, id DESC'); $pagination = pagination_make($query); return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function bgmSelectList($indexBy = 1) { $query = QuanziBgm::find()->where(['store_id' => $this->store_id]) ->select('id,name,is_show'); if ($indexBy) { $query->indexBy('id'); } $list = $query->orderBy('sort DESC, id DESC')->asArray()->all(); foreach ($list as &$item) { if (!$item['is_show']) { $item['name'] .= '(已隐藏)'; } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $list, ]; } public function bgmSave($params = []) { try { $model = $params['id'] ? QuanziBgm::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziBgm(); if (empty($model)) { throw new \Exception('参数错误' . $params['id']); } $model->name = $params['name']; $model->sort = $params['sort'] ?: 0; $model->is_show = $params['is_show']; $model->bgm_url = $params['bgm_url']; $model->store_id = $this->store_id; isset($params['pic_url']) && $model->pic_url = $params['pic_url']; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('保存失败。' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 修改字段(包括删除is_delete) * @param type $id * @param type $key * @param type $val * @return type * @throws \Exception */ public function bgmSaveKey($id, $key, $val) { try { if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $model = QuanziBgm::findOne($item); if (!$model) { throw new \Exception('参数错误' . $item); } $model->$key = $val; if (!$model->save()) { throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors())); } } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function followList($params = []) { try { $query = QuanziFollow::find()->where(['store_id' => $this->store_id]); if (!empty($params['user_id'])) { $query->andWhere(['user_id' => $params['user_id']]); } if (!empty($params['up_user_id'])) { $query->andWhere(['up_user_id' => $params['up_user_id']]); } $query->addOrderBy('id DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $user = User::findOne(!empty($params['user_id']) ? $item['up_user_id'] : $item['user_id']); $saasUser = SaasUser::findOne(['mobile' => $user['binding']]); $item['user'] = $user; $item['saasUser'] = $saasUser; if($params['is_client']){ $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]); } } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $pagination, 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function followAdd($up_user_id, $user_id = 0) { try{ $follow = QuanziFollow::findOne(['up_user_id' => $up_user_id, 'user_id' => $user_id]); if (!$follow) { $model = new QuanziFollow(); $model->store_id = $this->store_id; $model->up_user_id = $up_user_id; $model->user_id = $user_id; if (!$model->save()) { return [ 'code' => 1, 'msg' => '操作失败!' . array_shift($model->getFirstErrors()), ]; } QuanziUp::upInit($up_user_id)::updateAllCounters(['fans_num' => 1], ['user_id' => $up_user_id]); QuanziUp::upInit($user_id)::updateAllCounters(['follow_num' => 1], ['user_id' => $user_id]); } return [ 'code' => 0, 'msg' => '操作成功', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function followDel($up_user_id, $user_id = 0) { $up = QuanziFollow::deleteAll(['up_user_id' => $up_user_id, 'user_id' => $user_id]); if ($up) { try { QuanziUp::upInit($up_user_id)::updateAllCounters(['fans_num' => -1], ['user_id' => $up_user_id]); QuanziUp::upInit($user_id)::updateAllCounters(['follow_num' => -1], ['user_id' => $user_id]); } catch (\Exception $e) { } } return [ 'code' => 0, 'msg' => '操作成功', ]; } public function contentsAdd($user_id = 0, $saas_user_id = 0, $params = []) { try{ if(empty($params['cat_id']) || empty($params['title']) || empty($params['body'])){ throw new \Exception('操作失败!参数错误。'); } $id = $params['id']; $model = $id ? QuanziContents::findOne($id) : new QuanziContents(); $model->store_id = $this->store_id; $saas_user_id && $model->saas_user_id = $saas_user_id; $user_id && $model->user_id = $user_id; empty($params['cat_id']) || $model->cat_id = $params['cat_id']; $model->title = $params['title']; $model->body = $params['body']; empty($params['labels']) || $model->labels = $params['labels']; empty($params['pic_url']) || $model->pic_url = is_array($params['pic_url']) ? json_encode($params['pic_url']) : $params['pic_url']; empty($params['video_url']) || $model->video_url = $params['video_url']; empty($params['goods_id']) || $model->goods_id = $params['goods_id']; empty($params['bgm_url']) || $model->bgm_url = $params['bgm_url']; if (!$model->save()) { throw new \Exception('操作失败!' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $model, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function comment($del, $type, $contents_id, $user_id = 0, $saas_user_id = 0, $comment_id = 0, $at_comment_id = 0, $content = '', $type_weigui = 0) { try{ $contents = QuanziContents::findOne($contents_id); $up_user_id = $contents['user_id']; if($type == QuanziComment::TYPE_COMMENT && ($comment_id || $at_comment_id)){ $type = QuanziComment::TYPE_REPLY; } $model = QuanziComment::findOne([ 'type' => $type, 'up_user_id' => $up_user_id, 'user_id' => $user_id, 'contents_id' => $contents_id, 'comment_id' => $comment_id, 'at_comment_id' => $at_comment_id, ]); if($del){ if(!$model){ throw new \Exception('操作失败!没有数据。'); } if(!$model->delete()){ throw new \Exception('操作失败!数据异常。'); } }else{ if(in_array($type, [QuanziComment::TYPE_GOOD])){ if($model){ throw new \Exception('操作失败!已经点赞过。'); } } $model = new QuanziComment(); $model->store_id = $this->store_id; $model->type = $type; $model->type_weigui = $type_weigui; $model->up_user_id = $up_user_id; $model->contents_id = $contents_id; $model->comment_id = $comment_id; $model->at_comment_id = $at_comment_id; $model->user_id = $user_id; $model->saas_user_id = $saas_user_id; $model->content = $content; if($at_comment_id){ $at_comment = QuanziComment::findOne($at_comment_id); $model->at_comment_user_id = $at_comment->user_id; $model->at_comment_saas_user_id = $at_comment->saas_user_id; } if (!$model->save()) { throw new \Exception('操作失败!' . array_shift($model->getFirstErrors())); } } $arr = [ QuanziComment::TYPE_COMMENT => 'comment_num', QuanziComment::TYPE_REPLY => 'comment_num', QuanziComment::TYPE_GOOD => 'good_num', QuanziComment::TYPE_SHARE => 'share_num', ]; $count = $del ? -1 : 1; if(in_array($type, array_keys($arr)) && !$comment_id && !$at_comment_id){ QuanziUp::upInit($up_user_id)::updateAllCounters([$arr[$type] => $count], ['user_id' => $up_user_id]); QuanziContents::updateAllCounters([$arr[$type] => $count], ['id' => $contents_id]); } if(in_array($type, array_keys($arr)) && ($comment_id || $at_comment_id)){ QuanziComment::updateAllCounters([$arr[$type] => $count], ['id' => $at_comment_id ? $at_comment_id : $comment_id]); } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $del ? null : $this->commentsList(self::paramsClient(['id' => $model->id], $model->user_id, $model->saas_user_id))['data']['list'][0], ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }