[20211221] CmsWing code analysis - src/controller/extend/controller.js

2021SC@SDUSC

src/controller/extend/controller.js

Follow the above.

moment: moment,

The previously analyzed content includes moment, which will not be described.

mtpl(action = this.ctx.action) {
    const c = this.ctx.controller.split('/');
    c.splice((this.ctx.controller.split('/').length - 1), 0, 'mobile');
    return temp = `${c.join('/')}_${action}`;
  },

It is probably the meaning of multiply (abbreviation is not a good habit). Action is the action in the context. You will find that the content in the context is frequently used in the extension of the controller. The content of the context has been analyzed before, and now we want to refer to the explanation in the official document:

Context is an object in processing user requests in Koa, which runs through the whole request life cycle. It is generally used in middleware, controller and logic, referred to as ctx for short.

para(param) {
    return this.get(param) || this.post(param);
  },

For the operation of parameters, the parameters returned are obtained by get or post.

pagination(data, config = {}) {
    const ops = think.extend({
      desc: true, // show description
      pageNum: 2,
      url: '', // page url, when not set, it will auto generated
      class: 'nomargin', // pagenation extra class
      text: {
        next: 'next page',
        prev: 'previous page',
        total: 'total: __COUNT__ , the number of pages: __PAGE__'
      }}, config);
    return pagination(data, this.ctx, ops);
  },

Page paging operation. There are some default settings. The return values are data, context and ops (which should be setting data)

get isweixin() {
    let flag = false;
    const agent = this.userAgent.toLowerCase();
    // let key = ["mqqbrowser","micromessenger"];
    const key = ['micromessenger'];
    // Exclude Windows desktop systems
    if (!(agent.indexOf('windows nt') > -1) || (agent.indexOf('windows nt') > -1 && agent.indexOf('compatible; msie 9.0;') > -1)) {
      // Exclude Apple Desktop
      if (!(agent.indexOf('windows nt') > -1) && !agent.indexOf('macintosh') > -1) {
        for (const item of key) {
          if (agent.indexOf(item) > -1) {
            flag = true;
            break;
          }
        }
      }
    }
    return flag;
  },

Judging whether it is a wechat terminal is similar to judging whether it is a mobile terminal. Exclude the desktop system and finally return flag.

extModel(modelName = '', extName = '', config = this.config('model.mysql'), prefix = 'ext_') {
    const p = this.ctx.controller.split('/');
    extName = think.isEmpty(extName) ? p[1] : extName;
    return think.extModel(modelName, extName, config, prefix);
  },

Extended model. The return values are the model name, extension, configuration, and prefix. For the model, please refer to the section on relational database in the official document:

Extended model function
The framework does not provide the function of model by default. It needs to load the corresponding extension to support it. The corresponding module is think model. Modify the extended configuration file Src / config / extend JS (the multi module project is src/common/config/extend.js), add the following configuration:

const model = require('think-model');

module.exports = [
  model(think.app) // Let the framework support the function of the model
]

After adding the extension of the model, the method think. Is added Model,think. model,ctx.model,controller.model,service.model.

extService(name = '', ser = '', ...args) {
    const p = this.ctx.controller.split('/');
    ser = think.isEmpty(ser) ? p[1] : ser;
    return think.extService(name, ser, ...args);
  },

Is an extension of Service. The official document explains this: sometimes in the project, in addition to querying the database and other operations, it is also necessary to call some remote interfaces, such as the interface of GitHub and the interface of sending SMS. This function is not appropriate under the Model. Therefore, the framework provides a Service to solve this kind of problem.

The return value has a name, ser (if it is empty, it is separated by the controller, otherwise it is Ser), and various parameters.

extDisplay(p = this.ctx.action, m = '') {
    // console.log(this.ctx.controller);
    const c = this.ctx.controller.split('/');
    if (p === 'm' || !think.isEmpty(m)) {
      if (p === 'm') p = this.ctx.action;
      const pp = path.join(think.ROOT_PATH, 'src', 'controller', 'ext', c[1], 'view', 'mobile', c[2]);
      return this.display(`${pp}_${p}`);
    } else {
      const pp = path.join(think.ROOT_PATH, 'src', 'controller', 'ext', c[1], 'view', 'pc', c[2]);
      return this.display(`${pp}_${p}`);
    }
  },

About display extensions. p stands for Action. This is another place of use displayed differently according to the selection of mobile terminal and pc terminal.

Keywords: Javascript

Added by deckrdx on Sun, 26 Dec 2021 04:00:05 +0200