public static function getAll($wechatId) { $users = []; $all = self::where(['user_openid.is_delete' => 0])->get(); if ($all) { foreach ($all as $item) { $typeCode = UserType::getCode($item['user_type_id']); if ($typeCode == self::TYPE_MANAGER && $item['user_id']) { $model = ActivityManager::getManager($wechatId, $item['open_id']); if ($model) { $users[$model['open_id']] = ['open_id' => $model['open_id'], 'type' => $model['code'], 'id' => $model['id'], 'name' => $model['name'], 'mobile' => $model['mobile'], 'time_out' => (int) $model['activity_timeout'] * 60 * 60]; } } else { if ($typeCode == self::TYPE_DOCTOR && $item['user_id']) { $model = Doctor::getDoctor($wechatId, $item['open_id']); if ($model) { $users[$model['open_id']] = ['open_id' => $model['open_id'], 'type' => $model['code'], 'id' => $model['id'], 'name' => $model['name'], 'mobile' => $model['mobile']]; } } else { $users[$item['open_id']] = ['open_id' => $item['open_id'], 'type' => '', 'id' => '', 'name' => '', 'mobile' => '']; } } } } return $users; }
/** * Create doctor * * @param $app * @param $openId * @param $mobile * @return bool */ public static function createDoctor($app, $openId, $mobile) { $type = UserType::where('code', 'doctor')->get()->first(); $model = UserOpenid::where(['wechat_id' => $app['id'], 'open_id' => $openId])->get()->first(); if ($type && $model) { $doctor = new Doctor(); $doctor->id = UUID::v4(); $doctor->mobile = $mobile; $doctor->created_at = Carbon::now(); $doctor->updated_at = Carbon::now(); if ($doctor->save()) { $model->user_id = $doctor->id; $model->user_type_id = $type['id']; $model->updated_at = Carbon::now(); return $model->update(); } } return false; }
public static function managerQueryWinner($app, $manager, $replyArr) { //活动类型ID $typeId = Event::getActivityTypeId($replyArr['event_id']); $result = BaseActivity::managerGetActivitySession($app['id'], $typeId, $manager['id']); switch ($result['status']) { case ErrorCode::err_activity_session_not_exist: case ErrorCode::err_activity_not_exist: return ReplyFormat::replyText(KshParams::ksh_error['error_activity_not_exist']); break; case ErrorCode::success: $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'member_count'); $memberCount = $param['param']; $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'member_id'); $memberIdArr = $param['param']; $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'member_answers_results'); $memberAnswerArr = $param['param']; $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'question_count'); $quesCount = $param['param']; $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'prize_count'); $prizeCount = $param['param']; $param = BaseActivity::getActivityParam($app['id'], $typeId, $manager['id'], 'is_draw'); $isDraw = $param['param']; //检查活动题目是否设置 if ($quesCount == 0) { return ReplyFormat::replyText(KshParams::ksh_error['error_activity_question_not_set']); } //检查活动奖品是否设置 if ($prizeCount == 0) { return ReplyFormat::replyText(KshParams::ksh_error['error_activity_prize_not_set']); } //统计 $winner = []; foreach ($memberIdArr as $memberId) { $rightCount = 0; $memberResult = $memberAnswerArr[$memberId]; foreach ($memberResult as $ques => $answer) { if ($answer) { $rightCount++; } } if ($rightCount == $quesCount) { $winner[] = $memberId; } } //中奖人数 $winnerCount = count($winner); //中奖着手机后4位尾号 $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(KshParams::ksh_error['error_manager_not_draw_activity']); } else { //没有中奖的 if ($winnerCount == 0) { return ReplyFormat::replyText(sprintf(KshParams::ksh_error['error_activity_no_winner'], $memberCount, $winnerCount)); } else { if ($winnerCount > 0 && $winnerCount <= $memberCount) { return ReplyFormat::replyText(sprintf(KshParams::ksh_success['success_query_winners'], $memberCount, $winnerCount, $mobileString)); } } } break; } return false; }
private static function registerHandle($app, $user, $mobile) { $openId = $user['open_id']; if ($openId) { //新注册用户 if (!$user['mobile']) { //先寻找有无此手机号的代表, 有则绑定手机号和openid $manager = ActivityManager::getByMobile($mobile); if ($manager->bindManager($app, $openId, $mobile)) { return UserCache::updateUser($app, $openId); } else { if (Doctor::createDoctor($app, $openId, $mobile)) { return UserCache::updateUser($app, $openId); } } } else { if ($user['type'] == UserOpenid::TYPE_DOCTOR) { $doctor = Doctor::getById($user['id']); $doctor->mobile = $mobile; $doctor->updated_at = Carbon::now(); if ($doctor->save()) { return UserCache::updateUser($app, $openId); } } if ($user['type'] == UserOpenid::TYPE_MANAGER) { $manager = ActivityManager::getById($user['id']); $manager->mobile = $mobile; $manager->updated_at = Carbon::now(); if ($manager->save()) { return UserCache::updateUser($app, $openId); } } } } return false; }
private static function update($app, $openId) { $key = 'user-cache-' . $app['id']; if (Cache::has($key)) { $type = UserOpenid::getUserTypeCode($app['id'], $openId); $cache = json_decode(base64_decode(Cache::get($key)), true); if (self::get($app, $openId)) { //先删除 unset($cache[$openId]); //重新获取 $item = null; if ($type == UserOpenid::TYPE_DOCTOR) { $model = Doctor::getDoctor($app['id'], $openId); if ($model) { $item = ['open_id' => $model['open_id'], 'type' => $model['code'], 'id' => $model['id'], 'name' => $model['name'], 'mobile' => $model['mobile']]; $cache[$openId] = $item; } } if ($type == UserOpenid::TYPE_MANAGER) { $model = ActivityManager::getManager($app['id'], $openId); if ($model) { $item = ['open_id' => $model['open_id'], 'type' => $model['code'], 'id' => $model['id'], 'name' => $model['name'], 'mobile' => $model['mobile'], 'time_out' => (int) $model['activity_timeout'] * 60 * 60]; $cache[$openId] = $item; } } //更新 if ($item) { Cache::forget($key); $value = base64_encode(json_encode($cache)); Cache::forever($key, $value); return true; } } } return false; }