// +---------------------------------------------------------------------- namespace app\admin\controller; use app\weixin\model\AccountLog; use app\weixin\model\Books; use app\weixin\model\UserRechargeLog; use app\weixin\model\Wechat; use cmf\controller\AdminBaseController; use think\Db; use think\db\Query; use app\admin\model\BooksModel; use app\weixin\service\Activity; use app\weixin\service\PRedis; /** * Class UserController * @package app\admin\controller * @adminMenuRoot( * 'name' => '管理组', * 'action' => 'default', * 'parent' => 'user/AdminIndex/default', * 'display'=> true, * 'order' => 10000, * 'icon' => '', * 'remark' => '管理组' * ) */ class BooksController extends AdminBaseController { /** * 活动报名名额显示列表 */ public function index() { /**搜索条件**/ $keyword = $this->request->param('keyword'); //$userEmail = trim($this->request->param('user_email')); // echo $keyword;die; $books = Db::name('books') ->alias('b') ->join('user u', 'b.uid = u.id', 'left') ->join('activity a', 'b.aid = a.id', 'left') ->where('b.aid', input('id')) ->where('b.status', 'neq', 1) ->where('b.status', 'neq', 4) ->where(function (Query $query) use ($keyword) { if ($keyword) { $query->where('u.user_nickname|b.book_num', 'like', "%$keyword%"); } }) ->field("a.title,u.user_nickname,u.id as uids,u.is_vip,b.*") ->order("b.id DESC") ->paginate(20, false, ['query' => request()->param()]); // echo Db::name('books')->getLastSql();die; //$books->appends(['nick_name' => $nick_name]); // 获取分页显示 $page = $books->render(); $this->assign("page", $page); $this->assign("books", !empty($books)? $books : null); return $this->fetch(); } /** * 所有互选情况 */ public function allhuxuan() { /**搜索条件**/ $nick_name = $this->request->param('nick_name'); $books = Db::name('user_heart_match') ->alias('hm') ->join('user u', 'hm.uid= u.id', 'left') ->join('books b', 'b.uid=hm.uid and b.aid=hm.aid', 'left') ->where(function ($query) use ($nick_name) { if ($nick_name) { return $query->where('u.user_nickname', 'like', "%{$nick_name}%"); } }) // ->where(function (Query $query) use ($nick_name) { // }) ->where(['b.status' => 3]) ->where('hm.aid', input('id')) ->field("hm.id,u.user_nickname,hm.uid,hm.match_uid,hm.heart_uids,hm.`status`,hm.match_at,b.book_num") ->order("hm.status ASC") ->paginate(20, false, ['query' => request()->param()]) ->each(function ($item, $key) { //显示编号 // $booksary=Db::name('books')->field('book_num') // ->where('uid','in',$item['match_uid']) // ->where('aid',input('id')) // ->where('status',3) // ->find(); $booksary = Db::name('books')->where(['aid' => input('id'), 'status' => 3]) ->where('uid', 'in', $item['match_uid']) ->orderField('uid', explode(',', $item['match_uid'])) ->column('book_num'); //echo Db::name('books')->getLastSql();die; $item['match_name'] = implode(',', $booksary); $bookNums = Db::name('books')->where(['aid' => input('id'), 'status' => 3]) ->where('uid', 'in', $item['heart_uids']) ->orderField('uid', explode(',', $item['heart_uids'])) ->column('book_num'); // echo Db::name('books')->getLastSql();die; // var_dump($bookNums);die; $item['heart_name'] = implode(',', $bookNums); //选我的人 /*$htme=''; $htmeary=Db::name('user_heart_match') ->alias('hm') ->field('b.book_num') ->join('books b','hm.uid=b.uid') ->where('find_in_set(:uid,hm.heart_uids)',['uid'=>$item['uid']]) ->where('hm.aid',input('id')) ->where('b.status',3) ->select(); // echo Db::name('user_heart_match')->getLastSql();die; foreach ($htmeary as $key => $value) { if($keyalias('hm') ->field('b.book_num') ->join('books b', 'hm.uid=b.uid and b.aid=hm.aid') ->where('find_in_set(:uid,hm.heart_uids)', ['uid' => $item['uid']]) ->where('hm.aid', input('id')) ->where('b.status', 3) ->orderRaw("find_in_set('" . $item['uid'] . "',hm.heart_uids)") ->column('b.book_num'); // echo Db::name('user_heart_match')->getLastSql();die; $item['chooseme'] = $chooseArr ? implode(',', $chooseArr) : ''; return $item; }); // echo Db::name('user_heart_match')->getLastSql(); // 获取分页显示 $page = $books->render(); //echo Db::name('user_heart_match')->getLastSql();die; $this->assign("page", $page); $this->assign("books", $books); return $this->fetch(); } //增加活动 public function add() { return $this->fetch('add'); } /** * 活动添加提交 */ public function addPost() { if ($this->request->isPost()) { $data = $this->request->param(); $activityModel = new ActivityModel(); $result = $this->validate($data, 'Activity'); if ($result !== true) { $this->error($result); } $data['starttime'] = strtotime($data['starttime']); $data['endtime'] = strtotime($data['endtime']); $data['addtime'] = time(); $activityModel->allowField(true)->save($data); $this->success("添加成功!", url("Link/index")); } } /** * 活动编辑 */ public function edit() { $id = $this->request->param('id', 0, 'intval'); $ActivityModel = new ActivityModel(); $activity = $ActivityModel->get($id); $this->assign('activity', $activity); return $this->fetch(); } /** * 活动编辑提交 */ public function editPost() { $data = $this->request->param(); //处理开始时间和结束时间,转换为时间戳。 if (strtotime($data['starttime']) > strtotime($data['endtime'])) { $this->error('活动结束时间不能大于开始时间!'); } $data['starttime'] = strtotime($data['starttime']); //开始时间 $data['endtime'] = strtotime($data['endtime']); //结束时间 $activityModel = new ActivityModel(); $result = $this->validate($data, 'Activity'); if ($result !== true) { $this->error($result); } $activityModel->allowField(true)->isUpdate(true)->save($data); $this->success("保存成功!", url("Activity/index")); } /** * 删除活动编辑 * */ public function delete() { $id = $this->request->param('id', 0, 'intval'); ActivityModel::destroy($id); $this->success("删除成功!", url("Activity/index")); } /** * 报名列表退款 * */ public function refund() { $id = $this->request->param('id', 0, 'intval'); $aid = $this->request->param('aid', 0, 'intval'); $booksModel = new BooksModel(); $data['id'] = $id; $data['status'] = 5; //表示已退款 //代表线上的活动id,为单身推荐的活动下的用户 if ($aid == 15) { //变更is_tuijian=0 $result = Db::name('user')->where('id', input('uid'))->update(['is_tuijian' => 0]); if ($result) { $booksModel->isUpdate(true)->save($data); $this->success("退款成功!", url("Books/index", ['id' => input('aid')])); } else { $this->error("退款失败!", url("Books/index", ['id' => input('aid')])); } } else { $booksModel->isUpdate(true)->save($data); $this->success("退款成功!", url("Books/index", ['id' => input('aid')])); } } /** * 活动退款打款处理 * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function refundPay() { $id = $this->request->param('id', 0, 'intval'); $money = $this->request->param('money', 0, 'float'); $type = $this->request->param('type', 0, 'float'); $remark = $this->request->param('remark', ''); if ($money <= 0) { $this->error("请填写退款金额"); } $info = Books::alias('b') ->leftJoin('activity a', 'a.id=b.aid') ->leftJoin('user u', 'u.id=b.uid') ->where(['b.id' => $id]) ->field('u.id as uid, u.real_name,u.user_nickname,u.openid,b.id,b.order_sn,u.balance,b.money,b.credit,a.title,b.refund_credit,b.is_market,b.status') ->find(); if (empty($info)) { $this->error("报名信息错误或不存在"); } $status = isset($info['status']) ? $info['status'] : 0; $isMarket = isset($info['is_market']) ? $info['is_market'] : 0; if ($type == 1 && $isMarket == 1) { $this->error('报名订单已结算收益,无法全部退款'); } $bMoney = isset($info['money']) ? floatval($info['money']) : 0.00; $bCredit = isset($info['credit']) ? floatval($info['credit']) : 0.00; $userId = isset($info['uid']) ? intval($info['uid']) : 0; // 退全款 if ($type == 1) { if (!in_array($status, [2, 3])) { $this->error($status == 1 ? '报名订单未支付' : ($status == 4 ? '报名订单已取消' : '报名订单已退款处理')); } if ($money > $bMoney) { $this->error("退款金额超出支付金额"); } $remark = $remark ? $remark : "平台活动报名费用退款{$money}元,报名单号:{$info['order_sn']}"; } else if ($type == 2) { if($bCredit<=0){ $this->error("没有可退款的守时金"); } if ($money > $bCredit) { $this->error("退款金额超出支付金额"); } $refundCredit = isset($info['refund_credit']) ? $info['refund_credit'] : 0; if ($refundCredit) { $this->error("该报名订单守时金已退款,无需再处理"); } $remark = $remark ? $remark : "平台活动守时金退款{$money}元,报名单号:{$info['order_sn']}"; } else { $this->error("抱歉当前退款类型不支持"); } $orderSn = makeTradeNo('RF', $userId); $log = [ 'order_sn' => $orderSn, 'money' => $money, 'type' => 6, 'pay_type' => 2, 'pay_money' => $money, 'user_id' => $userId, 'balance' => $info['balance'], 'remark' => $remark, 'created_at' => date('Y-m-d H:i:s'), 'status' => 1, ]; $orderId = UserRechargeLog::insertGetId($log); if (!$orderId) { $this->error("创建退款订单失败"); } // 打款 Db::startTrans(); $order = array( 'orderNo' => $log['order_sn'], //订单号 'openid' => $info['openid'], 'amount' => $money, //实际支付金额 'desc' => $remark, //描述 ); $result = Wechat::transferOrder($order); PRedis::set("cache:bookRefund:u_{$userId}_{$type}", ['info'=> $info,'result'=> $result], 3600); $resultCode = isset($result['result_code'])? strtoupper($result['result_code']) : ''; $message = isset($result['message'])? $result['message'] : ''; if ($resultCode == "SUCCESS") { $accountData = [ 'type' => 9, //提现 'account_type' => 4, //余额类型 'change_type' => 1, //收入 'user_id' => $userId, 'money' => $money, 'balance' => $info['balance'], 'created_at' => date('Y-m-d H:i:s'), 'remark' => $remark, ]; if (!AccountLog::insertGetId($accountData)) { Db::rollback(); $this->error("账户明细处理失败"); } // 更新退款订单状态 UserRechargeLog::where(['id' => $orderId, 'type' => 6])->update(['status' => 2]); $data = ['remark' => $remark]; if($type == 1){ $data['status']= 5; }else{ $data['refund_credit'] = 1; } Books::where(['id' => $id])->update($data); Db::commit(); // 发送消息 $checkTime = date('Y.m.d H:i'); $title = isset($info['title']) ? '[' . $info['title'] . ']' : ''; if ($info['openid']) { $name = $type == 1 ? '报名费已退还' : '守时金费用已退还'; $params = [ 'title' => "尊敬的用户,您报名参加的活动{$title}{$name},请您查收\n\n报名单号:\t{$info['order_sn']}", 'remark' => '感谢您对拾光的信任,点击查看退款明细', 'type' => 'refund', 'keywords' => [ 'keyword1' => [ 'value' => $log['order_sn'], 'color' => '#173177' ], 'keyword2' => [ 'value' => $type == 1? '活动报名费退款' : '守时金退款', 'color' => '#173177' ], 'keyword3' => [ 'value' => $money, 'color' => '#173177' ], 'keyword4' => [ 'value' => $remark, 'color' => '#173177' ], ], 'url' => url('/weixin/account/index', '', '', true), ]; wechat::sendTplMsg($info['openid'], $params); } $this->success('活动退款处理成功'); } else { $this->error($message? $message : "打款处理失败,请核对订单后重试"); } } /** * 报名列表审核 * */ public function shenhe() { $id = $this->request->param('id', 0, 'intval'); //echo $id;die; //开始进行调用发送 $result = Activity::bookConfirm($id, 3); if (is_array($result)) { $this->success("审核报名成功!", url("Books/index", ['id' => input('aid')])); } else { $this->success("审核报名成功!", url("Books/index", ['id' => input('aid')])); } } /** * 审核通过和审核不通过的弹框 */ public function goconfrom() { $this->assign("id", input('id')); return $this->fetch('confrom'); } /** * 审核通过和审核不通过的弹框操作 */ public function doconfrom() { $param = $this->request->param(); if (isset($param['yes']) && isset($param['id'])) { //开始进行调用成功发送 $result = Activity::bookConfirm($param['id'], 3); if (is_array($result)) { echo 1; die; } else { echo 0; die; } } if (isset($param['no']) && isset($param['id'])) { //开始进行调用审核失败发送 $result = Activity::bookConfirm($param['id'], 5, $param['noremark']); if (is_array($result)) { echo 1; die; } else { echo 0; die; } } } /** * 报名列表签到弹框 * */ public function gosignin() { $id = input('id'); // $account_log = Db::name('account_log') // ->alias('alog') // ->join('user u','alog.user_id=u.id','left') // ->where(function (Query $query) use ($id) { // if ($id) { // $query->where('alog.id', $id); // } // }) // ->field('alog.*,u.user_nickname') // ->find(); $this->assign("id", $id); return $this->fetch('signin'); } /** * 报名列表正常签到 * */ public function zhengchang() { $id = $this->request->param('id', 0, 'intval'); $booksModel = new BooksModel(); $data['id'] = $id; $data['is_signin'] = 2; //表示已正常签到 $booksModel->isUpdate(true)->save($data); echo 1; die; // $this->success("正常签到成功!", url("Books/index",['id'=>$id])); } /** * 报名列表迟到签到 * */ public function chidao() { $id = $this->request->param('id', 0, 'intval'); $booksModel = new BooksModel(); $data['id'] = $id; $data['is_signin'] = 3; //表示已迟到签到 $booksModel->isUpdate(true)->save($data); echo 1; die; // $this->success("迟到签到成功!", url("Books/index",['id'=>$id])); } /* *批量签到 */ public function allqiandao() { $params = input(); $id = isset($params['id']) ? $params['id'] : 0; if (empty($id)) { $this->error('请先选择操作项!'); } $id = is_array($id) ? implode(',', $id) : $id; $data['is_signin'] = 2; //表示已正常签到 //Db::name('books')->where(['id' => ['in', $id]])->update($data); // $this->success($id); if (Db::name('books')->where('id', 'in', $id)->update($data)) { $this->success("批量签到成功!", ''); } else { $this->error('批量签到失败!'); } } /** *时间提醒显示 * */ public function reminder() { $actresult = Db::name('activity') ->field('alert_time1,alert_remark1,alert_time2,alert_remark2') ->where('id', input('id')) ->find(); //显示是否进行了发送 第一次的显示 $aid = input('id'); $type = 2; $result1 = PRedis::get('activity:pushStatus:' . $aid . '_' . $type); if (!empty($result1)) { //代表已经发送了,输出1 $result1 = 1; } else { $result1 = 0; } //显示是否进行了发送 第二次的显示 $aid = input('id'); $type = 1; $result2 = PRedis::get('activity:pushStatus:' . $aid . '_' . $type); if (!empty($result2)) { //代表已经发送了,输出1 $result2 = 1; } else { $result2 = 0; } $this->assign('showf', $result1); $this->assign('showt', $result2); $this->assign('id', input('id')); $this->assign('actresult', $actresult); return $this->fetch('reminder'); } /** * 提醒时间的保存1 */ public function savereminder1() { // input('id'); $result = Db::name('activity')->where('id', input('id'))->update(['alert_time2' => input('alert_time1'), 'alert_remark2' => input('alert_remark1')]); echo $result; die; } /** * 提醒时间的保存2 */ public function savereminder2() { // input('id'); $result = Db::name('activity')->where('id', input('id'))->update(['alert_time1' => input('alert_time2'), 'alert_remark1' => input('alert_remark2')]); echo $result; die; } /** * 第一次提醒时间发送 (代表前一天) */ public function sendreminderfirst() { $aid = input('id'); $type = 2; //开始进行调用发送 $result = Activity::sendMessage($aid, $type); if (is_array($result)) { echo 1; die; //代表发送成功 } else { echo lang($result); die; //代表发送失败 } } /** * 第二次提醒时间发送(代表前几个小时) */ public function sendremindertwo() { $aid = input('id'); $type = 1; //开始进行调用发送 $result = Activity::sendMessage($aid, $type); if (is_array($result)) { echo 1; die; //代表发送成功 } else { echo lang($result); die; //代表发送失败 } } /** *互动匹配 * */ public function matching() { $id = input('id'); $result = Activity::heartMatch($id); //调用互动匹配 if (is_array($result)) { $this->success('互动匹配发送成功'); } else { $this->error(lang($result)); } } /** *重新匹配 * */ public function twomatching() { $id = input('id'); // 更新匹配状态 $result0 = Db::name('activity')->where('id', $id)->update(['is_match' => 2]); $result = Activity::heartMatch($id); //调用互动匹配 if (is_array($result)) { $this->success('互动匹配发送成功'); } else { $this->error(lang($result)); } } /** * 删除报名人,不是2,3状态的时候变成4 */ public function delbooks() { $aid = $this->request->param('aid', 0, 'intval'); //代表线上的活动id,为单身推荐的活动下的用户 if ($aid == 15) { //变更is_tuijian=0 $result = Db::name('user')->where('id', input('uid'))->update(['is_tuijian' => 0]); if ($result) { Db::name('books')->where('id', input('id'))->update(['status' => 4]); $this->success("删除成功!", url("Books/index", ['id' => input('aid')])); } else { $this->error("删除失败!", url("Books/index", ['id' => input('aid')])); } } else { Db::name('books')->where('id', input('id'))->update(['status' => 4]); $this->success("删除成功!", url("Books/index", ['id' => input('aid')])); } } /* * PHPExcel导出报名用户列表 */ public function export() { $PHPExcel = new \PHPExcel(); $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象 $PHPSheet->setTitle('报名人员列表'); /* 给当前sheet表设置名称及表头 */ $PHPSheet->setCellValue('A1', '编号'); $PHPSheet->setCellValue('B1', '姓名'); $PHPSheet->setCellValue('C1', '年份'); $PHPSheet->setCellValue('D1', '身高'); $PHPSheet->setCellValue('E1', '学历'); $PHPSheet->setCellValue('F1', '职位'); $PHPSheet->setCellValue('G1', '手机号'); $PHPSheet->setCellValue('H1', '微信号'); $where = array('u.user_type' => 2); $field = 'b.book_num,`u`.`user_nickname`,u.real_name,YEAR(from_unixtime(`u`.`birthday`)) as birthday,up.height,up.education,up.occupation,u.mobile,up.wechat_code '; $dataList = Db::name('books')->alias('b') ->join('user u', 'b.uid=u.id', 'left') ->join('user_profile up', 'up.userid=u.id', 'left') ->where($where) ->where('b.status', 'neq', 1) ->where('b.status', 'neq', 4) ->where('b.status', 'neq', 5) ->where('b.aid', input('aid')) ->field($field) ->order("b.id DESC") ->select(); //echo Db::name('user')->getLastSql();die; foreach ($dataList as $key => $item) { $key += 2; $PHPSheet->setCellValue('A' . $key, $item['book_num']); $PHPSheet->setCellValue('B' . $key, $this->guolv($this->filterEmoji($item['user_nickname']))); $PHPSheet->setCellValue('C' . $key, $item['birthday']); $PHPSheet->setCellValue('D' . $key, $item['height']); $PHPSheet->setCellValue('E' . $key, $this->xueli($item['education'])); $PHPSheet->setCellValue('F' . $key, $item['occupation']); $PHPSheet->setCellValue('G' . $key, $item['mobile']); $PHPSheet->setCellValue('H' . $key, $item['wechat_code']); } $filename = "活动报名列表" . date('YmdHis') . ".xls"; header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename=' . $filename . ''); header("Content-Transfer-Encoding:binary"); $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5'); $PHPWriter->save('php://output'); } /* * 过滤昵称的特效字符 */ public function filterEmoji($str) { $str = preg_replace_callback( '/./u', function (array $match) { return strlen($match[0]) >= 4 ? '' : $match[0]; }, $str); return $str; } //通过学历数号得到学历 学历:1-大专,2-本科,3-硕士,4-博士 public function xueli($str) { switch ($str) { case '1': return '大专'; break; case '2': return '本科'; break; case '3': return '硕士'; break; case '4': return '博士'; break; } } //过滤字符串 public function guolv($str){ $str = str_replace('=', '等', $str); return $str; } //过滤字符串 public function guolv2($str){ $str = str_replace('`', '', $str); $str = str_replace('·', '', $str); $str = str_replace('~', '', $str); $str = str_replace('!', '', $str); $str = str_replace('!', '', $str); $str = str_replace('@', '', $str); $str = str_replace('#', '', $str); $str = str_replace('$', '', $str); $str = str_replace('¥', '', $str); $str = str_replace('%', '', $str); $str = str_replace('^', '', $str); $str = str_replace('……', '', $str); $str = str_replace('&', '', $str); $str = str_replace('*', '', $str); $str = str_replace('(', '', $str); $str = str_replace(')', '', $str); $str = str_replace('(', '', $str); $str = str_replace(')', '', $str); $str = str_replace('-', '', $str); $str = str_replace('_', '', $str); $str = str_replace('——', '', $str); $str = str_replace('+', '', $str); $str = str_replace('=', '', $str); $str = str_replace('|', '', $str); $str = str_replace('\\', '', $str); $str = str_replace('[', '', $str); $str = str_replace(']', '', $str); $str = str_replace('【', '', $str); $str = str_replace('】', '', $str); $str = str_replace('{', '', $str); $str = str_replace('}', '', $str); $str = str_replace(';', '', $str); $str = str_replace(';', '', $str); $str = str_replace(':', '', $str); $str = str_replace(':', '', $str); $str = str_replace('\'', '', $str); $str = str_replace('"', '', $str); $str = str_replace('“', '', $str); $str = str_replace('”', '', $str); $str = str_replace(',', '', $str); $str = str_replace(',', '', $str); $str = str_replace('<', '', $str); $str = str_replace('>', '', $str); $str = str_replace('《', '', $str); $str = str_replace('》', '', $str); $str = str_replace('.', '', $str); $str = str_replace('。', '', $str); $str = str_replace('/', '', $str); $str = str_replace('、', '', $str); $str = str_replace('?', '', $str); $str = str_replace('?', '', $str); return trim($str); } }