互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.2 KiB

/*
* @Description: In User Settings Edit
* @Author: your name
* @Date: 2019-08-22 09:26:48
* @LastEditTime: 2019-08-23 18:04:40
* @LastEditors: Please set LastEditors
*/
package pc
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/jwt"
"hudongzhuanjia/libs/qq"
"hudongzhuanjia/libs/wx"
"hudongzhuanjia/models"
"hudongzhuanjia/services/activity"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
)
//用户
type UserCtl struct {
controllers.BaseCtl
}
//登陆
// 子账号需要接收父级账号的控制,同步数据,
func (t *UserCtl) Login() {
username := t.MustGet("username")
password := t.MustGet("password")
customer := new(models.Customer)
err := customer.Author(username, password)
t.CheckErr(err)
// 获取用户拥有的activity
activities, err := activity_service.GetActivitiesByCustomerId(customer.Id)
t.CheckErr(err)
// 主账号无法确定
token, err := jwt.GenJwtToken("customer", customer.Id, customer.Id, customer.Pid, customer.AreaId, customer.ActivityId)
t.CheckErr(err)
t.SetSession(define.TOKEN, token)
customer.Token = token
customer.Activities = activities
pid := customer.Pid
if pid == 0 {
pid = customer.Id
}
customer.Tag = "activity"
t.RAW(customer)
}
//检查是否已经登陆
func (t *UserCtl) Checkin() {
_, exist := t.Get(define.TOKEN)
if exist {
t.SUCCESS("已登录")
}
t.ERROR("未登录", code.MSG_ERR)
}
// debug
func (t *UserCtl) DebugLogin() {
customerId := t.DefaultInt64("customer_id", 1)
customer := new(models.Customer)
exist, err := models.GetById(customer, customerId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
token, err := jwt.GenJwtToken("customer", customer.Id, customer.Id, customer.Pid, customer.AreaId, customer.ActivityId)
t.CheckErr(err)
t.SetSession(define.TOKEN, token)
t.JSON(map[string]interface{}{
"customer": customer,
"token": token,
})
}
//退出
func (t *UserCtl) Logout() {
if _, ok := t.Request.SESSION[define.TOKEN]; !ok {
t.DisplayByError("未登录", code.MSG_ERR_Authority)
}
t.DeleteSession(define.TOKEN)
t.DisplayByData("退出成功")
}
// 微信登陆 callback
func (t *UserCtl) WXLogin() {
//state := t.MustGet("state")
wxcode := t.MustGet("code")
token, err := wx.GetToken(wxcode)
t.CheckErr(err)
customer := new(models.Customer)
exist, err := customer.GetByOpenid(token.Openid)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
t.SetSession(define.TOKEN, fmt.Sprintf("%d", customer.Id))
activities, err := activity_service.GetActivitiesByCustomerId(customer.Id)
t.CheckErr(err)
customer.Activities = activities
t.RAW(customer)
}
// 手机验证码登陆
func (t *UserCtl) QQLogin() {
//state := t.MustGet("state")
qqcode := t.MustGet("code")
token, err := qq.GetToken(qqcode)
t.CheckErr(err)
open, err := qq.GetPcOpenid(token.AccessToken)
t.CheckErr(err)
customer := new(models.Customer)
customer.GetByQQOpenid(open.Openid)
t.CheckErr(err)
t.SetSession(define.TOKEN, fmt.Sprintf("%d", customer.Id))
activities, err := activity_service.GetActivitiesByCustomerId(customer.Id)
t.CheckErr(err)
customer.Activities = activities
t.RAW(customer)
}
func (t *UserCtl) QQCodeUrl() {
t.DisplayByData(qq.GetCodeUrl())
}