ViewTrait.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\DB;
  5. trait ViewTrait
  6. {
  7. /**
  8. * 获取视图信息
  9. *
  10. * @param $viewAlias
  11. * @return mixed
  12. */
  13. public function index($viewAlias)
  14. {
  15. $cacheKey = 'view:' . $viewAlias;
  16. if ($info = Cache::get($cacheKey)) {
  17. } else {
  18. $infoObj = DB::table('sys_view')->where('v_alias', $viewAlias)->first();
  19. if (empty($infoObj)) {
  20. return $this->responseMessage(2001, '视图不存在,请重试!');
  21. }
  22. $info = get_object_vars($infoObj);
  23. $groupData = json_decode($info['v_data'], true);
  24. if ($groupData) {
  25. //获取所有的属性id
  26. $attrIdArr = [];
  27. foreach ($groupData as $key => $item) {
  28. if (isset($item['attributes']) && $item['attributes']) {
  29. foreach ($item['attributes'] as $akey => $aItem) {
  30. $attrIdArr[] = $aItem['vd_attr_id'];
  31. }
  32. }
  33. }
  34. //获取数据
  35. $attrCodeArr = [];
  36. if ($attrIdArr) {
  37. $attrListObj = DB::table('sys_set_attribute')->where('set_id', $info['v_set_id'])->whereIn('sa_id', $attrIdArr)->get();
  38. $attrList = array_map('get_object_vars', $attrListObj->toArray());
  39. $attrKeyIds = array_column($attrList, 'sa_id');
  40. $attrValueCodes = array_column($attrList, 'sa_code');
  41. $attrCodeArr = array_combine($attrKeyIds, $attrValueCodes);
  42. }
  43. //重新赋值数据
  44. foreach ($groupData as $key => $item) {
  45. if (isset($item['attributes']) && $item['attributes']) {
  46. foreach ($item['attributes'] as $aKey => $aItem) {
  47. $item['attributes'][$aKey]['sa_code'] = $attrCodeArr[$aItem['vd_attr_id']] ?? '';
  48. }
  49. $groupData[$key]['attributes'] = $item['attributes'];
  50. }
  51. }
  52. }
  53. $info['v_data'] = $groupData;
  54. if (in_array($info['v_type'], [1, 2, 3, 4, 5])) {
  55. $info['v_type_data'] = json_decode($info['v_type_data']);
  56. }
  57. //获取接口信息
  58. if ($info['v_interface_id']) {
  59. $interInfoObj = DB::table('sys_interface')
  60. ->select(['i_alias', 'i_func', 'i_func_type', 'i_is_category', 'i_category_level'])->where('i_id', $info['v_interface_id'])->first();
  61. if ($interInfoObj) {
  62. $info['interfaceInfo'] = get_object_vars($interInfoObj);
  63. }
  64. }
  65. Cache::forever($cacheKey, $info);
  66. }
  67. return $this->responseMessage(1001, 'success', $info);
  68. }
  69. }