/** * Generate a valid username that is not already in use. * * If any arguments are passed they will be used it base for the username. * * @param mixed $tryWith * @return string */ function generate_username($tryWith = null) { $usernames = is_array($tryWith) ? $tryWith : func_get_args(); $username = array_shift($usernames); // If no username provided generate a random one if (empty($username)) { $username = '******' . str_random(rand(5, 8)); } else { // Trim non alphanumeric characters $username = preg_replace('/[^\\da-z]/i', null, $username); // Trim numbers from the begining $username = preg_replace('/^\\d+/', null, $username); // Ensure at least 4 characters length if (strlen($username) < 4) { return generate_username($usernames); } // Truncate and lower case $username = strtolower(substr($username, 0, 32)); } if (is_null(DB::table('users')->whereUsername($username)->first())) { return $username; } return generate_username($usernames); }
/** * 根据邮箱地址生成用户名 * * @param number $length * @return number */ function generate_username() { $username = '******' . rand_number(3); $charts = "ABCDEFGHJKLMNPQRSTUVWXYZ"; $max = strlen($charts); for ($i = 0; $i < 4; $i++) { $username .= $charts[mt_rand(0, $max)]; } $username .= rand_number(4); $sql = "select count(*) from " . $GLOBALS['ecs']->table('users') . " where user_name = '{$username}'"; $count = $GLOBALS['db']->getOne($sql); if ($count > 0) { return generate_username(); } return $username; }
/** * 用户注册 * @date: 2016年1月10日 下午10:54:46 * * @author : Elliot * @param * : variable * @return : */ public function register_post() { $this->load->model('user_model'); $data = $this->_post_args; try { // 手机号必须 if (!isset($data['tel'])) { throw new Exception('手机号必须', 400); } // test code // 判断手机号是否合法 if (!preg_match("/1[3458]{1}\\d{9}\$/", $data['tel'])) { throw new Exception('不符合规范的手机号码', 400); } // 判断此手机号是否存在 $user_ifexist = $this->user_model->getUser(array('tel' => $data['tel'])); if ($user_ifexist) { throw new Exception('手机号码已存在', 409); // test code } // 判断验证码 是否正确 $usercode = $this->user_model->getUserCode(array('tel' => $data['tel'], 'type' => 1)); if ($usercode['code'] != $data['verifycode']) { throw new Exception('验证码错误', 409); // test code } unset($data['verifycode']); // 邀请码 if (isset($data['invitationcode']) && $data['invitationcode'] !== '') { $userInvitation = $this->user_model->getUser(array('invitationcode' => $data['invitationcode'])); if (!$userInvitation) { throw new Exception('您填写的邀请码用户不存在', 400); } $data['ref_userid'] = $userInvitation['id']; // 推送通知PUSH $this->load->helper('push'); if ($userInvitation['invter_push'] && $userInvitation['device_tokens']) { sendAppPush($userInvitation['platform'], $userInvitation['device_tokens'], 4, '您推荐的手机号' . $data['tel'] . '用户注册成功'); // push } } // 随机生成 username password $this->load->helper('encrypt'); $uaUserPwd = encrypt_build_rand_password(3); $data['username'] = '******' . generate_username(6); // 用户名字规则user+6位数字随机 $data['passwd'] = encrypt_md5($uaUserPwd); // 默认省市位置 北京朝阳 $data['provice_sid'] = '001001'; $data['provice_name'] = '北京'; $data['city_sid'] = '001001005'; $data['city_name'] = '朝阳'; $data['createtime'] = date('Y-m-d H:i:s', time()); // 生成唯一邀请码 $this->load->helper('common'); $data['invitationcode'] = getInviteCode(); // 随机用户头像 $data['sysavatarid'] = rand(1, MAXRANDAVATARID); //判断新注册的手机号是否存在预关系 $inventdetail = $this->user_model->getinventrel(['phone' => $data['tel']]); if ($inventdetail) { $data['ref_userid'] = $inventdetail['uid']; } $id = $this->user_model->createUser($data); } catch (Exception $e) { $this->response(array('error' => $e->getMessage()), $e->getCode()); } if ($id) { $user = $this->user_model->getUser(array('id' => $id)); $user['passwd'] = $uaUserPwd; // 返回原始密码 $this->response($user, 201); // 201 being the HTTP response code } else { $this->response(array('error' => '用户注册失败'), 404); } }
/** * 添加/修改用户数据 */ public function ajaxUpdateUser() { $id = intval(true == isset($_POST['id']) ? $_POST['id'] : 0); $this->load->library('form'); $this->form->setHandler('filter_strip_tags_and_clear_space|filter_replace_escape_char'); $this->load->helper('form/userform'); $this->load->model('user_model'); if ($id) { $postData = $this->form->check(userupdateform_get_selector()); $errors = $this->form->getLastMessage(); if (false == empty($errors)) { response_to_json(5, $errors); } $data = array('username' => $postData['username']['value'], 'provice_sid' => $postData['provice_sid']['value'], 'city_sid' => $postData['city_sid']['value'], 'provice_name' => $postData['provice_name']['value'], 'city_name' => $postData['city_name']['value']); // 判断重名 $usernameexist = $this->user_model->getUser(array('username' => $postData['username']['value'], 'status' => array(1, -2), 'id' => array('$ne', $id))); if ($usernameexist) { response_to_json(4, '名字已被占用!'); } $affected = $this->user_model->updateUser(array('id' => $id), $data); $errors = $this->user_model->getLastMessage(); $code = 0 == $affected || false == empty($errors) ? 4 : 0; $message = false == empty($errors) ? $errors : (0 == $affected ? '操作失败' : '操作成功'); } else { $this->load->helper('encrypt'); $postData = $this->form->check(useraddform_get_selector()); $errors = $this->form->getLastMessage(); if (false == empty($errors)) { response_to_json(5, $errors); } // 判断此手机号是否存在 $user_ifexist = $this->user_model->getUser(array('tel' => $postData['tel']['value'])); if ($user_ifexist) { response_to_json(4, '此手机号已经存在!'); } $this->load->helper('encrypt'); $data['username'] = '******' . generate_username(6); $data['passwd'] = encrypt_md5(create_password(9)); $data['tel'] = $postData['tel']['value']; $data['usertype'] = $postData['usertype']['value']; $data['createtime'] = date('Y-m-d H:i:s', time()); //判断新注册的手机号是否存在预关系 $inventdetail = $this->user_model->getinventrel(['phone' => $postData['tel']['value']]); if ($inventdetail) { $data['ref_userid'] = $inventdetail['uid']; } $id = $this->user_model->createUser($data); $errors = $this->user_model->getLastMessage(); $code = 0 == $id || false == empty($errors) ? 4 : 0; $message = false == empty($errors) ? $errors : (0 == $id ? '操作失败' : '操作成功'); } response_to_json($code, $message); }
public function wechat_member_bind_get() { $this->load->library('curl'); $openid = $this->get('openid'); $name = $this->get('name'); $tel = $this->get('tel'); $response['status'] = FALSE; $this->general_mdl->setTable('wechat_member_bind'); if ($openid) { if ($name && $tel) { $query = $this->general_mdl->get_query_by_where(array("tel" => $tel)); if ($query->num_rows() > 0) { $response['info'] = '此电话已绑定,请不要重复绑定'; } else { $auto_increment = $this->general_mdl->get_auto_increment(); $create_data['wechat_cardno'] = generate_username($auto_increment); $create_data['openid'] = $openid; $create_data['name'] = $name; $create_data['tel'] = $tel; $this->general_mdl->setData($create_data); $this->general_mdl->create(); $response['status'] = TRUE; $response['wechat_cardno'] = $create_data['wechat_cardno']; } } else { $response['info'] = '请输入姓名和电话!'; } } else { $response['info'] = '非法进入!'; } $this->response($response, 200); }
/** * Create a new user with an automatillay generated username. * If an $account is provided, the username will try to match the account personal info. * * @param Account $account * * @return User * @throws \Exception */ public static function autoCreate(Account $account = null) { // Profile of the user $profile = Profile::findOrFail(config('site.auto-registration-profile')); // Seeds for the username $seeds = !$account ? [] : array_filter([$account->first_name, $account->last_name, $account->name, $account->nickname, preg_replace('/([^@]*).*/', '$1', $account->email), $account->first_name . str_random(4), $account->last_name . str_random(4), $account->name . str_random(4), $account->nickname . str_random(4), $account->provider->name . $account->uid]); // Attempt to create user $user = new User(['username' => generate_username($seeds), 'name' => $account ? $account->nameForHumans() : null, 'password' => $password = str_random(60), 'password_confirmation' => $password, 'profile_id' => $profile->id]); if (!$user->save()) { throw new \Exception(_('Unable to create user')); } return $user; }