Golang realizes the background system of wechat small program Mall (moshopserver)

Time:2020-10-24

Compared with C / C + +, golang is a new language, and I always want to learn it. I have collected some information on the Internet. Some people say that it is easy to learn. It is true that, compared with C / C + +, there is a lot less messy syntax. The best way to learn a new language is to write something. Recently, small programs are also popular. I also want to learn it. There are some open source projects searched on the Internet, which are basically not implemented by golang. Most of them are written by nodejs and Java. Then I will implement a version of golang, killing two birds with one stone.

Before and after the development of small programs need to develop, their own front-end experience is very little, search some open source code, a small program project let people see, nideshop, the interface is good, the code structure is also clear, and the front and rear end are achieved, their goal is to learn golang and small programs, simply use golang to rewrite nideshop.

The choice of Web Framework

Nodejs and java have some mature frameworks, such as nodejs thinkjs and Java spring framework. Golang has only become popular in recent years. Some web frameworks are relatively new, including the following:

Beego: open source high performance go language web framework. https://github.com/astaxie/beegohttps / / beego.me

Buffalo: use go language to build web applications quickly. https://github.com/gobuffalo/buffalohttps / / gobuffalo.io

Echo: a simple and high performance go language web framework. https://github.com/labstack/echohttps / / echo.labstack.com

Gin: a web framework written in go language to implement APIs similar to Martini framework with better performance. https://github.com/gin-gonic/ginhttps ://gin- gonic.github.io/gin

Iris: the fastest go language web framework in the universe. Complete MVC support, the future is in control. https://github.com/kataras/irishttps ://iris- go.com

Release: go language efficient, full stack web framework. https://github.com/revel/revelhttps / / revel.github.io

Beego is a framework written by Chinese people. There are many documents, many examples, and many people use it. Finally, I decided to use this framework.

Using xorm to generate database model

MySQL is used in the database, and ORM framework has been implemented by using golang to read and write MySQL beego. But the database table needs to generate the corresponding struct, beego’s ORM seems to have no automatic generation function. An open source library go xorm / CMD is adopted. The final database table generated is like the following. It is strange that there is no JSON tag, and the xorm tag is not recognized by golang. Another problem is that some field types xorm are generated incorrectly. For example, for the decimal field in the database, the field type in the structure generated by xorm is string, which is obviously wrong.


type NideshopAd struct {
AdPositionId int `xorm:"not null default 0 index SMALLINT(5)"`
Content  string `xorm:"not null default '' VARCHAR(255)"`
Enabled  int `xorm:"not null default 1 index TINYINT(3)"`
EndTime  int `xorm:"not null default 0 INT(11)"`
Id   int `xorm:"not null pk autoincr SMALLINT(5)"`
ImageUrl  string `xorm:"not null TEXT"`
Link   string `xorm:"not null default '' VARCHAR(255)"`
MediaType int `xorm:"not null default 0 TINYINT(3)"`
Name   string `xorm:"not null default '' VARCHAR(60)"`
}

Moshop server framework

The framework of moshop server is very clear and simple.

The front-end applet initiates an HTTP request to the router (the router can do some filtering in each stage of forwarding the request, which will be discussed later). The router identifies the request link and forwards it to the corresponding controller. There are also three underlying packages:

models

The interfaces that only deal with databases are placed under this package.

services

It mainly realizes three kinds of functions: the interface of interaction with wechat, the interface of Express query and the interface of token generation and detection.

Utils

Some basic function functions are placed under this package.

Token verification

The third-party library JWT go is used to generate and verify the token. This library is used to resolve the userid through the token, and create and verify whether the token is expired. The whole interaction process is as follows:

  1. When the applet is opened, it does not log in by default, and calls the background interface. Because there is no token, the userid cannot be resolved. The applet segment is returned to prompt the user to log in.
  2. The applet calls the wechat background service to obtain userinfo, and calls the moshopserver background login interface.
  3. The background interface calls the wechat background, decrypts the relevant fields in userinfo, generates a user information and inserts it into the moshopserver database. Then the user ID is extracted from the database to generate the token with expiration time. Return to the applet.
  4. The applet calls the wechat background interface and stores the token to the wechat server. Next, every time we call the moshopserver background, we need to take the token from the wechat background and pass it to the moshopsever background.
  5. If the token is expired or invalid, the backend cannot resolve the userid and returns it to the applet to log in again. Generate a new token. So again and again.

Router filter

Nideshop has done some design, some interfaces can be accessed even if the token is expired, and the user does not need to be prompted to log in again. This function is also implemented in moshop server. The filter of beego is used


beego.InsertFilter("/api/*", beego.BeforeExec, services.FilterFunc, true, true)

The beforeexec parameter is used. In this stage, the router has identified the specific types of controller and action. We can judge by ourselves which controllers and actions require user login permission. These controllers and actions are placed in the configuration file api.conf Chinese:


[controller]
publicController= {'index','catalog','topic','auth','goods','brand','search','region'}
[action]
publicAction={'comment/list','comment/count','cart/index','cart/add','cart/checked','cart/update','cart/delete','cart/goodscount','pay/notify'}

If the controller and action of HTTP request link are not in the configuration file, skip the interface call and return to the applet directly to prompt it to log in again.

problem

There are still some bugs in moshop server, because the testing is not enough. There should also be some unknown bugs, which will be fixed slowly in the future.

Problems:

  • Item information is not displayed in the order list.
  • The login is not displayed after the user logs in (the avatar and user name are not displayed)

Welcome to star, welcome to your questions.

https://github.com/harlanc/moshopserver

Screenshot of client applet interface

Finally, a few screenshots are attached

summary

This article on the implementation of golang wechat mall back-end system (moshopserver) will be introduced here. For more related content of golang app mall system, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!

Recommended Today

Explain module, import and export in JavaScript

Author: Tania rascia Crazy technology house Original text:https://www.taniarascia.com/j… In the era of the Internet, websites are mainly developed with HTML and CSS. If you load JavaScript into a page, it usually provides effects and interactions in the form of small fragments. Generally, all JavaScript code is written in a file and loaded into a filescriptTag. […]