QuanziForm.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\quanzi;
  8. use app\models\Option;
  9. use app\constants\OptionSetting;
  10. use app\models\User;
  11. use app\models\SaasUser;
  12. use app\models\QuanziCat;
  13. use app\models\QuanziLabel;
  14. use app\models\QuanziBgm;
  15. use app\models\QuanziUp;
  16. use app\models\QuanziComment;
  17. use app\models\QuanziFollow;
  18. use app\models\QuanziLabelUser;
  19. use app\models\QuanziContents;
  20. use app\utils\Notice\NoticeSend;
  21. use app\modules\client\models\v1\ShareQrcodeForm;
  22. use app\utils\Baidu\BaiduAiTools;
  23. use app\utils\DeepSeek\DeepSeekAiTools;
  24. use app\models\AccountLog;
  25. class QuanziForm extends Model {
  26. public $store_id;
  27. public $id;
  28. public $ids;
  29. public $goods_name;
  30. public $supplier_name;
  31. public $goods_id;
  32. public $cat_id;
  33. public $supplier_id;
  34. public $warehouse_id;
  35. public $warehouse_zone_id;
  36. public $is_delete;
  37. public $warning_num;
  38. public $eiId;
  39. public $eipId;
  40. public $nickname;
  41. public $phone;
  42. public $order_no;
  43. public $type;
  44. public $purchase_in_price;
  45. public function init() {
  46. parent::init();
  47. if (empty($this->store_id)) {
  48. $this->store_id = get_store_id();
  49. }
  50. }
  51. public static function paramsClient($params, $client_user_id, $client_saas_user_id) {
  52. return array_merge($params, ['is_client' => 1, 'client_user_id' => $client_user_id, 'client_saas_user_id' => $client_saas_user_id]);
  53. }
  54. public static $confs = [];
  55. public static function conf($store_id = 0, $refresh = 0) {
  56. if (isset(self::$confs[$store_id]) && !$refresh) {
  57. return self::$confs[$store_id];
  58. }
  59. $confDef = [
  60. 'deepseek_apiKey' => '',
  61. 'baidu_api_appid' => '',
  62. 'baidu_api_key' => '',
  63. 'jifen_get_single_up_contents' => 0,
  64. 'jifen_get_types' => array_keys(QuanziComment::type()),
  65. 'jifen_get_single' => 0,
  66. 'jifen_max_get_day' => 0,
  67. 'style_list_page' => 0,
  68. ];
  69. $conf = Option::get(OptionSetting::QUANZI_SETTING, $store_id, 'store')['value'];
  70. if ($conf) {
  71. $conf = array_merge($confDef, json_decode($conf, true));
  72. } else {
  73. $conf = $confDef;
  74. }
  75. self::$confs[$store_id] = $conf;
  76. return $conf;
  77. }
  78. public static function confSave($store_id = 0, $config = []) {
  79. $oldConf = self::conf($store_id, 1);
  80. $conf = array_merge($oldConf, $config);
  81. $set = Option::set(OptionSetting::QUANZI_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store');
  82. self::conf($store_id, 1);
  83. return $set;
  84. }
  85. public static function chatConf($store_id = 0) {
  86. $conf = self::conf($store_id);
  87. return [
  88. 'apiKey' => $conf['deepseek_apiKey'],
  89. 'appid' => $conf['baidu_api_appid'],
  90. 'key' => $conf['baidu_api_key'],
  91. ];
  92. }
  93. public static function chat2($store_id = 0, $question = '') {
  94. $data = BaiduAiTools::chat(self::chatConf($store_id), $question);
  95. return $data;
  96. }
  97. public static function chat($store_id = 0, $question = '') {
  98. $data = DeepSeekAiTools::chat(self::chatConf($store_id), $question, '');
  99. return $data;
  100. }
  101. public static function afterContentsSave($contents, $insert, $changedAttributes) {
  102. if (!$insert && isset($changedAttributes['status']) && $contents->status == 1) {
  103. self::contentsUpJifen($contents);
  104. }
  105. }
  106. public static function afterCommentSave($comment, $insert, $changedAttributes) {
  107. if ($insert) {
  108. self::commentJifen($comment);
  109. }
  110. }
  111. public function upInfo($user_id) {
  112. $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]);
  113. $user = User::findOne($user_id);
  114. $saasUser = SaasUser::findOne(['mobile' => $user['binding']]);
  115. return [
  116. 'code' => 0,
  117. 'data' => $info,
  118. 'id' => $info['id'],
  119. 'user' => $user,
  120. 'saasUser' => $saasUser,
  121. ];
  122. }
  123. public function upHome($user_id, $up_user_id, $params = []) {
  124. $up = $this->upInfo($up_user_id);
  125. $isSelfHome = $user_id == $up_user_id;
  126. !$isSelfHome && QuanziUp::upInit($up_user_id)::updateAllCounters(['look_num' => 1], ['user_id' => $up_user_id]);
  127. $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();
  128. $contentsAllCount = array_sum(array_column($contentsCatCount, 'cat_count'));
  129. $cats = $this->catSelectList()['data'];
  130. foreach($contentsCatCount as &$item){
  131. $item['cat_name'] = $cats[$item['cat_id']] ? $cats[$item['cat_id']]['name'] : '-';
  132. }
  133. $client_user_follow = QuanziFollow::findOne(['up_user_id' => $up_user_id, 'user_id' => $user_id]);
  134. return array_merge($up, [
  135. 'contentsAllCount' => $contentsAllCount,
  136. 'contentsCatCount' => $contentsCatCount,
  137. 'isSelfHome' => $isSelfHome,
  138. 'cats' => $cats,
  139. 'client_user_follow' => $client_user_follow,
  140. ]);
  141. }
  142. public function blackUpUserIdAdd($user_id, $up_user_id, $del = 0) {
  143. try {
  144. QuanziUp::blackUpUserIdAdd($user_id, $up_user_id, $del);
  145. return [
  146. 'code' => 0,
  147. 'msg' => '操作成功',
  148. ];
  149. } catch (\Exception $e) {
  150. \Yii::error($e);
  151. return [
  152. 'code' => 1,
  153. 'msg' => $e->getMessage()
  154. ];
  155. }
  156. }
  157. public function blackUpUserList($user_id) {
  158. try {
  159. $info = QuanziUp::upInit($user_id)::findOne(['user_id' => $user_id]);
  160. $ids = $info['black_up_user_ids'] ? explode(',', $info['black_up_user_ids']) : [];
  161. $query = User::find()->where(['id' => $ids]);
  162. $pagination = pagination_make($query);
  163. foreach ($pagination['list'] as &$item) {
  164. $saasUser = SaasUser::findOne(['mobile' => $item['binding']]);
  165. $item['saasUser'] = $saasUser;
  166. }
  167. return [
  168. 'code' => 0,
  169. 'msg' => '操作成功',
  170. 'data' => $pagination,
  171. 'q' => $query->createCommand()->getRawSql(),
  172. ];
  173. return [
  174. 'code' => 0,
  175. 'msg' => '操作成功',
  176. ];
  177. } catch (\Exception $e) {
  178. \Yii::error($e);
  179. return [
  180. 'code' => 1,
  181. 'msg' => $e->getMessage()
  182. ];
  183. }
  184. }
  185. public function contentsList($params = []) {
  186. try {
  187. $query = QuanziContents::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
  188. if ($params['is_client']) {
  189. $up = QuanziUp::upInit($params['client_user_id'])::findOne($params['client_user_id']);
  190. if($up && $up->black_up_user_ids){
  191. $query->andWhere(['not in', 'user_id', explode(',', $up->black_up_user_ids)]);
  192. }
  193. }
  194. if (!empty($params['id'])) {
  195. if(!is_array($params['id']) && strstr($params['id'], ',')){
  196. $params['id'] = explode(',', $params['id']);
  197. }
  198. $query->andWhere(['id' => $params['id']]);
  199. }
  200. if (!empty($params['user_id'])) {
  201. $query->andWhere(['user_id' => $params['user_id']]);
  202. }
  203. if (isset($params['status'])) {
  204. if ($params['status'] >= 0) {
  205. $query->andWhere(['status' => $params['status']]);
  206. }
  207. }else{
  208. $query->andWhere(['status' => 1]);
  209. }
  210. if (!empty($params['title'])) {
  211. $query->andWhere([
  212. 'or',
  213. ['like', 'title', trim($params['title'])],
  214. ['like', 'labels', trim($params['title'])],
  215. ['like', 'body', trim($params['title'])],
  216. ['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', trim($params['title'])])],
  217. ]);
  218. }
  219. if (!empty($params['cat_id'])) {
  220. $query->andWhere(['cat_id' => $params['cat_id']]);
  221. }
  222. if (!empty($params['label'])) {
  223. $query->andWhere('FIND_IN_SET("'. $params['label'] .'", labels)');
  224. }
  225. if (!empty($params['start_time'])) {
  226. $query->andWhere(['>=', 'created_at', strtotime($params['start_time'])]);
  227. }
  228. if (!empty($params['end_time'])) {
  229. $query->andWhere(['<=', 'created_at', strtotime($params['end_time']) + 86400]);
  230. }
  231. if ($params['user_mobile']) {
  232. $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'mobile', $params['user_mobile']])]);
  233. }
  234. if ($params['user_name']) {
  235. $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', $params['user_name']])]);
  236. }
  237. if (!empty($params['orderByAdmin'])) {
  238. $query->addOrderBy('status ASC,id DESC');
  239. }else{
  240. $query->addOrderBy('id DESC');
  241. }
  242. $pagination = pagination_make($query);
  243. $cats = $this->catSelectList()['data'];
  244. foreach ($pagination['list'] as &$item) {
  245. $item['timeAgo'] = timeAgo($item['created_at']);
  246. $item['cat_name'] = $cats[$item['cat_id']] ? $cats[$item['cat_id']]['name'] : '-';
  247. $user = User::findOne($item['user_id']);
  248. $saasUser = SaasUser::findOne($item['saas_user_id']);
  249. $item['user'] = $user;
  250. $item['saasUser'] = $saasUser;
  251. if ($params['is_client']) {
  252. $isSelfHome = $params['client_user_id'] == $item['user_id'];
  253. $item['isSelfHome'] = $isSelfHome;
  254. $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]);
  255. $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]);
  256. }
  257. if (!empty($params['id'])) {
  258. $activity_goods = \app\models\Goods::find()->where(['in', 'id', explode(',', $item['goods_id'])])->andWhere(['is_delete' => 0])->asArray()->all();
  259. $item['activity_goods'] = $activity_goods;
  260. }
  261. }
  262. return [
  263. 'code' => 0,
  264. 'msg' => '操作成功',
  265. 'data' => $pagination,
  266. 'q' => $query->createCommand()->getRawSql(),
  267. ];
  268. } catch (\Exception $e) {
  269. \Yii::error($e);
  270. return [
  271. 'code' => 1,
  272. 'msg' => $e->getMessage()
  273. ];
  274. }
  275. }
  276. public function contentsInfo($id, $params = []) {
  277. $info = $this->contentsList(array_merge($params, ['id' => $id, 'status' => -1]));
  278. if($info['code']){
  279. return $info;
  280. }
  281. if(empty($info['data']['list'])){
  282. return [
  283. 'code' => 1,
  284. 'msg' => '数据不存在或参数错误',
  285. ];
  286. }
  287. if($info['data'] && $info['data']['list']){
  288. $info = $info['data']['list'][0];
  289. }
  290. if($info['status'] != 1){
  291. return [
  292. 'code' => 1,
  293. 'msg' => '帖子未审核通过,无法查看',
  294. ];
  295. }
  296. if ($params['is_client']) {
  297. if(!cache_lock(['QuanziContents', 'look_num', $id, $params['client_user_id']], 86400)){
  298. QuanziContents::updateAllCounters(['look_num' => 1], ['id' => $id]);
  299. }
  300. QuanziContents::lookUserIdAdd($id, $params['client_saas_user_id']);
  301. $info['lookUser'] = QuanziContents::lookUserLogo($id);
  302. $info['body'] = nl2br($info['body']);
  303. }
  304. return [
  305. 'code' => 0,
  306. 'data' => $info,
  307. ];
  308. }
  309. public static function contentsUpJifen($contents) {
  310. $conf = self::conf($contents->store_id);
  311. $jifen = $conf['jifen_get_single_up_contents'];
  312. if($jifen <= 0){
  313. return;
  314. }
  315. $contents->jifen = $jifen;
  316. $contents->save();
  317. AccountLog::saveLog($contents->user_id, $jifen, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '发帖ID:'.$contents['id']);
  318. }
  319. /**
  320. * 修改字段(包括删除is_delete)
  321. * @param type $id
  322. * @param type $key
  323. * @param type $val
  324. * @return type
  325. * @throws \Exception
  326. */
  327. public function contentsSaveKey($id, $key, $val) {
  328. try {
  329. if (!is_array($id)) {
  330. $id = explode(',', $id);
  331. }
  332. foreach ($id as $item) {
  333. $model = QuanziContents::findOne($item);
  334. if (!$model) {
  335. throw new \Exception('参数错误' . $item);
  336. }
  337. $model->$key = $val;
  338. if (!$model->save()) {
  339. throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors()));
  340. }
  341. }
  342. return [
  343. 'code' => 0,
  344. 'msg' => '操作成功',
  345. ];
  346. } catch (\Exception $e) {
  347. \Yii::error($e);
  348. return [
  349. 'code' => 1,
  350. 'msg' => $e->getMessage()
  351. ];
  352. }
  353. }
  354. public function commentsList($params = []) {
  355. try {
  356. $query = QuanziComment::find()->where(['store_id' => $this->store_id]);
  357. if (!empty($params['id'])) {
  358. $query->andWhere(['id' => $params['id']]);
  359. }else{
  360. if (!empty($params['contents_id'])) {
  361. $query->andWhere(['contents_id' => $params['contents_id']]);
  362. }
  363. if (isset($params['comment_id']) && $params['comment_id'] >= 0) {
  364. $query->andWhere(['comment_id' => $params['comment_id']]);
  365. }
  366. if (!empty($params['type_weigui'])) {
  367. $query->andWhere(['type_weigui' => $params['type_weigui']]);
  368. }
  369. if (empty($params['type'])) {
  370. $params['type'] = $params['comment_id'] ? QuanziComment::TYPE_REPLY : QuanziComment::TYPE_COMMENT;
  371. }
  372. if (isset($params['type']) && $params['type'] >= 0) {
  373. $query->andWhere(['type' => $params['type']]);
  374. }
  375. if ($params['jifen'] !== null) {
  376. $query->andWhere(['!=', 'jifen', 0]);
  377. }
  378. if (!empty($params['contents_title'])) {
  379. $query->andWhere(['contents_id' => QuanziContents::find()->select('id')->where(['like', 'title', trim($params['contents_title'])])]);
  380. }
  381. if (!empty($params['cat_id'])) {
  382. $query->andWhere(['contents_id' => QuanziContents::find()->select('id')->where(['cat_id' => $params['cat_id']])]);
  383. }
  384. if (!empty($params['start_time'])) {
  385. $query->andWhere(['>=', 'created_at', strtotime($params['start_time'])]);
  386. }
  387. if (!empty($params['end_time'])) {
  388. $query->andWhere(['<=', 'created_at', strtotime($params['end_time']) + 86400]);
  389. }
  390. if ($params['user_id']) {
  391. $query->andWhere(['user_id' => $params['user_id']]);
  392. }
  393. if ($params['user_mobile']) {
  394. $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'binding', $params['user_mobile']])]);
  395. }
  396. if ($params['user_name']) {
  397. $query->andWhere(['user_id' => User::find()->alias('u')
  398. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  399. ->select('u.id')
  400. ->where(['like', 'su.name', $params['user_name']])
  401. ]);
  402. }
  403. if ($params['at_comment_user_id']) {
  404. $query->andWhere(['at_comment_user_id' => $params['at_comment_user_id']]);
  405. }
  406. if ($params['up_user_id']) {
  407. $query->andWhere(['up_user_id' => $params['up_user_id']]);
  408. }
  409. if ($params['up_mobile']) {
  410. $query->andWhere(['contents_id' => QuanziContents::find()->alias('qc')
  411. ->leftJoin(['su' => SaasUser::tableName()], 'qc.saas_user_id = su.id')
  412. ->select('qc.id')
  413. ->where(['like', 'su.mobile', $params['up_mobile']])
  414. ]);
  415. }
  416. if ($params['up_name']) {
  417. $query->andWhere(['contents_id' => QuanziContents::find()->alias('qc')
  418. ->leftJoin(['su' => SaasUser::tableName()], 'qc.saas_user_id = su.id')
  419. ->select('u.id')
  420. ->where(['like', 'su.name', $params['user_name']])
  421. ]);
  422. }
  423. }
  424. if (!empty($params['orderBy'])) {
  425. $query->addOrderBy($params['orderBy']);
  426. }else{
  427. $query->addOrderBy('id DESC');
  428. }
  429. $pagination = pagination_make($query);
  430. $cats = $this->catSelectList()['data'];
  431. foreach ($pagination['list'] as &$item) {
  432. $item['type_name'] = QuanziComment::type($item['type'], $item);
  433. $item['type_weigui_name'] = QuanziComment::typeWeigui($item['type_weigui']);
  434. $item['contents'] = QuanziContents::findOne($item['contents_id']);
  435. $item['cat_name'] = $cats[$item['contents']['cat_id']] ? $cats[$item['contents']['cat_id']]['name'] : '-';
  436. $user = User::findOne($item['user_id']);
  437. $saasUser = SaasUser::findOne($item['saas_user_id']);
  438. $at_comment_saas_user = SaasUser::findOne($item['at_comment_saas_user_id']);
  439. $item['user'] = $user;
  440. $item['saasUser'] = $saasUser;
  441. $item['at_comment_saas_user'] = $at_comment_saas_user;
  442. if ($params['is_client']) {
  443. $isSelfHome = $params['client_user_id'] == $item['user_id'];
  444. $item['isSelfHome'] = $isSelfHome;
  445. $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]);
  446. $client_user_good_cond = ['comment_id' => $item['id'], 'at_comment_id' => 0];
  447. if($item['type'] == QuanziComment::TYPE_REPLY){
  448. $client_user_good_cond['comment_id'] = $item['comment_id'];
  449. $client_user_good_cond['at_comment_id'] = $item['id'];
  450. }
  451. $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);
  452. }
  453. }
  454. return [
  455. 'code' => 0,
  456. 'msg' => '操作成功',
  457. 'data' => $pagination,
  458. 'typeWeigui' => QuanziComment::typeWeigui(),
  459. 'q' => $query->createCommand()->getRawSql(),
  460. ];
  461. } catch (\Exception $e) {
  462. \Yii::error($e);
  463. return [
  464. 'code' => 1,
  465. 'msg' => $e->getMessage()
  466. ];
  467. }
  468. }
  469. public static function commentJifen($comment) {
  470. $conf = self::conf($comment->store_id);
  471. $jifen_get_types = $conf['jifen_get_types'];
  472. $jifen_get_single = $conf['jifen_get_single'];
  473. $jifen_max_get_day = $conf['jifen_max_get_day'];
  474. if(!in_array($comment->type, (array)$jifen_get_types) || $jifen_get_single <= 0){
  475. return;
  476. }
  477. $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');
  478. if($daySum + $jifen_get_single > $jifen_max_get_day){
  479. return;
  480. }
  481. $jifen = $jifen_get_single;
  482. $comment->jifen = $jifen;
  483. $comment->save();
  484. AccountLog::saveLog($comment->user_id, $jifen, AccountLog::TYPE_INTEGRAL, AccountLog::LOG_TYPE_INCOME, 0, 0, '评赞ID:'.$comment['id']);
  485. }
  486. public function catList($params = []) {
  487. try {
  488. $is_delete = 0;
  489. if ($params['is_delete'] == 1) {
  490. $is_delete = 1;
  491. }
  492. $query = QuanziCat::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  493. if (!empty($params['id'])) {
  494. $query->andWhere(['id' => $params['id']]);
  495. }
  496. if (!is_null($params['is_show']) && $params['is_show'] > -1) {
  497. $query->andWhere(['is_show' => $params['is_show']]);
  498. }
  499. if (!empty($params['name'])) {
  500. $query->andWhere(['like', 'name', trim($params['name'])]);
  501. }
  502. $query->orderBy('sort DESC, id DESC');
  503. $pagination = pagination_make($query);
  504. return [
  505. 'code' => 0,
  506. 'msg' => '操作成功',
  507. 'data' => $pagination,
  508. // 'q' => $query->createCommand()->getRawSql(),
  509. ];
  510. } catch (\Exception $e) {
  511. \Yii::error($e);
  512. return [
  513. 'code' => 1,
  514. 'msg' => $e->getMessage()
  515. ];
  516. }
  517. }
  518. public function catSelectList($indexBy = 1, $is_delete = 0) {
  519. $query = QuanziCat::find()->where(['store_id' => $this->store_id, 'is_delete' => $is_delete])
  520. ->select('id,name,is_show,pic_url');
  521. if ($indexBy) {
  522. $query->indexBy('id');
  523. }
  524. $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
  525. foreach ($list as &$item) {
  526. if (!$item['is_show']) {
  527. $item['name'] .= '(已隐藏)';
  528. }
  529. }
  530. return [
  531. 'code' => 0,
  532. 'msg' => '操作成功',
  533. 'data' => $list,
  534. ];
  535. }
  536. public function catSave($params = []) {
  537. try {
  538. $model = $params['id'] ? QuanziCat::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziCat();
  539. if (empty($model)) {
  540. throw new \Exception('参数错误' . $params['id']);
  541. }
  542. $model->name = $params['name'];
  543. $model->sort = $params['sort'] ?: 0;
  544. $model->is_show = $params['is_show'];
  545. $model->store_id = $this->store_id;
  546. isset($params['pic_url']) && $model->pic_url = $params['pic_url'];
  547. if (!$model->save()) {
  548. \Yii::error([__METHOD__, $model->attributes]);
  549. throw new \Exception('保存失败。' . array_shift($model->getFirstErrors()));
  550. }
  551. return [
  552. 'code' => 0,
  553. 'msg' => '操作成功!'
  554. ];
  555. } catch (\Exception $e) {
  556. \Yii::error($e);
  557. return [
  558. 'code' => 1,
  559. 'msg' => $e->getMessage()
  560. ];
  561. }
  562. }
  563. /**
  564. * 修改字段(包括删除is_delete)
  565. * @param type $id
  566. * @param type $key
  567. * @param type $val
  568. * @return type
  569. * @throws \Exception
  570. */
  571. public function catSaveKey($id, $key, $val) {
  572. try {
  573. if (!is_array($id)) {
  574. $id = explode(',', $id);
  575. }
  576. foreach ($id as $item) {
  577. $model = QuanziCat::findOne($item);
  578. if (!$model) {
  579. throw new \Exception('参数错误' . $item);
  580. }
  581. $model->$key = $val;
  582. if (!$model->save()) {
  583. throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors()));
  584. }
  585. }
  586. return [
  587. 'code' => 0,
  588. 'msg' => '操作成功',
  589. ];
  590. } catch (\Exception $e) {
  591. \Yii::error($e);
  592. return [
  593. 'code' => 1,
  594. 'msg' => $e->getMessage()
  595. ];
  596. }
  597. }
  598. public function labelList($params = []) {
  599. try {
  600. $is_delete = 0;
  601. if ($params['is_delete'] == 1) {
  602. $is_delete = 1;
  603. }
  604. $query = QuanziLabel::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  605. if (!empty($params['id'])) {
  606. $query->andWhere(['id' => $params['id']]);
  607. }
  608. if (!is_null($params['is_show']) && $params['is_show'] > -1) {
  609. $query->andWhere(['is_show' => $params['is_show']]);
  610. }
  611. if (!empty($params['name'])) {
  612. $query->andWhere(['like', 'name', trim($params['name'])]);
  613. }
  614. $query->orderBy('sort DESC, id DESC');
  615. $pagination = pagination_make($query);
  616. return [
  617. 'code' => 0,
  618. 'msg' => '操作成功',
  619. 'data' => $pagination,
  620. // 'q' => $query->createCommand()->getRawSql(),
  621. ];
  622. } catch (\Exception $e) {
  623. \Yii::error($e);
  624. return [
  625. 'code' => 1,
  626. 'msg' => $e->getMessage()
  627. ];
  628. }
  629. }
  630. public function labelSelectList($indexBy = 1, $is_delete = 0) {
  631. $query = QuanziLabel::find()->where(['store_id' => $this->store_id, 'is_delete' => $is_delete])
  632. ->select('id,name,is_show');
  633. if ($indexBy) {
  634. $query->indexBy('id');
  635. }
  636. $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
  637. foreach ($list as &$item) {
  638. if (!$item['is_show']) {
  639. $item['name'] .= '(已隐藏)';
  640. }
  641. }
  642. return [
  643. 'code' => 0,
  644. 'msg' => '操作成功',
  645. 'data' => $list,
  646. ];
  647. }
  648. public function labelSave($params = []) {
  649. try {
  650. $model = $params['id'] ? QuanziLabel::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziLabel();
  651. if (empty($model)) {
  652. throw new \Exception('参数错误' . $params['id']);
  653. }
  654. $model->name = $params['name'];
  655. $model->sort = $params['sort'] ?: 0;
  656. $model->is_show = $params['is_show'];
  657. $model->store_id = $this->store_id;
  658. isset($params['pic_url']) && $model->pic_url = $params['pic_url'];
  659. if (!$model->save()) {
  660. \Yii::error([__METHOD__, $model->attributes]);
  661. throw new \Exception('保存失败。' . array_shift($model->getFirstErrors()));
  662. }
  663. return [
  664. 'code' => 0,
  665. 'msg' => '操作成功!'
  666. ];
  667. } catch (\Exception $e) {
  668. \Yii::error($e);
  669. return [
  670. 'code' => 1,
  671. 'msg' => $e->getMessage()
  672. ];
  673. }
  674. }
  675. /**
  676. * 修改字段(包括删除is_delete)
  677. * @param type $id
  678. * @param type $key
  679. * @param type $val
  680. * @return type
  681. * @throws \Exception
  682. */
  683. public function labelSaveKey($id, $key, $val) {
  684. try {
  685. if (!is_array($id)) {
  686. $id = explode(',', $id);
  687. }
  688. foreach ($id as $item) {
  689. $model = QuanziLabel::findOne($item);
  690. if (!$model) {
  691. throw new \Exception('参数错误' . $item);
  692. }
  693. $model->$key = $val;
  694. if (!$model->save()) {
  695. throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors()));
  696. }
  697. }
  698. return [
  699. 'code' => 0,
  700. 'msg' => '操作成功',
  701. ];
  702. } catch (\Exception $e) {
  703. \Yii::error($e);
  704. return [
  705. 'code' => 1,
  706. 'msg' => $e->getMessage()
  707. ];
  708. }
  709. }
  710. public function labelUserSave($user_id, $label = '') {
  711. try {
  712. QuanziUp::labelUserSave($user_id, $label);
  713. return [
  714. 'code' => 0,
  715. 'msg' => '操作成功!'
  716. ];
  717. } catch (\Exception $e) {
  718. \Yii::error($e);
  719. return [
  720. 'code' => 1,
  721. 'msg' => $e->getMessage()
  722. ];
  723. }
  724. }
  725. public function bgmList($params = []) {
  726. try {
  727. $is_delete = 0;
  728. if ($params['is_delete'] == 1) {
  729. $is_delete = 1;
  730. }
  731. $query = QuanziBgm::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  732. if (!empty($params['id'])) {
  733. $query->andWhere(['id' => $params['id']]);
  734. }
  735. if (!is_null($params['is_show']) && $params['is_show'] > -1) {
  736. $query->andWhere(['is_show' => $params['is_show']]);
  737. }
  738. if (!empty($params['name'])) {
  739. $query->andWhere(['like', 'name', trim($params['name'])]);
  740. }
  741. $query->orderBy('sort DESC, id DESC');
  742. $pagination = pagination_make($query);
  743. return [
  744. 'code' => 0,
  745. 'msg' => '操作成功',
  746. 'data' => $pagination,
  747. // 'q' => $query->createCommand()->getRawSql(),
  748. ];
  749. } catch (\Exception $e) {
  750. \Yii::error($e);
  751. return [
  752. 'code' => 1,
  753. 'msg' => $e->getMessage()
  754. ];
  755. }
  756. }
  757. public function bgmSelectList($indexBy = 1) {
  758. $query = QuanziBgm::find()->where(['store_id' => $this->store_id])
  759. ->select('id,name,is_show');
  760. if ($indexBy) {
  761. $query->indexBy('id');
  762. }
  763. $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
  764. foreach ($list as &$item) {
  765. if (!$item['is_show']) {
  766. $item['name'] .= '(已隐藏)';
  767. }
  768. }
  769. return [
  770. 'code' => 0,
  771. 'msg' => '操作成功',
  772. 'data' => $list,
  773. ];
  774. }
  775. public function bgmSave($params = []) {
  776. try {
  777. $model = $params['id'] ? QuanziBgm::findOne(['id' => $params['id'], 'store_id' => $this->store_id]) : new QuanziBgm();
  778. if (empty($model)) {
  779. throw new \Exception('参数错误' . $params['id']);
  780. }
  781. $model->name = $params['name'];
  782. $model->sort = $params['sort'] ?: 0;
  783. $model->is_show = $params['is_show'];
  784. $model->bgm_url = $params['bgm_url'];
  785. $model->store_id = $this->store_id;
  786. isset($params['pic_url']) && $model->pic_url = $params['pic_url'];
  787. if (!$model->save()) {
  788. \Yii::error([__METHOD__, $model->attributes]);
  789. throw new \Exception('保存失败。' . array_shift($model->getFirstErrors()));
  790. }
  791. return [
  792. 'code' => 0,
  793. 'msg' => '操作成功!'
  794. ];
  795. } catch (\Exception $e) {
  796. \Yii::error($e);
  797. return [
  798. 'code' => 1,
  799. 'msg' => $e->getMessage()
  800. ];
  801. }
  802. }
  803. /**
  804. * 修改字段(包括删除is_delete)
  805. * @param type $id
  806. * @param type $key
  807. * @param type $val
  808. * @return type
  809. * @throws \Exception
  810. */
  811. public function bgmSaveKey($id, $key, $val) {
  812. try {
  813. if (!is_array($id)) {
  814. $id = explode(',', $id);
  815. }
  816. foreach ($id as $item) {
  817. $model = QuanziBgm::findOne($item);
  818. if (!$model) {
  819. throw new \Exception('参数错误' . $item);
  820. }
  821. $model->$key = $val;
  822. if (!$model->save()) {
  823. throw new \Exception('操作失败。' . $item . array_shift($model->getFirstErrors()));
  824. }
  825. }
  826. return [
  827. 'code' => 0,
  828. 'msg' => '操作成功',
  829. ];
  830. } catch (\Exception $e) {
  831. \Yii::error($e);
  832. return [
  833. 'code' => 1,
  834. 'msg' => $e->getMessage()
  835. ];
  836. }
  837. }
  838. public function followList($params = []) {
  839. try {
  840. $query = QuanziFollow::find()->where(['store_id' => $this->store_id]);
  841. if (!empty($params['user_id'])) {
  842. $query->andWhere(['user_id' => $params['user_id']]);
  843. }
  844. if (!empty($params['up_user_id'])) {
  845. $query->andWhere(['up_user_id' => $params['up_user_id']]);
  846. }
  847. $query->addOrderBy('id DESC');
  848. $pagination = pagination_make($query);
  849. foreach ($pagination['list'] as &$item) {
  850. $user = User::findOne(!empty($params['user_id']) ? $item['up_user_id'] : $item['user_id']);
  851. $saasUser = SaasUser::findOne(['mobile' => $user['binding']]);
  852. $item['user'] = $user;
  853. $item['saasUser'] = $saasUser;
  854. if($params['is_client']){
  855. $item['client_user_follow'] = QuanziFollow::findOne(['up_user_id' => $item['user_id'], 'user_id' => $params['client_user_id']]);
  856. }
  857. }
  858. return [
  859. 'code' => 0,
  860. 'msg' => '操作成功',
  861. 'data' => $pagination,
  862. 'q' => $query->createCommand()->getRawSql(),
  863. ];
  864. } catch (\Exception $e) {
  865. \Yii::error($e);
  866. return [
  867. 'code' => 1,
  868. 'msg' => $e->getMessage()
  869. ];
  870. }
  871. }
  872. public function followAdd($up_user_id, $user_id = 0) {
  873. try{
  874. $follow = QuanziFollow::findOne(['up_user_id' => $up_user_id, 'user_id' => $user_id]);
  875. if (!$follow) {
  876. $model = new QuanziFollow();
  877. $model->store_id = $this->store_id;
  878. $model->up_user_id = $up_user_id;
  879. $model->user_id = $user_id;
  880. if (!$model->save()) {
  881. return [
  882. 'code' => 1,
  883. 'msg' => '操作失败!' . array_shift($model->getFirstErrors()),
  884. ];
  885. }
  886. QuanziUp::upInit($up_user_id)::updateAllCounters(['fans_num' => 1], ['user_id' => $up_user_id]);
  887. QuanziUp::upInit($user_id)::updateAllCounters(['follow_num' => 1], ['user_id' => $user_id]);
  888. }
  889. return [
  890. 'code' => 0,
  891. 'msg' => '操作成功',
  892. ];
  893. } catch (\Exception $e) {
  894. \Yii::error($e);
  895. return [
  896. 'code' => 1,
  897. 'msg' => $e->getMessage()
  898. ];
  899. }
  900. }
  901. public function followDel($up_user_id, $user_id = 0) {
  902. $up = QuanziFollow::deleteAll(['up_user_id' => $up_user_id, 'user_id' => $user_id]);
  903. if ($up) {
  904. try {
  905. QuanziUp::upInit($up_user_id)::updateAllCounters(['fans_num' => -1], ['user_id' => $up_user_id]);
  906. QuanziUp::upInit($user_id)::updateAllCounters(['follow_num' => -1], ['user_id' => $user_id]);
  907. } catch (\Exception $e) {
  908. }
  909. }
  910. return [
  911. 'code' => 0,
  912. 'msg' => '操作成功',
  913. ];
  914. }
  915. public function contentsAdd($user_id = 0, $saas_user_id = 0, $params = []) {
  916. try{
  917. if(empty($params['cat_id']) || empty($params['title']) || empty($params['body'])){
  918. throw new \Exception('操作失败!参数错误。');
  919. }
  920. $id = $params['id'];
  921. $model = $id ? QuanziContents::findOne($id) : new QuanziContents();
  922. $model->store_id = $this->store_id;
  923. $saas_user_id && $model->saas_user_id = $saas_user_id;
  924. $user_id && $model->user_id = $user_id;
  925. empty($params['cat_id']) || $model->cat_id = $params['cat_id'];
  926. $model->title = $params['title'];
  927. $model->body = $params['body'];
  928. empty($params['labels']) || $model->labels = $params['labels'];
  929. empty($params['pic_url']) || $model->pic_url = is_array($params['pic_url']) ? json_encode($params['pic_url']) : $params['pic_url'];
  930. empty($params['video_url']) || $model->video_url = $params['video_url'];
  931. empty($params['goods_id']) || $model->goods_id = $params['goods_id'];
  932. empty($params['bgm_url']) || $model->bgm_url = $params['bgm_url'];
  933. if (!$model->save()) {
  934. throw new \Exception('操作失败!' . array_shift($model->getFirstErrors()));
  935. }
  936. return [
  937. 'code' => 0,
  938. 'msg' => '操作成功',
  939. 'data' => $model,
  940. ];
  941. } catch (\Exception $e) {
  942. \Yii::error($e);
  943. return [
  944. 'code' => 1,
  945. 'msg' => $e->getMessage()
  946. ];
  947. }
  948. }
  949. 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) {
  950. try{
  951. $contents = QuanziContents::findOne($contents_id);
  952. $up_user_id = $contents['user_id'];
  953. if($type == QuanziComment::TYPE_COMMENT && ($comment_id || $at_comment_id)){
  954. $type = QuanziComment::TYPE_REPLY;
  955. }
  956. $model = QuanziComment::findOne([
  957. 'type' => $type,
  958. 'up_user_id' => $up_user_id,
  959. 'user_id' => $user_id,
  960. 'contents_id' => $contents_id,
  961. 'comment_id' => $comment_id,
  962. 'at_comment_id' => $at_comment_id,
  963. ]);
  964. if($del){
  965. if(!$model){
  966. throw new \Exception('操作失败!没有数据。');
  967. }
  968. if(!$model->delete()){
  969. throw new \Exception('操作失败!数据异常。');
  970. }
  971. }else{
  972. if(in_array($type, [QuanziComment::TYPE_GOOD])){
  973. if($model){
  974. throw new \Exception('操作失败!已经点赞过。');
  975. }
  976. }
  977. $model = new QuanziComment();
  978. $model->store_id = $this->store_id;
  979. $model->type = $type;
  980. $model->type_weigui = $type_weigui;
  981. $model->up_user_id = $up_user_id;
  982. $model->contents_id = $contents_id;
  983. $model->comment_id = $comment_id;
  984. $model->at_comment_id = $at_comment_id;
  985. $model->user_id = $user_id;
  986. $model->saas_user_id = $saas_user_id;
  987. $model->content = $content;
  988. if($at_comment_id){
  989. $at_comment = QuanziComment::findOne($at_comment_id);
  990. $model->at_comment_user_id = $at_comment->user_id;
  991. $model->at_comment_saas_user_id = $at_comment->saas_user_id;
  992. }
  993. if (!$model->save()) {
  994. throw new \Exception('操作失败!' . array_shift($model->getFirstErrors()));
  995. }
  996. }
  997. $arr = [
  998. QuanziComment::TYPE_COMMENT => 'comment_num',
  999. QuanziComment::TYPE_REPLY => 'comment_num',
  1000. QuanziComment::TYPE_GOOD => 'good_num',
  1001. QuanziComment::TYPE_SHARE => 'share_num',
  1002. ];
  1003. $count = $del ? -1 : 1;
  1004. if(in_array($type, array_keys($arr)) && !$comment_id && !$at_comment_id){
  1005. QuanziUp::upInit($up_user_id)::updateAllCounters([$arr[$type] => $count], ['user_id' => $up_user_id]);
  1006. QuanziContents::updateAllCounters([$arr[$type] => $count], ['id' => $contents_id]);
  1007. }
  1008. if(in_array($type, array_keys($arr)) && ($comment_id || $at_comment_id)){
  1009. QuanziComment::updateAllCounters([$arr[$type] => $count], ['id' => $at_comment_id ? $at_comment_id : $comment_id]);
  1010. }
  1011. return [
  1012. 'code' => 0,
  1013. 'msg' => '操作成功',
  1014. 'data' => $del ? null : $this->commentsList(self::paramsClient(['id' => $model->id], $model->user_id, $model->saas_user_id))['data']['list'][0],
  1015. ];
  1016. } catch (\Exception $e) {
  1017. \Yii::error($e);
  1018. return [
  1019. 'code' => 1,
  1020. 'msg' => $e->getMessage()
  1021. ];
  1022. }
  1023. }
  1024. }