| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- namespace addons\Clockin\api\modules\v1\controllers;
- use addons\Clockin\common\models\ClockinApply;
- use addons\Clockin\common\models\ClockinRewardLog;
- use addons\Clockin\common\models\ClockinUser;
- use common\enums\ApiStatusEnum;
- use common\enums\StatusEnum;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Clockin\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- $user = $this->user;
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
- if (empty($config['is_enable'])) return $this->error('插件未开启');
- $data = [
- 'user_id' => $this->user_id,
- 'nickname' => $user['nickname'],
- 'avatar_url' => $user['avatar_url'],
- 'success_num' => 0,
- 'total_reward' => 0,
- 'reward_limit' => $config['reward_limit'] ?? 0,
- 'is_show_reward_limit' => $config['is_show_reward_limit'] ?? 1,
- ];
- $clockin_user = ClockinUser::findOne(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
- if (!empty($clockin_user)) {
- $data['success_num'] = $clockin_user['success_num'];
- $data['total_reward'] = $clockin_user['total_reward'];
- }
- return $this->success('success', $data);
- }
- public function actionActivityDetail()
- {
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
- $data = [
- 'explain' => $config['explain'] ?? '',
- ];
- return $this->success('success', $data);
- }
- public function actionRewardLogList()
- {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['limit', 'page'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['user_id' => $this->user_id];
- $order = 'id desc,created_at desc';
- $params['where'] = $where;
- $params['order'] = $order;
- $params['select'] = 'id,apply_id,reward,created_at';
- if (empty($params['limit'])) $params['limit'] = 10;
- if (empty($params['page'])) $params['limit'] = 1;
- $list = ClockinRewardLog::listPage($params, false, true);
- if (!empty($list['list'])) {
- $apply_ids = array_column($list['list'], 'apply_id');
- $apply_list = ClockinApply::lists([
- 'where' => [
- ['id' => $apply_ids],
- ],
- 'index' => 'id',
- ]);
- foreach ($list['list'] as &$item) {
- $item['desc'] = '';
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- if (isset($apply_list[$item['apply_id']])) {
- $item['desc'] = '您在' . date('m/d H:i', $apply_list[$item['apply_id']]['created_at']) . '分享审核已通过!';
- }
- unset($item['apply_id']);
- }
- }
- return $this->success('success', $list);
- }
- public function actionApplyList()
- {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['limit', 'page'], 'integer'],
- [['is_pass'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['user_id' => $this->user_id];
- if (isset($params['is_pass'])) {
- if ($params['is_pass'] == -1) {
- $where[] = ['is_pass' => [-1,-2]];// 驳回+撤销
- } else {
- $where[] = ['is_pass' => $params['is_pass']];
- }
- }
- $order = 'id desc,created_at desc';
- $params['where'] = $where;
- $params['order'] = $order;
- if (empty($params['limit'])) $params['limit'] = 10;
- if (empty($params['page'])) $params['limit'] = 1;
- $list = ClockinApply::listPage($params, false, true);
- if (!empty($list['list'])) {
- foreach ($list['list'] as &$item) {
- $item['pic'] = json_decode($item['pic'], true);
- }
- }
- $list['is_pass_map'] = [
- -2 => '已撤销',
- -1 => '未通过',
- 0 => '审核中',
- 1 => '已通过',
- ];
- return $this->success('success', $list);
- }
- public function actionApply()
- {
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['pic'], 'required'],
- [['remark','id'], 'safe'],
- ]);
- if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
- $res = $this->check();
- $upload_limit = $res['upload_limit'] ?? 1;
- if (count($params['pic']) > $upload_limit) {
- throw new \Exception("至多可上传{$upload_limit}张图片");
- }
- if(!empty($params['id'])){
- $params['is_pass'] = 0;
- }
- $params['user_id'] = $this->user_id;
- $params['mall_id'] = $this->mall_id;
- $params['pic'] = json_encode($params['pic']);
- $res = ClockinApply::setData($this->mall_id, $params);
- if ($res == false) throw new \Exception(ClockinApply::getStaticError());
- return $this->success('success');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 用户主动撤销
- * @Author: hua
- * @DateTime: 2023/8/8 0008 17:59
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionCancel()
- {
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
- $this->check();
- $params['user_id'] = $this->user_id;
- $params['mall_id'] = $this->mall_id;
- $params['reason'] = '用户已撤销';
- $params['is_pass'] = -2;
- $res = ClockinApply::changeStatus($this->mall_id, $params);
- if ($res == false) throw new \Exception(ClockinApply::getStaticError());
- return $this->success('success');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionApplyDetail()
- {
- try {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
- $this->check();
- $detail = ClockinApply::getOne([
- ['id'=>$params['id']],
- ['mall_id'=>$this->mall_id],
- ['user_id'=>$this->user_id]
- ]
- );
- if (empty($detail)) throw new \Exception('记录不存在');
- $detail['pic'] = json_decode($detail['pic'],true);
- $detail['reason'] = $detail['reason'] ??'';
- return $this->success('success',$detail,ApiStatusEnum::CODE_SUCCESS, 0);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionCheck()
- {
- try {
- $res = $this->check();
- return $this->success('success', ['upload_limit' => $res['upload_limit'] ?? 1]);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function check()
- {
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
- if (empty($config['is_enable'])) throw new \Exception('插件未开启');
- if (!empty($config['is_seniority'])) {
- $seniority = json_decode($config['seniority'], true);
- if (empty($seniority)) $seniority = [];
- $user_level = $this->user['level'];
- if (!in_array($user_level, $seniority)) {
- throw new \Exception('你还没有参与打卡奖励资格!请尽快升级哦!');
- }
- }
- return $config;
- }
- }
|