ScoreClockinActive.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Facades\Servers\WechatMini\Mini;
  3. use App\Http\Requests\Admin\ScoreClockinActive as Request;
  4. use App\Models\City;
  5. use App\Models\Score\ClockinActive as Model;
  6. use App\Models\Score\Clockin as Clockin;
  7. use App\Models\WeiBan\Tags as WeiBanTags;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 打卡活动
  11. *
  12. * @author jun
  13. *
  14. */
  15. class ScoreClockinActive extends Auth{
  16. protected function _initialize(){
  17. parent::_initialize();
  18. $this->assign('breadcrumb1','任务打卡');
  19. $this->assign('breadcrumb2','打卡任务');
  20. }
  21. /**
  22. * 列表页
  23. *
  24. * */
  25. public function index(Request $request, Model $Model, City $City){
  26. // 接收参数
  27. $name = request('name','');
  28. // 查询条件
  29. $map = [];
  30. // 组合条件
  31. if( $name ) $map[] = ['name','=',$name];
  32. // 查询数据
  33. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  34. // 循环处理数据
  35. foreach ($list as $key => $value) {
  36. // 小程序链接
  37. $value['mp_urllink']= $this->getUrlLink($value['id']);
  38. if( $value['city_ids'] ) {
  39. // 解析数组
  40. $cityids = explode(',',$value['city_ids']);
  41. // 获取城市
  42. foreach ($cityids as $kk=>$vv) {
  43. // 获取值
  44. $vv = $City->getOne($vv,'name');
  45. // 获取城市名
  46. $cityids[$kk] = $vv;
  47. }
  48. // 城市列表
  49. $value['city_ids'] = implode('、',$cityids);
  50. }
  51. // 重组
  52. $list[$key] = $value;
  53. }
  54. // 分配数据
  55. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  56. $this->assign('list',$list);
  57. // 加载模板
  58. return $this->fetch();
  59. }
  60. /**
  61. * 获取小程序链接
  62. *
  63. */
  64. private function getUrlLink($id){
  65. // 结果数据
  66. $link = cache('admin:clockin:active:urllink:'.$id);
  67. // 不存在数据
  68. if ( is_null($link) ) {
  69. // 从数据库获取数据
  70. $link = Mini::getUrlLink('pages/clockin/active','?id='.$id);
  71. // 存起来
  72. cache(['admin:clockin:active:urllink:'.$id=>$link],$link ? now()->addDays(28) : now()->addMinutes(3));
  73. }
  74. // 返回结果
  75. return $link;
  76. }
  77. /**
  78. * 添加
  79. *
  80. * */
  81. public function add(Request $request, Model $Model, City $City, WeiBanTags $WeiBanTags){
  82. if( request()->isMethod('post') ){
  83. // 验证参数
  84. $request->scene('add')->validate();
  85. // 接收数据
  86. $data['banner_img'] = request('banner_img','');
  87. $data['name'] = request('name','');
  88. $data['active_rule'] = request('active_rule','');
  89. $data['start_time'] = request('start_time','');
  90. $data['end_time'] = request('end_time','');
  91. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  92. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  93. $cityIds = request('city_ids',[]);
  94. $tagScope = request('tag_scope',[]);
  95. $data['city_ids'] = implode(',',$cityIds);
  96. $data['tag_scope'] = implode(',',$tagScope);
  97. $data['status'] = 1;
  98. // 写入数据表
  99. $id = $Model->add($data);
  100. // 如果操作失败
  101. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  102. // 记录行为
  103. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  104. // 告知结果
  105. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  106. }
  107. // 获取列表
  108. $cityList = $City->getCityList();
  109. // 标签列表
  110. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  111. // 标签列表
  112. $tagList = [];
  113. // 循环数据
  114. foreach ($tagData as $value) {
  115. $tagList[$value['group']][] = $value['name'];
  116. }
  117. // 分配数据
  118. $this->assign('cityList',$cityList);
  119. $this->assign('tagList',$tagList);
  120. $this->assign('crumbs','新增');
  121. // 加载模板
  122. return $this->fetch();
  123. }
  124. /**
  125. * 修改
  126. *
  127. * */
  128. public function edit(Request $request,Model $Model,City $City,WeiBanTags $WeiBanTags){
  129. // 接收参数
  130. $id = request('id',0);
  131. // 查询用户
  132. $oldData = $Model->where(['id'=>$id])->first();
  133. // 修改
  134. if(request()->isMethod('post')){
  135. // 验证参数
  136. $request->scene('edit')->validate();
  137. // 接收数据
  138. $data['banner_img'] = request('banner_img','');
  139. $data['name'] = request('name','');
  140. $data['active_rule'] = request('active_rule','');
  141. $data['start_time'] = request('start_time','');
  142. $data['end_time'] = request('end_time','');
  143. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  144. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  145. $cityIds = request('city_ids',[]);
  146. $tagScope = request('tag_scope',[]);
  147. $data['city_ids'] = implode(',',$cityIds);
  148. $data['tag_scope'] = implode(',',$tagScope);
  149. // 写入数据表
  150. $result = $Model->edit($id,$data);
  151. // 如果操作失败
  152. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  153. // 记录行为
  154. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  155. // 告知结果
  156. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  157. }
  158. // 错误告知
  159. if( !$oldData ) return $this->error('查无数据');
  160. // 获取城市ID
  161. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  162. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  163. // 获取列表
  164. $cityList = $City->getCityList();
  165. // 标签列表
  166. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  167. // 标签列表
  168. $tagList = [];
  169. // 循环数据
  170. foreach ($tagData as $value) {
  171. $tagList[$value['group']][] = $value['name'];
  172. }
  173. // 分配数据
  174. $this->assign('cityList',$cityList);
  175. $this->assign('tagList',$tagList);
  176. $this->assign('oldData',$oldData);
  177. $this->assign('crumbs','修改');
  178. // 加载模板
  179. return $this->fetch();
  180. }
  181. /**
  182. * 修改状态
  183. *
  184. * */
  185. public function set_status(Request $request,Model $Model){
  186. // 验证参数
  187. $request->scene('set_status')->validate();
  188. // 设置状态
  189. $id = request('id',0);
  190. $status = request('status',0);
  191. // 查询用户
  192. $oldData = $Model->where(['id'=>$id])->first();
  193. // 如果用户不存在
  194. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  195. // 执行修改
  196. $result = $Model->edit($id,['status'=>$status]);
  197. // 提示新增失败
  198. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  199. // 记录行为
  200. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  201. // 告知结果
  202. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  203. }
  204. /**
  205. * 复制
  206. *
  207. * */
  208. public function copy(Request $request,Model $Model,Clockin $Clockin){
  209. // 验证参数
  210. $request->scene('copy')->validate();
  211. // 设置状态
  212. $id = request('id',0);
  213. $time = time();
  214. // 查询
  215. $data = $Model->where(['id'=>$id])->first()->toArray();
  216. if (!$data) json_send(['code'=>'error','msg'=>'复制失败,活动不存在']);
  217. $clockinList = $Clockin->getActiveList($id);
  218. DB::beginTransaction();
  219. try {
  220. unset($data['id']);
  221. $data['status'] = 1;
  222. $data['name'] = $data['name'].'复制';
  223. //复制活动
  224. $newId = $Model->add($data);
  225. if (!$newId){
  226. DB::rollBack();
  227. return json_send(['code'=>'error','msg'=>'复制失败']);
  228. }
  229. if ($clockinList){
  230. foreach ($clockinList as &$clockin){
  231. $clockin['active_id'] = $newId;
  232. $clockin['insert_time'] = $time;
  233. $clockin['update_time'] = $time;
  234. unset($clockin['id']);
  235. }
  236. $re = $Clockin->Query()->insert($clockinList);
  237. if (!$re) return json_send(['code'=>'error','msg'=>'复制失败']);
  238. }
  239. DB::commit();
  240. }catch (\Exception $e){
  241. // 回退数据
  242. DB::rollBack();
  243. // 失败提示
  244. return json_send(['code'=>'error','msg'=>'复制失败','data'=>['error'=>$e->getMessage().$e->getLine()]]);
  245. }
  246. return json_send(['code'=>'success','msg'=>'复制成功','path'=>'']);
  247. }
  248. }