| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace app\modules\admin\controllers\pond;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\Pond;
- use app\models\PondSetting;
- use app\modules\admin\controllers\BaseController;
- use app\modules\admin\models\pond\PondForm;
- use app\modules\admin\models\pond\PondSettingForm;
- use yii\helpers\ArrayHelper;
- class PondController extends BaseController
- {
- public function actionIndex()
- {
- $model = PondSetting::findOne([
- 'store_id' => get_store_id()
- ]);
- if (!$model) {
- $model = new PondSetting();
- }
- if (\Yii::$app->request->isPost) {
- $form = new PondSettingForm();
- $form->store_id = get_store_id();
- $form->model = $model;
- $data = post_params();
- $form->rule = $data['rule'];
- $form->start_time = $data['start_time'];
- $form->end_time = $data['end_time'];
- $form->integral = $data['integral'];
- $result = $form->save();
- if ($result['code'] === 0) {
- Option::set(OptionSetting::WEB_KF_WECHAT_IMG, $data['kf_wechat_img'], get_store_id(), 'display');
- }
- return $this->asJson($result);
- }
- $model = ArrayHelper::toArray($model);
- $model['kf_wechat_img'] = Option::get(OptionSetting::WEB_KF_WECHAT_IMG, get_store_id(), 'display')['value'];
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'setting' => $model
- ],
- ]);
- }
- public function actionEdit()
- {
- $query = Pond::find()
- ->where([
- 'store_id' => get_store_id(),
- ])->with(['gift' => function ($query) {
- $query->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0
- ]);
- }])
- ->orderBy('id ASC');
- $list = $this->simplifyData($query->all()) ? $this->simplifyData($query->all()) : [];
- return $this->asJson([
- 'code' => 0,
- 'data' => $list
- ]);
- }
- protected function simplifyData($data)
- {
- foreach ($data as $key => $val) {
- $newData[$key] = $val->attributes;
- if ($val->gift) {
- $newData[$key]['gift'] = $val->gift->attributes['name'];
- $newData[$key]['gift_img_url'] = $val->gift->attributes['cover_pic'];
- }
- }
- return $newData;
- }
- public function actionSubmit()
- {
- $data = post_params('data');
- if (!is_array($data)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '格式不正确',
- ]);
- }
- //处理保存后 前端不刷新 一直点击添加导致的问题
- if (count($data) >= 6) {
- Pond::deleteAll(['store_id' => get_store_id()]);
- }
- if ($data) {
- $pond_data_type = array_column($data, 'type');
- if (!in_array(1, $pond_data_type)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '至少设置一个谢谢惠顾奖',
- ]);
- }
- }
- foreach ($data as $v) {
- $model = Pond::findOne($v['id']);
- if (!$model) {
- $model = new Pond();
- }
- $form = new PondForm();
- $form->store_id = get_store_id();
- $form->model = $model;
- $form->name = $v['name'];
- $form->image_url = $v['image_url'];
- $form->winning_rate = $v['winning_rate'];
- $form->type = $v['type'];
- $form->stock = $v['stock'];
- $form->gift_id = $v['gift_id'];
- $form->user_id = $v['user_id'];
- $form->save();
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功',
- ]);
- }
- }
|