|
@@ -176,63 +176,80 @@ func IsSameTime(timeStr1, timeStr2, layout string) bool {
|
|
|
}
|
|
|
|
|
|
// 通讯录同步
|
|
|
-func SynchronousContacts(robotCode, contactsStr string) {
|
|
|
- config.WxRobot.Update("user_address_book", map[string]interface{}{
|
|
|
- "robotCode": robotCode,
|
|
|
- }, map[string]interface{}{
|
|
|
- "status": 1,
|
|
|
- })
|
|
|
- for _, v := range gconv.Maps(contactsStr) {
|
|
|
+func SynchronousContacts(robotCode, contactsStr string) error {
|
|
|
+ // 1. 先更新所有联系人为待删除状态
|
|
|
+ err := config.WxRobot.Update("user_address_book",
|
|
|
+ map[string]interface{}{"robotCode": robotCode},
|
|
|
+ map[string]interface{}{"status": 1})
|
|
|
+
|
|
|
+ // 2. 转换输入数据并检查错误
|
|
|
+ contacts := gconv.Maps(contactsStr)
|
|
|
+ if contacts == nil || len(contacts) == 0 {
|
|
|
+ return fmt.Errorf("解析联系人数据失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 批量处理联系人
|
|
|
+ for _, v := range contacts {
|
|
|
wxId := gconv.String(v["wxid"])
|
|
|
phone := gconv.String(v["phone"])
|
|
|
- datas, _ := config.Mgo.Find("user", map[string]interface{}{
|
|
|
+
|
|
|
+ // 查询用户数据
|
|
|
+ datas, err := config.Mgo.Find("user", map[string]interface{}{
|
|
|
"i_appid": 2,
|
|
|
"$or": []map[string]interface{}{
|
|
|
{"s_phone": phone},
|
|
|
{"s_m_phone": phone},
|
|
|
},
|
|
|
}, `{"s_phone":-1}`, `{"s_phone":1,"s_m_phone":1,"base_user_id":1}`, false, 0, 1)
|
|
|
- if datas != nil || len(*datas) == 0 {
|
|
|
- //删除通讯录
|
|
|
- config.WxRobot.Update("user_address_book", map[string]interface{}{
|
|
|
- "wxid": wxId,
|
|
|
- "robotCode": robotCode,
|
|
|
- }, map[string]interface{}{
|
|
|
- "status": 1,
|
|
|
- })
|
|
|
+
|
|
|
+ if datas == nil || len(*datas) == 0 {
|
|
|
+ // 没有匹配的用户,跳过处理(已标记为待删除)
|
|
|
+ continue
|
|
|
}
|
|
|
- //做一个通讯录变更
|
|
|
+
|
|
|
+ // 准备更新/插入数据
|
|
|
baseUserId := gconv.Int64((*datas)[0]["base_user_id"])
|
|
|
- if config.WxRobot.Count("user_address_book", map[string]interface{}{
|
|
|
- "wxid": wxId, "robotCode": robotCode}) > 0 {
|
|
|
- //修改数据
|
|
|
- config.WxRobot.Update("user_address_book", map[string]interface{}{
|
|
|
- "wxid": wxId,
|
|
|
- "robotCode": robotCode,
|
|
|
- }, map[string]interface{}{
|
|
|
- "remark": gconv.String(v["remark"]),
|
|
|
- "phone": phone,
|
|
|
- "base_user_id": baseUserId,
|
|
|
- "update_time": time.Now().Format(date.Date_Full_Layout),
|
|
|
- "status": 0,
|
|
|
- })
|
|
|
+ contactData := map[string]interface{}{
|
|
|
+ "remark": gconv.String(v["remark"]),
|
|
|
+ "phone": phone,
|
|
|
+ "base_user_id": baseUserId,
|
|
|
+ "update_time": time.Now().Format(date.Date_Full_Layout),
|
|
|
+ "status": 0,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否存在
|
|
|
+ count := config.WxRobot.Count("user_address_book",
|
|
|
+ map[string]interface{}{"wxid": wxId, "robotCode": robotCode})
|
|
|
+ ok := false
|
|
|
+ if count > 0 {
|
|
|
+ // 更新现有记录
|
|
|
+ ok = config.WxRobot.Update("user_address_book",
|
|
|
+ map[string]interface{}{"wxid": wxId, "robotCode": robotCode},
|
|
|
+ contactData)
|
|
|
} else {
|
|
|
- //新增数据
|
|
|
- config.WxRobot.Insert("user_address_book", map[string]interface{}{
|
|
|
- "remark": gconv.String(v["remark"]),
|
|
|
- "phone": phone,
|
|
|
- "base_user_id": baseUserId,
|
|
|
- "status": 0,
|
|
|
- "wxid": wxId,
|
|
|
- "code": gconv.String(v["code"]),
|
|
|
- "name": gconv.String(v["name"]),
|
|
|
- "is_refuse": 0,
|
|
|
- "create_time": time.Now().Format(date.Date_Full_Layout),
|
|
|
- "update_time": time.Now().Format(date.Date_Full_Layout),
|
|
|
- "robotCode": robotCode,
|
|
|
- })
|
|
|
+ data := config.WxRobot.FindOne("user_address_book", map[string]interface{}{
|
|
|
+ "wxid": wxId,
|
|
|
+ }, "", "")
|
|
|
+ is_refuse := 0
|
|
|
+ if data != nil && len(*datas) > 0 {
|
|
|
+ is_refuse = gconv.Int((*data)["is_refuse"])
|
|
|
+ }
|
|
|
+ // 插入新记录
|
|
|
+ contactData["wxid"] = wxId
|
|
|
+ contactData["code"] = gconv.String(v["code"])
|
|
|
+ contactData["name"] = gconv.String(v["name"])
|
|
|
+ contactData["is_refuse"] = is_refuse
|
|
|
+ contactData["create_time"] = time.Now().Format(date.Date_Full_Layout)
|
|
|
+ contactData["robotCode"] = robotCode
|
|
|
+ ok = config.WxRobot.Insert("user_address_book", contactData) > 0
|
|
|
+ }
|
|
|
+
|
|
|
+ if !ok {
|
|
|
+ // 记录错误但继续处理其他联系人
|
|
|
+ log.Printf("处理联系人 %s 失败: %v", wxId, err)
|
|
|
}
|
|
|
}
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// 聊天记录保存
|
|
@@ -244,6 +261,9 @@ func AddChatRecord(robotCode string, data string) {
|
|
|
"wxid": wxId,
|
|
|
"robotCode": robotCode,
|
|
|
}, "", "")
|
|
|
+ if userData == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
if len(*userData) > 0 {
|
|
|
baseUserId := gconv.Int64((*userData)["base_user_id"])
|
|
|
if (content == "R" || content == "r") && !gconv.Bool(chatData["IsSelf"]) {
|
|
@@ -252,7 +272,7 @@ func AddChatRecord(robotCode string, data string) {
|
|
|
"send_status": 0,
|
|
|
}) > 0 {
|
|
|
config.WxRobot.Update("user_address_book", map[string]interface{}{
|
|
|
- "base_user_id": baseUserId,
|
|
|
+ "wxid": wxId,
|
|
|
}, map[string]interface{}{
|
|
|
"is_refuse": 1,
|
|
|
"update_time": time.Now().Format(date.Date_Full_Layout),
|