PageSourceSearch

https://www.linyangpvc.com/src/api/template.js

js linyangpvc.com collected 2026-10-04 04:54:23 UTC 13,393 bytes, 332 lines download raw bytes

1import apiAddress from '../constant/apiAddress.js'
2import { getPageType } from '../../config/index.js'
3
4export function getTemplateData(ctx, params) {
5  return new Promise((resolve, reject) => {
6    if (ctx.state.cache && ctx.state.cache.getApiData('template')) {
7      resolve(getTemplateData(ctx.state.cache.getApiData('template')['data']))
8    } else if (ctx.state.postCtxState && ctx.state.postCtxState.templateData) {
9      resolve(getTemplateData(ctx.state.postCtxState.templateData))
10    } else {
11      ctx.state.request(ctx, {
12        name: '板块数据',
13        url: apiAddress.templateData,
14        method: 'POST',
15        prefix: '/qqt',
16        log: 2,
17        params
18      })
19        .then(res => {
20          if (ctx.state.cache) ctx.state.cache.fillApiData(res, 'template')
21          // 将处理好的数据返回出去
22          resolve(getTemplateData(res.data))
23        })
24        .catch(error => {
25          reject(error)
26        })
27    }
28  })
29
30  function getTemplateData(res) {
31    const templateData = {
32      use_template: res.use_template
33    }
34    // 页面是否保存过
35    ctx.state.site.is_page_saved = res.block_exist && res.use_template === 0
36    templateData.block_exist = res.block_exist
37
38    // 如果site是空数组
39    res.site = res.site || {}
40
41    if (res.site instanceof Array) res.site = { style_sign: {} }
42    if (res.site.style_sign instanceof Array) res.site.style_sign = {}
43    templateData.site_style_sign = res.site.style_sign
44
45    // 如果packages是空数组
46    if (res.packages instanceof Array) res.packages = { block: {}, module: {}, grid: {}, cell: {}, unit: {} }
47    // 检查packages里的各项
48    if (res.packages.block instanceof Array) res.packages.block = {}
49    if (res.packages.module instanceof Array) res.packages.module = {}
50    if (res.packages.grid instanceof Array) res.packages.grid = {}
51    if (res.packages.cell instanceof Array) res.packages.cell = {}
52    if (res.packages.unit instanceof Array) res.packages.unit = {}
53    templateData.packages = res.packages
54    
55    // 如果data是空数组
56    if (res.data instanceof Array) res.data = { detail: {}, html: {}, config: {}, style: {} }
57    // 检查data里的各项
58    if (res.data.detail instanceof Array) res.data.detail = {}
59    if (res.data.html instanceof Array) res.data.html = {}
60    if (res.data.config instanceof Array) res.data.config = {}
61    if (res.data.style instanceof Array) res.data.style = {}
62
63    Object.keys(res.data.style).forEach(sign => {
64      if (res.data.style[sign].default instanceof Array) res.data.style[sign].default = {}
65      if (res.data.style[sign].md instanceof Array) res.data.style[sign].md = {}
66      if (res.data.style[sign].xl instanceof Array) res.data.style[sign].xl = {}
67    })
68    templateData.data = res.data
69
70    // 如果page_block是空数组,则表示这个站未被保存过
71    if (res.page_block instanceof Array) {
72      templateData.page_block = { header: '', breadcrumb: '', info: '', main: '', extend: '', footer: '', copyright: '' }
73      templateData.site_unsaved = true
74    } else {
75      // 这个站被保存过,需要调整下接口返回的数据格式
76      // packages里的detail_list/children转为数组
77      Object.keys(res.packages).forEach(type => {
78        Object.keys(res.packages[type]).forEach(sign => {
79          // 当前package如果是空数组
80          if (res.packages[type][sign] instanceof Array) res.packages[type][sign] = {}
81          if (type === 'unit') {
82            if (res.packages[type][sign].detail_list instanceof Array) {
83              templateData.packages[type][sign].detail_list = res.packages[type][sign].detail_list
84            } else {
85              // 组件需要把detail_list转为数组
86              res.packages[type][sign].detail_list = res.packages[type][sign].detail_list || ''
87              templateData.packages[type][sign].detail_list = res.packages[type][sign].detail_list.split(',').filter(item => item)
88            }
89            if (templateData.packages[type][sign].content.hover_img) {
90              ctx.state.page.effect['animate'] = true
91            }
92          } else {
93            if (res.packages[type][sign].children instanceof Array) {
94              templateData.packages[type][sign].children = res.packages[type][sign].children
95            } else {
96              // 非组件需要把children转为数组
97              res.packages[type][sign].children = res.packages[type][sign].children || ''
98              templateData.packages[type][sign].children = res.packages[type][sign].children.split(',').filter(item => item)
99            }
100          }
101        })
102      })
103      // data.detail.children转为数组
104      Object.keys(res.data.detail).forEach(sign => {
105        if (res.data.detail[sign].children instanceof Array) {
106          templateData.data.detail[sign].children = res.data.detail[sign].children
107        } else {
108          res.data.detail[sign].children = res.data.detail[sign].children || ''
109          templateData.data.detail[sign].children = res.data.detail[sign].children.split(',').filter(item => item)
110        }
111      })
112      // 保存过的站点,使用保存的板块数据初始化 page_block
113      templateData.page_block = {
114        header: res.page_block.header || {},
115        breadcrumb: res.page_block.breadcrumb || {},
116        info: res.page_block.info || {},
117        main: res.page_block.main || {},
118        extend: res.page_block.extend || {},
119        footer: res.page_block.footer || {},
120        copyright: res.page_block.copyright || {}
121      }
122    }
123    /**
124     * 如果是通过线上模板创建的站点,第一次进首页装修的时候清掉href 
125     * @params (use_template) 是否是通过线上模板建站的,然后没被保存过
126     */
127    if (res.use_template && params.page_type == 'index') {
128      let detail = templateData.data.detail
129      let html = templateData.data.html
130      
131      Object.keys(detail).forEach(sign => {
132        if (_checkReplace(detail[sign].href)) {
133          detail[sign].href = 'javascript:;'
134          detail[sign].target = '_self'
135        }
136      })
137      Object.keys(html).forEach(sign => {
138        // html里的href属性截取下来
139        let text = html[sign].text
140        text = text.replace(/\shref=[\'|\"](.*?)[\'|\"]/g, _replaceFunction)
141        text = text.replace(/\sdata-mce-href=[\'|\"](.*?)[\'|\"]/g, _replaceFunction)
142        text = text.replace(/\starget=[\'|\"](.*?)[\'|\"]/g, (match, p1) => p1 === '_blank' ? '_self' : match)
143        html[sign].text = text
144      })
145      /* 检查是否替换href */
146      function _checkReplace(url) {
147        return getPageType(url, ctx.state.site.site_type) == 404
148      }
149      function _replaceFunction(match, p1) {
150        return _checkReplace(p1) ? match.replace(p1, 'javascript:;') : match
151      }
152    }
153
154    return templateData
155  }
156}
157
158export function getTemplateModuleData(ctx, params) {
159  return new Promise((resolve, reject) => {
160    ctx.state.request(ctx, {
161      name: '模块数据',
162      url: apiAddress.moduleData,
163      method: 'GET',
164      prefix: '/qqt',
165      log: 2,
166      params
167    })
168      .then(res => {
169        const getChildren = function(data, children, type) {
170          let child = []
171          Object.keys(data.packages[type]).forEach(item => {
172            if (children.indexOf(item) > -1) {
173              let temp = data.packages[type][item]
174              delete temp.sign
175              temp.style = {}
176              Object.keys(temp.style_sign).forEach(styleItem => {
177                temp.style[styleItem] = data.data.style[temp.style_sign[styleItem]]
178                delete temp.style[styleItem].sign
179              })
180              delete temp.style_sign
181
182              let subtype = ''
183              if (type === 'module') {
184                subtype = 'grid'
185              } else if (type === 'grid') {
186                subtype = 'cell'
187              } else if (type === 'cell') {
188                subtype = 'unit'
189              } else if (type === 'unit') {
190                temp.config = {}
191                if (temp.config_list) {
192                  const configList = temp.config_list.split(',')
193                  configList.forEach(configItem => {
194                    if (!configItem || !data.data.config[configItem]) return
195                    let tempConfig = data.data.config[configItem]
196                    delete tempConfig.sign
197
198                    if (data.data.config[configItem]['type'] === 'form' && data.data.config[configItem]['list'].length) {
199                      // 拼装表单
200                      const configList = []
201                      data.data.config[configItem]['list'].forEach(formItem => {
202                        let tempFormConfig = data.data.config[formItem]
203                        delete tempFormConfig.sign
204
205                        Object.keys(tempFormConfig).forEach(keyItem => {
206                          if (keyItem.indexOf('_sign') > 0) {
207                            if (tempFormConfig[keyItem] && data.data.html[tempFormConfig[keyItem]]) {
208                              tempFormConfig[keyItem.replace('_sign', '')] = data.data.html[tempFormConfig[keyItem]]['text']
209                            }
210                            delete tempFormConfig[keyItem]
211                          }
212                        })
213                        configList.push(tempFormConfig)
214                      })
215                      tempConfig.list = configList
216                    }
217
218                    temp.config[data.data.config[configItem]['type']] = tempConfig
219                  })
220                }
221
222                temp.data = []
223                if (temp.detail_list) {
224                  const detailList = temp.detail_list.split(',')
225                  detailList.forEach(detailItem => {
226                    if (!detailItem) return
227                    let tempDetail = data.data.detail[detailItem]
228                    delete tempDetail.sign
229                    Object.keys(tempDetail).forEach(keyItem => {
230                      if (keyItem.indexOf('_sign') > 0) {
231                        if (tempDetail[keyItem] && data.data.html[tempDetail[keyItem]]) {
232                          tempDetail[keyItem.replace('_sign', '')] = data.data.html[tempDetail[keyItem]]['text']
233                        }
234                        delete tempDetail[keyItem]
235                      }
236                    })
237                    temp.data.push(tempDetail)
238                  })
239                }
240              }
241
242              if (temp.children && subtype) {
243                temp[subtype] = getChildren(data, temp.children.split(','), subtype)
244              }
245              child.push(temp)
246            }
247          })
248          return child
249        }
250
251        const result = getChildren(res.data, Object.keys(res.data.packages['module']), 'module')
252        // console.log(1212, JSON.stringify(result))
253
254        resolve(result[0])
255      })
256      .catch(error => {
257        reject(error)
258      })
259  })
260}
261
262export function getProductAttrList(ctx, params) {
263  return new Promise((resolve, reject) => {
264    const cacheParams = {
265      site_id: params.site_id,
266      language_code: ctx.state.site.language_code || 'en',
267      type: '1,2,3', 
268      status: 1, 
269      is_show: 1
270    }
271    if (params['page_category_id']) {
272      cacheParams['page_category_id'] = params['page_category_id']
273    }
274    params = {...cacheParams, ...params}
275
276    const cacheValid = ctx.query.force === 'html' && ctx.state.cache && JSON.stringify(params, Object.keys(params).sort()) === JSON.stringify(cacheParams, Object.keys(cacheParams).sort())
277    if (cacheValid) {
278      const cacheData = ctx.state.cache?.getApiData('product_attr_list', cacheParams)?.data
279      if (cacheData) {
280        resolve(cacheData?.list)
281        return
282      }
283    }
284    ctx.state.request(ctx, {
285      name: '产品列表属性',
286      url: apiAddress.productAddrList, 
287      params
288    })
289      .then(async res => {
290        if (cacheValid) ctx.state.cache.fillApiData(res, 'product_attr_list', cacheParams)
291        resolve(res.data?.list)
292      })
293      .catch(error => reject(error))
294  })
295}
296
297// export function getStructData(ctx, params) {
298//   return new Promise((resolve, reject) => {
299//     const cacheParams = {
300//       site_id: params.site_id,
301//       language_code: ctx.state.site.language_code || 'en',
302//       // type: '1,2,3', 
303//       // status: 1, 
304//       // is_show: 1
305//       page_url: params.page_url
306//     }
307//     // if (params['page_url']) {
308//     //   cacheParams['page_url'] = params['page_url']
309//     // }
310//     params = {...cacheParams, ...params}
311
312//     // 暂时不需要接口缓存
313//     // const cacheValid = ctx.query.force === 'html' && ctx.state.cache && JSON.stringify(params, Object.keys(params).sort()) === JSON.stringify(cacheParams, Object.keys(cacheParams).sort())
314//     // if (cacheValid) {
315//     //   const cacheData = ctx.state.cache?.getApiData('struct_data', cacheParams)?.data
316//     //   if (cacheData) {
317//     //     resolve(cacheData?.data)
318//     //     return
319//     //   }
320//     // }
321//     ctx.state.request(ctx, {
322//       name: '结构化数据',
323//       url: apiAddress.getStructData, 
324//       params: cacheParams
325//     })
326//       .then(res => {
327//         // if (cacheValid) ctx.state.cache.fillApiData(res, 'struct_data', cacheParams)
328//         resolve(res)
329//       })
330//       .catch(error => reject(error))
331//   })
332// }

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.