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

8
app/api/controller/Passport.php

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

18
app/api/service/UserService.php

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