1, 'is_test'=> 0]; /*$type = isset($params['type']) ? intval($params['type']) : 0; if ($type) { $where['type'] = $type; }*/ $closeConfig = cmf_get_option('close_config'); $closeConfig = $closeConfig? $closeConfig : config('weixin.site'); $accessIp = isset($closeConfig['access_ip'])? $closeConfig['access_ip'] : ''; $accessIp = $accessIp? explode(',', $accessIp) : []; if($accessIp && !in_array(get_client_ip(),$accessIp)){ unset($where['is_test']); } $order = $order ? $order : 'is_top desc,is_sort desc,starttime desc,id desc'; $field = $field ? $field : 'id,title,cover_img as thumb,price,act_nums,addtime,starttime,address,endtime'; return ActivityModel::where($where) ->where('addtime','<=',time()) ->field($field) ->order($order) ->paginate($pageSize) ->each(function ($item, $k) { $startTime = isset($item['starttime']) ? $item['starttime'] : 0; $endTime = isset($item['endtime']) ? $item['endtime'] : 0; $addTime = isset($item['addtime']) ? $item['addtime'] : 0; if ($startTime <= time() && $endTime >= time()) { $item['act_status'] = 1; // 进行中 } else if ($endTime < time()) { $item['act_status'] = 2; // 已结束 } else if ($startTime > time()) { $item['act_status'] = 0; // 未开始 } $item['join_at'] = ''; if(date('Y-m-d',$endTime) == date('Y-m-d', $startTime)){ $item['join_at'] = date('Y.m.d H:i',$startTime).'~'.date('H:i', $endTime); } $item['thumb'] = isset($item['thumb']) ? cmf_get_image_preview_url($item['thumb']) : ''; $item['publish_at'] = $addTime ? date('Y-m-d H:i:s', $addTime) : date('Y-m-d H:i:s'); $item['start_at'] = $startTime ? date('Y.m.d H:i', $startTime) : date('Y.m.d H:i'); $item['end_at'] = $endTime ? date('Y.m.d H:i', $endTime) : date('Y.m.d H:i'); // 报名人列表和人数 $showNum = config('activity.showNum'); $showNum = $showNum ? $showNum : 6; $id = isset($item['id']) ? $item['id'] : 0; $result = Books::getBookList($id, $showNum); $item['book_num'] = isset($result['total']) ? intval($result['total']) : 0; $item['book_list'] = isset($result['data']) ? $result['data'] : []; return $item; }); } /** * 获取用户活动 * @param $params 参数 * @param int $pageSize 分页大小 * @param string $field 字段 * @param string $order 排序 * @return $this * @throws \think\exception\DbException */ public static function getMemberActivityList($params, $pageSize = 15, $field = '', $order = '') { $order = $order ? $order : 'a.starttime desc,a.id desc'; $userId = isset($params['user_id']) ? intval($params['user_id']) : 0; $type = isset($params['type']) ? intval($params['type']) : 0; $order = $type == 1 ? $order : 'b.book_at desc,b.id desc'; $where = ['a.status' => 1,'b.status'=> 3,'b.uid'=> $userId]; $field = $field ? $field : 'a.id,a.title,a.cover_img as thumb,b.status,b.is_signin,a.price,a.act_nums,a.addtime,a.starttime,a.address,a.endtime,b.id as book_id'; $dataList = ActivityModel::alias('a') ->join('books b', 'a.id=b.aid', 'inner') ->where('addtime','<=',time()) ->where($where) ->where(function($query) use($type){ if ($type == 2) { return $query->where('b.is_signin','=', 1); } else if ($type == 3) { return $query->where('b.is_signin','in', [2,3]); } }) ->field($field) ->order($order) ->group('a.id') ->paginate($pageSize) ->each(function ($item, $k) { $startTime = isset($item['starttime']) ? $item['starttime'] : 0; $endTime = isset($item['endtime']) ? $item['endtime'] : 0; $addTime = isset($item['addtime']) ? $item['addtime'] : 0; if ($startTime <= time() && $endTime >= time()) { $item['act_status'] = 1; // 进行中 } else if ($endTime < time()) { $item['act_status'] = 2; // 已结束 } else if ($startTime > time()) { $item['act_status'] = 0; // 未开始 } $item['join_at'] = ''; if(date('Y-m-d',$endTime) == date('Y-m-d', $startTime)){ $item['join_at'] = date('Y.m.d H:i',$startTime).'~'.date('H:i', $endTime); } $item['thumb'] = isset($item['thumb']) ? cmf_get_image_preview_url($item['thumb']) : ''; $item['publish_at'] = $addTime ? date('Y-m-d H:i:s', $addTime) : date('Y-m-d H:i:s'); $item['start_at'] = $startTime ? date('Y-m-d H:i', $startTime) : date('Y-m-d H:i'); $item['end_at'] = $endTime ? date('Y-m-d H:i', $endTime) : date('Y-m-d H:i'); // 报名人列表和人数 $showNum = config('activity.showNum'); $showNum = $showNum ? $showNum : 6; $id = isset($item['id']) ? $item['id'] : 0; $result = Books::getBookList($id, $showNum); $item['book_num'] = isset($result['total']) ? intval($result['total']) : 0; $item['book_list'] = isset($result['data']) ? $result['data'] : []; return $item; }); return $dataList; } /** * 获取详情 * @param $where * @param string $field * @return array|null|\PDOStatement|string|\think\Model */ public static function getInfo($where, $field = '') { $field = $field ? $field : 'id,title,cover_img as thumb,price,market_price,market_time,member_discount,credit,act_nums,url,addtime,starttime,endtime,description,address'; $info = ActivityModel::where($where) ->where('addtime','<=',time()) ->where('id', '>', 0) ->field($field) ->find(); if ($info) { $addTime = isset($info['addtime']) ? $info['addtime'] : 0; $startTime = isset($info['starttime']) ? $info['starttime'] : 0; $endTime = isset($info['endtime']) ? $info['endtime'] : 0; if ($startTime <= time() && $endTime >= time()) { $info['act_status'] = 1; // 进行中 } else if ($endTime < time()) { $info['act_status'] = 2; // 已结束 } else if ($startTime > time()) { $info['act_status'] = 0; // 未开始 } $info['join_at'] = ''; if(date('Y-m-d',$endTime) == date('Y-m-d', $startTime)){ $info['join_at'] = date('Y.m.d H:i',$startTime).'~'.date('H:i', $endTime); } if($startTime==0 && $endTime == 0){ $info['act_status'] = 1; // 进行中 } $isHide = floor((time()-$endTime)/86400)>=3? 1 : 0; $info['publish_at'] = $addTime ? date('Y-m-d H:i:s', $addTime) : date('Y-m-d H:i:s'); $info['publish_format_at'] = getFormatTime($info['publish_at']); $info['start_at'] = $startTime ? date('Y.m.d H:i', $startTime) : date('Y.m.d H:i'); $info['end_at'] = $endTime ? date('Y.m.d H:i', $endTime) : date('Y.m.d H:i'); $info['thumb'] = isset($info['thumb']) ? cmf_get_image_preview_url($info['thumb']) : ''; $info['is_hide'] = $isHide; if (isset($info['content'])) { $info['content'] = htmlspecialchars_decode($info['content']); } } return $info; } /** * 报名 * @param $userId * @param $aid * @return array|int */ public static function catchBook($userId, $aid) { // 验证活动是否存在 $field = 'id,starttime,endtime,price,market_price,market_time,member_discount,addtime,is_top,credit,act_nums'; $activityInfo = Activity::getInfo(['id' => $aid, 'status' => 1], $field); if (empty($activityInfo)) { return 5001; } // 验证报名时间 $startTime = isset($activityInfo['starttime']) ? intval($activityInfo['starttime']) : 0; $endTime = isset($activityInfo['endtime']) ? intval($activityInfo['endtime']) : 0; $addTime = isset($activityInfo['addtime']) ? intval($activityInfo['addtime']) : 0; $isTop = isset($activityInfo['is_top']) ? intval($activityInfo['is_top']) : 0; if($addTime > time()){ return 5000; } if ($endTime && time() > $endTime) { return 5002; } /*if ($startTime && time() < $startTime) { return 5003; }*/ // 活动截止前多久不允许报名 $siteInfo = cmf_get_site_info(); $bookTime = isset($siteInfo['book_time']) ? intval($siteInfo['book_time']) : 0; if ($isTop==0 && $startTime && $bookTime) { if(time() > $startTime){ return 5026; } if(time() + $bookTime*3600 > $startTime){ return lang('book_end', ['time' => $bookTime]); } } // 验证当前用户是否已经报名 if (Books::checkUserBook($userId, $aid)) { return 5004; } // 验证操作频繁 $lockCache = "books:lock:" . $userId . "_" . $aid; if (PRedis::get($lockCache)) { return 1013; } // 验证报名人数 $sex = Member::where(['id' => $userId])->value('sex'); $limitNum = isset($activityInfo['act_nums']) ? intval($activityInfo['act_nums']) : 0; $limitNum = floor($limitNum/2); $bookNum = Books::getBookCount($aid, $sex, [2,3]); // 已报名人数 if ($limitNum && $bookNum >= $limitNum) { $sexText = $isTop? '' : ($sex==1? '男生' : '女生'); return lang('book_limit',['sex'=> $sexText]); } // 验证价格 $price = isset($activityInfo['price']) ? floatval($activityInfo['price']) : 0; // 活动价格 $marketPrice = isset($activityInfo['market_price']) ? floatval($activityInfo['market_price']) : 0; // 早市价 $marketTime = isset($activityInfo['market_time']) ? intval($activityInfo['market_time']) : 0; $payPrice = $marketTime >= time() && $marketPrice>0? $marketPrice : $price; $credit = isset($activityInfo['credit']) ? floatval($activityInfo['credit']) : 0; if (empty($payPrice)) { return 1012; } // 验证VIP折扣价 $memberInfo = Member::where(['id' => $userId])->field('vip_auth,vip_expire')->find(); $vipAuth = isset($memberInfo['vip_auth']) ? intval($memberInfo['vip_auth']) : 0; $vipExpire = isset($memberInfo['vip_expire']) ? intval($memberInfo['vip_expire']) : 0; // VIP有效期内 $oldPrice = $payPrice; $memberDiscount = isset($activityInfo['member_discount']) ? floatval($activityInfo['member_discount']) : 0; if($vipAuth && $vipExpire > time() && $memberDiscount && $memberDiscount < 1){ $payPrice = floatval($memberDiscount * $payPrice); } // 报名数据 $orderSn = makeTradeNo('BK', $userId); $bookData = [ 'book_num' => '', 'uid' => $userId, 'aid' => $aid, 'order_sn' => $orderSn, 'money' => $payPrice, 'credit' => $credit, 'created_at' => date('Y-m-d H:i:s'), 'remark' => "活动报名:原价{$oldPrice},折扣{$memberDiscount}折", 'status' => 1, ]; // 写入记录 Db::startTrans(); $logData = ['info' => $activityInfo, 'bookData' => $bookData, 'date' => date('Y-m-d H:i:s')]; PRedis::set($lockCache, $logData, 3); PRedis::set('books:logs:' . $userId . '_' . $aid, $logData, 600); if (!$bookId = Books::insertGetId($bookData)) { PRedis::del($lockCache); Db::rollback(); return 5006; } // 用户操作记录 UserLog::saveLog(['user_id' => $userId, 'type' => 4, 'content' => '报名下单:' . $orderSn]); Db::commit(); PRedis::del($lockCache); return ['id' => $bookId]; } /** * 报名审核 * @param $bookId 报名记录ID * @param $checkStatus 审核状态:3-审核成功,5-审核失败退款 * @param $failRemark 审核失败原因 * @return int */ public static function bookConfirm($bookId, $checkStatus, $failRemark='') { try { $bookInfo = Books::getInfo(['id' => $bookId]); $status = isset($bookInfo['status']) ? intval($bookInfo['status']) : 0; $userId = isset($bookInfo['uid']) ? intval($bookInfo['uid']) : 0; $aid = isset($bookInfo['aid']) ? intval($bookInfo['aid']) : 0; if (empty($userId) || empty($aid)) { return 1012; } if ($status != 2) { return 5016; } if(!in_array($checkStatus, [3,5])){ return 2129; } // 审核数据更新 $bookNo = ''; $updateData = ['status' => $checkStatus, 'remark' => $checkStatus==3? '审核成功':'审核失败']; if($checkStatus == 3){ $sex = Member::where(['id' => $userId])->value('sex'); $prefix = $sex == 1 ? 'G' : ($sex == 2 ? 'M' : ''); $bookNo = Books::makeBookNo($aid, $prefix, $sex); $updateData['book_num'] = $bookNo; $updateData['book_num_val'] = str_replace(['M','G'],'', $bookNo); } $res = Books::saveData(['id' => $bookId], $updateData); if ($res) { // 更新加入单身推荐 if($aid == 15 && $checkStatus == 3){ Member::saveData(['id'=> $userId],['is_tuijian'=> 1,'tuijian_time'=> date('Y-m-d H:i:s')]); } $memberInfo = Member::getInfo(['id' => $userId]); $openid = isset($memberInfo['openid']) ? $memberInfo['openid'] : ''; $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : ''; // 发送消息 if ($openid) { $activityInfo = ActivityModel::where(['id' => $aid]) ->field('id,starttime,title,join_address') ->find(); $title = isset($activityInfo['title']) ? trim($activityInfo['title']) : '无'; $address = isset($activityInfo['join_address']) ? trim($activityInfo['join_address']) : '无'; $startTime = isset($activityInfo['starttime']) ? intval($activityInfo['starttime']) : 0; $startTime = $startTime ? date('Y.m.d H:i', $startTime) : '详情查看'; $startTime = $aid==15? date('Y.m.d H:i') : $startTime; $orderSn = isset($bookInfo['order_sn']) ? trim($bookInfo['order_sn']) : ''; $money = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 0; $credit = isset($bookInfo['credit']) ? floatval($bookInfo['credit']) : 0; if($checkStatus==3){ $url = $aid==15? url('/weixin/activity/book?id=' . $aid, '', '', true) : url('/weixin/activity/bookdetail?id=' . $aid, '', '', true); $params = [ 'title' => "恭喜!您报名的活动已经通过审核!请提前安排好时间,准时参加!风雨无阻,真诚第一\n\n姓名:\t{$nickname}(昵称)\n\n支付金额:\t{$money}\n\n报名编号:\t{$bookNo}\n\n当前进度:\t待参加", 'remark' => "亲,记得安排好时间哦!", 'type' => 'book_confirm', 'keywords' => [ 'keyword1' => [ 'value' => $title, 'color' => '#173177', ], 'keyword2' => [ 'value' => $startTime, 'color' => '#173177', ], 'keyword3' => [ 'value' => $address, 'color' => '#173177', ], /* 'keyword4' => [ 'value' => "其中支付守时金{$credit}元,请准时签到,否则不予退还", 'color' => '#173177', ],*/ ], 'url' => $url, ]; }else{ $params = [ 'title' => ($failRemark? $failRemark : '报名审核失败')."\n\n姓名:\t{$nickname}(昵称)\n\n支付金额:\t{$money}", 'remark' => "转发积姻缘,把活动分享给身边的朋友,一起来脱单哦!", 'type' => 'book', 'keywords' => [ 'keyword1' => [ 'value' => $title, 'color' => '#173177', ], 'keyword2' => [ 'value' => $startTime, 'color' => '#173177', ], 'keyword3' => [ 'value' => '审核失败', 'color' => '#173177', ], 'keyword4' => [ 'value' => "...", 'color' => '#173177', ], ], 'url' => url('/weixin/activity/book?id=' . $aid, '', '', true), ]; } PRedis::set('payments:books:confirmMessage_' . $orderSn, ['book' => $bookInfo, 'params' => $params], 600); Wechat::sendTplMsg($openid, $params); } return ['id' => $bookId]; } else { return 5015; } } catch (\Exception $exception){ return 1015; } } /** * 互选匹配 * @param $aid 活动ID * @return int */ public static function heartMatch($aid) { try { // 验证活动是否有效 $activityInfo = ActivityModel::where(['id' => $aid, 'status' => 1]) ->field('id,starttime,title,address,status,is_match') ->find(); if (empty($activityInfo)) { return 5001; } // 验证状态和匹配状态 $isMatch = isset($activityInfo['is_match']) ? intval($activityInfo['is_match']) : 0; if ($isMatch == 1) { return 6002; } // 活动开始时间 $startTime = isset($activityInfo['starttime']) ? intval($activityInfo['starttime']) : 0; if (time() < $startTime) { return 5003; } // 互选模式匹配 $siteInfo = $siteInfo = cmf_get_site_info(); $contactType = isset($siteInfo['contact_type']) ? $siteInfo['contact_type'] : 1; if($contactType == 3){ // 更新匹配状态 ActivityModel::where(['id'=> $aid])->update(['is_match'=> 1]); // 获取用户列表 // 处理互选 return ['result'=> $aid]; } // 获取匹配互选列表 $matchList = Books::getMatchUserList($aid); if (empty($matchList)) { return 6003; } // 更新匹配状态 ActivityModel::where(['id'=> $aid])->update(['is_match'=> 1]); // 用户匹配列表处理 $results = []; PRedis::set('matchs:activity_' . $aid.':users', $matchList, 600); foreach ($matchList as $k => $userData) { $userId = isset($userData['uid']) ? intval($userData['uid']) : 0; $heartUids = isset($userData['heart_uids'])? $userData['heart_uids'] : ''; $heartUids = $heartUids? explode(',', $heartUids) : []; if(empty($heartUids)){ continue; } foreach ($heartUids as $kk => $uid) { $matchData = isset($matchList[$uid]) ? $matchList[$uid] : []; $uids = isset($matchData['heart_uids']) ? $matchData['heart_uids'] : ''; $uids = $uids? explode(',', $uids) : []; $index = array_search($userId, $uids); if ($index !== false && $index >= 0) { $dvalue = abs($kk - $index)+1; if(($kk + 1)>= ($index + 1)){ $key = ($kk + 1) . '_' . $dvalue . '_' . ($index + 1); }else{ $key = ($index + 1) . '_' . $dvalue . '_' . ($kk + 1); } $results[$key] = isset($results[$key]) ? $results[$key] : []; $results[$key][] = [$userData, $matchData]; } } } ksort($results, 1); $successArr = $successId = []; foreach ($results as $result) { foreach ($result as $val) { // 匹配用户数据 $userData = isset($val[0]) ? $val[0] : 0; $matchData = isset($val[1]) ? $val[1] : 0; $mid = isset($userData['id'])? intval($userData['id']) : 0; $userId = isset($userData['uid'])? intval($userData['uid']) : 0; $matchId = isset($matchData['id']) ? intval($matchData['id']) : 0; $matchUid = isset($matchData['uid']) ? intval($matchData['uid']) : 0; // 验证用户是否已经被匹配,若是则不需要再次匹配 if (!$userId || !$matchUid || PRedis::get('matchs:activity_' . $aid . ':lock:u_' . $userId) || PRedis::get('matchs:activity_' . $aid . ':lock:u_' . $matchUid)) { PRedis::set('matchs:activity_' . $aid . ':error:u_' . $userId.'_'.$matchUid, ['data' => $userData, 'match'=> $matchData, 'error' => '当前匹配用户已被匹配过'], 600); continue; } // 更新匹配数据 Db::startTrans(); $matchAt = time(); $updateData = [ 'match_uid' => $matchUid, 'match_at' => date('Y-m-d H:i:s', $matchAt), 'status' => 2, ]; PRedis::set('matchs:activity_' . $aid . ':update:u_' . $userId, ['data' => $updateData, 'match' => $matchData], 600); if (!HeartMatch::where(['id' => $mid])->update($updateData)) { Db::rollback(); continue; } $updateData = [ 'match_uid' => $userId, 'match_at' => date('Y-m-d H:i:s', $matchAt), 'status' => 2, ]; PRedis::set('matchs:activity_' . $aid . ':update:u_' . $matchUid, ['data' => $updateData, 'match' => $userData], 600); if (!HeartMatch::where(['id' => $matchId])->update($updateData)) { Db::rollback(); continue; } // 提交 Db::commit(); $successArr[] = $userId.' 匹配 '.$matchUid; //echo $userId.' 匹配 '.$matchUid."\n"; $successId[] = $userId; $successId[] = $matchUid; // 匹配加锁 PRedis::set('matchs:activity_' . $aid . ':lock:u_' . $userId, $matchData, 24 * 3600); PRedis::set('matchs:activity_' . $aid . ':lock:u_' . $matchUid, $userData, 24 * 3600); // 推送匹配成功消息 $openid = isset($userData['openid']) ? trim($userData['openid']) : ''; if ($openid) { $bookNo = isset($userData['book_no']) ? trim($userData['book_no']) : '无'; $matchName = isset($matchData['user_nickname']) ? $matchData['user_nickname'] : ''; $matchNo = isset($matchData['book_no']) ? trim($matchData['book_no']) : '无'; $matchWechatCode = isset($matchData['wechat_code']) ? trim($matchData['wechat_code']) : '无'; $matchSex = isset($matchData['sex']) ? intval($matchData['sex']) : 0; $matchSex = $matchSex == 1 ? '男' : '女'; $title = isset($activityInfo['title']) ? trim($activityInfo['title']) : '无'; $matchTime = $matchAt ? date('Y.m.d H:i', $matchAt) : date('Y.m.d H:i'); $params = [ 'title' => "恭喜!活动缘分互选匹配成功,已为您匹配到缘分用户!\n\n活动主题:\t{$title}\n\n您的编号:\t{$bookNo}\n\n对方编号:\t{$matchNo}\n\n匹配时间:\t{$matchTime}\n\n对方微信号:\t{$matchWechatCode}", 'remark' => "感谢您的使用,点击详情查看互选信息", 'type' => 'match', 'keywords' => [ 'name' => [ 'value' => $matchName.'(昵称)', 'color' => '#173177', ], 'sex' => [ 'value' => $matchSex, 'color' => '#173177', ], 'tel' => [ 'value' => '隐私信息不显示', 'color' => '#173177', ], ], 'url' => url('/weixin/activity/match?id=' . $aid . '&type=2', '', '', true), ]; PRedis::set('matchs:activity_' . $aid . ':message:' . $userId, ['result' => $matchData, 'params' => $params], 600); Wechat::sendTplMsg($openid, $params); } // 通知被匹配的用户 $matchOpenId = isset($matchData['openid'])? trim($matchData['openid']) : ''; if ($matchOpenId) { $bookNo = isset($matchData['book_no']) ? trim($matchData['book_no']) : '无'; $matchName = isset($userData['real_name']) ? $userData['real_name'] : ''; $matchNo = isset($userData['book_no']) ? trim($userData['book_no']) : '无'; $matchWechatCode = isset($userData['wechat_code']) ? trim($userData['wechat_code']) : '无'; $matchSex = isset($userData['sex']) ? intval($userData['sex']) : 0; $matchSex = $matchSex == 1 ? '男' : '女'; $title = isset($activityInfo['title']) ? trim($activityInfo['title']) : '无'; $matchTime = $matchAt ? date('Y.m.d H:i', $matchAt) : date('Y.m.d H:i', time()); $params = [ 'title' => "活动缘分互选匹配成功,已为您匹配到缘分用户!\n\n活动主题:\t{$title}\n\n您的编号:\t{$bookNo}\n\n对方编号:\t{$matchNo}\n\n匹配时间:\t{$matchTime}\n\n对方微信号:\t{$matchWechatCode}", 'remark' => "感谢您的使用,点击详情查看报名活动信息", 'type' => 'match', 'keywords' => [ 'name' => [ 'value' => $matchName.'(昵称)', 'color' => '#173177', ], 'sex' => [ 'value' => $matchSex, 'color' => '#173177', ], 'tel' => [ 'value' => '隐私信息不展示', 'color' => '#173177', ], ], 'url' => url('/weixin/activity/match?id=' . $aid . '&type=2', '', '', true), ]; PRedis::set('matchs:activity_' . $aid . ':message:' . $matchUid, ['result' => $userData, 'params' => $params], 600); Wechat::sendTplMsg($matchOpenId, $params); } } } // 更新匹配错误或失败用户数据 PRedis::set('matchs:activity_'.$aid.':result', ['list'=> $matchList, 'results'=> $results, 'success'=> $successArr,'successId'=> $successId,'sorts'=> array_keys($results)], 600); if ($successId) { $updateData = ['updated_at' => date('Y-m-d H:i:s'), 'status' => 3]; HeartMatch::where(['aid'=> $aid])->where('uid', 'not in', $successId)->update($updateData); } //var_dump($successArr); return $successArr ? $successArr : 6004; } catch (\Exception $exception){ return 1015; } } /** * 推送活动消息 * @param $aid * @param int $type 类型:默认1活动即将开始,2-活动开始前一天提醒 * @return array|int */ public static function sendMessage($aid, $type=1){ try { $field = 'id,title,cover_img as thumb,price,credit,act_nums,addtime,starttime,endtime,description,address,alert_time1,alert_remark1,alert_time2,alert_remark2'; $activityInfo = Activity::getInfo(['id'=> $aid], $field); if(empty($activityInfo)){ return 5001; } // 验证3天之内是否已经发过 if(PRedis::get('activity:pushStatus:'.$aid.'_'.$type)){ return 2115; } // 准备开始 $title = isset($activityInfo['title']) ? trim($activityInfo['title']) : ''; $signTime = isset($activityInfo['alert_time'.$type]) ? trim($activityInfo['alert_time'.$type]) : ''; $remark = isset($activityInfo['alert_remark'.$type]) ? trim($activityInfo['alert_remark'.$type]) : '无'; if(empty($signTime)){ return 5021; } // 报名的用户列表 $bookList = Books::getPushUserList($aid); $title = $type == 2? "您报名了明天的“{$title}”记得安排好时间,风雨无阻,准时参加,真诚第一" : "亲,您报名的“{$title}”即将开始,赶快收拾妥当,准时参加!"; $params = [ 'title' => $title, 'type' => 'activity_start', 'keywords' => [ 'keyword1' => [ 'value' => '待参加活动', 'color' => '#173177', ], 'keyword2' => [ 'value' => $signTime, 'color' => '#173177', ], 'keyword3' => [ 'value' => $remark, 'color' => '#173177', ], ], 'url' => url('/weixin/activity/bookdetail?id=' . $aid, '', '', true), ]; $count = 0; if(empty($bookList)){ return 5022; } // var_dump($bookList); foreach($bookList as $userData){ $userId = isset($userData['uid'])? intval($userData['uid']) : 0; $openid = isset($userData['openid'])? trim($userData['openid']) : ''; if($openid){ PRedis::set('activity:pushMessage:' . $aid . '_' . $userId, ['result' => $userData, 'params' => $params], 600); $result = Wechat::sendTplMsg($openid, $params); if(is_array($result)){ $count++; } } } if($count){ PRedis::set('activity:pushStatus:'.$aid.'_'.$type, ['info'=> $activityInfo,'pushList'=> $bookList], 5 * 24 * 3600); return ['id'=> $aid, 'count'=> $count]; } else { return 2116; } } catch (\Exception $exception){ return 1015; } } }