CmsWing source code analysis user home page

2021SC@SDUSC

As I stated in my first blog, I mainly need to analyze user behavior in my group

User behavior analysis generally refers to viewing the user's feedback on some indicators of the product from various dimensions.

The various functions of the user's home page are the premise for users to obtain feedback

This part of the code is mainly concentrated in the user.js file

 

The user home page is divided into two modes: ordinary user and administrator user

This time, the main analysis is the administrator user

The database used is member

constructor(ctx) {
    super(ctx); 
    this.db = this.model('cmswing/member');
    this.tactive = 'user';
  }

User list

Force start and length in the database to type int

 async userlistAction() {
    const gets = this.get();
    const start = parseInt(gets.start);
    const length = parseInt(gets.length);
    const draw = gets.draw;
    const key = gets['search[value]'];

Update the last login time and login IP of the user

It is then stored as data for the user list

const userList = await this.db.join({
      table: 'customer',
      join: 'left',
      as: 'u',
      on: ['id', 'user_id']
    }).field('id,username,score,login,last_login_ip,last_login_time,status,u.real_name,u.group_id,u.balance').limit(start, length).where({username: ['like', '%' + key + '%'], status: ['>', -1]}).order('id DESC').countSelect();
    userList.data.forEach(v => {
      v.last_login_time = times(v.last_login_time);
      v.last_login_ip = _int2iP(v.last_login_ip);
    });

join is used to convert an array to a string without changing the original array

For details, please refer to JavaScript Array join() method

Then, the data properties of this web page will become as follows

Finally, return the newly updated data attribute

 const data = {
      'draw': draw,
      'recordsTotal': userList.count,
      'recordsFiltered': userList.count,
      'data': userList.data
    };
    return this.json(data);
  }

 

Display user information

Get the ID of the current user from the database and initialize a user object

If this user belongs to the manager, he / she has permission to display the following content

Then you can start to get the list of member groups and administrator groups

Finally, set the title of this page to "personal information"

async showuserAction() {
    const id = this.get('id');
    const user = await this.model('member').find(id);
    this.assign('user', user);
    if (user.is_admin == 1) {
      const roleid = await this.model('auth_user_role').where({user_id: user.id}).getField('role_id', true);
      this.assign('roleid', roleid);
    }
    const usergroup = await this.model('member_group').select();
    this.assign('usergroup', usergroup);
    const role = await this.model('auth_role').where({status: 1}).select();
    this.assign('role', role);
    this.meta_title = 'personal information';
    return this.display();
  }

Add user

The first time I saw the function that allows administrators to add users... Won't ordinary users be very upset?

First, verify whether the password entered is consistent with the password transmitted in the past. If it is inconsistent, an error will be returned

If successful, set the input password to this number

async adduserAction() {
    if (this.isPost) {
      const data = this.post();
      if (data.password != data.repassword) {
        return this.fail('The passwords filled in twice are inconsistent');
      }
      data.password = encryptPassword(data.password);

reg should refer to the operation time

Then judge whether the user to be established is a vip

data.reg_time = new Date().getTime();
      if (data.vip == 1) {
        data.overduedate = new Date(data.overduedate).getTime();
      } else {
        data.overduedate = think.isEmpty(data.overduedate) ? 0 : data.overduedate;
      }
      console.log(data);

Force the status in the user's data to be set to 1

If you are adding an administrator, add it to self.db

Otherwise, it is added directly to the current database

data.status = 1;
      const self = this;
      let res;
      if (data.is_admin == 1) {
        res = await this.db.transaction(async() => {
          const userId = await self.db.add(data);
          return await self.model('auth_user_role').db(self.db.db()).add({
            user_id: userId,
            role_id: data.role_id
          });
        });
      } else {
        res = await this.db.add(data);
      }

After that, it is gratifying to judge whether the addition is successful

 if (res) {
        return this.success({name: 'Successfully added!'});
      } else {
        return this.fail('Add failed!');
      }
    

If you fail to receive the returned contents of the database at the beginning

You can display any page

The title is "add user"

else {
      // Member group
      const usergroup = await this.model('member_group').select();
      this.assign('usergroup', usergroup);
      // Get management group
      const role = await this.model('auth_role').where({status: 1}).select();
      this.assign('role', role);
      this.meta_title = 'Add user';
      return this.display();
    }

Keywords: Javascript

Added by mlcheatham on Sun, 05 Dec 2021 21:48:06 +0200