▌ Project Background

In this project, one person is responsible for the conception and development from the back end to the front end. Now I will talk about the conception of the educational assistant small program to the development and implementation (based on cloud development).

1. Source of inspiration

Educational administration small program inspiration source: run out, check a score and schedule, no need to download APP or go to look at the history of the public number.

In addition, I want to realize the development of a similar APP for a long time, but the development of APP is not friendly to the development of small white, I do not know where to start! Fortunately, the emergence of small program · cloud development has solved my needs. Its advantages of low entry threshold and free back-end operation and maintenance enable me to quickly develop an application small program without any training.

2,

Educational affairs small program needs the core is: result query, curriculum query, educational affairs notice query! So the problem comes, the school administration office only web version, educational administration applets data from where? After a series of thinking, Baidu various problems, ideas come:

Back-end simulation landing – get page data – collate data – feedback to the small program front-end rendering

The general structure is as follows:

▌ Project development

1, the back end

The implementation of the back end is entirely cloud-based. Partial contents:

Using cloud to develop back-end Node. js language, mainly using modules:

The Router module:

const cloud = require('tcb-admin-node');
// npm install tcb-router
const TcbRouter = require('tcb-router');
cloud.init({
  env: '/ /'
})
const db = cloud.database();
const _ = db.command;
// Cloud function entry function
exports.main = async (event, context) => {
  const app = new TcbRouter({ event });
  /** log in to eg*/
  app.router('login'.async (ctx, next) => {
    const test = require('login/login.js');
    ctx.body = test.main(event, context);
  });
  /** select result */
  app.router('getpoint'.async (ctx, next) => {
    const logList = require('getpoint/index.js');
    ctx.body = logList.main(event, context);
  });
  /** Academic activities */
  app.router('academic'.async (ctx, next) => {
    const userList = require('schoolnews/academic.js');
    ctx.body = userList.main(event, context);
  });
  app.router('xsxx'.async (ctx, next) => {
    const userList = require('schoolnews/xsxx.js');
    ctx.body = userList.main(event, context);
  });
  return app.serve();
}
Copy the code

Cherrio implements class schedule and scores, etc.

const cloud = require('tcb-admin-node')
const rp = require('request-promise');
var cheerio = require("cheerio");
cloud.init()
module.exports = {
  main: async (event, context) => {
    var url ='URL'
    var res = await rp({method: 'get'.uri: url,json: false}).then((body) = > {
      var academic = [];
      var $ = cheerio.load(body);
      $('.fl').find('dl').each(function (i, elem) {
        // The business code is not written
        / * * * * /
        academic.push({date: date,time:time,title:title,speaker:speaker,place:place,link:link})
      });
      return academic
    }).catch(err= > {
      return err;
    })
    return res
  }
}
Copy the code

Database access_token changes periodically

// Cloud function entry file
const cloud = require('wx-server-sdk'),rp = require('request-promise'),key=require('key.js')
cloud.init({
  env: "/ /"
})
// Specify the database environment
const db = cloud.database({
  env: "/ /"
}), _ = db.command;
// Cloud function entry function
exports.main = async (event, context) => {
  try { 
    var res = await rp(
      {
        method: 'get'.uri: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + key.APPID + '&secret=' + key.APPSECRET,
        qs: {},/ / parameters
        headers: {},/ / request header
        json: true  // Whether the data is json
      }
    ).then((body) = > {
      return body
    }).catch(err= > {
      return {errmsg:"Failed to get rp function"}})/* Saves the obtained access_token to the database */
    console.log(res)
    if (res.hasOwnProperty('access_token')) {
      await db.collection('key').where({
        type: "accesstoken"
      }).update({
        data: {
          accesstoken: res.access_token,
          datearray: _.unshift(new Date(new Date().getTime())),
          num: _.inc(1)}}}else {
      console.log("Err error" + res)
    }
  } catch (err) {
    console.log(err)
  } 
  
}
Copy the code

In addition, with the help of other modules to achieve the login, data processing (data formatting such as class schedule), cloud development database operation (user information storage, message release), user authority identification (to ensure the security of background information), and so on, will not be described here.

2, the front end,

Xiao Bai is “simple and crude” for a variety of if, var operations;

The details are as follows:

① Class schedule:

Realized the one-key import (in fact, the function of class schedule can form a general small program online), every day in the form of a card on the home page to remind: what class today, finished?

② Theme global replacement:

Support custom theme color, to the user custom ability.

(3) calendar:

Use the pole calendar plugin to beautify it.

▌ comprehension

From a small white to from the back end to the front end to the UI all their own beginners to participate in the heart of the process is so much, the current source temporarily not open, this small program have questions and suggestions can be in the message, at the same time, I hope we can use the small program to achieve their own ideas and creativity!

The source address

Github.com/TencentClou…


If you want to know more about the cloud development of CloudBase related technical stories/technical combat experience, please scan the code to follow [Tencent cloud development] public account ~

About Cloud Development

CloudBase is a product solution of cloud integration. It adopts serverless architecture, eliminates operation and maintenance transactions such as environment construction, supports one cloud and multiple terminals, and helps to quickly build small programs, Web applications and mobile applications.

  • Technical documentation: www.cloudbase.net/
  • Wechat search: Tencent Cloud Development, get the latest progress of the project