Ejemplo n.º 1
0
 /**
  * Handler activity by command
  *
  * @param $app WechatApp
  * @param $user User
  * @return string message
  */
 public static function handle($app, $openId, $replyArr)
 {
     $user = UserCache::getByOpenId($app, $openId);
     $eventCode = Event::getEventCode($replyArr['event_id']);
     if (!$user) {
         SysLog::error(__FILE__, __METHOD__, __LINE__, 'user not found by openid');
         return false;
     }
     if (!$user['mobile']) {
         return ReplyFormat::replyText(SysParams::sys_error['error_user_not_register']);
     }
     switch ($eventCode) {
         case self::ACTIVITY_INIT:
             return KshActivity::startKsh($app, $user, $replyArr, $user['time_out']);
             break;
         case self::ACTIVITY_CLOSE:
             return KshActivity::stopKsh($app, $user, $replyArr);
             break;
         case self::ACTIVITY_DRAW:
             return KshActivity::drawKsh($app, $user, $replyArr);
             break;
         case self::QUESTION_BROWSE:
             return KshActivity::browseKshQuestionStore($app, $user, $replyArr);
             break;
         case self::PRIZE_BROWSE:
             return KshActivity::browseKshPrizeStore($app, $user, $replyArr);
             break;
         case self::ACTIVITY_JOIN:
             return KshActivity::joinKsh($app, $user, $replyArr);
             break;
         case self::ACTIVITY_ANSWER:
             return KshActivity::answerKsh($app, $user, $replyArr);
             break;
         case self::QUERY_WINNER:
             return KshActivity::queryWinner($app, $user, $replyArr);
             break;
         case self::QUERY_PRIZE:
             return KshActivity::queryPrize($app, $user, $replyArr);
             break;
             //            case self::QUESTION_CHOOSE:
             //                return Ksh::setActivityQuestion($app, $user, $replyArr);
             //                break;
             //            case self::PRIZE_CHOOSE:
             //                return Ksh::setActivityPrize($app, $user, $replyArr);
             //                break;
         //            case self::QUESTION_CHOOSE:
         //                return Ksh::setActivityQuestion($app, $user, $replyArr);
         //                break;
         //            case self::PRIZE_CHOOSE:
         //                return Ksh::setActivityPrize($app, $user, $replyArr);
         //                break;
         default:
             return false;
             break;
     }
 }
Ejemplo n.º 2
0
 /**
  * Validate user if is register mobie
  *
  * @param $openId
  * @return bool
  */
 public static function isRegister($openId)
 {
     if (isset($openId)) {
         $doctor = self::getDoctorByOpenId($openId);
         if ($doctor) {
             SysLog::error(__FILE__, __METHOD__, __LINE__, 'not found user by openid');
             return false;
         }
         if ($doctor['mobile']) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 3
0
 /**
  * event类型消息解析, 根据不同事件类型自定义回复消息
  *
  *@param Message $message 消息对象
  *
  * @return Response
  */
 private static function event($event)
 {
     $orgId = $event->ToUserName;
     $openId = $event->FromUserName;
     $app = WechatApp::getAppByOrgId($orgId);
     if (!isset($app)) {
         SysLog::error(__FILE__, __METHOD__, __LINE__, 'get wechat app failed');
         return false;
     }
     $replyArr = Command::getReplyDefine($app, $event->MsgType, $event->Event);
     if (!$replyArr) {
         SysLog::error(__FILE__, __METHOD__, __LINE__, 'get event reply define array failed');
         return false;
     }
     return ReplyHandler::handle($app, $openId, $replyArr);
 }
Ejemplo n.º 4
0
 public static function draw($wechatId, $activityTypeId, $managerId)
 {
     //检查活动是否有参与者
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'member_count');
     $merberCount = $result['param'];
     if ($merberCount == 0) {
         return ReplyFormat::replyText(KshParams::ksh_error['error_no_members_draw_activity']);
     }
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'activity_id');
     $activityId = $result['param'];
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'member_id');
     $memberIds = $result['param'];
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'question_count');
     $quesCount = $result['param'];
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'member_answers_results');
     $merberAnswers = $result['param'];
     $result = BaseActivity::getActivityParam($wechatId, $activityTypeId, $managerId, 'is_draw');
     $isDraw = $result['param'];
     //检查活动是否有参与者进行答题
     if (empty($merberAnswers)) {
         return ReplyFormat::replyText(KshParams::ksh_error['error_no_members_answer_activity']);
     }
     //统计答题全部正确的用户Id
     $winner = [];
     foreach ($memberIds as $memberId) {
         $rightCount = 0;
         foreach ($merberAnswers[$memberId] as $ques => $answer) {
             if ($answer) {
                 $rightCount++;
             }
         }
         if ($rightCount == $quesCount) {
             $winner[] = $memberId;
         }
     }
     //中奖人数
     $winnerCount = count($winner);
     //中奖着手机号序列
     $mobileString = '';
     $Doctors = Doctor::whereIn('id', $winner)->get();
     foreach ($Doctors as $doctor) {
         $mobileString .= substr_replace($doctor['mobile'], '****', 3, 4) . ', ' . PHP_EOL;
     }
     //检查是否已开奖
     if ($isDraw) {
         return ReplyFormat::replyText(sprintf(KshParams::ksh_error['error_repeat_draw_activity'], $merberCount, $winnerCount, $mobileString));
     }
     //开奖
     $result = BaseActivity::updateActivityParam($wechatId, $activityTypeId, $managerId, 'is_draw', 1);
     if ($result['status'] != ErrorCode::success) {
         SysLog::error(__FILE__, __METHOD__, __LINE__, ' draw ksh error' . $result['status']);
         return false;
     }
     //存储中奖者
     if ($winnerCount > 0) {
         $isSave = ActivityWinner::saveWinners($wechatId, $activityId, $winner);
         if (!$isSave) {
             SysLog::error(__FILE__, __METHOD__, __LINE__, ' save winners failed');
         }
     }
     //没有中奖的
     if ($winnerCount == 0) {
         return ReplyFormat::replyText(sprintf(KshParams::ksh_error['error_activity_no_winner'], $merberCount, $winnerCount));
     } else {
         if ($winnerCount > 0 && $winnerCount <= $merberCount) {
             return ReplyFormat::replyText(sprintf(KshParams::ksh_success['success_draw_activity'], $merberCount, $winnerCount, $mobileString));
         }
     }
     return false;
 }
Ejemplo n.º 5
0
 public static function joinKsh($app, $user, $replyArr)
 {
     //权限检查
     if ($user['type'] != UserOpenid::TYPE_DOCTOR) {
         return KshNotice::replyText($app, $user, KshParams::ksh_error['error_no_authority_join_activity']);
     }
     //活动编号是否为空
     if (!array_key_exists('cmd_number', $replyArr)) {
         return KshNotice::replyText($app, $user, KshParams::ksh_error['error_empty_join_activity_code']);
     }
     //获取活动类型ID
     $typeId = Event::getActivityTypeId($replyArr['event_id']);
     $code = $replyArr['cmd_number'][0];
     $result = BaseActivity::memberJoinActivity($app['id'], $typeId, $code, $user['id']);
     switch ($result['status']) {
         case ErrorCode::err_create_activity_member_fail:
             SysLog::error(__FILE__, __METHOD__, __LINE__, ' create activity member failed');
             return false;
             break;
         case ErrorCode::err_update_activity_session_fail:
             SysLog::error(__FILE__, __METHOD__, __LINE__, ' update activity session failed');
             return false;
             break;
         case ErrorCode::err_activity_not_exist:
         case ErrorCode::err_activity_manager_not_found:
         case ErrorCode::err_activity_session_not_exist:
             return KshNotice::replyText($app, $user, KshParams::ksh_error['error_join_activity_not_exist']);
             break;
         case ErrorCode::err_pre_joined_activity_ongoing:
             $preActivity = $result['param'];
             return KshNotice::replyText($app, $user, sprintf(KshParams::ksh_error['error_pre_joined_activity_ongoing'], $preActivity['code']));
             break;
         case ErrorCode::err_member_repeat_join:
             $activity = $result['param'];
             KshUtils::addMemberMsgRecord($app['id'], $typeId, $activity['activity_manager_id'], $user['id'], $replyArr['cmd_original'], 'error_repeat_join_activity');
             return KshNotice::replyText($app, $user, sprintf(KshParams::ksh_error['error_repeat_join_activity'], $code));
             break;
         case ErrorCode::success:
             $activity = $result['param'];
             KshUtils::addMemberMsgRecord($app['id'], $typeId, $activity['activity_manager_id'], $user['id'], $replyArr['cmd_original'], 'success_join_activity');
             return KshNotice::replyText($app, $user, sprintf(KshParams::ksh_success['success_join_activity'], $code));
             break;
     }
     return false;
 }
Ejemplo n.º 6
0
 /**
  * Member get current activity manager
  *
  * @param $wechatId
  * @param $activityTypeId
  * @param $memberId
  * @return array
  */
 public static function getActivityManager($wechatId, $activityTypeId, $memberId)
 {
     $result = ['status' => ErrorCode::success, 'param' => null];
     //获取活动管理着
     $manager = Activity::getActivityManager($wechatId, $activityTypeId, $memberId);
     if (!$manager) {
         SysLog::error(__FILE__, __METHOD__, __LINE__, 'activity manager not found');
         $result['status'] = ErrorCode::err_activity_manager_not_found;
         return $result;
     }
     $result['param'] = $manager;
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * Create validate code and Cache, invalid after 10 minutes
  *
  * @param $app
  * @param $user
  * @param $mobile
  * @return bool
  */
 private static function createRegisterCache($app, $user, $mobile)
 {
     $key = 'sms-code-' . $app['id'] . $user['id'];
     if (Cache::has($key)) {
         Cache::forget($key);
     }
     if (isset($mobile)) {
         $data = ['code' => self::getRandCode(6), 'mobile' => $mobile];
         $value = base64_encode(json_encode($data));
         $expiresAt = Carbon::now()->addMinutes(10);
         Cache::put($key, $value, $expiresAt);
         SysLog::info(__FILE__, __METHOD__, __LINE__, PHP_EOL . '[======================================User Register :]' . PHP_EOL . '[mobile: ] ' . $mobile . PHP_EOL . '[code: ] ' . $data['code'] . PHP_EOL);
         return true;
     }
     return false;
 }