DefaultController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace addons\SignIn\backend\controllers;
  3. use addons\SignIn\common\models\SignInAwardConfig;
  4. use addons\SignIn\common\models\UserSignInAward;
  5. use addons\SignIn\common\models\UserSignInAwardRecord;
  6. use addons\SignIn\common\models\UserSignInRecord;
  7. use addons\Coupon\common\models\AddonsCoupon;
  8. use common\enums\RedisKeyEnum;
  9. use common\models\user\User;
  10. use common\models\user\UserLevel;
  11. use common\helpers\csv\CsvExportHelper;
  12. use common\enums\DownloadEnum;
  13. use common\models\mall\MallAddons;
  14. use common\enums\AddonsEnum;
  15. use Yii;
  16. use function GuzzleHttp\json_decode;
  17. /**
  18. * 默认控制器
  19. *
  20. * Class DefaultController
  21. * @package addons\SignIn\backend\controllers
  22. */
  23. class DefaultController extends BaseController
  24. {
  25. public $enableCsrfValidation = false;
  26. /**
  27. * 首页
  28. *
  29. * @return string
  30. */
  31. public function actionIndex()
  32. {
  33. if (Yii::$app->request->isAjax) {
  34. if (Yii::$app->request->isGet) {
  35. //setting reward
  36. $group = Yii::$app->request->get('group','');
  37. $group = $group ?: 'reward';
  38. $config = Yii::$app->services->setting->addons->get($this->mall_id, 'SignIn', $group);
  39. $list = [];
  40. if ($group == 'setting') {
  41. $status = $config['status'] ?? 1;
  42. $rule = $config['rule'] ?? '';
  43. $list = ['status' => $status, 'rule' => $rule];
  44. } else {
  45. $reward_coupon = $config['reward_coupon'] ? json_decode($config['reward_coupon']) : [];
  46. $reward_score = $config['reward_score'] ? json_decode($config['reward_score'],true) : [];
  47. $today_coupon = $config['today_coupon'] ? json_decode($config['today_coupon'], true) : [];
  48. $today_score = $config['today_score'] ? json_decode($config['today_score'],true) : [];
  49. $is_coupon_sign_repeat = $config['is_coupon_sign_repeat'] ?? 0;
  50. $is_score_sign_repeat = $config['is_score_sign_repeat'] ?? 0;
  51. if(!isset($today_score['give_type'])) $today_score['give_type'] = '';
  52. foreach ($reward_score as &$item){
  53. if(!isset($item['give_type'])) $item['give_type'] = '';
  54. }
  55. $coupon_list = [];
  56. if(MallAddons::isInstall($this->mall_id,'Coupon')){
  57. $coupon_list = AddonsCoupon::getMallCoupon($this->mall_id);
  58. if (!empty($reward_coupon)) {
  59. //过滤过期,数量不足,删除的优惠券
  60. foreach ($reward_coupon as $key => $val) {
  61. if (!isset($coupon_list[$val->coupon_id])) {
  62. unset($reward_coupon[$key]);
  63. continue;
  64. }
  65. $reward_coupon[$key]->coupon_name = $coupon_list[$val->coupon_id]['name'];
  66. }
  67. }
  68. if (!empty($today_coupon)) {
  69. if (!isset($coupon_list[$today_coupon['coupon_id']])) {
  70. $today_coupon['coupon_id'] = 0;
  71. $today_coupon['num'] = 0;
  72. }
  73. }
  74. }
  75. $list = [
  76. 'reward_score' => $reward_score,
  77. 'reward_coupon' => array_values($reward_coupon),
  78. 'coupon_list' => $coupon_list,
  79. 'today_coupon' => $today_coupon,
  80. 'today_score' => $today_score,
  81. 'is_coupon_sign_repeat' => $is_coupon_sign_repeat,
  82. 'is_score_sign_repeat' => $is_score_sign_repeat,
  83. ];
  84. }
  85. $list['isShow'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_SIGNIN);
  86. $give_type = [
  87. [
  88. 'name'=>'score','title'=>Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name') ?? '积分',
  89. ],
  90. [
  91. 'name'=>'balance','title'=>Yii::$app->services->setting->mall->get($this->mall_id, 'balance.balance_name') ?? '余额',
  92. ],
  93. ];
  94. if(MallAddons::isInstall($this->mall_id,'Redpack')){
  95. $config = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_REDPACK, 'basic');
  96. $name = !empty($config['redpack_name'])?$config['redpack_name']:'红包';
  97. $give_type[]=['name'=>'RedpackFrozen','title'=>'冻结'.$name,];
  98. $give_type[]=['name'=>'Redpack','title'=>'激活'.$name,];
  99. }
  100. $list['give_type'] = $give_type;
  101. return $this->success('操作成功', $list);
  102. } else {
  103. $post = Yii::$app->request->post();
  104. $res = Yii::$app->services->validate->validate($post, [
  105. [['status','is_coupon_sign_repeat', 'is_score_sign_repeat'], 'in', 'range' => [0, 1]],
  106. [['score', 'coupon', 'today_score', 'today_coupon', 'status', 'rule', 'group'], 'safe']
  107. ]);
  108. if ($post['group'] == 'setting') {
  109. $setting['setting'] = ['rule' => $post['rule'], 'status' => $post['status']];
  110. Yii::$app->services->setting->addons->set($this->mall_id, 'SignIn', $setting);
  111. } else {
  112. if (!empty($post['coupon'])) {
  113. array_walk($post['coupon'], function(&$val, $index) {
  114. unset($val['coupon_name']);
  115. });
  116. }
  117. if ($post['today_coupon']['coupon_id'] == 0) $post['today_coupon'] = ['coupon_id' => 0, 'num' => 0];
  118. $data['reward'] = [
  119. 'reward_score' => $post['score'],
  120. 'reward_coupon' => $post['coupon'],
  121. 'today_score' => $post['today_score'],
  122. 'today_coupon' => $post['today_coupon'],
  123. 'is_coupon_sign_repeat' => $post['is_coupon_sign_repeat'],
  124. 'is_score_sign_repeat' => $post['is_score_sign_repeat'],
  125. ];
  126. Yii::$app->services->setting->addons->set($this->mall_id, 'SignIn', $data);
  127. }
  128. return $this->success('设置成功');
  129. }
  130. }
  131. return $this->render('index');
  132. }
  133. /**
  134. * 签到记录
  135. */
  136. public function actionRecord() {
  137. if (Yii::$app->request->isAjax) {
  138. if (Yii::$app->request->isPost) {
  139. $filename = '签到列表-' . date('YmdHis').'_'.rand(10000,99999);
  140. $data = \Yii::$app->request->post();
  141. $data['table'] = 'score';
  142. $data['mall_id'] = $this->mall_id;
  143. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_MEMBER,UserSignInRecord::class,'exportSignIn',$data);
  144. if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
  145. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  146. }
  147. $where[] = ['=', 'mall_id', $this->mall_id];
  148. $get = Yii::$app->request->get();
  149. $keyword = $get['keyword'] ?? '';
  150. if (!empty($level)) $userPraams['where'] = ['=', 'level', $level];
  151. if (!empty($keyword)) {
  152. $model = User::find();
  153. $userPraams['where'] = ['=', 'mall_id', $this->mall_id];
  154. $userPraams['where'] = [['or',
  155. ['=', 'id', $keyword],
  156. ['=', 'mobile', $keyword]]];
  157. $userPraams['select'] = ['id', 'nickname', 'username', 'avatar_url', 'mobile', 'level'];
  158. $userPraams['index'] = ['id'];
  159. User::paramPack($userPraams, $model);
  160. $userList = $model->asArray()->all();
  161. }
  162. $userLevel = UserLevel::getListIndexByLevelColumn($this->mall_id);
  163. //筛选
  164. if ($get['start_date'] != '') {
  165. $where[] = ['>', 'created_at', strtotime($get['start_date'])];
  166. }
  167. if ($get['end_date'] != '') {
  168. $where[] = ['<', 'created_at', strtotime($get['end_date'])];
  169. }
  170. if ($keyword && empty($userList)) {
  171. $list['list'] = [];
  172. } else {
  173. if (!empty($userList)) $where[] = ['in', 'user_id', array_column($userList, 'id')];
  174. $params['where'] = $where;
  175. $params['limit'] = 10;
  176. $params['select'] = ['user_id', 'max(id) id', 'max(created_at) as created_at'];
  177. $params['order'] = 'id desc';
  178. $params['group'] = 'user_id';
  179. $list = UserSignInRecord::listPage($params, false);
  180. if (!empty($list['list'])) {
  181. if (empty($userList)) {
  182. $user_ids = array_column($list['list'], 'user_id');
  183. $userList = User::find()
  184. ->select(['id', 'nickname', 'username', 'avatar_url', 'mobile', 'level'])
  185. ->where(['id' => $user_ids])->indexBy('id')->all();
  186. }
  187. foreach ($list['list'] as &$user) {
  188. if($user['created_at']>=strtotime(date("Y-m-d"))){ //只查今日奖励
  189. $award = UserSignInAwardRecord::find()->select(['num', 'type'])->where(['sign_id' => $user['id']])->all();
  190. $user['score_num'] = 0;
  191. $user['coupon_num'] = 0;
  192. if (!empty($award)) {
  193. foreach ($award as $val) {
  194. if ($val['type'] == 1) $user['score_num'] += $val['num'];
  195. if ($val['type'] == 2) $user['coupon_num'] += $val['num'];
  196. }
  197. }
  198. }
  199. $user['nickname'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['nickname'] : '';
  200. $user['username'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['username'] : '';
  201. $user['avatar_url'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['avatar_url'] : '';
  202. $user['mobile'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['mobile'] : '';
  203. $user['level'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['level'] : '';
  204. }
  205. }
  206. }
  207. $list['list'] = !empty($list['list']) ? $list['list'] : [];
  208. $list['export_list'] = UserSignInRecord::exportFieldsList();
  209. $list['user_level'] = $userLevel;
  210. return $this->success('操作成功', $list);
  211. }
  212. return $this->render('record');
  213. }
  214. /**
  215. * 用户签到
  216. */
  217. public function actionUserSignIn() {
  218. ini_set('memory_limit', '256M'); // 将内存限制设置为256M
  219. if (Yii::$app->request->isAjax) {
  220. $user_id = Yii::$app->request->get('user_id');
  221. $user_id = $user_id ?? 0;
  222. $user = User::findOne(['id' => $user_id]);
  223. if (empty($user)) throw new \Exception('用户不存在');
  224. $where = [['=', 'mall_id', $this->mall_id], ['=', 'user_id', $user_id]];
  225. $params['where'] = $where;
  226. $params['limit'] = 10;
  227. $params['order'] = 'created_at desc';
  228. $params['select'] = ['num', 'coupon_id', 'type', 'describe', 'created_at'];
  229. $list = UserSignInAwardRecord::listPage($params);
  230. return $this->success('操作成功', $list);
  231. }
  232. return $this->render('user_sign_in');
  233. }
  234. }