module_shop_act_images_reorder.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package modules
  2. import (
  3. "encoding/json"
  4. "golang-fave/engine/wrapper"
  5. )
  6. type OrderItem struct {
  7. Id int
  8. Order int
  9. }
  10. type Orders struct {
  11. Items []OrderItem
  12. }
  13. func (this *Modules) RegisterAction_ShopImagesReorder() *Action {
  14. return this.newAction(AInfo{
  15. WantDB: true,
  16. Mount: "shop-images-reorder",
  17. WantAdmin: true,
  18. }, func(wrap *wrapper.Wrapper) {
  19. pf_data := wrap.R.FormValue("data")
  20. var orders Orders
  21. if err := json.Unmarshal([]byte(pf_data), &orders); err != nil {
  22. wrap.MsgError(err.Error())
  23. return
  24. }
  25. if len(orders.Items) > 0 {
  26. if err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  27. for _, value := range orders.Items {
  28. if _, err := tx.Exec("UPDATE shop_product_images SET ord = ? WHERE id = ?;", value.Order, value.Id); err != nil {
  29. return err
  30. }
  31. }
  32. return nil
  33. }); err != nil {
  34. wrap.MsgError(err.Error())
  35. return
  36. }
  37. }
  38. wrap.RecreateProductXmlFile()
  39. wrap.ResetCacheBlocks()
  40. // Remove loading effect
  41. wrap.Write(`$('#list-images').removeClass('loading');`)
  42. })
  43. }