DefaultController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\Recharge\backend\controllers;
  3. use addons\Recharge\common\enums\RechargeEnum;
  4. use addons\Recharge\common\models\AddonsRechargeActive;
  5. use addons\Recharge\common\models\AddonsRechargeLog;
  6. use common\enums\StatusEnum;
  7. use common\models\user\User;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * 默认控制器
  12. *
  13. * Class DefaultController
  14. * @package addons\Recharge\backend\controllers
  15. */
  16. class DefaultController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. /**
  20. * 首页
  21. */
  22. public function actionIndex()
  23. {
  24. $request = Yii::$app->request;
  25. if ($request->isAjax && $request->isPost) {
  26. $post = Yii::$app->request->post();
  27. $where = [];
  28. $where[] = ['=', 'mall_id', $this->mall_id];
  29. if ($post['give_cond'] != '') {
  30. $where[] = ['=', 'give_cond', $post['give_cond']];
  31. }
  32. if ($post['name'] != '') {
  33. $where[] = ['like', 'name', $post['name']];
  34. }
  35. if ($post['active_status'] != '') {
  36. $where[] = ['=', 'active_status', $post['active_status']];
  37. }
  38. $where[] = ['>', 'status', StatusEnum::DELETE];
  39. $with = [];
  40. $order = ["FIELD(active_status, 1,3,0,2)" => true];
  41. $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $post['limit']);
  42. $list = AddonsRechargeActive::listPage($params, false, true);
  43. if ($list['list']) {
  44. foreach ($list['list'] as &$item) {
  45. if ($item['give_info']) {
  46. $item['give_info'] = \Qiniu\json_decode($item['give_info']);
  47. }
  48. }
  49. }
  50. $this->success('获取成功', $list);
  51. } else {
  52. return $this->render('index', []);
  53. }
  54. }
  55. /**
  56. * 编辑配置
  57. */
  58. public function actionEdit()
  59. {
  60. if (\Yii::$app->request->isAjax) {
  61. $post = \Yii::$app->request->post();
  62. $post['give_info'] = Json::encode($post['give_info']);
  63. $now = time();
  64. if (!isset($post['id'])) {
  65. if ($post['start_at'] < $now && $post['end_at'] > $now + 5) {
  66. $post['active_status'] = RechargeEnum::ACTIVE_STATUS1;
  67. } else {
  68. $post['active_status'] = RechargeEnum::ACTIVE_STATUS0;
  69. }
  70. $post['status'] = StatusEnum::ENABLED;
  71. }
  72. $res = AddonsRechargeActive::setData($post, $this->mall_id);
  73. $this->success('操作成功', $res);
  74. } else {
  75. return $this->render('edit');
  76. }
  77. }
  78. public function actionDetail()
  79. {
  80. $post = \Yii::$app->request->post();
  81. $detail = AddonsRechargeActive::getOne([['id' => $post['id']]]);
  82. if ($detail) {
  83. $detail['give_info'] = Json::decode($detail['give_info']);
  84. }
  85. $this->success('操作成功', $detail);
  86. }
  87. public function actionDelete()
  88. {
  89. if (\Yii::$app->request->isAjax) {
  90. $post = \Yii::$app->request->post();
  91. $res = AddonsRechargeActive::setData($post, $this->mall_id);
  92. $this->success('操作成功', $res);
  93. } else {
  94. return $this->render('data');
  95. }
  96. }
  97. public function actionList()
  98. {
  99. if (\Yii::$app->request->isAjax) {
  100. $post = \Yii::$app->request->post();
  101. $where = [];
  102. $all = [];
  103. if ($post['page'] == 1) {
  104. $all = AddonsRechargeLog::getOne([['recharge_active_id' => $post['recharge_active_id']]], true, [], false, 'sum(give_score) as all_score,sum(give_balance) as all_balance');
  105. }
  106. if ($post['user_id'] != '') {
  107. $where[] = ['=', 'user_id', $post['user_id']];
  108. }
  109. if ($post['word'] != '') {
  110. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], ['or', ['like', 'mobile', trim($post['word'])], ['like', 'nickname', trim($post['word'])]], ['status' => StatusEnum::ENABLED]], 'select' => 'id']);
  111. $user_id_arr = array_column($user_list, 'id');
  112. $where[] = ['user_id' => $user_id_arr];
  113. }
  114. if ($post['start_date'] != '') {
  115. $where[] = ['>', 'start_date', strtotime($post['start_date'])];
  116. }
  117. if ($post['end_date'] != '') {
  118. $where[] = ['<', 'end_date', strtotime($post['end_date'])];
  119. }
  120. $where[] = ['=', 'recharge_active_id', $post['recharge_active_id']];
  121. $with = ['user'];
  122. $order = 'created_at desc';
  123. $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $post['limit']);
  124. $list = AddonsRechargeLog::listPage($params, false, true);
  125. if ($list['list']) {
  126. foreach ($list['list'] as $k => &$v) {
  127. $v['give_content'] = \Qiniu\json_decode($v['give_content']);
  128. }
  129. }
  130. $this->success('获取成功', compact('all', 'list'));
  131. } else {
  132. return $this->render('list');
  133. }
  134. }
  135. public function actionUpdate()
  136. {
  137. if (\Yii::$app->request->isAjax) {
  138. $post = \Yii::$app->request->post();
  139. $post['active_status'] = $post['active_status'] == 1 ? 3 : 1;
  140. $res = AddonsRechargeActive::setData($post, $this->mall_id);
  141. $this->success('修改成功', $res);
  142. } else {
  143. return $this->render('index');
  144. }
  145. }
  146. }