company_controller.go 1.6 KB
package controllers

import (
	"errors"

	"github.com/astaxie/beego/logs"
	companyService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/company/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

type CompanyController struct {
	BaseController
}

//Prepare 重写 BaseController 的Prepare方法
func (c *CompanyController) Prepare() {
	c.BaseController.Prepare()
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMISSION_ENTERPRISE_SET); !ok {
		return
	}
}

func (c *CompanyController) SetPhone() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		Type  int    `json:"type"`
		Phone string `json:"phone"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	switch param.Type {
	case 1:
		c.editPhone(param.Phone)
	case 2:
		c.getPhone()
	default:
		c.ResponseError(errors.New("参数错误"))
	}
	return
}

func (c *CompanyController) getPhone() {
	companyid := c.GetUserCompany()
	srv := companyService.NewCompanyService(nil)
	data, err := srv.GetCompanyData(companyid)
	if err != nil {
		c.ResponseError(err)
		return
	}
	result := map[string]interface{}{
		"phone": data.Phone,
	}
	c.ResponseData(result)
	return
}

func (c *CompanyController) editPhone(phone string) {
	companyid := c.GetUserCompany()
	srv := companyService.NewCompanyService(nil)
	err := srv.UpdateCompanyPhone(companyid, phone)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}