123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Traits;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- trait ViewTrait
- {
- /**
- * 获取视图信息
- *
- * @param $viewAlias
- * @return mixed
- */
- public function index($viewAlias)
- {
- $cacheKey = 'view:' . $viewAlias;
- if ($info = Cache::get($cacheKey)) {
- } else {
- $infoObj = DB::table('sys_view')->where('v_alias', $viewAlias)->first();
- if (empty($infoObj)) {
- return $this->responseMessage(2001, '视图不存在,请重试!');
- }
- $info = get_object_vars($infoObj);
- $groupData = json_decode($info['v_data'], true);
- if ($groupData) {
- //获取所有的属性id
- $attrIdArr = [];
- foreach ($groupData as $key => $item) {
- if (isset($item['attributes']) && $item['attributes']) {
- foreach ($item['attributes'] as $akey => $aItem) {
- $attrIdArr[] = $aItem['vd_attr_id'];
- }
- }
- }
- //获取数据
- $attrCodeArr = [];
- if ($attrIdArr) {
- $attrListObj = DB::table('sys_set_attribute')->where('set_id', $info['v_set_id'])->whereIn('sa_id', $attrIdArr)->get();
- $attrList = array_map('get_object_vars', $attrListObj->toArray());
- $attrKeyIds = array_column($attrList, 'sa_id');
- $attrValueCodes = array_column($attrList, 'sa_code');
- $attrCodeArr = array_combine($attrKeyIds, $attrValueCodes);
- }
- //重新赋值数据
- foreach ($groupData as $key => $item) {
- if (isset($item['attributes']) && $item['attributes']) {
- foreach ($item['attributes'] as $aKey => $aItem) {
- $item['attributes'][$aKey]['sa_code'] = $attrCodeArr[$aItem['vd_attr_id']] ?? '';
- }
- $groupData[$key]['attributes'] = $item['attributes'];
- }
- }
- }
- $info['v_data'] = $groupData;
- if (in_array($info['v_type'], [1, 2, 3, 4, 5])) {
- $info['v_type_data'] = json_decode($info['v_type_data']);
- }
- //获取接口信息
- if ($info['v_interface_id']) {
- $interInfoObj = DB::table('sys_interface')
- ->select(['i_alias', 'i_func', 'i_func_type', 'i_is_category', 'i_category_level'])->where('i_id', $info['v_interface_id'])->first();
- if ($interInfoObj) {
- $info['interfaceInfo'] = get_object_vars($interInfoObj);
- }
- }
- Cache::forever($cacheKey, $info);
- }
- return $this->responseMessage(1001, 'success', $info);
- }
- }
|