ApiController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Repositories\DistProduct;
  4. use App\Distributor\Repositories\DistProductCategory;
  5. use App\Distributor\Repositories\DistVideo;
  6. use App\Distributor\Repositories\DistVideoCategory;
  7. use App\Distributor\Repositories\SitePages;
  8. use App\Distributor\Repositories\SitePagesTag;
  9. use App\Distributor\Repositories\SmmPostLog;
  10. use App\Distributor\Repositories\SmmUserAccount;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Routing\Controller;
  13. class ApiController extends Controller
  14. {
  15. /**
  16. * 产品下接API,默认返回50个最新的产品
  17. */
  18. public function products(Request $request)
  19. {
  20. $q = $request->get('q');
  21. if ($q != null) {
  22. // 模糊搜索
  23. $obj = new DistProduct();
  24. return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('status', 2)->paginate(null, ['id', 'title as text']);
  25. } else {
  26. // 获取最新的N个
  27. $selectOptionsNew = DistProduct::selectOptionsNew();
  28. return $this->changeOptions($selectOptionsNew);
  29. }
  30. }
  31. public function pages(Request $request)
  32. {
  33. $q = $request->get('q');
  34. if ($q != null) {
  35. // 模糊搜索
  36. $obj = new SitePages();
  37. return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('page_type', 0)->paginate(null, ['id', 'title as text']);
  38. } else {
  39. // 获取最新的N个
  40. $selectOptionsNew = SitePages::selectOptionsNew(30,0);
  41. return $this->changeOptions($selectOptionsNew);
  42. }
  43. }
  44. public function landingPages(Request $request)
  45. {
  46. $q = $request->get('q');
  47. if ($q != null) {
  48. // 模糊搜索
  49. $obj = new SitePages();
  50. return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->where('page_type', 1)->paginate(null, ['id', 'title as text']);
  51. } else {
  52. // 获取最新的N个
  53. $selectOptionsNew = SitePages::selectOptionsNew(30,1);
  54. return $this->changeOptions($selectOptionsNew);
  55. }
  56. }
  57. public function videos(Request $request)
  58. {
  59. $q = $request->get('q');
  60. if ($q != null) {
  61. // 模糊搜索
  62. $obj = new DistVideo();
  63. return $obj->model()->where('title', 'like', "%$q%")->where('dist_id', getDistributorId())->paginate(null, ['id', 'title as text']);
  64. } else {
  65. // 获取最新的N个
  66. $selectOptionsNew = DistVideo::selectOptionsNew(30);
  67. return $this->changeOptions($selectOptionsNew);
  68. }
  69. }
  70. public function tag(Request $request)
  71. {
  72. $q = $request->get('q');
  73. if ($q != null) {
  74. // 模糊搜索
  75. $obj = new SitePagesTag();
  76. return $obj->model()->where('name', 'like', "%$q%")->where('dist_id', getDistributorId())->paginate(null, ['id', 'name as text']);
  77. } else {
  78. // 获取最新的N个
  79. $selectOptionsNew = SitePagesTag::selectOptionsNew();
  80. return $this->changeOptions($selectOptionsNew);
  81. }
  82. }
  83. public function generateSlug(Request $request)
  84. {
  85. $model = $request->get('model');
  86. $title = $request->get('title');
  87. $result = null;
  88. switch ($model) {
  89. case 'pages':
  90. $obj = new SitePages();
  91. $result = $obj->generateSlug($title);
  92. break;
  93. case 'productCategory':
  94. $obj = new DistProductCategory();
  95. $result = $obj->generateSlug($title);
  96. break;
  97. case 'videoCategory':
  98. $obj = new DistVideoCategory();
  99. $result = $obj->generateSlug($title);
  100. break;
  101. case 'pagesTag':
  102. $obj = new SitePagesTag();
  103. $result = $obj->generateSlug($title);
  104. break;
  105. }
  106. return ['slug' => $result];
  107. }
  108. /*
  109. * 把数据转换成select需要的格式
  110. */
  111. private function changeOptions($data) {
  112. // 初始化结果数组
  113. $result = [];
  114. // 遍历原始数据并转换格式
  115. foreach ($data as $id => $text) {
  116. $result[] = [
  117. 'id' => (int)$id, // 将字符串转换为整数
  118. 'text' => $text
  119. ];
  120. }
  121. return $result;
  122. }
  123. /*
  124. * 得到要发送的日志(社发爬虫工具使用)
  125. */
  126. public function ssmGetSendLog(){
  127. $result = SmmPostLog::getSendLogDist();
  128. return response()->json(['status' => 1,'data' => $result]);
  129. }
  130. /*
  131. * 更新发送日志状态(社发爬虫工具使用)
  132. */
  133. public function ssmUpdateSendLog(Request $request)
  134. {
  135. $id = $request->input('id');
  136. $status = $request->input('status');
  137. $request_count = $request->input('request_count');
  138. $remark = $request->input('remark');
  139. SmmPostLog::updateSendLogDist($id, $status, $request_count, $remark);
  140. return response()->json(['status' => 1,'msg' => '更新成功']);
  141. }
  142. public function ssmSaveAccount(Request $request){
  143. $mediaName = $request->input('media_name') ? $request->input('media_name') : '';
  144. $accountId = $request->input('account_id') ? $request->input('account_id') : '';
  145. $accountName = $request->input('account_name') ? $request->input('account_name') : '';
  146. $cookieId = $request->input('cookie_id') ? $request->input('cookie_id') : '';
  147. $cookieData = $request->input('cookie_data') ? $request->input('cookie_data') : '';
  148. SmmUserAccount::createAccountIfMediaExistsCookie($mediaName, $accountId,$accountName, $cookieId,$cookieData);
  149. return response()->json(['status' => 1,'msg' => '更新成功']);
  150. }
  151. }