123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Log;
- class User extends Model
- {
- protected $table = "users";
- public $timestamps = false;
- public function CreateUser(array $params)
- {
- // todo 是否需要保证用户名唯一的功能
- $this->username = $params["username"];
- $this->password = $params["password"];
- $this->nickname = $params["nickname"];
- $this->icon = $params["icon"];
- $this->tel = $params["tel"];
- $this->email = $params["email"];
- if ($this->username == "") {
- return ["code" => EMPTY_USER_NAME];
- }
- if ($this->password == "") {
- return ["code" => EMPTY_USER_PASSWORD];
- }
- if ($this->nickname == "") {
- $this->nickname = $this->username;
- $params["nickname"] = $this->username;
- }
- // todo 这里需要对密码加密
- $this->save();
- unset($params["password"]);
- $params["id"] = $this->getQueueableId();
- return ["code" => SUCCESS, "data" => $params];
- }
- public function ChangePassword($uid, $oldPwd, $newPwd)
- {
- }
- public function ModifyUser(array $params)
- {
- $update = [];
- $uid = $params["id"];
- if ($uid == "") {
- return "empty user id";
- }
- // check if user exist
- $user = $this->where("id", $uid)->where("is_del", false)->first();
- if (!$user) {
- return INVALID_USER_ID;
- }
- if ($params["username"] != "") {
- $update["username"] = $params["username"];
- }
- if ($params["nickname"] != "") {
- $update["nickname"] = $params["nickname"];
- }
- if ($params["icon"] != "") {
- $update["icon"] = $params["icon"];
- }
- if ($params["tel"] != "") {
- $update["tel"] = $params["tel"];
- }
- if ($params["email"] != "") {
- $update["email"] = $params["email"];
- }
- if (count($update) == 0) {
- return "nothing to update";
- }
- $this->where("id", $uid)->where("is_del", false)->update($update);
- return SUCCESS;
- }
- public function DeleteUser($uid)
- {
- if ($uid == "") {
- return "empty user id";
- }
- $user = $this->where("id", $uid)->where("is_del", false)->first();
- if (!$user) {
- return 110;
- }
- $this->where("id", $uid)->where("is_del", false)->update(["is_del" => true]);
- return 0;
- }
- public function ListUser($page, $pageCount, $keyword)
- {
- if ($page < 1) {
- $page = 15;
- }
- if ($pageCount > 15 || $pageCount < 1) {
- $pageCount = 15;
- }
- $data = $this->select("id", "username", "nickname", "icon", "tel", "email")
- ->where("is_del", false)->orderBy("created_at", "asc")
- ->paginate($pageCount, ["*"], "page", $page);
- return $data;
- }
- public function ListUserByIds($ids)
- {
- $users = $this->select("id", "username", "nickname", "icon")
- ->whereIn("id", $ids)->where("is_del", false)->get();
- return $users;
- }
- public function ListUserByIds_KV($ids)
- {
- $users = $this->ListUserByIds($ids);
- $data = [];
- foreach ($users as $user) {
- $data[$user["id"]] = $user;
- }
- return $data;
- }
- }
|