Browse Source

注册接口

master
xyiege 1 year ago
parent
commit
c1b2511f05
  1. 26
      app/api/common.php
  2. 8
      app/api/controller/Passport.php
  3. 18
      app/api/service/UserService.php

26
app/api/common.php

@ -1,14 +1,14 @@
<?php
/**
* 生成随机字符串
* make salt
* @param int $len
*/
function makeSalt(int $len){
$ss = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$salt = '';
for ($i = 0; $i < $len; $i++) {
$salt .= $ss[mt_rand(0, strlen($ss) - 1)];
}
return $salt;
}
/**
* 随机盐值
* @param int $len
* @return string
*/
function makeSalt(int $len){
$ss = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$salt = '';
for ($i = 0; $i < $len; $i++) {
$salt .= $ss[mt_rand(0, strlen($ss) - 1)];
}
return $salt;
}

8
app/api/controller/Passport.php

@ -40,13 +40,11 @@ class Passport extends ApiController{
if (!$this->request->isPost()) {
return $this->renderError('不支持GET请求');
}
$data = $this->postData();
$model = new UserService;
//
$data = $this->postData();
if (($userInfo = $model->register($data['uname'],$data['upass'])) === false) {
return $this->renderSuccess("登录成功");
if (($userInfo = $model->register($this->postData())) === false) {
return $this->renderError($model->getError() ?: '注册失败');
}
return $this->renderSuccess("注册成功");
}
}

18
app/api/service/UserService.php

@ -1,4 +1,5 @@
<?php
declare (strict_types=1);
namespace app\api\service;
use app\api\model\User;
@ -33,10 +34,11 @@ class UserService {
}
return false;
}
/**
* 注册用户
* @param array $arr 传递过来的注册数组
*/
/**
* @param array $arr
* @return bool
*/
public function register(array $arr){
// 密码加密
$arr['password'] = password($arr['password']);
@ -45,9 +47,9 @@ class UserService {
$dtime =time();
$arr['create_time'] = $dtime;
$arr['update_time'] = $dtime;
$user = new User();
$user->save($arr);
return true;
// 保存
$model = new User;
$uid = $model->save($arr);
return isset($uid)?true:false;
}
}
Loading…
Cancel
Save