1. //request json
    2. {
    3. "data": [
    4. {
    5. "shop_id": 410,
    6. "tiktok": "tiktok1,tiktok2,tiktok3,tiktok4,tiktok5,tiktok6,tiktok7,tiktok8,tiktok1"
    7. }
    8. ]
    9. }
    10. type SetTraceResult struct {
    11. ShopId int `json:"shop_id"`
    12. Result bool `json:"result"`
    13. }
    14. resCh := make(chan SetTraceResult, 1) //结果channel
    15. for _, info := range data.Data {
    16. go func(info TraceInfo) {
    17. record := models.GetShopSettingByShopId(info.ShopId, []string{"id", "shop_id"}) //查询店铺设置
    18. res := SetTraceResult{ //默认的返回result
    19. ShopId: info.ShopId,
    20. Result: true,
    21. }
    22. if record.ID > 0 {
    23. var infoMap = make(map[string]interface{})
    24. infoMap, err = utils.Struct2MapWithJson(info)
    25. if err != nil {
    26. res.Result = false //转义失败
    27. resCh <- res
    28. return
    29. }
    30. } else {
    31. record := models.SailShopSetting{}
    32. utils.StructCopy(info, &record)
    33. err = models.CreateShopSetting(record) //增加表记录
    34. if err != nil {
    35. res.Result = false
    36. }
    37. }
    38. // 自定义代码应用
    39. if info.CustomGlobalScript != nil && *info.CustomGlobalScript != "" {
    40. appInfo := models.GetAppByName("customCode")
    41. installRecord := models.GetInstallationRecord(record.ShopId, appInfo.ID)
    42. customConfig := fmt.Sprintf(`{"customCode":"%s"}`, *info.CustomGlobalScript)
    43. if installRecord.ID > 0 {
    44. err = models.UpdateInstallationRecord(record.ShopId, appInfo.ID, "config", customConfig)
    45. } else {
    46. installRecode := models.SailShopInstalledApplications{
    47. ShopID: record.ShopId,
    48. ApplicationID: appInfo.ID,
    49. AppType: appInfo.AppType,
    50. Config: customConfig,
    51. Status: 1,
    52. InstalledAt: time.Now(),
    53. Installed: 1,
    54. IsNew: 1,
    55. }
    56. err = models.CreateInstallationRecord(installRecode)
    57. }
    58. if err != nil {
    59. res.Result = false
    60. }
    61. }
    62. resCh <- res
    63. }(info)
    64. }
    65. var r []SetTraceResult
    66. for i := 0; i < len(data.Data); i++ {
    67. r = append(r, <-resCh)
    68. }
    69. app.Success(c, r)