🎈1. Requirement analysis

1.1 Obtaining Data

Crawlers are used to crawl some commodity information on an e-commerce platform, including but not limited to commodity pictures, prices, names, etc.

1.2 User Operations

The customer

  • Sign up, log in, log out
  • User profile display and edit, upload profile picture
  • Change password

merchants

  • Have all the functions of the customer
  • Can upload snacks information (including pictures, prices and other information)

The administrator

  • Has all the functionality of the above user
  • User management
  • Commodity information management

1.3 Other Functions

  • Add virtual currency function.
  • The order has an expiration date
  • Add a recharge interface for the administrator, the administrator can add money for a user.
  • Add shopping cart and backpack features. The purchase operation is completed in the shopping cart interface, and the item transfer and currency transfer are completed after the purchase (the items are automatically removed from the shelves after the purchase).
  • The items in the backpack can be uploaded by the user, but by default, they will not appear in the shopping page, and the owner needs to put them on the shelf before they can be purchased by other users.

1.4 Expanding Functions

  • Pay the password
  • Comment under merchandise
  • Pay attention to concurrency

1.5 Development Environment

Backend: Python V3.8, Golang V1.9.1

Database: MySql v5.7.30, Redis V4.0.9

File storage: Qiniu Cloud storage

Payment interface: Payment FM


🎉2. Back-end logic code

2.1 Python – Crawler

  • The database table
class product_img_info(Base) :
    __tablename__ = 'product_param_img'  Table name in database
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(), nullable=False) title = Column(String()) category_id = Column(String()) product_id = Column(String()) info = Column(String()) img_path  = Column(String()) price = Column(String()) discount_price = Column(String()) created_at = Column(DateTime, default=datetime.now) updated_at = Column(DateTime, default=datetime.now) deleted_at = Column(DateTime, default =None)

    def __repr__(self) :
        return """ 
      
        """
      (id:%s,> % (self.id,self.product_id)
Copy the code
  • Crawl operation
def getHTMLText(url) :
    try:
        header = {
            'user-agent': 'the Mozilla / 5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'.'cookie': ' ' Copy cookie to browser
        }
        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        # print(r.text)
        return r.text
    except:
        return ""

def parsePage(html) :
    view_prices = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
    view_fees = re.findall(r'\"view_fee\"\:\"[\d\.]*\"', html)
    raw_titles = re.findall(r'\"raw_title\"\:\".*? \ "', html)
    titles = re.findall(r'\"title\"\:\".*? \ "', html)
    user_ids = re.findall(r'\"user_id\"\:\"[\d\.]*\"',html)
    pic_urls = re.findall(r'\"pic_url\"\:\".*? \ "',html)
    detail_urls = re.findall(r'\"detail_url\"\:\".*? \ "',html)
    for view_price,view_fee,title,raw_title,user_id,pic_url,detail_url in zip(view_prices,view_fees,titles,raw_titles,user_ids,pic_urls,detail_urls):
        price=eval(view_price.split(':') [1])
        discount_price=eval(view_fee.split(':') [1])
        name=eval(title.split(':') [1])
        a4=eval(raw_title.split(':') [1])
        product_id=eval(user_id.split(':') [1])
        img_path=eval(pic_url.split(':') [1])
        persopn = product_img_info(name=name,title=name,product_id=product_id,category_id="6",info=name,img_path=img_path,price=price,discount_price=discount_price)
        session.add(persopn)  # Add one
        session.commit() # Submit to database

# 1 Mobile phone 2 women 3 computer 4 cup 5 Snack 6 earphone

def main() :
    goods = 'headphone'
    depth = 1
    start_url = 'https://s.taobao.com/search?q=' + goods
    for i in range(depth):
        try:
            url = start_url + '&s=' + str(44 * i)
            html = getHTMLText(url)
            parsePage(html)
        except:
            continue

if __name__ == '__main__':
   # Base.metadata.create_all() # Create table
    #sqlOperation()
    main()
Copy the code

2.2 Golang – Gin

2.2.1 Database

Partial database construction

  • The user model
//User User model
type User struct {
	gorm.Model
	UserName       string `gorm:"unique"`
	Email          string //`gorm:"unique"`
	PasswordDigest string
	Nickname       string `gorm:"unique"`
	Status         string
	Limit          int   // 0 Non-administrator 1 Administrator
	Type           int    // 0 indicates the user and 1 indicates the merchant
	Avatar         string `gorm:"size:1000"`
	Monery 		   int
}

Copy the code
  • Product model
// Commodity model
type Product struct {
	gorm.Model
	ProductID     string `gorm:"primary_key"`
	Name          string
	CategoryID    int
	Title         string
	Info          string `gorm:"size:1000"`
	ImgPath       string
	Price         string
	DiscountPrice string
	OnSale 		  string
	Num 		  int
	BossID        int
	BossName      string
	BossAvatar    string
}
Copy the code

2.2.1 Services

Partial logic code

  • Increase the commodity
func (service *UpProductService) UpProduct(a) serializer.Response {
	var product model.Product
	code := e.SUCCESS
	err := model.DB.First(&product,service.ID).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	product.OnSale = service.OnSale
	err = model.DB.Save(&product).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Data:   serializer.BuildProduct(product),
		Msg:    e.GetMsg(code),
	}
}
Copy the code
  • Modify the goods
// Update the product
func (service *UpdateProductService) Update(a) serializer.Response {
	product := model.Product{
		Name:          service.Name,
		CategoryID:    service.CategoryID,
		Title:         service.Title,
		Info:          service.Info,
		ImgPath:       service.ImgPath,
		Price:         service.Price,
		DiscountPrice: service.DiscountPrice,
		OnSale: 	   service.OnSale,
	}
	product.ID = service.ID
	code := e.SUCCESS
	err := model.DB.Save(&product).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Msg:    e.GetMsg(code),
	}
}
Copy the code

✨3. Front-end core code

3.1 Interaction between the Front and rear ends of AXIOS

import axios from 'axios'

// Create the product
const postProduct = form= >
  	axios.post('/api/v1/products', form).then(res= > res.data)

// Read the details
const showProduct = id= >
  	axios.get(`/api/v1/products/${id}`).then(res= > res.data)

// Read the list of items
const listProducts = (category_id, start, limit) = >
  axios
    .get('/api/v1/products', { params: { category_id, start, limit } })
    .then(res= > res.data)
    
// Read the picture of the product
const showPictures = id= > axios.get(`/api/v1/imgs/${id}`).then(res= > res.data)

// Search for products
const searchProducts = form= >
  	axios.post('/api/v1/searches', form).then(res= > res.data)

export {
  	postProduct,
  	showProduct,
  	listProducts,
  	showPictures,
  	searchProducts
}
Copy the code

🎊4. Some pages are displayed

4.1 Front Page

  • The main page

  • Commodity page

  • Release goods

  • The shopping cart

  • The settlement page

  • Personal center

4.2 Background Management

  • User management

  • Commodity management


🎆 5. Conclusion

This mall is based on the author’s open source project to be improved! I really appreciate the author’s open source so that I can through this project, know a lot of knowledge of Gin+Vue before and after the separation!! Also understand some like the payment function and so on, have not done before the function. But I haven’t figured out what the extreme test does yet. We’ll refine it later.


🎇 finally

Xiao Sheng Fan Yi, looking forward to your attention.