1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Modules\Admin\Services;
- use App\Base\BaseService;
- use App\Exceptions\ClientException;
- use App\Models\Auth\AdminPermission;
- use App\Models\User\User;
- use Illuminate\Support\Facades\Auth;
- class AuthService extends BaseService
- {
- public function login($data)
- {
- $u = User::wherePhone($data['username'])->orWhere("email", $data['username'])->first();
- if (is_null($u)) {
- throw new ClientException("账号或密码错误,请重新输入");
- }
- if (!$u->checkPassword($data['password'])) {
- throw new ClientException("账号或密码错误,请重新输入!");
- }
- if ($u->status != User::STATUS_OK) {
- throw new ClientException("当前用户被禁用,请联系管理员");
- }
- if (!$u->group_id) {
- throw new ClientException("无权限");
- }
- return [
- "admin_token" => $this->encryptToken($u->id),
- ];
- }
- public function profile()
- {
- $u = Auth::user();
- return [
- "id" => $u->id,
- "phone" => $u->phone,
- "email" => $u->email,
- "group" => $u->group->name,
- "roles" => $this->role($u),
- ];
- }
- public function role(User $u)
- {
- $permissions = \Arr::get($u->extra, "permissions");
- return array_map(function ($pid) {
- $per = AdminPermission::find($pid);
- return [
- "id" => $per->id,
- "name" => $per->name,
- "code" => $per->code,
- ];
- }, $permissions);
- }
- public function changePassword($data)
- {
- $u = Auth::user();
- if (!$u->checkPassword($data['old_password'])) {
- throw new ClientException("原密码错误");
- }
- $u->password = $u->hashPassword($data['password']);
- $u->save();
- return true;
- }
- public function encryptToken($uid)
- {
- return md5($uid) . $uid;
- }
- public function decryptToken($token)
- {
- return substr($token, 32);
- }
- }
|