PondController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\modules\admin\controllers\pond;
  3. use app\constants\OptionSetting;
  4. use app\models\Option;
  5. use app\models\Pond;
  6. use app\models\PondSetting;
  7. use app\modules\admin\controllers\BaseController;
  8. use app\modules\admin\models\pond\PondForm;
  9. use app\modules\admin\models\pond\PondSettingForm;
  10. use yii\helpers\ArrayHelper;
  11. class PondController extends BaseController
  12. {
  13. public function actionIndex()
  14. {
  15. $model = PondSetting::findOne([
  16. 'store_id' => get_store_id()
  17. ]);
  18. if (!$model) {
  19. $model = new PondSetting();
  20. }
  21. if (\Yii::$app->request->isPost) {
  22. $form = new PondSettingForm();
  23. $form->store_id = get_store_id();
  24. $form->model = $model;
  25. $data = post_params();
  26. $form->rule = $data['rule'];
  27. $form->start_time = $data['start_time'];
  28. $form->end_time = $data['end_time'];
  29. $form->integral = $data['integral'];
  30. $result = $form->save();
  31. if ($result['code'] === 0) {
  32. Option::set(OptionSetting::WEB_KF_WECHAT_IMG, $data['kf_wechat_img'], get_store_id(), 'display');
  33. }
  34. return $this->asJson($result);
  35. }
  36. $model = ArrayHelper::toArray($model);
  37. $model['kf_wechat_img'] = Option::get(OptionSetting::WEB_KF_WECHAT_IMG, get_store_id(), 'display')['value'];
  38. return $this->asJson([
  39. 'code' => 0,
  40. 'data' => [
  41. 'setting' => $model
  42. ],
  43. ]);
  44. }
  45. public function actionEdit()
  46. {
  47. $query = Pond::find()
  48. ->where([
  49. 'store_id' => get_store_id(),
  50. ])->with(['gift' => function ($query) {
  51. $query->where([
  52. 'store_id' => get_store_id(),
  53. 'is_delete' => 0
  54. ]);
  55. }])
  56. ->orderBy('id ASC');
  57. $list = $this->simplifyData($query->all()) ? $this->simplifyData($query->all()) : [];
  58. return $this->asJson([
  59. 'code' => 0,
  60. 'data' => $list
  61. ]);
  62. }
  63. protected function simplifyData($data)
  64. {
  65. foreach ($data as $key => $val) {
  66. $newData[$key] = $val->attributes;
  67. if ($val->gift) {
  68. $newData[$key]['gift'] = $val->gift->attributes['name'];
  69. $newData[$key]['gift_img_url'] = $val->gift->attributes['cover_pic'];
  70. }
  71. }
  72. return $newData;
  73. }
  74. public function actionSubmit()
  75. {
  76. $data = post_params('data');
  77. if (!is_array($data)) {
  78. return $this->asJson([
  79. 'code' => 1,
  80. 'msg' => '格式不正确',
  81. ]);
  82. }
  83. //处理保存后 前端不刷新 一直点击添加导致的问题
  84. if (count($data) >= 6) {
  85. Pond::deleteAll(['store_id' => get_store_id()]);
  86. }
  87. if ($data) {
  88. $pond_data_type = array_column($data, 'type');
  89. if (!in_array(1, $pond_data_type)) {
  90. return $this->asJson([
  91. 'code' => 1,
  92. 'msg' => '至少设置一个谢谢惠顾奖',
  93. ]);
  94. }
  95. }
  96. foreach ($data as $v) {
  97. $model = Pond::findOne($v['id']);
  98. if (!$model) {
  99. $model = new Pond();
  100. }
  101. $form = new PondForm();
  102. $form->store_id = get_store_id();
  103. $form->model = $model;
  104. $form->name = $v['name'];
  105. $form->image_url = $v['image_url'];
  106. $form->winning_rate = $v['winning_rate'];
  107. $form->type = $v['type'];
  108. $form->stock = $v['stock'];
  109. $form->gift_id = $v['gift_id'];
  110. $form->user_id = $v['user_id'];
  111. $form->save();
  112. }
  113. return $this->asJson([
  114. 'code' => 0,
  115. 'msg' => '保存成功',
  116. ]);
  117. }
  118. }