package utils

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model/request"
	"gin-vue-admin/utils/wx"
)

func WxUserLogin(req request.MobileLogin) (error, *wx.LoginResponse) {
	var loginRes *wx.LoginResponse
	var err error
	//获取openid、phone ,union_id
	if req.UserType == 1 {
		loginRes, err = wx.GetWxLoginRes(global.GVA_CONFIG.Wx.Appid, global.GVA_CONFIG.Wx.Appsecret,
			req.WxCode, req.EncryptedDate, req.Iv)
	} else if req.UserType == 2 {
		loginRes, err = wx.GetWxLoginRes(global.GVA_CONFIG.Bkwx.Appid, global.GVA_CONFIG.Bkwx.Appsecret,
			req.WxCode, req.EncryptedDate, req.Iv)
	}

	if err != nil {
		fmt.Println("WxUserLogin ERR:", err)
		return err, &wx.LoginResponse{}
	}
	return nil, loginRes
}

func GetOpenid(req request.MobileLogin) (error, string) {
	var openId string
	var err error
	//获取openid、phone ,union_id
	if req.UserType == 1 {
		openId, err = wx.GetOpenId(global.GVA_CONFIG.Wx.Appid, global.GVA_CONFIG.Wx.Appsecret,
			req.WxCode)
	} else if req.UserType == 2 {
		openId, err = wx.GetOpenId(global.GVA_CONFIG.Bkwx.Appid, global.GVA_CONFIG.Bkwx.Appsecret,
			req.WxCode)
	}

	if err != nil {
		fmt.Println(444)
		fmt.Println(err)
		return err, ""
	}
	return nil, openId
}
