| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- <?php
- /**
- * 活动
- */
- namespace app\weixin\service;
- use app\weixin\model\Activity as ActivityModel;
- use app\weixin\model\Books;
- use app\weixin\model\HeartMatch;
- use app\weixin\model\Invitation;
- use app\weixin\model\Member;
- use app\weixin\model\UserLog;
- use app\weixin\model\Wechat;
- use think\Db;
- class Activity
- {
- /**
- * 获取活动列表
- * @param $params 参数
- * @param int $pageSize 分页大小
- * @param string $field 字段
- * @param string $order 排序
- * @return mixed
- */
- public static function getList($params, $pageSize = 15, $field = '', $order = '')
- {
- $where = ['status' => 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())
- ->whereNotIn('is_top',[1])
- ->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,service_time,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
- * @param $inviteCode 邀请码
- * @return array|int
- */
- public static function catchBook($userId, $aid, $inviteCode='')
- {
- // 验证活动是否存在
- $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;
- }
- // 活动截止前多久不允许报名
- $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('id,vip_auth,vip_expire,openid,redheart,user_nickname,real_name')->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);
- }
- // 验证邀请码如果有
- if($inviteCode){
- // 验证邀请码是否有效
- $codeInfo = Invitation::getInfo($inviteCode);
- if(empty($codeInfo)){
- return 5027;
- }
- if($codeInfo['user_id']>0 && $codeInfo['user_id'] != $userId){
- return 5030;
- }
- // 已被使用
- if($codeInfo['status'] == 3){
- return 5028;
- }
- // 过期
- if($codeInfo['expired_at'] < time() || $codeInfo['status'] == 4){
- return 5029;
- }
- // 验证邀请码使用已经使用报名
- if(Books::checkInviteCodeBook($inviteCode)){
- return 5028;
- }
- // 验证邀请码是否符合使用金额
- if($payPrice>$codeInfo['quota']){
- return lang('5031',['money'=> $codeInfo['quota']]);
- }
- }
- // 报名数据
- $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,
- ];
- // 如果是邀请码报名直接支付
- if($inviteCode){
- $bookData['pay_type'] = 4;
- $bookData['money'] = 0;
- $bookData['credit'] = 0;
- $bookData['invite_code'] = $inviteCode;
- $bookData['book_at'] = date('Y-m-d H:i:s');
- $bookData['remark'] = '邀请码报名:'.$inviteCode;
- $bookData['status'] = 2;
- }
- // 写入记录
- 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();
- // 如果是邀请码报名发送消息
- self::catchBookByInviteCode($memberInfo, $bookData, $activityInfo);
- PRedis::del($lockCache);
- return ['id' => $bookId];
- }
- /**
- * 邀请码报名支付成功处理
- * @param $memberInfo 用户信息
- * @param $bookInfo 报名信息
- * @param $activityInfo 活动信息
- * @return false
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- protected static function catchBookByInviteCode($memberInfo,$bookInfo, $activityInfo)
- {
- $openid = isset($memberInfo['openid']) ? $memberInfo['openid'] : '';
- $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
- $outTradeNo = isset($bookInfo['order_sn'])? trim($bookInfo['order_sn']) : '';
- $inviteCode = isset($bookInfo['invite_code'])? trim($bookInfo['invite_code']) : '';
- $bookStatus = isset($bookInfo['status'])? intval($bookInfo['status']) : 0;
- $money = isset($bookInfo['money'])? floatval($bookInfo['money']) : 0;
- $credit = isset($bookInfo['credit'])? floatval($bookInfo['credit']) : 0;
- $payType = isset($bookInfo['pay_type'])? intval($bookInfo['pay_type']) : 0;
- $title = isset($activityInfo['title'])? trim($activityInfo['title']) : '无';
- $bookAt = date('Y.m.d H:i');
- if($bookStatus != 2 || $payType!= 4 || empty($inviteCode)){
- return false;
- }
- // 更新邀请码数据
- Invitation::where(['code'=> $inviteCode])
- ->update(['user_id'=> $memberInfo['id'],'status'=> 3,'update_time'=>time()]);
- // 发送报名消息
- if($openid){
- $params = [
- 'title' => "我们已经收到您的报名,稍后会进行审核,请留意后续的通知哦!\n\n姓名:\t{$nickname}(昵称)\n\n支付单号:\t{$outTradeNo}\n\n支付金额:\t邀请码支付",
- 'remark' => "转发积姻缘,把活动分享给身边的朋友,一起来脱单哦!",
- 'type' => 'book',
- 'keywords' => [
- 'keyword1' => [
- 'value' => $title,
- 'color' => '#173177',
- ],
- 'keyword2' => [
- 'value' => $bookAt,
- 'color' => '#173177',
- ],
- 'keyword3' => [
- 'value' => '待审核',
- 'color' => '#173177',
- ],
- 'keyword4' => [
- 'value' => "活动费用{$money}元,活动守时金{$credit}元。",
- 'color' => '#173177',
- ],
- ],
- 'url' => url('/weixin/activity/bookdetail?id='.$activityInfo['id'], '', '', true),
- ];
- PRedis::set('payments:books:message_' . $outTradeNo, ['bookInfo' => $bookInfo,'activity'=> $activityInfo, 'params' => $params], 600);
- Wechat::sendTplMsg($openid, $params);
- }
- }
- /**
- * 置顶报名
- * @param $userId
- * @param $aid
- * @return array|int
- */
- public static function catchTopBook($userId, $aid)
- {
- // 验证活动是否存在
- $field = 'id,starttime,endtime,price,market_price,market_time,service_time,member_discount,addtime,is_top,credit,act_nums';
- $activityInfo = Activity::getInfo(['id' => $aid], $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;
- $serviceTime = isset($activityInfo['service_time']) ? intval($activityInfo['service_time']) : 0;
- $isTop = isset($activityInfo['is_top']) ? intval($activityInfo['is_top']) : 0;
- if($addTime > time()){
- return 5000;
- }
- if ($endTime && time() > $endTime) {
- return 5002;
- }
- // 活动截止前多久不允许报名
- $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]);
- }
- }
- // 验证操作频繁
- $lockCache = "books:lock:" . $userId . "_" . $aid;
- if (PRedis::get($lockCache)) {
- return 1013;
- }
- // 验证已报名
- $memberInfo = Member::where(['id' => $userId])->field('sex,is_top,top_expire')->find();
- $isTopShow = isset($memberInfo['is_top'])? $memberInfo['is_top'] : 0;
- $topExpire = isset($memberInfo['top_expire'])? $memberInfo['top_expire'] : '';
- if($isTopShow && $topExpire && date('Y-m-d H:i:s') < $topExpire){
- return 2046;
- }
- $sex = isset($memberInfo['sex'])? $memberInfo['sex'] : 1;
- $limitNum = isset($activityInfo['act_nums']) ? intval($activityInfo['act_nums']) : 0;
- $bookCount = Member::where(['user_status'=> 1,'is_top'=>1,'sex'=> $sex])
- ->where('top_expire','>', date('Y-m-d H:i:s'))
- ->count('id');
- if ($limitNum && $bookCount >= $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' => "首页置顶报名:价格{$payPrice}",
- '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;
- $payType = isset($bookInfo['pay_type']) ? intval($bookInfo['pay_type']) : 0;
- $inviteCode = isset($bookInfo['invite_code']) ? trim($bookInfo['invite_code']) : '';
- $money = isset($bookInfo['money']) ? floatval($bookInfo['money']) : 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;
- }
- // 活动信息
- $activityInfo = ActivityModel::where(['id' => $aid])
- ->field('id,starttime,act_nums,service_time,title,join_address')
- ->find();
- // 审核数据更新
- $bookNo = '';
- $updateData = ['status' => $checkStatus, 'remark' => $checkStatus==3? '审核成功':'审核失败'];
- if($checkStatus == 3){
- $sex = Member::where(['id' => $userId])->value('sex');
- // 置顶报名
- if($aid == 16){
- $limitNum = isset($activityInfo['act_nums']) ? intval($activityInfo['act_nums']) : 0;
- $bookCount = Member::where(['user_status'=> 1,'is_top'=>1,'sex'=> $sex])
- ->where('top_expire','>', date('Y-m-d H:i:s'))
- ->count('id');
- if ($limitNum && $bookCount >= $limitNum) {
- return lang('book_limit',['sex'=> ($sex==1?'男生':'女生')]);
- }
- }
- $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')]);
- }
- // 置顶推荐更新
- $servceTime = isset($activityInfo['service_time'])? $activityInfo['service_time'] : 0;
- if($servceTime && $aid == 16 && $checkStatus == 3){
- $expire = $servceTime * 3600 * 24 + time();
- Member::saveData(['id'=> $userId],['is_top'=> 1,'top_expire'=> date('Y-m-d H:i:s', $expire)]);
- }
- // 邀请码报名审核不通过
- $money = ($payType == 4 || $inviteCode)? '邀请码支付': $money;
- if(($payType == 4 || $inviteCode) && $checkStatus==5){
- Invitation::where(['user_id'=> $userId,'code'=> $inviteCode])->update(['status'=>2,'update_time'=>time()]);
- }
- $memberInfo = Member::getInfo(['id' => $userId]);
- $openid = isset($memberInfo['openid']) ? $memberInfo['openid'] : '';
- $nickname = isset($memberInfo['user_nickname']) ? $memberInfo['user_nickname'] : '';
- // 发送消息
- if ($openid) {
- $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']) : '';
- 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',
- ],
- ],
- '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;
- }
- }
- }
|