| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\quanzi;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\User;
- use app\models\SaasUser;
- use app\models\QuanziCat;
- use app\models\QuanziLabel;
- use app\models\QuanziBgm;
- use app\models\QuanziUp;
- use app\models\QuanziComment;
- use app\models\QuanziFollow;
- use app\models\QuanziLabelUser;
- use app\models\QuanziContents;
- use app\utils\Notice\NoticeSend;
- use app\modules\client\models\v1\ShareQrcodeForm;
- use app\utils\Baidu\BaiduAiTools;
- use app\utils\DeepSeek\DeepSeekAiTools;
- use app\models\AccountLog;
- class QuanziForm extends Model {
- public $store_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $cat_id;
- public $supplier_id;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $is_delete;
- public $warning_num;
- public $eiId;
- public $eipId;
- public $nickname;
- public $phone;
- public $order_no;
- public $type;
- public $purchase_in_price;
- public function init() {
- parent::init();
- if (empty($this->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()
- ];
- }
- }
- }
|