|
@@ -9,6 +9,7 @@ import (
|
|
|
"gas-cylinder-api/conf"
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "gogs.baozhida.cn/zoie/OAuth-core/pkg"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/service"
|
|
|
"gorm.io/gorm"
|
|
@@ -29,7 +30,7 @@ func GetAppletCustomerId(c *gin.Context) string {
|
|
|
}
|
|
|
|
|
|
// GetPage 获取Customer列表
|
|
|
-func (e *AppletCustomer) Login(c *dto.AppletCustomerLoginReq) (token, expiresAt string, err error) {
|
|
|
+func (e *AppletCustomer) WxLogin(c *dto.AppletCustomerWxLoginReq) (token, expiresAt string, err error) {
|
|
|
var data model.Customer
|
|
|
|
|
|
url := "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
|
|
@@ -88,6 +89,78 @@ func (e *AppletCustomer) Login(c *dto.AppletCustomerLoginReq) (token, expiresAt
|
|
|
return token, expiresAt, nil
|
|
|
|
|
|
}
|
|
|
+func (e *AppletCustomer) Login(c *dto.AppletCustomerLoginReq) (token, expiresAt string, err error) {
|
|
|
+ var data model.Customer
|
|
|
+
|
|
|
+ err = e.Orm.Where("principal_phone = ?", c.Phone).First(&data).Error
|
|
|
+ id := data.Id
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return token, expiresAt, errors.New("用户名或密码错误")
|
|
|
+ } else {
|
|
|
+ return token, expiresAt, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = pkg.CompareHashAndPassword(data.Password, c.Password)
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("user login error, %s", err.Error())
|
|
|
+ return token, expiresAt, errors.New("用户名或密码错误")
|
|
|
+ }
|
|
|
+
|
|
|
+ token, expiresAt, _ = e.GeneratorToken(id)
|
|
|
+ return token, expiresAt, nil
|
|
|
+
|
|
|
+}
|
|
|
+func (e *AppletCustomer) Register(c *dto.AppletCustomerRegisterReq) (err error) {
|
|
|
+ var data model.Customer
|
|
|
+
|
|
|
+ err = e.Orm.Where("principal_phone = ?", c.Phone).First(&data).Error
|
|
|
+ id := data.Id
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ // 添加客户
|
|
|
+ for {
|
|
|
+ var count int64
|
|
|
+ id = utils.GetUUID()
|
|
|
+ var i int64
|
|
|
+ err = e.Orm.Model(&data).Where("id = ?", id).Count(&count).Error
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if i == 0 {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err = e.Orm.Create(&model.Customer{
|
|
|
+ ProvCustomer: model.ProvCustomer{
|
|
|
+ Id: id,
|
|
|
+ PrincipalPhone: c.Phone,
|
|
|
+ },
|
|
|
+ //Openid: wxRes.Openid,
|
|
|
+ Password: c.Password,
|
|
|
+ }).Error
|
|
|
+ } else {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(data.Id) > 0 {
|
|
|
+ if len(data.Password) > 0 {
|
|
|
+ return errors.New("该手机号已注册")
|
|
|
+ } else {
|
|
|
+ data.Password = c.Password
|
|
|
+ err = e.Orm.Save(&data).Error
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
func (e *AppletCustomer) GeneratorToken(customerId string) (string, string, error) {
|
|
|
// 创建一个我们自己的声明
|