delete_relationship.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. header('Content-Type: application/json');
  5. if (!isset($_POST['id']) || !is_numeric($_POST['id'])) {
  6. echo json_encode(['success' => false, 'message' => '参数错误']);
  7. exit;
  8. }
  9. $id = intval($_POST['id']);
  10. $isAdmin = checkIfAdmin();
  11. // 验证权限
  12. if (!$isAdmin) {
  13. // 检查当前用户是否是源客户或目标客户的负责人
  14. $sql = "SELECT cr.source_customer_id, cr.target_customer_id
  15. FROM customer_relationship cr
  16. WHERE cr.id = $id";
  17. $result = mysqli_query($conn, $sql);
  18. if ($row = mysqli_fetch_assoc($result)) {
  19. $sourceId = $row['source_customer_id'];
  20. $targetId = $row['target_customer_id'];
  21. $employeeId = $_SESSION['employee_id'];
  22. $customerSql = "SELECT id FROM customer WHERE (id = $sourceId OR id = $targetId) AND cs_belong = $employeeId";
  23. $customerResult = mysqli_query($conn, $customerSql);
  24. if (mysqli_num_rows($customerResult) == 0) {
  25. echo json_encode(['success' => false, 'message' => '您没有权限删除此客户关系']);
  26. exit;
  27. }
  28. } else {
  29. echo json_encode(['success' => false, 'message' => '未找到客户关系']);
  30. exit;
  31. }
  32. }
  33. // 执行删除
  34. $deleteSql = "DELETE FROM customer_relationship WHERE id = $id";
  35. $result = mysqli_query($conn, $deleteSql);
  36. if ($result) {
  37. echo json_encode(['success' => true, 'message' => '客户关系已删除']);
  38. } else {
  39. echo json_encode(['success' => false, 'message' => '删除失败: ' . mysqli_error($conn)]);
  40. }
  41. ?>