ServiceProviderController.go 3.55 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
package controllers

import (
	database "WorldEpcho/src/datasource"
	"WorldEpcho/src/models"
	"WorldEpcho/src/service"
	"WorldEpcho/src/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
	"strings"
)

// CreateActiveCode 获取一次性用户激活码
func CreateServiceProvider(c *gin.Context) {
	/*
	   判断用户是否登录
	*/
	userID, isLogin := service.IsUserLoggedIn(c)
	if !isLogin {
		log.Println("用户未登录")
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "用户未登录"})
		return
	}
	//开启事务
	session := database.Engine.NewSession()
	defer session.Close() //延迟关闭session
	err := session.Begin()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "开启事务失败!"})
		fmt.Println("开启事务失败")
		return
	}
	//判断用户的权限level是否为0
	level, err1 := models.GetUserLevel(userID)
	if err1 != nil {
		log.Println("查询用户权限出错")
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "查询用户权限出错"})
		return
	}
	if level != 0 {
		log.Println("用户无权创建服务商")
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "用户无权创建服务商"})
		return
	}
	//从前端获取值
	name := c.PostForm("name")
	phone := c.PostForm("phone")
	description := c.PostForm("description")
	accessibleTags := c.PostForm("accessibleTags")
	//可以访问的标签
	TagsArr := []string{}
	// 分割字符串,得到单独的数值字符串
	tagsSplit := strings.Split(accessibleTags, ",")
	for _, tagStr := range tagsSplit {
		// 将转换后的数值添加到数组
		TagsArr = append(TagsArr, tagStr)
	}
	inspirationValue := c.PostForm("inspirationValue")
	// 将字符串转换为 int64 类型
	museValue, err := strconv.ParseInt(inspirationValue, 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "灵感值类型转换出错"})
		//continue
		return
	}
	//生成服务商id,32位UUID
	SP_id := utils.GenerateUUID()
	/*fmt.Println("服务商id:", SP_id)
	_, isExist, err := models.GetUserByPhone(session, phone)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "根据服务商手机号查询用户出错"})
		return
	}
	if isExist {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "服务商手机对应的用户已存在"})
		return
	}
	// 密码加盐操作
	salt := config.Conf.Md5Salt
	password, err := utils.MD5BySalf(phone, salt)
	// 创建一个用户用于服务商
	NewUser := models.User{
		Username:   name,
		Phone:      phone,
		Password:   password,
		Createtime: time.Now().Unix(),
		Level:      9,
	}
	//添加用户
	rows, user_id, err := models.AddUser(session, &NewUser)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建服务商的用户失败"})
		log.Println(err.Error())
		return
	}

	if rows <= 0 {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建服务商的用户失败"})
		fmt.Println("创建服务商的用户失败!")
		return
	}*/
	// 构建服务商对象
	serviceProvider := &models.ServiceProvider{
		ID: SP_id,
		//Uid:              user_id,
		Name:             name,
		Description:      description,
		ContactNumber:    phone,
		AccessibleTags:   TagsArr,
		InspirationValue: museValue,
	}
	err = models.CreateServiceProvider(serviceProvider)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建服务商失败"})
		return
	}
	//提交事务
	err = session.Commit()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建服务商失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 1, "message": "创建服务商成功"})
}