/** * Create default user * * @return bool */ public static function createManager($mobile) { $manager = new ActivityManager(); $manager->id = UUID::v4(); $manager->mobile = $mobile; $manager->created_at = Carbon::now(); $manager->updated_at = Carbon::now(); return $manager->save(); }
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; }
/** * Run the database seeds. * * @return void */ public function run() { //init wechat_app--------------------------------------- DB::table('wechat_app')->insert(['app_id' => 'wxdc06d6c8ed39889e', 'app_secret' => '5921188a2889d0e7a41a842d0969b6e8', 'token' => 'suffrajetWechatToken', 'aes_key' => 'B3Qv3pjv0NE5ytOmPUZW0oCh1ssdoYLGsla1SD7ezB2', 'original_id' => 'gh_8d23d6695d07', 'name' => 'dll', 'type' => 2, 'is_auth' => 1, 'email' => '*****@*****.**', 'describe' => '个人测试使用', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $app = WechatApp::where('app_id', 'wxdc06d6c8ed39889e')->get()->first(); //init table user_type---------------------------------- DB::table('user_type')->insert(['wechat_id' => $app['id'], 'name' => '医生', 'code' => 'doctor', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('user_type')->insert(['wechat_id' => $app['id'], 'name' => '代表', 'code' => 'manager', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table activity_type------------------------------ DB::table('activity_type')->insert(['name' => '科室会', 'code' => 'ksh', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init manager------------------------------------------ DB::table('activity_manager')->insert(['id' => UUID::v4(), 'name' => 'dll', 'mobile' => '18613860084', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table user_openid------------------------------- $userType = UserType::where('name', '代表')->get()->first(); $manager = ActivityManager::where('name', 'dll')->get()->first(); DB::table('user_openid')->insert(['wechat_id' => $app['id'], 'user_type_id' => $userType['id'], 'open_id' => 'oCFvTvkmM5joT-h8pr7p-q2F0Dno', 'user_id' => $manager['id'], 'activity_timeout' => 24, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init reply------------------------------------------- $replyText = ['content' => '回复文本内容']; $replyNews = [array('title' => '测试标题', 'description' => '描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg'), array('title' => '查看更多帮助', 'description' => '描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg')]; //init public event------------------------------------------ DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '关注', 'code' => 'subscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '取消关注', 'code' => 'unsubscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '注册', 'code' => 'register', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'name' => '验证', 'code' => 'validate', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity event----------------------------------------- $actType = ActivityType::where('name', '科室会')->get()->first(); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '激活活动', 'code' => 'init_activity', 'sort' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '查看题库', 'code' => 'browse_question', 'sort' => 2, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '查看奖品库', 'code' => 'browse_prize', 'sort' => 3, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '加入活动', 'code' => 'join_activity', 'sort' => 4]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '活动答题', 'code' => 'answer', 'sort' => 5, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '开奖', 'code' => 'draw', 'sort' => 6, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '中奖查询', 'code' => 'query_winner', 'sort' => 7, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '奖品查询', 'code' => 'query_prize', 'sort' => 8, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('event')->insert(['wechat_id' => $app['id'], 'activity_type_id' => $actType['id'], 'name' => '关闭活动', 'code' => 'close_activity', 'sort' => 9, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init table command----------------------------------- $replyNews[0]['title'] = '欢迎关注'; $type = Event::where('code', 'subscribe')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'event', 'cmd_content' => 'subscribe', 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'unsubscribe')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'event', 'cmd_content' => 'unsubscribe', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'register')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '注册', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '注册成功'; $type = Event::where('code', 'validate')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '验证', 'is_prefix' => 1, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'init_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '开会', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '题库详情'; $type = Event::where('code', 'browse_question')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '题库', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '奖品库详情'; $type = Event::where('code', 'browse_prize')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '奖品库', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'join_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '加入', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'answer')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '我的答案', 'is_prefix' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'draw')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '开奖', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'query_winner')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '中奖情况', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $replyNews[0]['title'] = '奖品展示'; $type = Event::where('code', 'query_prize')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '查看奖品', 'is_prefix' => 0, 'reply_type' => 'news', 'reply_json' => base64_encode(json_encode($replyNews)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); $type = Event::where('code', 'close_activity')->get()->first(); DB::table('command')->insert(['wechat_id' => $app['id'], 'event_id' => $type['id'], 'cmd_type' => 'text', 'cmd_content' => '散会', 'is_prefix' => 0, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity questions------------------------------ $options = ['1' => '选项1', '2' => '选项2', '3' => '选项3', '4' => '选项4']; DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题1', 'content' => '问题1的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 1, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题2', 'content' => '问题2的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 2, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题3', 'content' => '问题3的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 3, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题4', 'content' => '问题4的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 1, 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题5', 'content' => '问题5的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 3, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_question')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '问题6', 'content' => '问题6的答案是啥?', 'options_json' => base64_encode(json_encode($options)), 'right_answer' => 4, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init activity prizes--------------------------------- $prizeDefine = [array('title' => '活动奖品名称', 'description' => '描述描述描述描述描述', 'url' => 'http://www.baidu.com', 'picUrl' => 'http://123.56.69.6/resources/640.jpg')]; DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品1', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'is_default' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品2', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('activity_prize')->insert(['wechat_id' => $app['id'], 'activity_manager_id' => $manager['id'], 'name' => '活动奖品3', 'describe_json' => base64_encode(json_encode($prizeDefine)), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); //init notice----------------------------------------- DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展1', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展2', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); DB::table('notice')->insert(['wechat_id' => $app['id'], 'doctor_mobile' => '18613860084', 'notice_name' => '活动延迟开展3', 'notice_content' => '由于今天临时有事, 活动开展时间推迟到明天上午10:00开始,请各位见谅!', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]); }
/** * Member join in ongoing activity * * @param $wechatId * @param $activityTypeId * @param $activityCode * @param $memberId * @return array */ public static final function memberJoinActivity($wechatId, $activityTypeId, $activityCode, $memberId) { $result = ['status' => ErrorCode::success, 'param' => null]; //根据编号获取活动 $activity = Activity::getActivityByCode($wechatId, $activityTypeId, $activityCode); if (!$activity) { $result['status'] = ErrorCode::err_activity_not_exist; return $result; } //获取manager $manager = ActivityManager::getById($activity['activity_manager_id']); if (!$manager) { $result['status'] = ErrorCode::err_activity_manager_not_found; return $result; } //获取活动session $key = 'activity-' . $wechatId . '-' . $manager['id']; $session = Session::get($key); if (!$session) { $result['status'] = ErrorCode::err_activity_session_not_exist; return $result; } if ($session['activity_code'] != $activityCode) { $result['status'] = ErrorCode::err_activity_session_not_exist; return $result; } //检查是否重复加入活动 if (in_array($memberId, $session['member_id'])) { $result['status'] = ErrorCode::err_member_repeat_join; $result['param'] = $activity; return $result; } //检查是否已加入过另外的活动 $preId = ActivityMember::getActivityIdOngoing($wechatId, $memberId); $pre = Activity::getActivityById($preId['activity_id']); if ($pre) { $result['status'] = ErrorCode::err_pre_joined_activity_ongoing; $result['param'] = $pre; return $result; } //添加用户到活动成员表 $member = new ActivityMember(); $member->wechat_id = $wechatId; $member->activity_id = $session['activity_id']; $member->user_id = $memberId; $member->is_ongoing = 1; if (!$member->save()) { SysLog::error(__FILE__, __METHOD__, __LINE__, 'create activity member error'); $result['status'] = ErrorCode::err_create_activity_member_fail; return $result; } //更新活动session, 添加用户ID到session $session['member_count']++; $session['member_id'][] = $memberId; $session['timestamps'] = time(); if (!Session::update($key, $session, $session['duration'])) { $result['status'] = ErrorCode::err_update_activity_session_fail; return $result; } Log::info(PHP_EOL . '[===================================================Session Member Join :]' . PHP_EOL . '[activity type:] ' . $activityTypeId . PHP_EOL . '[activity code:] ' . $session['activity_code'] . PHP_EOL . '[session key:] ' . $key . PHP_EOL . '[session value:] ' . json_encode($session) . PHP_EOL); $result['status'] = ErrorCode::success; $result['param'] = $activity; return $result; }
public static function getActivityManager($wechatId, $activityType, $memberId) { $member = ActivityMember::where(['wechat_id' => $wechatId, 'doctor_id' => $memberId, 'is_ongoing' => 1])->get()->first(); if (!$member) { $activity = self::where(['id' => $member['activity_id'], 'wechat_id' => $wechatId, 'activity_type_id' => $activityType, 'status' => self::STATUS_ONGOING])->get()->first(); if ($activity) { return ActivityManager::getById($activity['activity_manager_id']); } } return null; }
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; }