common.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. //权限控制
  3. \think\Hook::add('check_auth','app\\common\\behavior\\AuthenticateBehavior');
  4. use think\Db;
  5. /**
  6. * 处理相关团队
  7. * @author
  8. * @param types 类型
  9. * @param types 类型ID
  10. * @param type 权限 1只读2读写
  11. * @param user_id [array] 协作人
  12. * @param is_del 1 移除操作, 2编辑操作, 3添加操作
  13. * @param owner_user_id 操作人
  14. * @param is_module 相关 1相关,不进行数据权限判断
  15. */
  16. function teamUserId($types, $types_id, $type, $user_id, $is_del, $owner_user_id, $is_module = 0)
  17. {
  18. $userModel = new \app\admin\model\User();
  19. $authIds = [];
  20. switch ($types) {
  21. case 'crm_leads' :
  22. $data_name = 'leads_id';
  23. $authIds = $userModel->getUserByPer('crm', 'leads', 'teamsave');
  24. break;
  25. case 'crm_customer' :
  26. $data_name = 'customer_id';
  27. $authIds = $userModel->getUserByPer('crm', 'customer', 'teamsave');
  28. break;
  29. case 'crm_contacts' :
  30. $data_name = 'contacts_id';
  31. $authIds = $userModel->getUserByPer('crm', 'contacts', 'teamsave');
  32. break;
  33. case 'crm_business' :
  34. $data_name = 'business_id';
  35. $authIds = $userModel->getUserByPer('crm', 'business', 'teamsave');
  36. break;
  37. case 'crm_contract' :
  38. $data_name = 'contract_id';
  39. $authIds = $userModel->getUserByPer('crm', 'contract', 'teamsave');
  40. break;
  41. }
  42. if (!is_array($types_id) && $types_id) {
  43. $types_id = [$types_id];
  44. }
  45. $errorMessage = [];
  46. foreach ($types_id as $k=>$v) {
  47. $resData = db($types)->where([$data_name => $v])->field('name,owner_user_id,rw_user_id,ro_user_id')->find();
  48. if (!in_array($resData['owner_user_id'],$authIds) && $resData['owner_user_id'] && $is_module !== 1) {
  49. $errorMessage[] = $resData['name'].'处理团队操作失败,错误原因:无权限';
  50. continue;
  51. }
  52. $type = $type ? : 1;
  53. $data = [];
  54. //读写
  55. $old_rw_user_id = stringToArray($resData['rw_user_id']) ? : []; //去重
  56. //只读
  57. $old_ro_user_id = stringToArray($resData['ro_user_id']) ? : []; //去重
  58. if ($is_del == 1) {
  59. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $user_id) : ''; // 差集
  60. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  61. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $user_id) : ''; // 差集
  62. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  63. } elseif ($is_del == 2) {
  64. if ($type == 2) {
  65. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $user_id) : []; // 差集
  66. $all_rw_user_id = $old_rw_user_id ? array_merge($old_rw_user_id, $user_id) : $user_id; // 合并
  67. } else {
  68. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $user_id) : []; // 差集
  69. $all_ro_user_id = $old_ro_user_id ? array_merge($old_ro_user_id, $user_id) : $user_id; // 合并
  70. }
  71. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  72. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  73. } else {
  74. $del_ro_user_id = []; //需要删除的只读
  75. $del_rw_user_id = []; //需要删除的读写
  76. foreach ($user_id as $key=>$val) {
  77. if (in_array($val, $old_ro_user_id) && !in_array($val, $old_rw_user_id) && $type == 2) {
  78. $del_ro_user_id[] = $val;
  79. }
  80. if (in_array($val, $old_rw_user_id) && !in_array($val, $old_ro_user_id) && $type == 1) {
  81. $del_rw_user_id[] = $val;
  82. }
  83. }
  84. if ($type == 2) {
  85. $all_rw_user_id = $old_rw_user_id ? array_diff(array_merge($old_rw_user_id, $user_id), $del_rw_user_id) : $user_id; // 合并
  86. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $del_ro_user_id) : $user_id; // 合并
  87. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  88. if ($del_ro_user_id) {
  89. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  90. }
  91. } else {
  92. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $del_rw_user_id) : $user_id; // 合并
  93. $all_ro_user_id = $old_ro_user_id ? array_diff(array_merge($old_ro_user_id, $user_id), $del_ro_user_id) : $user_id; // 合并
  94. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  95. if ($del_rw_user_id) {
  96. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  97. }
  98. }
  99. }
  100. $upData = db($types)->where([$data_name => $v])->update($data);
  101. if (!$upData) {
  102. $errorMessage[] = $resData['name'].'处理团队操作失败';
  103. }
  104. }
  105. return $errorMessage ? : 1;
  106. }
  107. //根据时间段获取所包含的年份
  108. function getYearByTime($start_time, $end_time)
  109. {
  110. $yearArr = [];
  111. $monthArr = monthList($start_time, $end_time);
  112. foreach ($monthArr as $v) {
  113. $yearArr[date('Y',$v)] = date('Y',$v);
  114. }
  115. return $yearArr;
  116. }
  117. //根据时间段获取所包含的月份
  118. function getmonthByTime($start_time, $end_time)
  119. {
  120. $monthList = [];
  121. $monthArr = monthList($start_time, $end_time);
  122. foreach ($monthArr as $v) {
  123. $monthList[date('Y',$v)][] = date('m',$v);
  124. }
  125. return $monthList;
  126. }