Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
[SQL]
Browse files Browse the repository at this point in the history
  • Loading branch information
Thxzzzzz committed May 16, 2019
1 parent 0052b5a commit aa6cb40
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 83 deletions.
36 changes: 22 additions & 14 deletions controllers/common.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package controllers

import (
"errors"
"fmt"
"funding/enums"
"funding/models"
"funding/objects"
Expand All @@ -18,6 +16,25 @@ type BaseController struct {
beego.Controller
}

// 检查并获取对应的 User 信息
func (c *BaseController) CheckAndGetUser() (*models.User, error) {
userId := c.GetSession(SESSION_USER_KEY)
var result *models.User
if userId == nil {
return nil, &resultError.NotLoginError
}
id, ok := userId.(uint64)
if !ok {
return nil, &resultError.NotLoginError
}
// 获取当前 Session 中的 userId 字段对应的值
result, err := models.FindUserById(id)
if err != nil {
return nil, &resultError.UserNotExitError
}
return result, nil
}

// 所有请求都需要身份验证的 Controller
type VailUserController struct {
BaseController
Expand All @@ -26,21 +43,12 @@ type VailUserController struct {

// 实现 Prepare 验证身份
func (c *VailUserController) Prepare() {
userId := c.GetSession(SESSION_USER_KEY)
var result *models.User
if userId == nil {
c.ResponseErrJson(errors.New("没有登录"))
return
}
id, _ := userId.(uint64)
// 获取当前 Session 中的 userId 字段对应的值
result, err := models.FindUserById(id)
user, err := c.CheckAndGetUser()
if err != nil {
c.ResponseErrJson(errors.New("没有该用户"))
c.ResponseErrJson(err)
return
}
fmt.Println(result)
c.User = result
c.User = user
}

// 校验是否为对应角色
Expand Down
2 changes: 1 addition & 1 deletion controllers/productManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (c *ProductMangerController) SaveProductPackage() {
// 如果找到了记录,则标记为更新,后面对相应的套餐进行更新
isUpdate = true
}
// 总数增加,对库存进行校正
// 总数修改,对库存进行校正
if isUpdate && form.Total != 0 && oldPkg.Total != form.Total {
form.Stock += (form.Total - oldPkg.Total)
}
Expand Down
21 changes: 2 additions & 19 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@ type UserControllers struct {
BaseController
}

// 检查并获取对应的 User 信息
func (c *UserControllers) CheckAndGetUser() (*models.User, error) {
userId := c.GetSession(SESSION_USER_KEY)
var result *models.User
if userId == nil {
return nil, &resultError.NotLoginError
}
id, ok := userId.(uint64)
if !ok {
return nil, &resultError.NotLoginError
}
// 获取当前 Session 中的 userId 字段对应的值
result, err := models.FindUserById(id)
if err != nil {
return nil, &resultError.NotLoginError
}
return result, nil
}

//////////////// Users 用户信息相关 ///////////////

// @Title 根据 id 获取 User
Expand Down Expand Up @@ -137,6 +118,8 @@ func (c *UserControllers) Register() {
c.ResponseErrJson(err)
return
}
//角色设置为买家
user.RoleId = enums.Role_Buyer
//向数据库中插入数据
err = models.InsertUser(&user)
if err != nil {
Expand Down
54 changes: 26 additions & 28 deletions funding.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CREATE TABLE IF NOT EXISTS `addresses` (
KEY `idx_addresses_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='用户收货地址表';

-- 正在导出表 funding.addresses 的数据:~7 rows (大约)
-- 正在导出表 funding.addresses 的数据:~15 rows (大约)
/*!40000 ALTER TABLE `addresses` DISABLE KEYS */;
REPLACE INTO `addresses` (`id`, `user_id`, `name`, `address`, `phone`, `created_at`, `updated_at`, `deleted_at`) VALUES
(1, 20003, '李小明5', '广西壮族自治区桂林市七星区桂林电子科技大学', '18500000012', '2019-04-15 14:24:56', '2019-04-23 21:24:44', NULL),
Expand Down Expand Up @@ -119,9 +119,9 @@ CREATE TABLE IF NOT EXISTS `comments_infos` (
KEY `idx_comments_infos_deleted_at` (`deleted_at`) USING BTREE,
KEY `idx_comments_infos_user_id` (`user_id`) USING BTREE,
KEY `idx_comments_infos_product_id` (`product_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='评论主表';
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='评论主表';

-- 正在导出表 funding.comments_infos 的数据:~9 rows (大约)
-- 正在导出表 funding.comments_infos 的数据:~12 rows (大约)
/*!40000 ALTER TABLE `comments_infos` DISABLE KEYS */;
REPLACE INTO `comments_infos` (`id`, `product_id`, `user_id`, `is_seller`, `content`, `created_at`, `updated_at`, `deleted_at`) VALUES
(1, 11128, 20003, 0, '这是第一条评论,哈哈', '2019-05-15 01:21:26', '2019-05-15 01:21:26', NULL),
Expand All @@ -132,7 +132,10 @@ REPLACE INTO `comments_infos` (`id`, `product_id`, `user_id`, `is_seller`, `cont
(6, 11128, 20002, 1, '评论测试4', '2019-05-15 16:51:33', '2019-05-15 16:51:33', NULL),
(7, 11128, 20002, 1, '评论测试5', '2019-05-15 16:53:56', '2019-05-15 16:53:56', NULL),
(8, 11128, 20002, 1, '评论测试6', '2019-05-15 16:55:07', '2019-05-15 16:55:07', NULL),
(9, 11128, 20002, 1, '评论测试7', '2019-05-15 16:55:34', '2019-05-15 16:55:34', NULL);
(9, 11128, 20002, 1, '评论测试7', '2019-05-15 16:55:34', '2019-05-15 16:55:34', NULL),
(10, 11120, 20002, 1, '评论测试', '2019-05-16 09:28:45', '2019-05-16 09:28:45', NULL),
(11, 11120, 20002, 1, '评论测试', '2019-05-16 09:30:15', '2019-05-16 09:30:15', NULL),
(12, 11120, 20002, 1, '新评论测试', '2019-05-16 09:35:21', '2019-05-16 09:35:21', NULL);
/*!40000 ALTER TABLE `comments_infos` ENABLE KEYS */;

-- 导出 表 funding.comments_replys 结构
Expand All @@ -150,27 +153,36 @@ CREATE TABLE IF NOT EXISTS `comments_replys` (
KEY `idx_comments_replys_deleted_at` (`deleted_at`) USING BTREE,
KEY `idx_comments_replys_user_id` (`user_id`) USING BTREE,
KEY `idx_comments_replys_comment_id` (`comment_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='评论回复表';
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='评论回复表';

-- 正在导出表 funding.comments_replys 的数据:~3 rows (大约)
-- 正在导出表 funding.comments_replys 的数据:~11 rows (大约)
/*!40000 ALTER TABLE `comments_replys` DISABLE KEYS */;
REPLACE INTO `comments_replys` (`id`, `comment_id`, `user_id`, `is_seller`, `content`, `created_at`, `updated_at`, `deleted_at`) VALUES
(1, 1, 20003, 0, '这是第一条回复,哈哈', '2019-05-15 01:21:26', '2019-05-15 01:21:26', NULL),
(2, 1, 20002, 1, '这是第一条商家回复,哈哈', '2019-05-15 01:21:26', '2019-05-15 15:19:02', NULL),
(3, 9, 20002, 1, '测试回复', '2019-05-15 20:59:06', '2019-05-15 20:59:06', NULL);
(3, 9, 20002, 1, '测试回复', '2019-05-15 20:59:06', '2019-05-15 20:59:06', NULL),
(4, 10, 20002, 1, '回复测试', '2019-05-16 09:28:50', '2019-05-16 09:28:50', NULL),
(5, 10, 20002, 1, '回复测试', '2019-05-16 09:29:04', '2019-05-16 09:29:04', NULL),
(6, 10, 20002, 1, '回复测试', '2019-05-16 09:30:01', '2019-05-16 09:30:01', NULL),
(7, 11, 20002, 1, '回复测试', '2019-05-16 09:30:21', '2019-05-16 09:30:21', NULL),
(8, 10, 20002, 1, '回复测试', '2019-05-16 09:30:29', '2019-05-16 09:30:29', NULL),
(9, 11, 20002, 1, '添加新评论测试', '2019-05-16 09:35:08', '2019-05-16 09:35:08', NULL),
(10, 12, 20002, 1, '回复测试', '2019-05-16 09:35:36', '2019-05-16 09:35:36', NULL),
(11, 12, 20002, 1, '添加新评论测试', '2019-05-16 09:35:46', '2019-05-16 09:35:46', NULL);
/*!40000 ALTER TABLE `comments_replys` ENABLE KEYS */;

-- 导出 表 funding.licenses 结构
DROP TABLE IF EXISTS `licenses`;
CREATE TABLE IF NOT EXISTS `licenses` (
`id` bigint(24) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Id',
`company_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '公司名称',
`description` varchar(500) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '公司描述',
`description` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '公司描述',
`user_id` bigint(24) NOT NULL COMMENT '对应账号ID',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '联系地址',
`phone` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '官方电话',
`license_image_url` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '执照图片地址',
`verify` tinyint(1) NOT NULL DEFAULT '2' COMMENT '审核情况 1:已通过 2:待审核 3: 待提交 4: 未通过 ',
`verify_status` tinyint(1) NOT NULL DEFAULT '2' COMMENT '审核情况 1:已通过 2:待审核 3: 待提交 4: 未通过 ',
`verify_message` text COLLATE utf8mb4_general_ci NOT NULL COMMENT '审核消息(不通过时显示)',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted_at` timestamp NULL DEFAULT NULL COMMENT '删除时间(软删除) NULL为未删除',
Expand All @@ -179,10 +191,10 @@ CREATE TABLE IF NOT EXISTS `licenses` (
KEY `idx_licenses_deleted_at` (`deleted_at`)
) ENGINE=InnoDB AUTO_INCREMENT=11111112 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='执照信息 项目发起人资质信息';

-- 正在导出表 funding.licenses 的数据:~0 rows (大约)
-- 正在导出表 funding.licenses 的数据:~1 rows (大约)
/*!40000 ALTER TABLE `licenses` DISABLE KEYS */;
REPLACE INTO `licenses` (`id`, `company_name`, `description`, `user_id`, `address`, `phone`, `license_image_url`, `verify`, `created_at`, `updated_at`, `deleted_at`) VALUES
(11111111, '北京驰讯通科技有限公司', '我们是一家科技服务公司', 20002, '北京海淀区三环到四环之间 中关村东路18号财智国际大厦702', '13570828180', 'https://img30.360buyimg.com/cf/jfs/t1/4374/9/6975/444185/5ba469f2E849b4a18/9ad35bddbd55accf.jpg', 1, '2019-03-09 18:40:38', '2019-05-16 01:27:32', NULL);
REPLACE INTO `licenses` (`id`, `company_name`, `description`, `user_id`, `address`, `phone`, `license_image_url`, `verify_status`, `verify_message`, `created_at`, `updated_at`, `deleted_at`) VALUES
(11111111, '北京驰讯通科技有限公司', '我们是一家科技服务公司', 20002, '北京海淀区三环到四环之间 中关村东路18号财智国际大厦702', '13570828180', 'https://img30.360buyimg.com/cf/jfs/t1/4374/9/6975/444185/5ba469f2E849b4a18/9ad35bddbd55accf.jpg', 1, '', '2019-03-09 18:40:38', '2019-05-16 01:27:32', NULL);
/*!40000 ALTER TABLE `licenses` ENABLE KEYS */;

-- 导出 表 funding.orders 结构
Expand Down Expand Up @@ -215,7 +227,7 @@ CREATE TABLE IF NOT EXISTS `orders` (
KEY `idx_orders_package_id` (`product_package_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='订单表,用来存储订单信息';

-- 正在导出表 funding.orders 的数据:~12 rows (大约)
-- 正在导出表 funding.orders 的数据:~14 rows (大约)
/*!40000 ALTER TABLE `orders` DISABLE KEYS */;
REPLACE INTO `orders` (`id`, `user_id`, `name`, `address`, `phone`, `seller_id`, `product_id`, `product_package_id`, `nums`, `unit_price`, `total_price`, `status`, `checking_number`, `paid_at`, `close_at`, `finished_at`, `created_at`, `updated_at`, `deleted_at`) VALUES
(1, 20003, '测试', '广西壮族自治区桂林市七星区桂林电子科技大学', '18545456464', 20002, 11111, 111111111, 2, 269.00, 538.00, 1, '', NULL, NULL, NULL, '2019-05-06 00:49:00', '2019-05-12 01:20:42', '2019-05-06 00:58:30'),
Expand All @@ -234,20 +246,6 @@ REPLACE INTO `orders` (`id`, `user_id`, `name`, `address`, `phone`, `seller_id`,
(14, 20003, '新的人', '新的地址', '12345646712', 20002, 11128, 111111165, 1, 1090.00, 1090.00, 1, '', NULL, NULL, NULL, '2019-05-15 22:30:39', '2019-05-15 22:30:39', NULL);
/*!40000 ALTER TABLE `orders` ENABLE KEYS */;

-- 导出 表 funding.permissions 结构
DROP TABLE IF EXISTS `permissions`;
CREATE TABLE IF NOT EXISTS `permissions` (
`id` int(3) NOT NULL COMMENT '权限类型的 ID',
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '权限名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=COMPACT COMMENT='权限表,用来存储各种权限的名称';

-- 正在导出表 funding.permissions 的数据:~0 rows (大约)
/*!40000 ALTER TABLE `permissions` DISABLE KEYS */;
REPLACE INTO `permissions` (`id`, `name`) VALUES
(1, 'All');
/*!40000 ALTER TABLE `permissions` ENABLE KEYS */;

-- 导出 表 funding.products 结构
DROP TABLE IF EXISTS `products`;
CREATE TABLE IF NOT EXISTS `products` (
Expand All @@ -260,7 +258,7 @@ CREATE TABLE IF NOT EXISTS `products` (
`current_price` decimal(12,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '已经筹集的金额',
`target_price` decimal(12,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '目标金额',
`verify_status` tinyint(3) unsigned NOT NULL DEFAULT '2' COMMENT '1:已通过 2:待审核 3: 待提交 4: 未通过 ',
`verify_message` text COLLATE utf8mb4_general_ci NOT NULL COMMENT '审核消息,在审核未通过的时候会显示给商家',
`verify_message` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '审核消息,在审核未通过的时候会显示给商家',
`backers` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '支持者人数',
`end_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '众筹截止时间',
`detail_html` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '详情介绍Html',
Expand Down
2 changes: 1 addition & 1 deletion lastupdate.tmp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"C:\\GoPath\\src\\funding\\controllers":1557939540684428000}
{"C:\\GoPath\\src\\funding\\controllers":1557988903283354400}
1 change: 1 addition & 0 deletions managerControllers/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package managerControllers
1 change: 1 addition & 0 deletions managerControllers/product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package managerControllers
116 changes: 116 additions & 0 deletions managerControllers/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package managerControllers

import (
"encoding/json"
"funding/controllers"
"funding/enums"
"funding/forms"
"funding/models"
"funding/objects"
"funding/resultModels"
"github.com/jinzhu/gorm"
)

// 管理端的 UserController
// 一些通用的 API 还是会用 /controllers 里面的
type ManagerUserController struct {
controllers.BaseController
}

// @Title 注册
// @Description 注册
// @Param user body models.User true "注册信息"
// @Description 注册
// @Success 200
// @Failure 400
// @router /register [post]
func (c *ManagerUserController) Register() {
currentUser, err := c.CheckAndGetUser()
if err != nil {
c.ResponseErrJson(err)
return
}
// 超级管理员才能创建账号
if currentUser.RoleId != enums.Role_SuperAdmin {
c.ResponseErrJson(&resultError.UserRoleVerifyError)
return
}

// 首先获取请求中的数据
//先声明一个 struct 其结构对应请求的 form 表单数据
form := models.User{}

//这里由于 前端的 Axios 默认请求为 json 格式,所以先改为解析Json
err = json.Unmarshal(c.Ctx.Input.RequestBody, &form)
if err != nil || form == (models.User{}) {
c.ResponseErrJson(err)
return
}
//查询是否已存在用户名
dbExisted, err := models.FindUserByUsername(form.Username)
//查询出错
if err != nil && err.Error() != gorm.ErrRecordNotFound.Error() {
c.ResponseErrJson(err)
return
}
//已存在
if dbExisted != nil && dbExisted.Username == form.Username {
c.ResponseErrJson(resultError.NewFallFundingErr("用户名已存在"))
return
}

//向数据库中插入数据
err = models.InsertUser(&form)
if err != nil {
c.ResponseErrJson(err)
return
}
c.ResponseSuccessJson(nil)
}

// @Title 登录
// @Description 用账号密码登录
// @Param userForm body forms.LoginForm true "登录账号密码"
// @Success 200 {object} models.User
// @Failure 400
// @router /login [post]
func (c *ManagerUserController) Login() {
// 1. 首先获取请求中的数据
//先声明一个 struct 其结构对应请求的 form 表单数据
loginForm := forms.LoginForm{}
//这里由于 前端的 Axios 默认请求为 json 格式,所以先改为解析Json
err := json.Unmarshal(c.Ctx.Input.RequestBody, &loginForm)
if err != nil {
c.ResponseErrJson(err)
return
}

// 2. 获取数据库中的数据并与请求数据进行比较
dbResult, err := models.FindUserByUsername(loginForm.Username)

//数据库查找出错则返回错误
if err != nil {
c.ResponseErrJson(err)
return
}

// 这是管理端的登录接口,所以只能是审核员或管理员
if dbResult.RoleId != enums.Role_Auditor && dbResult.RoleId != enums.Role_SuperAdmin {
c.ResponseErrJson(&resultError.UserPasswordError)
return
}

// 3. 比较得出结果后,如果正确登录则将信息加入到 Session 中
if dbResult.Password != loginForm.Password {
// 密码不正确也返回错误
c.ResponseErrJson(&resultError.UserPasswordError)
return
}

result := resultModels.SuccessResult(dbResult)
//向当前 Session 写入 userId
c.SetSession(controllers.SESSION_USER_KEY, dbResult.ID)

// 4.. 返回 Json 信息
c.ResponseJson(result)
}
10 changes: 9 additions & 1 deletion models/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ type License struct {
Address string `json:"address"` // 联系地址
Phone string `json:"phone"` // 联系电话
LicenseImageUrl string `json:"license_image_url"` // 营业执照照片地址
VerifyStatus int `json:"verify_status"` // 审核状态
VerifyStatus int `json:"verify_status"` // 审核状态 1:已通过 2:待审核 3: 待提交 4: 未通过
VerifyMessage string `json:"verify_message"` // 审核消息(审核不通过时显示)
}

///////////////////// 基本增删改查 /////////////////////
Expand Down Expand Up @@ -54,4 +55,11 @@ func UpdateLicense(license *License) error {
return err
}

// 获取全部执照信息
func GetAllLicense() ([]*License, error) {
var results []*License
err := db.Find(&results).Error
return results, err
}

///////////////////// EMD 基本增删改查 /////////////////////
Loading

0 comments on commit aa6cb40

Please sign in to comment.