DefaultController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace addons\Clockin\api\modules\v1\controllers;
  3. use addons\Clockin\common\models\ClockinApply;
  4. use addons\Clockin\common\models\ClockinRewardLog;
  5. use addons\Clockin\common\models\ClockinUser;
  6. use common\enums\ApiStatusEnum;
  7. use common\enums\StatusEnum;
  8. use Yii;
  9. use api\controllers\OnAuthController;
  10. /**
  11. * 默认控制器
  12. *
  13. * Class DefaultController
  14. * @package addons\Clockin\api\modules\v1\controllers
  15. */
  16. class DefaultController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. *
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = ['index'];
  28. /**
  29. * 首页
  30. *
  31. * @return string
  32. */
  33. public function actionIndex()
  34. {
  35. $user = $this->user;
  36. $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
  37. if (empty($config['is_enable'])) return $this->error('插件未开启');
  38. $data = [
  39. 'user_id' => $this->user_id,
  40. 'nickname' => $user['nickname'],
  41. 'avatar_url' => $user['avatar_url'],
  42. 'success_num' => 0,
  43. 'total_reward' => 0,
  44. 'reward_limit' => $config['reward_limit'] ?? 0,
  45. 'is_show_reward_limit' => $config['is_show_reward_limit'] ?? 1,
  46. ];
  47. $clockin_user = ClockinUser::findOne(['user_id' => $this->user_id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id]);
  48. if (!empty($clockin_user)) {
  49. $data['success_num'] = $clockin_user['success_num'];
  50. $data['total_reward'] = $clockin_user['total_reward'];
  51. }
  52. return $this->success('success', $data);
  53. }
  54. public function actionActivityDetail()
  55. {
  56. $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
  57. $data = [
  58. 'explain' => $config['explain'] ?? '',
  59. ];
  60. return $this->success('success', $data);
  61. }
  62. public function actionRewardLogList()
  63. {
  64. $params = Yii::$app->request->get();
  65. $res = Yii::$app->services->validate->validate($params, [
  66. [['limit', 'page'], 'integer'],
  67. ]);
  68. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  69. $where = [];
  70. $where[] = ['status' => StatusEnum::ENABLED];
  71. $where[] = ['mall_id' => $this->mall_id];
  72. $where[] = ['user_id' => $this->user_id];
  73. $order = 'id desc,created_at desc';
  74. $params['where'] = $where;
  75. $params['order'] = $order;
  76. $params['select'] = 'id,apply_id,reward,created_at';
  77. if (empty($params['limit'])) $params['limit'] = 10;
  78. if (empty($params['page'])) $params['limit'] = 1;
  79. $list = ClockinRewardLog::listPage($params, false, true);
  80. if (!empty($list['list'])) {
  81. $apply_ids = array_column($list['list'], 'apply_id');
  82. $apply_list = ClockinApply::lists([
  83. 'where' => [
  84. ['id' => $apply_ids],
  85. ],
  86. 'index' => 'id',
  87. ]);
  88. foreach ($list['list'] as &$item) {
  89. $item['desc'] = '';
  90. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  91. if (isset($apply_list[$item['apply_id']])) {
  92. $item['desc'] = '您在' . date('m/d H:i', $apply_list[$item['apply_id']]['created_at']) . '分享审核已通过!';
  93. }
  94. unset($item['apply_id']);
  95. }
  96. }
  97. return $this->success('success', $list);
  98. }
  99. public function actionApplyList()
  100. {
  101. $params = Yii::$app->request->get();
  102. $res = Yii::$app->services->validate->validate($params, [
  103. [['limit', 'page'], 'integer'],
  104. [['is_pass'], 'safe'],
  105. ]);
  106. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  107. $where = [];
  108. $where[] = ['status' => StatusEnum::ENABLED];
  109. $where[] = ['mall_id' => $this->mall_id];
  110. $where[] = ['user_id' => $this->user_id];
  111. if (isset($params['is_pass'])) {
  112. if ($params['is_pass'] == -1) {
  113. $where[] = ['is_pass' => [-1,-2]];// 驳回+撤销
  114. } else {
  115. $where[] = ['is_pass' => $params['is_pass']];
  116. }
  117. }
  118. $order = 'id desc,created_at desc';
  119. $params['where'] = $where;
  120. $params['order'] = $order;
  121. if (empty($params['limit'])) $params['limit'] = 10;
  122. if (empty($params['page'])) $params['limit'] = 1;
  123. $list = ClockinApply::listPage($params, false, true);
  124. if (!empty($list['list'])) {
  125. foreach ($list['list'] as &$item) {
  126. $item['pic'] = json_decode($item['pic'], true);
  127. }
  128. }
  129. $list['is_pass_map'] = [
  130. -2 => '已撤销',
  131. -1 => '未通过',
  132. 0 => '审核中',
  133. 1 => '已通过',
  134. ];
  135. return $this->success('success', $list);
  136. }
  137. public function actionApply()
  138. {
  139. try {
  140. $params = Yii::$app->request->post();
  141. $res = Yii::$app->services->validate->validate($params, [
  142. [['pic'], 'required'],
  143. [['remark','id'], 'safe'],
  144. ]);
  145. if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
  146. $res = $this->check();
  147. $upload_limit = $res['upload_limit'] ?? 1;
  148. if (count($params['pic']) > $upload_limit) {
  149. throw new \Exception("至多可上传{$upload_limit}张图片");
  150. }
  151. if(!empty($params['id'])){
  152. $params['is_pass'] = 0;
  153. }
  154. $params['user_id'] = $this->user_id;
  155. $params['mall_id'] = $this->mall_id;
  156. $params['pic'] = json_encode($params['pic']);
  157. $res = ClockinApply::setData($this->mall_id, $params);
  158. if ($res == false) throw new \Exception(ClockinApply::getStaticError());
  159. return $this->success('success');
  160. } catch (\Exception $e) {
  161. return $this->error($e->getMessage());
  162. }
  163. }
  164. /**
  165. * 用户主动撤销
  166. * @Author: hua
  167. * @DateTime: 2023/8/8 0008 17:59
  168. * @Copyright: copyright (c) 2021 广东七件事集团
  169. * @return mixed|void
  170. */
  171. public function actionCancel()
  172. {
  173. try {
  174. $params = Yii::$app->request->post();
  175. $res = Yii::$app->services->validate->validate($params, [
  176. [['id'], 'required'],
  177. ]);
  178. if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
  179. $this->check();
  180. $params['user_id'] = $this->user_id;
  181. $params['mall_id'] = $this->mall_id;
  182. $params['reason'] = '用户已撤销';
  183. $params['is_pass'] = -2;
  184. $res = ClockinApply::changeStatus($this->mall_id, $params);
  185. if ($res == false) throw new \Exception(ClockinApply::getStaticError());
  186. return $this->success('success');
  187. } catch (\Exception $e) {
  188. return $this->error($e->getMessage());
  189. }
  190. }
  191. public function actionApplyDetail()
  192. {
  193. try {
  194. $params = Yii::$app->request->get();
  195. $res = Yii::$app->services->validate->validate($params, [
  196. [['id'], 'required'],
  197. ]);
  198. if ($res === false) throw new \Exception (Yii::$app->services->validate->getErrorMessage());
  199. $this->check();
  200. $detail = ClockinApply::getOne([
  201. ['id'=>$params['id']],
  202. ['mall_id'=>$this->mall_id],
  203. ['user_id'=>$this->user_id]
  204. ]
  205. );
  206. if (empty($detail)) throw new \Exception('记录不存在');
  207. $detail['pic'] = json_decode($detail['pic'],true);
  208. $detail['reason'] = $detail['reason'] ??'';
  209. return $this->success('success',$detail,ApiStatusEnum::CODE_SUCCESS, 0);
  210. } catch (\Exception $e) {
  211. return $this->error($e->getMessage());
  212. }
  213. }
  214. public function actionCheck()
  215. {
  216. try {
  217. $res = $this->check();
  218. return $this->success('success', ['upload_limit' => $res['upload_limit'] ?? 1]);
  219. } catch (\Exception $e) {
  220. return $this->error($e->getMessage());
  221. }
  222. }
  223. public function check()
  224. {
  225. $config = Yii::$app->services->setting->addons->get($this->mall_id, 'Clockin', 'basic');
  226. if (empty($config['is_enable'])) throw new \Exception('插件未开启');
  227. if (!empty($config['is_seniority'])) {
  228. $seniority = json_decode($config['seniority'], true);
  229. if (empty($seniority)) $seniority = [];
  230. $user_level = $this->user['level'];
  231. if (!in_array($user_level, $seniority)) {
  232. throw new \Exception('你还没有参与打卡奖励资格!请尽快升级哦!');
  233. }
  234. }
  235. return $config;
  236. }
  237. }