Activity.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\User;
  4. use library\Controller;
  5. use think\Db;
  6. /**
  7. * 活动
  8. * Class Activity
  9. * @package app\operate\controller
  10. */
  11. class Activity extends Controller
  12. {
  13. protected $table = 'Activity';
  14. /**
  15. * 列表
  16. * @auth true
  17. * @menu true
  18. * @throws \think\Exception
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws \think\exception\DbException
  22. * @throws \think\exception\PDOException
  23. */
  24. public function index()
  25. {
  26. $this->title = '列表';
  27. $where = [];
  28. $where[] = ['f.is_deleted','=',0];
  29. if($title = input('title')) $where[] = ['f.title','like','%'.$title.'%'];
  30. $query = $this->_query($this->table)->alias('f')
  31. ->field('f.*')
  32. ->where($where)
  33. ->order('sort desc,f.id asc')->page();
  34. }
  35. /**
  36. * 添加
  37. * @auth true
  38. * @menu true
  39. * @throws \think\Exception
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. * @throws \think\exception\PDOException
  44. */
  45. public function add()
  46. {
  47. $this->title = '添加';
  48. $this->_form($this->table, 'form');
  49. }
  50. /**
  51. * 编辑
  52. * @auth true
  53. * @menu true
  54. * @throws \think\Exception
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \think\exception\DbException
  58. * @throws \think\exception\PDOException
  59. */
  60. public function edit()
  61. {
  62. $this->title = '编辑';
  63. $this->_form($this->table, 'form');
  64. }
  65. /**
  66. * 删除
  67. * @auth true
  68. * @throws \think\Exception
  69. * @throws \think\exception\PDOException
  70. */
  71. public function del()
  72. {
  73. $this->_save($this->table, ['is_deleted' => '1']);
  74. }
  75. /**
  76. * 表单数据处理
  77. * @param array $data
  78. */
  79. protected function _form_filter(&$data)
  80. {
  81. if ($this->request->isGet() && $this->request->action() == 'add') {
  82. $this->isAddMode = 1;
  83. $this->ladder = [];
  84. }
  85. if ($this->request->isGet() && $this->request->action() == 'edit') {
  86. $this->isAddMode = 0;
  87. $this->ladder = isset_full($data,'ladder') ? json_decode($data['ladder'],true):[];
  88. }
  89. if($this->request->isPost())
  90. {
  91. list($post,$ladder_data) = [$this->request->post(),[]];
  92. foreach (array_keys($post['ladder_title']) as $key) {
  93. array_push($ladder_data, [
  94. 'ladder_title' => $post['ladder_title'][$key],
  95. 'ladder_num' => intval($post['ladder_num'][$key]),
  96. 'ladder_price' => sprintf('%.2f',$post['ladder_price'][$key]),
  97. ]);
  98. }
  99. if(!empty($post['phone'])) {
  100. $user_id = User::where('phone|email',$post['phone'])->value('id');
  101. if(!$user_id) $this->error('账号未注册');
  102. $data['user_id'] = $user_id;
  103. }else{
  104. $data['user_id'] = '';
  105. }
  106. $data['ladder'] = json_encode($ladder_data);
  107. }else{
  108. if (!empty($data)){
  109. $data['covers'] = $data['cover'];
  110. }
  111. }
  112. }
  113. /**
  114. * 报名记录
  115. * @auth true
  116. * @menu true
  117. * @throws \think\Exception
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. * @throws \think\exception\DbException
  121. * @throws \think\exception\PDOException
  122. */
  123. public function apply()
  124. {
  125. $id = $this->request->get('act_id');
  126. $name = $this->request->get('name');
  127. $phone = $this->request->get('phone');
  128. $this->title = '报名记录';
  129. $where = [];
  130. $where[]= ['a.act_id','=' ,$id];
  131. $where[]= ['a.is_deleted','=' ,0];
  132. $where[]= ['a.status','=' ,1];
  133. $where[]= ['a.status','=' ,1];
  134. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  135. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  136. $query = $this->_query('activity_apply')
  137. ->alias('a')
  138. ->field('a.*,u.name user_name,u.headimg')
  139. ->where($where)
  140. ->leftJoin('store_member u','u.id = a.user_id')
  141. ->order('a.id desc')->page();
  142. $this->fetch();
  143. }
  144. /**
  145. * 上架
  146. * @auth true
  147. * @menu true
  148. * @throws \think\Exception
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. * @throws \think\exception\PDOException
  153. */
  154. public function up()
  155. {
  156. $this->_save($this->table, ['status' => '1']);
  157. }
  158. /**
  159. * 取消
  160. * @auth true
  161. * @menu true
  162. * @throws \think\Exception
  163. * @throws \think\db\exception\DataNotFoundException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws \think\exception\DbException
  166. * @throws \think\exception\PDOException
  167. */
  168. public function down()
  169. {
  170. $this->_save($this->table, ['status' => '2']);
  171. }
  172. public function export(){
  173. $id = $this->request->get('act_id');
  174. $name = $this->request->get('name');
  175. $phone = $this->request->get('phone');
  176. $this->title = '报名记录';
  177. $where = [];
  178. $where[]= ['a.act_id','=' ,$id];
  179. $where[]= ['a.is_deleted','=' ,0];
  180. $where[]= ['a.status','=' ,1];
  181. $where[]= ['a.status','=' ,1];
  182. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  183. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  184. $data =Db::name('activity_apply')
  185. ->alias('a')
  186. ->field('a.*,u.name user_name,u.headimg')
  187. ->where($where)
  188. ->leftJoin('store_member u','u.id = a.user_id')
  189. ->order('a.id desc')->select();
  190. if(empty($data)) $this->error('暂无可以导出的数据');
  191. foreach ($data as $k=>&$v) {
  192. }
  193. $field=array(
  194. 'A' => array('order_no', '订单号'),
  195. 'B' => array('name', '联系人'),
  196. 'C' => array('phone', '电话'),
  197. 'D' => array('money','订单金额'),
  198. 'E' => array('num','人数'),
  199. 'F' => array('email','邮箱'),
  200. 'G' => array('create_at', '时间'),
  201. );
  202. $this->phpExcelList($field,$data,'报名列表');
  203. }
  204. public function phpExcelList($field=[],$list=[],$title='文件'){
  205. $PHPExcel=new \PHPExcel();
  206. $PHPSheet=$PHPExcel->getActiveSheet();
  207. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  208. foreach($list as $key=>$value)
  209. {
  210. foreach($field as $k=>$v){
  211. if($key == 0){
  212. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  213. }
  214. $i=$key+2;
  215. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  216. }
  217. }
  218. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  219. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  220. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  221. header('Cache-Control: max-age=0'); //禁止缓存
  222. $PHPWriter->save("php://output"); //输出到浏览器
  223. }
  224. protected function _form_result(&$data)
  225. {
  226. $this->success('操作成功', 'javascript:history.back()');
  227. }
  228. }