common.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. //权限控制
  3. \think\Hook::add('check_auth', 'app\\common\\behavior\\AuthenticateBehavior');
  4. use think\Db;
  5. /**
  6. * 处理相关团队
  7. * @param types 类型
  8. * @param types 类型ID
  9. * @param type 权限 1只读2读写
  10. * @param user_id [array] 协作人
  11. * @param is_del 1 移除操作, 2编辑操作, 3添加操作
  12. * @param owner_user_id 操作人
  13. * @param is_module 相关 1相关,不进行数据权限判断
  14. * @author
  15. */
  16. function teamUserId($param, $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. case 'crm_receivables' :
  42. $data_name = 'receivables_id';
  43. $authIds = $userModel->getUserByPer('crm', 'receivables', 'teamsave');
  44. break;
  45. }
  46. if (!is_array($types_id) && $types_id) {
  47. $types_id = [$types_id];
  48. }
  49. $errorMessage = [];
  50. foreach ($types_id as $k => $v) {
  51. if ($types == 'crm_receivables') {
  52. $resData = db($types)->where([$data_name => $v])->field('number as name,owner_user_id,rw_user_id,ro_user_id')->find();
  53. } else {
  54. $resData = db($types)->where([$data_name => $v])->field('name,owner_user_id,rw_user_id,ro_user_id')->find();
  55. }
  56. if (!in_array($resData['owner_user_id'], $authIds) && $resData['owner_user_id'] && $is_module !== 1) {
  57. $errorMessage[] = $resData['name'] . '处理团队操作失败,错误原因:无权限';
  58. continue;
  59. }
  60. $type = $type ?: 1;
  61. $data = [];
  62. //读写
  63. $old_rw_user_id = stringToArray($resData['rw_user_id']) ?: []; //去重
  64. //只读
  65. $old_ro_user_id = stringToArray($resData['ro_user_id']) ?: []; //去重
  66. if ($is_del == 1) {
  67. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $user_id) : ''; // 差集
  68. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  69. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $user_id) : ''; // 差集
  70. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  71. } elseif ($is_del == 2) {
  72. if ($type == 2) {
  73. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $user_id) : []; // 差集
  74. $all_rw_user_id = $old_rw_user_id ? array_merge($old_rw_user_id, $user_id) : $user_id; // 合并
  75. } else {
  76. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $user_id) : []; // 差集
  77. $all_ro_user_id = $old_ro_user_id ? array_merge($old_ro_user_id, $user_id) : $user_id; // 合并
  78. }
  79. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  80. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  81. } else {
  82. $del_ro_user_id = []; //需要删除的只读
  83. $del_rw_user_id = []; //需要删除的读写
  84. foreach ($user_id as $key => $val) {
  85. if (in_array($val, $old_ro_user_id) && !in_array($val, $old_rw_user_id) && $type == 2) {
  86. $del_ro_user_id[] = $val;
  87. }
  88. if (in_array($val, $old_rw_user_id) && !in_array($val, $old_ro_user_id) && $type == 1) {
  89. $del_rw_user_id[] = $val;
  90. }
  91. }
  92. if ($type == 2) {
  93. $all_rw_user_id = $old_rw_user_id ? array_diff(array_merge($old_rw_user_id, $user_id), $del_rw_user_id) : $user_id; // 合并
  94. $all_ro_user_id = $old_ro_user_id ? array_diff($old_ro_user_id, $del_ro_user_id) : $user_id; // 合并
  95. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  96. if ($del_ro_user_id) {
  97. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  98. }
  99. } else {
  100. $all_rw_user_id = $old_rw_user_id ? array_diff($old_rw_user_id, $del_rw_user_id) : $user_id; // 合并
  101. $all_ro_user_id = $old_ro_user_id ? array_diff(array_merge($old_ro_user_id, $user_id), $del_ro_user_id) : $user_id; // 合并
  102. $data['ro_user_id'] = $all_ro_user_id ? arrayToString($all_ro_user_id) : ''; //去空
  103. if ($del_rw_user_id) {
  104. $data['rw_user_id'] = $all_rw_user_id ? arrayToString($all_rw_user_id) : ''; //去空
  105. }
  106. }
  107. }
  108. $res = !empty($param['user_id']) ?$param['user_id'] :[];
  109. $types_data = ['crm_leads' => 6, 'crm_customer' => 1, 'crm_contacts' => 2, 'crm_business' => 3, 'crm_contract' => 4, 'crm_receivables' => 5];
  110. $target_time = $param['target_time'];
  111. $request = [];
  112. foreach ($res as $val) {
  113. $request['team_user_id'] = $val;
  114. $request['target_time'] = $target_time;
  115. $request['auth'] = $type;
  116. $request['target_id'] = $v;
  117. $dataInfo = db('crm_team')->where(['target_id' => $v, 'types' => $types_data[$types],'team_user_id'=>$val])->find();
  118. if ($dataInfo) {
  119. $res = db('crm_team')->where(['target_id' => $v, 'types' => $types_data[$types],'team_user_id'=>$val])->update($request);
  120. } else {
  121. $request['types'] = $types_data[$types];
  122. $res = db('crm_team')->insert($request);
  123. }
  124. }
  125. $upData = db($types)->where([$data_name => $v])->update($data);
  126. if (!$upData && !$res) {
  127. $errorMessage[] = $resData['name'] . '处理团队操作失败';
  128. }
  129. }
  130. return $errorMessage ?: 1;
  131. }
  132. //根据时间段获取所包含的年份
  133. function getYearByTime($start_time, $end_time)
  134. {
  135. $yearArr = [];
  136. $monthArr = monthList($start_time, $end_time);
  137. foreach ($monthArr as $v) {
  138. $yearArr[date('Y', $v)] = date('Y', $v);
  139. }
  140. return $yearArr;
  141. }
  142. //根据时间段获取所包含的月份
  143. function getmonthByTime($start_time, $end_time)
  144. {
  145. $monthList = [];
  146. $monthArr = monthList($start_time, $end_time);
  147. foreach ($monthArr as $v) {
  148. $monthList[date('Y', $v)][] = date('m', $v);
  149. }
  150. return $monthList;
  151. }
  152. function encrypt($data, $key)
  153. {
  154. header('Content-type:text/html;charset=utf-8');
  155. $key = md5($key);
  156. $x = 0;
  157. $len = mb_strlen($data);
  158. $l = mb_strlen($key);
  159. for ($i = 0; $i < $len; $i++) {
  160. if ($x == $l) {
  161. $x = 0;
  162. }
  163. $char .= $key{$x};
  164. $x++;
  165. }
  166. for ($i = 0; $i < $len; $i++) {
  167. $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
  168. }
  169. return base64_encode($str);
  170. }
  171. /**
  172. * [对加密的数据进行解密]
  173. * @E-mial wuliqiang_aa@163.com
  174. * @TIME 2017-04-07
  175. * @WEB http://blog.iinu.com.cn
  176. * @param [数据] $data [已经进行加密的数据]
  177. * @param [密钥] $key [解密的唯一方法]
  178. */
  179. function decrypt($data, $key = '72-crm')
  180. {
  181. header('Content-type:text/html;charset=utf-8');
  182. $key = md5($key);
  183. $x = 0;
  184. $data = base64_decode($data);
  185. $len = mb_strlen($data);
  186. $l = mb_strlen($key);
  187. for ($i = 0; $i < $len; $i++) {
  188. if ($x == $l) {
  189. $x = 0;
  190. }
  191. $char .= mb_substr($key, $x, 1);
  192. $x++;
  193. }
  194. for ($i = 0; $i < $len; $i++) {
  195. if (ord(mb_substr($data, $i, 1)) < ord(mb_substr($char, $i, 1))) {
  196. $str .= chr((ord(mb_substr($data, $i, 1)) + 256) - ord(mb_substr($char, $i, 1)));
  197. } else {
  198. $str .= chr(ord(mb_substr($data, $i, 1)) - ord(mb_substr($char, $i, 1)));
  199. }
  200. }
  201. return $str;
  202. }