KeyboardController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\Admin\Telegram;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Telegram\TelegramBot;
  8. use App\Models\Telegram\TelegramBotKeyboard;
  9. use App\Models\Telegram\TelegramBotKeyreplyKeyboard;
  10. use Telegram\Bot\Api;
  11. class KeyboardController extends Controller
  12. {
  13. public $KeyboardStatus = ['启用','禁用'];
  14. public $KeyboardType = ['1' => '键盘','2' => '内联按钮'];
  15. public $InlineType = ['0' => '-','1' => 'url','2' => '回调'];
  16. public function index(Request $request)
  17. {
  18. $KeyboardStatus = $this->KeyboardStatus;
  19. $KeyboardType = $this->KeyboardType;
  20. $InlineType = $this->InlineType;
  21. return view('admin.telegram.keyboard.index',compact("KeyboardStatus","KeyboardType","InlineType"));
  22. }
  23. //列表
  24. public function getData(Request $request)
  25. {
  26. $model = TelegramBotKeyboard::where(function($query) use ($request){
  27. if ($request->keyboard_name != '') {
  28. $query->where('keyboard_name', 'like' ,"%" . $request->keyboard_name ."%");
  29. }
  30. });
  31. $count = $model->count();
  32. $limit = $request->limit ?? 15;
  33. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  34. $data = $model->limit($limit)->offset($offset)->orderBy('rid','desc')->get();
  35. $keys = ["KeyboardType" => $this->KeyboardType, "InlineType" => $this->InlineType];
  36. $data = $data->map(function($query) use ($keys){
  37. $query->keyboard_type_val = $keys["KeyboardType"][$query->keyboard_type];
  38. $query->inline_type_val = $keys["InlineType"][$query->inline_type];
  39. return $query;
  40. });
  41. return ['code' => '0', 'data' => $data, 'count' => $count];
  42. }
  43. //添加
  44. public function add(Request $request)
  45. {
  46. $data = TelegramBotKeyboard::where('keyboard_name', $request->keyboard_name)->where('keyboard_type', $request->keyboard_type)->where('inline_type', $request->inline_type)->where('keyboard_value', $request->keyboard_value)->first();
  47. if(!empty($data)){
  48. return $this->responseData(400, '键盘名称已存在');
  49. }
  50. $res = TelegramBotKeyboard::create([
  51. 'keyboard_type' => $request->keyboard_type,
  52. 'keyboard_name' => $request->keyboard_name,
  53. 'inline_type' => $request->inline_type,
  54. 'keyboard_value' => $request->keyboard_value ?? '-',
  55. 'seq_sn' => $request->seq_sn,
  56. 'create_time' => nowDate()
  57. ]);
  58. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  59. }
  60. //删除
  61. public function delete(Request $request)
  62. {
  63. $data = TelegramBotKeyreplyKeyboard::where('keyboard_rid', $request->rid);
  64. if($data->count() > 0){
  65. return $this->responseData(400, '请先删除关键字键盘');
  66. }
  67. $res = TelegramBotKeyboard::where('rid', $request->rid)->delete();
  68. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  69. }
  70. //编辑
  71. public function update(Request $request)
  72. {
  73. $data = TelegramBotKeyboard::where('keyboard_name', $request->keyboard_name)->where('keyboard_type', $request->keyboard_type)->where('inline_type', $request->inline_type)->where('keyboard_value', $request->keyboard_value)->where('rid', '<>',$request->rid)->first();
  74. if(!empty($data)){
  75. return $this->responseData(400, '键盘名称已存在');
  76. }
  77. DB::beginTransaction();
  78. try {
  79. $data = TelegramBotKeyboard::where('rid', $request->rid)->first();
  80. $data->keyboard_type = $request->keyboard_type;
  81. $data->keyboard_name = $request->keyboard_name;
  82. $data->inline_type = $request->inline_type;
  83. $data->keyboard_value = $request->keyboard_value ?? '-';
  84. $data->seq_sn = $request->seq_sn;
  85. $data->update_time = nowDate();
  86. $data->save();
  87. DB::commit();
  88. return $this->responseData(200, '更新成功');
  89. } catch (\Exception $e) {
  90. DB::rollBack();
  91. return $this->responseData(400, '更新失败'.$e->getMessage());
  92. }
  93. }
  94. //编辑状态
  95. public function change_status(Request $request)
  96. {
  97. DB::beginTransaction();
  98. try {
  99. $data = TelegramBotKeyboard::where('rid', $request->rid)->first();
  100. $data->status = $request->status == 1 ? 0 : 1;
  101. $data->save();
  102. DB::commit();
  103. return $this->responseData(200, '更新成功');
  104. } catch (\Exception $e) {
  105. DB::rollBack();
  106. return $this->responseData(400, '更新失败'.$e->getMessage());
  107. }
  108. }
  109. }