ActivityController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 文件说明:幻灯片
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | Date: 2017-5-25
  12. // +----------------------------------------------------------------------
  13. namespace api\activity\controller;
  14. use api\activity\model\ActivityJoinModel;
  15. use api\activity\model\ActivityModel;
  16. use api\activity\model\ActivitySiteJoinModel;
  17. use api\applet\model\UserModel;
  18. use cmf\controller\RestBaseController;
  19. class ActivityController extends RestBaseController
  20. {
  21. /**
  22. * 列表
  23. */
  24. public function index()
  25. {
  26. $param = $this->request->param();
  27. $page = empty($param['page']) ? 1 : $param['page'];
  28. $size = empty($param['size']) ? 10 : $param['size'];
  29. //搜索条件
  30. $where = [['status', '=', 2]];
  31. if (!empty($param['keyword'])) {
  32. $where[] = ['title', 'like', "%{$param['keyword']}%"];
  33. }
  34. if (!empty($param['user_id'])) {
  35. $where[] = ['user_id', '=', $param['user_id']];
  36. }
  37. $list = ActivityModel::where($where)->order('create_time desc')->page($page, $size)->select();
  38. //数据处理
  39. if (!$list->isEmpty()) {
  40. foreach ($list as $v) {
  41. $v['main_image'] = cmf_get_image_preview_url($v['main_image']);
  42. $v['status_text'] = ActivityModel::statusText($v);
  43. $v['start_time'] = date('Y-m-d H:i', $v['start_time']);
  44. $v['end_time'] = date('Y-m-d H:i', $v['end_time']);
  45. }
  46. }
  47. $this->success('成功', $list);
  48. }
  49. /**
  50. * 详情
  51. */
  52. public function detail()
  53. {
  54. $id = $this->request->post('id');
  55. $info = ActivityModel::get($id, ['review']);
  56. $info['main_image'] = cmf_get_image_preview_url($info['main_image']);
  57. $info['status_text'] = ActivityModel::statusText($info);
  58. $info['start_time'] = date('Y-m-d H:i', $info['start_time']);
  59. $info['end_time'] = date('Y-m-d H:i', $info['end_time']);
  60. $info['create_time'] = date('Y-m-d H:i', $info['create_time']);
  61. if (!empty($info['options'])) {
  62. $options = [];
  63. foreach ($info['options'] as $v) {
  64. $options[] = [
  65. 'url' => cmf_get_file_download_url($v['url']),
  66. 'name' => $v['name'],
  67. ];
  68. }
  69. $info['options'] = $options;
  70. }
  71. $this->success('成功', $info);
  72. }
  73. /**
  74. * 报名列表
  75. */
  76. public function joinList()
  77. {
  78. $param = $this->request->param();
  79. $page = empty($param['page']) ? 1 : $param['page'];
  80. $size = empty($param['size']) ? 10 : $param['size'];
  81. $user_id = $this->getUserId();
  82. $activityIds = ActivityJoinModel::where('user_id', '=', $user_id)
  83. ->order('create_time desc')
  84. ->page($page, $size)
  85. ->column('activity_id');
  86. $list = ActivityModel::where('id', 'in', $activityIds)->order('create_time desc')->select();
  87. //数据处理
  88. if (!$list->isEmpty()) {
  89. foreach ($list as $v) {
  90. $v['main_image'] = cmf_get_image_preview_url($v['main_image']);
  91. $v['status_text'] = ActivityModel::statusText($v);
  92. $v['start_time'] = date('Y-m-d H:i', $v['start_time']);
  93. $v['end_time'] = date('Y-m-d H:i', $v['end_time']);
  94. }
  95. }
  96. $this->success('成功', $list);
  97. }
  98. /**
  99. * 报名
  100. */
  101. public function join()
  102. {
  103. $activity_id = $this->request->param('id');
  104. $user_id = $this->getUserId();
  105. $user = UserModel::get($user_id);
  106. if (empty($user['user_name']) || empty($user['mobile'])) {
  107. $this->success('', ['code' => 1001, 'msg' => '请完善信息']);
  108. }
  109. $check = ActivityJoinModel::where('user_id', '=', $user_id)->where('activity_id', '=', $activity_id)->find();
  110. if (empty($check)) {
  111. //活动报名
  112. ActivityJoinModel::create([
  113. 'user_id' => $user_id,
  114. 'activity_id' => $activity_id,
  115. 'create_time' => time(),
  116. ]);
  117. $site_id = ActivityModel::where('id', $activity_id)->value('user_id');
  118. //站点报名记录
  119. $site_check = ActivitySiteJoinModel::where('user_id', '=', $user_id)->where('site_id', '=', $site_id)->find();
  120. if (empty($site_check)) {
  121. ActivitySiteJoinModel::create([
  122. 'user_id' => $user_id,
  123. 'site_id' => $site_id,
  124. 'create_time' => time(),
  125. ]);
  126. }
  127. $this->success('报名成功', ['msg' => '报名成功']);
  128. } else {
  129. $this->success('请勿重复报名', ['msg' => '请勿重复报名']);
  130. }
  131. }
  132. /**
  133. * 签到
  134. */
  135. public function signin()
  136. {
  137. $activity_id = $this->request->param('activity_id');
  138. $code = $this->request->param('code');
  139. //查找记录
  140. $activity = ActivityModel::get(['id' => $activity_id, 'signin_code' => $code]);
  141. if (empty($activity)) {
  142. $this->error('二维码有误!请重新确认');
  143. }
  144. //签到
  145. $user_id = $this->getUserId();
  146. $join = ActivityJoinModel::get(['user_id' => $user_id, 'activity_id' => $activity_id]);
  147. if (empty($join)) {
  148. $this->error('请先报名该活动!');
  149. }
  150. if ($join['status'] == 1) {
  151. $this->error('请勿重复签到!');
  152. }
  153. //增加积分
  154. if ($activity->score > 0) {
  155. $model = new \app\user\model\UserModel();
  156. $res = $model->changeScore($activity->score, "活动签到:{$activity->title}");
  157. if ($res['code'] == 0) {
  158. $this->error($res['msg']);
  159. }
  160. }
  161. //更改签到状态
  162. $join->status = 1;
  163. $join->save();
  164. $this->success('签到成功', ['msg' => '签到成功']);
  165. }
  166. }