NewsService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\ArticleData;
  5. use App\Model\Category;
  6. use App\Model\WebsiteCategory;
  7. use App\Model\ArticleSurvey;
  8. use App\Model\Good;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\RpcServer\Annotation\RpcService;
  11. use App\Tools\Result;
  12. use Ramsey\Uuid\Uuid;
  13. use Hyperf\Utils\Random;
  14. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class NewsService implements NewsServiceInterface
  16. {
  17. /**
  18. * 获取导航池列表
  19. * @param array $data
  20. * @return array
  21. */
  22. public function getCategoryList(array $data): array
  23. {
  24. $where = [];
  25. if (isset($data['name']) && $data['name']) {
  26. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  27. }
  28. if (isset($data['department_id']) && $data['department_id']) {
  29. array_push($where, ['category.department_id', '=', $data['department_id']]);
  30. }
  31. $city_id = '';
  32. if (isset($data['city_id']) && $data['city_id']) {
  33. $city_id = intval($data['city_id']);
  34. }
  35. $rep = Category::where($where)
  36. ->when($city_id, function ($query) use ($city_id) {
  37. if (isset($city_id) && $city_id) {
  38. $query->whereJsonContains("category.city_arr_id", $city_id);
  39. }
  40. })
  41. ->leftJoin('district', 'category.city_id', 'district.id')
  42. ->leftJoin('department', 'category.department_id', 'department.id')
  43. ->select("category.*", "district.name as city_name", "department.name as department_name")
  44. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page'] - 1) * $data['pageSize'])->get();
  45. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  46. if (isset($city_id) && $city_id) {
  47. $query->whereJsonContains("category.city_arr_id", $city_id);
  48. }
  49. })->count();
  50. $data = [
  51. 'rows' => $rep->toArray(),
  52. 'count' => $count,
  53. ];
  54. if (empty($rep->toArray())) {
  55. return Result::error("没有导航池数据");
  56. }
  57. return Result::success($data);
  58. }
  59. public function myCategoryList(array $data): array
  60. {
  61. $sszq = $data['sszq'] ?? '';
  62. unset($data['sszq']);
  63. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  64. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  65. $where[] = [
  66. 'pid', '=', $data['pid'],
  67. ];
  68. if (isset($data['name'])) {
  69. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  70. }
  71. var_dump($where);
  72. //根据分类id,从category表中取出分类信息
  73. $result = Category::where($where)
  74. ->whereIn('category.id', $catorytids)
  75. ->select('category.*', 'category.id as category_id')->get();
  76. if (empty($result)) {
  77. return Result::error("没有栏目数据");
  78. }
  79. return Result::success($result);
  80. }
  81. /**
  82. * @param array $data
  83. * @return array
  84. */
  85. public function categoryList(array $data): array
  86. {
  87. $where[] = [
  88. 'pid', '=', $data['pid'],
  89. ];
  90. if (isset($data['name'])) {
  91. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  92. }
  93. var_dump($where);
  94. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  95. if (empty($result)) {
  96. return Result::error("没有栏目数据");
  97. }
  98. return Result::success($result);
  99. }
  100. /**
  101. * @param array $data
  102. * @return array
  103. */
  104. public function addCategory(array $data): array
  105. {
  106. $id = Category::insertGetId($data);
  107. if (empty($id)) {
  108. return Result::error("添加失败");
  109. }
  110. return Result::success(['id' => $id]);
  111. }
  112. /**
  113. * @param array $data
  114. * @return array
  115. */
  116. public function delCategory(array $data): array
  117. {
  118. $categoryList = Category::where(['pid' => $data['id']])->get();
  119. var_dump("分类列表:", $data, $categoryList);
  120. if ($categoryList->toArray()) {
  121. return Result::error("分类下面有子分类不能删除");
  122. }
  123. $articleList = Article::where(['catid' => $data['id']])->get();
  124. var_dump("文章列表:", $articleList);
  125. if ($articleList->toArray()) {
  126. return Result::error("分类下面有资讯不能删除");
  127. }
  128. $result = Category::where($data)->delete();
  129. if (!$result) {
  130. return Result::error("删除失败");
  131. }
  132. return Result::success($result);
  133. }
  134. /**
  135. * @param array $data
  136. * @return array
  137. */
  138. public function updateCategory(array $data): array
  139. {
  140. $where = [
  141. 'id' => $data['id'],
  142. ];
  143. $result = Category::where($where)->update($data);
  144. if ($result) {
  145. return Result::success($result);
  146. } else {
  147. return Result::error("更新失败");
  148. }
  149. }
  150. /**
  151. * 获取导航池信息
  152. * @param array $data
  153. * @return array
  154. */
  155. public function getCategoryInfo(array $data): array
  156. {
  157. $where = [
  158. 'id' => $data['id'],
  159. ];
  160. $result = Category::where($where)->first();
  161. if ($result) {
  162. return Result::success($result);
  163. } else {
  164. return Result::error("更新失败");
  165. }
  166. }
  167. /**
  168. * @param array $data
  169. * @return array
  170. */
  171. public function getArticleList(array $data): array
  172. {
  173. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  174. $type_id = $data['type_id'];
  175. unset($data['type_id']);
  176. $user_id = $data['user_id'];
  177. unset($data['user_id']);
  178. $where = [];
  179. if (isset($data['title']) && $data['title']) {
  180. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  181. }
  182. if (isset($data['category_name']) && $data['category_name']) {
  183. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  184. }
  185. if (isset($data['author']) && $data['author']) {
  186. array_push($where, ['article.author', '=', $data['author']]);
  187. }
  188. if (isset($data['islink']) && $data['islink'] !== "") {
  189. array_push($where, ['article.islink', '=', $data['islink']]);
  190. }
  191. if (isset($data['status']) && $data['status'] !== "") {
  192. array_push($where, ['article.status', '=', $data['status']]);
  193. }
  194. //不是管理员展示个人数据;
  195. if ($type_id != 10000) {
  196. $where[] = ['article.admin_user_id', '=', $user_id];
  197. }
  198. $rep = Article::where($where)
  199. ->whereNotIn('article.status', [404])
  200. ->leftJoin('category', 'article.catid', 'category.id')
  201. ->select("article.*", "category.name as category_name")
  202. ->orderBy("article.id", "desc")
  203. ->limit($data['pageSize'])
  204. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  205. $count = Article::where($where)->whereNotIn('article.status', [404])
  206. ->leftJoin('category', 'article.catid', 'category.id')->count();
  207. $data = [
  208. 'rows' => $rep->toArray(),
  209. 'count' => $count,
  210. ];
  211. if (empty($rep)) {
  212. return Result::error("没有信息数据");
  213. }
  214. return Result::success($data);
  215. }
  216. /**
  217. * @param array $data
  218. * @return array
  219. */
  220. public function addArticle(array $data): array
  221. {
  222. var_dump($data, '----------12-----------1');
  223. var_dump($data, '----------12-----------1');
  224. unset($data['user_type']);
  225. // unset($data['web_site_id']);
  226. unset($data['nav_add_pool_id']);
  227. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  228. Db::beginTransaction();
  229. try {
  230. //处理投票
  231. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  232. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  233. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  234. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  235. unset($data['is_survey']);
  236. unset($data['survey_name']);
  237. unset($data['suvey_array']);
  238. unset($data['website_id']);
  239. if ($data['hits'] == '') {
  240. $data['hits'] = 0;
  241. }
  242. if ($data['is_original'] == '') {
  243. $data['is_original'] = 0;
  244. }
  245. if ($data['status'] == '') {
  246. $data['status'] = 0;
  247. }
  248. $articleData = $data;
  249. unset($articleData['content']);
  250. $id = Article::insertGetId($articleData);
  251. $articleDataContent = [
  252. 'article_id' => $id,
  253. 'content' => $data['content'],
  254. ];
  255. ArticleData::insertGetId($articleDataContent);
  256. //处理投票
  257. if ($is_survey == 1) {
  258. //生成年月日时分秒+8位随机数
  259. $uuid = date('YmdHis') . rand(10000000, 99999999);
  260. var_dump($suvey_array, 'suvey_array________');
  261. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  262. var_dump($suveys_array, '---------------------1');
  263. var_dump($suvey_array, '---------------------2');
  264. $suvey_data = [];
  265. foreach ($suveys_array as $key => $value) {
  266. if ($value == '') {
  267. continue;
  268. }
  269. if (is_array($value)) {
  270. $suvey_data[] = [
  271. 'sur_id' => $uuid,
  272. 'art_id' => $id,
  273. 'website_id' => $website_id ?? 2,
  274. 'survey_name' => $survey_name,
  275. 'choice_name' => $value[1],
  276. 'is_other' => 1,
  277. 'other_id' => 0,
  278. 'results' => 0,
  279. ];
  280. } else {
  281. $suvey_data[] = [
  282. 'sur_id' => $uuid,
  283. 'art_id' => $id,
  284. 'website_id' => $website_id ?? 2,
  285. 'survey_name' => $survey_name,
  286. 'choice_name' => $value,
  287. 'is_other' => 0,
  288. 'other_id' => 0,
  289. 'results' => 0,
  290. ];
  291. }
  292. if (empty($suvey_data)) {
  293. throw new \Exception("投票数据为空");
  294. }
  295. }
  296. $result = ArticleSurvey::insert($suvey_data);
  297. if (!$result) {
  298. throw new \Exception("投票失败,ArticleSurvey插入失败");
  299. }
  300. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  301. if (!$result) {
  302. throw new \Exception("投票失败,更新主表失败");
  303. }
  304. }
  305. Db::commit();
  306. return Result::success(['id' => $id]);
  307. } catch (\Throwable $ex) {
  308. Db::rollBack();
  309. var_dump($ex->getMessage());
  310. return Result::error("创建失败", 0);
  311. }
  312. }
  313. /**
  314. * @param array $data
  315. * @return array
  316. */
  317. public function delArticle(array $data): array
  318. {
  319. $result = Article::where($data)->delete();
  320. //survey投票删除
  321. articleSurvey::where(['art_id' => $data['id']])->delete();
  322. if (!$result) {
  323. return Result::error("删除失败");
  324. }
  325. return Result::success($result);
  326. }
  327. /**
  328. * @param array $data
  329. * @return array
  330. */
  331. public function updateArticle(array $data): array
  332. {
  333. var_dump($data, '----------12-----------1');
  334. Db::beginTransaction();
  335. unset($data['user_type']);
  336. // unset($data['web_site_id']);
  337. unset($data['nav_add_pool_id']);
  338. try {
  339. //处理投票
  340. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  341. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  342. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  343. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  344. unset($data['is_survey']);
  345. unset($data['survey_name']);
  346. unset($data['suvey_array']);
  347. unset($data['website_id']);
  348. if ($data['hits'] == '') {
  349. $data['hits'] = 0;
  350. }
  351. if ($data['is_original'] == '') {
  352. $data['is_original'] = 0;
  353. }
  354. if ($data['status'] == '') {
  355. $data['status'] = 0;
  356. }
  357. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  358. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  359. $articleData = $data;
  360. unset($articleData['content']);
  361. unset($articleData['status_name']);
  362. unset($articleData['name']);
  363. unset($articleData['content']);
  364. unset($articleData['pid_arr']);
  365. unset($articleData['pid']);
  366. $id = Article::where(['id' => $data['id']])->update($articleData);
  367. $articleDataContent = [
  368. 'content' => $data['content'],
  369. ];
  370. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  371. //处理投票
  372. $id = $data['id'];
  373. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  374. var_dump($suvey_array, 'suvey_array________delete');
  375. //处理投票
  376. if ($is_survey == 1) {
  377. //生成年月日时分秒+8位随机数
  378. $uuid = date('YmdHis') . rand(10000000, 99999999);
  379. $suveys_array = is_array($suvey_array) ? $suvey_array : json_decode($suvey_array);
  380. var_dump($suveys_array, '---------------------1');
  381. var_dump($suvey_array, '---------------------2');
  382. $suvey_data = [];
  383. if (is_array($suveys_array)) {
  384. foreach ($suveys_array as $key => $value) {
  385. if ($value == '') {
  386. continue;
  387. }
  388. if (is_array($value)) {
  389. $suvey_data[] = [
  390. 'sur_id' => $uuid,
  391. 'art_id' => $id,
  392. 'website_id' => $website_id ?? 2,
  393. 'survey_name' => $survey_name,
  394. 'choice_name' => $value[1],
  395. 'is_other' => 1,
  396. 'other_id' => 0,
  397. 'results' => 0,
  398. ];
  399. } else {
  400. $suvey_data[] = [
  401. 'sur_id' => $uuid,
  402. 'art_id' => $id,
  403. 'website_id' => $website_id ?? 2,
  404. 'survey_name' => $survey_name,
  405. 'choice_name' => $value,
  406. 'is_other' => 0,
  407. 'other_id' => 0,
  408. 'results' => 0,
  409. ];
  410. }
  411. if (empty($suvey_data)) {
  412. throw new \Exception("投票数据为空");
  413. }
  414. }
  415. }
  416. $result = ArticleSurvey::insert($suvey_data);
  417. if (!$result) {
  418. throw new \Exception("投票失败");
  419. }
  420. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  421. if (!$result) {
  422. throw new \Exception("投票失败");
  423. }
  424. } else {
  425. $result = Article::where('id', $id)->update(['survey_id' => '', 'survey_name' => '', 'is_survey' => 0]);
  426. }
  427. Db::commit();
  428. return Result::success([]);
  429. } catch (\Throwable $ex) {
  430. Db::rollBack();
  431. var_dump($ex->getMessage());
  432. return Result::error("更新失败1" . $ex->getMessage(), 0);
  433. }
  434. }
  435. /**
  436. * 更新资讯状态
  437. * @param array $data
  438. * @return array
  439. */
  440. public function upArticleStatus(array $data): array
  441. {
  442. $result = Article::where(['id' => $data['id']])->update($data);
  443. if ($result) {
  444. return Result::success();
  445. } else {
  446. return Result::error("更新状态失败", 0);
  447. }
  448. }
  449. /**
  450. * @param array $data
  451. * @return array
  452. */
  453. public function getArticleInfo(array $data): array
  454. {
  455. $where = [
  456. 'article.id' => $data['id'],
  457. // 'article.status' => 1,
  458. ];
  459. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  460. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get()->all();
  461. var_dump($articleSurvey, 'articleSurvey');
  462. $info = $result;
  463. var_dump($info, 'info');
  464. // $info['survey_array'] = $articleSurvey == null ? [] : $info['survey_array'];
  465. if ($result) {
  466. return Result::success($info);
  467. } else {
  468. return Result::error("查询失败", 0);
  469. }
  470. }
  471. /**
  472. * 获取新闻
  473. * @param array $data
  474. * @return array
  475. */
  476. public function getWebsiteArticlett(array $data): array
  477. {
  478. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  479. $category = $category->toArray();
  480. $result = [];
  481. if ($category) {
  482. $category_ids = [];
  483. foreach ($category as $val) {
  484. array_push($category_ids, $val['category_id']);
  485. }
  486. if (isset($data['placeid'])) {
  487. $placeid = $data['placeid'] - 1;
  488. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  489. } else {
  490. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  491. }
  492. if (empty($result)) {
  493. return Result::error("暂无头条新闻", 0);
  494. }
  495. return Result::success($result);
  496. } else {
  497. return Result::error("本网站下暂无相关栏目", 0);
  498. }
  499. }
  500. /**
  501. * 获取模块新闻
  502. * @param array $data
  503. * @return array
  504. */
  505. public function getWebsiteModelArticles(array $data): array
  506. {
  507. $catid = $data['catid'];
  508. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  509. $category = $category->toArray();
  510. if (!empty($category)) {
  511. $where = [
  512. 'status' => 1,
  513. 'catid' => $catid,
  514. ];
  515. if ($data['level'] == 1) {
  516. $level = [
  517. 0 => '1',
  518. 1 => '4',
  519. 2 => '5',
  520. ];
  521. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  522. } elseif ($data['level'] == 2) {
  523. $level = '2';
  524. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  525. } else {
  526. $level = '3';
  527. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  528. }
  529. $result = $result->toArray();
  530. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  531. $placeid = $data['placeid'] - 1;
  532. if ($level == 2 || $level == 3) {
  533. $where = [
  534. 'level' => $level,
  535. ];
  536. $result = Article::where($where)
  537. ->orderBy("created_at", "desc")
  538. ->offset($placeid)
  539. ->limit($data['pagesize'])->get();
  540. } else {
  541. $result = Article::where($where)
  542. ->whereIn('level', $level)
  543. ->offset($placeid)
  544. ->orderBy("created_at", "desc")
  545. ->limit($data['pagesize'])->get();
  546. }
  547. }
  548. if (empty($result)) {
  549. return Result::error("此栏目暂无相关新闻", 0);
  550. }
  551. } else {
  552. return Result::error("此网站暂无此栏目", 0);
  553. }
  554. return Result::success($result);
  555. }
  556. //20250226 产品列表
  557. public function getGoodList(array $data): array
  558. {
  559. $where = [];
  560. //类型
  561. if (isset($data['type_id']) && $data['type_id']) {
  562. $where = [
  563. 'type_id' => $data['type_id'],
  564. ];
  565. }
  566. //名称
  567. if (isset($data['name']) && $data['name']) {
  568. $where = [
  569. 'good.name' => $data['name'],
  570. ];
  571. }
  572. $where1 = [];
  573. //website_id
  574. // if (isset($data['website_id']) && $data['website_id']) {
  575. // $where1 = [
  576. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  577. // ];
  578. // }
  579. // website_name
  580. if (isset($data['website_name']) && $data['website_name']) {
  581. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  582. }
  583. // catid
  584. if (isset($data['category_name']) && $data['category_name']) {
  585. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  586. }
  587. // $result = Good::where($where)
  588. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  589. $result = Good::where($where)
  590. ->when(!empty($where1), function ($query) use ($where1) {
  591. return $query->where($where1);
  592. })
  593. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  594. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  595. ->leftJoin('category', 'good.catid', '=', 'category.id')
  596. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  597. ->orderBy("id", "desc")
  598. ->limit($data['page_size'])
  599. ->offset(($data['page'] - 1) * $data['page_size'])
  600. ->get();
  601. $count = Good::where($where)
  602. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  603. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  604. ->leftJoin('category', 'good.catid', '=', 'category.id')
  605. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  606. ->orderBy("updated_at", "desc")->count();
  607. $data = [
  608. 'rows' => $result->toArray(),
  609. 'count' => $count,
  610. ];
  611. if (empty($result)) {
  612. return Result::error("此栏目暂无相关产品", 0);
  613. }
  614. return Result::success($data);
  615. }
  616. public function getGoodInfo(array $data): array
  617. {
  618. $result = Good::where('id', $data['id'])->first();
  619. if (empty($result)) {
  620. return Result::error("此产品不存在", 0);
  621. }
  622. return Result::success($result);
  623. }
  624. public function addGood(array $data): array
  625. {
  626. // unset($data['city_arr_id']);
  627. // unset($data['cat_arr_id']);
  628. $data['city_id'] = end($data['city_arr_id']);
  629. $data['catid'] = end($data['cat_arr_id']);
  630. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  631. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  632. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  633. unset($data['imgUrl']);
  634. $result = Good::insert($data);
  635. if (empty($result)) {
  636. return Result::error("添加失败", 0);
  637. }
  638. return Result::success($result);
  639. }
  640. public function updateGood(array $data): array
  641. {
  642. $data['city_id'] = end($data['city_arr_id']);
  643. $data['catid'] = end($data['cat_arr_id']);
  644. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  645. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  646. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  647. //设置东八区
  648. date_default_timezone_set('Asia/Shanghai');
  649. $data['updated_at'] = date('Y-m-d H:i:s');
  650. $result = Good::where('id', $data['id'])->update($data);
  651. if (empty($result)) {
  652. return Result::error("更新失败", 0);
  653. }
  654. return Result::success($result);
  655. }
  656. public function delGood(array $data): array
  657. {
  658. $result = Good::where('id', $data['id'])->delete();
  659. if (empty($result)) {
  660. return Result::error("删除失败", 0);
  661. }
  662. return Result::success($result);
  663. }
  664. //20250226 产品列表
  665. }