/**
* create user
* @route({"POST","/"})
* @param({"account", "$._POST.mobile"}) cell-phone number, required
* @param({"password", "$._POST.password"}) password, required
* @param({"alias", "$._POST.alias"}) user's alias, required
* @param({"avatar", "$._FILES.avatar.tmp_name"}) user's avatar, optional
* @param({"token", "$._COOKIE.token"})
*
* @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie invalid
*
* @throws({"AliasConflict","res", "409 Conflict",{"error":"AliasConflict"}}) alias conflict
*
* @throws({"AccountConflict","res", "409 Conflict",{"error":"AccountConflict"}}) account conflict
*
* @return({"cookie","uid","$uid","+365 days","/"}) uid
* @return user's id
* {"uid":"1233"}
*/
public function createUser(&$uid, $token, $account, $alias, $password, $avatar = null)
{
$tokens = $this->factory->create('Tokens');
$token = $tokens->getToken($token);
Verify::isTrue(!$token['uid'], new BadRequest('invalid token'));
Verify::isTrue($token['account'] == $account, new Forbidden('invalid mobile ' . $account));
if ($avatar) {
$avatar = $this->uploadAvatar($avatar);
} else {
$avatar = '';
}
$pdo = $this->db;
$pdo->beginTransaction();
try {
//is account conflict
$res = Sql::select('uid')->from('uc_members')->where('username = ? OR email = ? OR mobile = ?', $account, $account, $account)->forUpdate()->get($pdo);
Verify::isTrue(count($res) == 0, new AccountConflict("account {$account} conflict"));
//is avatar conflict
$res = Sql::select('uid')->from('pre_common_member_profile')->where('realname = ?', $alias)->forUpdate()->get($pdo);
Verify::isTrue(count($res) == 0, new AliasConflict("alias {$alias} conflict"));
$uid = Sql::insertInto('uc_members')->values(['username' => $account, 'password' => $password, 'regdate' => Sql::native('UNIX_TIMESTAMP(now())'), 'salt' => ''])->exec($pdo)->lastInsertId();
Sql::insertInto('pre_common_member_profile')->values(['realname' => $alias, 'uid' => $uid, 'avatar' => $avatar])->exec($pdo);
$pdo->commit();
} catch (Exception $e) {
Logger::warning("createUser({$account}) failed with " . $e->getMessage());
$pdo->rollBack();
throw $e;
}
$token['uid'] = $uid;
$tokens->updateToken($token, $token);
return ['uid' => $uid];
}