github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/user_invite.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"gopkg.in/mgo.v2/bson"
     7  	"net/http"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  const (
    13  	invite_success = 0
    14  	invite_fail    = 1
    15  	invite_missing = 2
    16  	invite_wrong   = 3
    17  	invite_repeat  = 4
    18  )
    19  
    20  // 没有加唯一索引
    21  func handleInvite(w http.ResponseWriter, r *http.Request) {
    22  	switch r.Method {
    23  	case "POST":
    24  		w.Header().Set("Access-Control-Allow-Origin", "*") // 解决跨域问题
    25  		unionID := r.URL.Query().Get("unionid")
    26  		inviteID := r.URL.Query().Get("inviteid")
    27  		result := &struct {
    28  			Code int
    29  		}{
    30  			Code: -1,
    31  		}
    32  		if unionID == "" || inviteID == "" {
    33  			result.Code = invite_missing
    34  			res, _ := json.Marshal(result)
    35  			fmt.Fprintf(w, "%v", string(res))
    36  			return
    37  		}
    38  		accountID, _ := strconv.Atoi(inviteID) // 邀请人账号
    39  		db := mongoDB.Ref()
    40  		defer mongoDB.UnRef(db)
    41  		tempData := struct {
    42  			InviteUserID int `bson:"_id"`
    43  		}{}
    44  		// 检查邀请账号是否存在
    45  		err := db.DB(DB).C("users").Find(bson.M{"accountid": accountID}).One(&tempData)
    46  		if err != nil {
    47  			result.Code = invite_wrong
    48  			res, _ := json.Marshal(result)
    49  			fmt.Fprintf(w, "%v", string(res))
    50  			return
    51  		}
    52  		// 检查是否已经被邀请
    53  		err = db.DB(DB).C("invite_userid").Find(bson.M{"unionid": unionID}).One(&tempData)
    54  		if err == nil {
    55  			result.Code = invite_repeat
    56  			res, _ := json.Marshal(result)
    57  			fmt.Fprintf(w, "%v", string(res))
    58  			return
    59  		}
    60  		// 创建邀请关联
    61  		err = db.DB(DB).C("invite_userid").Insert(&struct {
    62  			UnionID      string
    63  			InviteUserID int
    64  		}{
    65  			UnionID:      unionID,
    66  			InviteUserID: tempData.InviteUserID,
    67  		})
    68  		if err != nil {
    69  			result.Code = invite_fail
    70  			res, _ := json.Marshal(result)
    71  			fmt.Fprintf(w, "%v", string(res))
    72  			return
    73  		}
    74  		result.Code = invite_success
    75  		res, _ := json.Marshal(result)
    76  		fmt.Fprintf(w, "%v", string(res))
    77  	}
    78  }
    79  
    80  func inviteTask(unionid string) {
    81  	if time.Now().Unix() < time.Date(2018, 4, 28, 0, 0, 0, 0, time.Local).Unix() ||
    82  		time.Now().Unix() > time.Date(2018, 5, 6, 0, 0, 0, 0, time.Local).Unix() {
    83  		return
    84  	}
    85  	skeleton.Go(func() {
    86  		db := mongoDB.Ref()
    87  		defer mongoDB.UnRef(db)
    88  		tempData := struct {
    89  			InviteUserID int
    90  		}{}
    91  		err := db.DB(DB).C("invite_userid").Find(bson.M{"unionid": unionid}).One(&tempData)
    92  		if err != nil {
    93  			return
    94  		}
    95  		doActivityTask(tempData.InviteUserID, 1017)
    96  	}, nil)
    97  }