Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

semour / semour_web

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • semour_web
  • app
  • Http
  • Controllers
  • Api
  • UserApiController.php
UserApiController.php 3.7 KB
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
1 2 3 4
<?php

namespace App\Http\Controllers\Api;

杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
5
use App\Http\Requests\UserUpdate;
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
6
use App\Models\UserModel;
杨树贤's avatar
地址接口不分页
e42b1a4e
 
杨树贤 committed 2 years ago
7
use http\Client\Curl\User;
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;

class UserApiController extends Controller
{
    public function info(Request $request)
    {
        $user = $request->user();
        $user = \Arr::only($user->toArray(), [
            'id',
            'name',
            'email',
            'phone',
            'account_properties',
            'company_name',
            'first_name',
            'last_name',
        ]);
        return $this->setSuccessData($user);
    }

杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
35
    public function update(UserUpdate $request)
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
36 37 38 39 40 41 42 43 44 45 46
    {
        $data = $request->only([
            'account_properties',
            'company_name',
            'first_name',
            'last_name',
        ]);
        $id = $request->user->id;
        $data['update_time'] = time();
        $result = UserModel::where('id', $id)->update($data);
        if (!$result) {
杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
47
            return $this->setError('Update user info failed');
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
48 49
        }

杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
50
        return $this->setSuccess('Update user info success');
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }

    public function changePhone(Request $request)
    {
        $password = $request->input('password');
        $phone = $request->input('phone');
        $validator = Validator::make($request->all(), [
            'phone' => 'required',
            'password' => 'required|max:100'
        ]);

        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $userId = Auth::user()->id;
        $hashedPassword = UserModel::where('id', $userId)->value('password');
        if (!Hash::check($password, $hashedPassword)) {
            return $this->setError('Wrong Password!');
        }

        $result = UserModel::where('id', $userId)->update(['phone' => $phone]);
        if (!$result) {
            return $this->setError('Change phone failed');
        }

        return $this->setSuccess('Change phone success');
    }

    public function changeEmail(Request $request)
    {
        $password = $request->input('password');
        $email = $request->input('email');
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
85 86
            'password' => 'required|max:100',
            'email_code' => 'required'
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        ]);

        if ($validator->fails()) {
            return $this->setError($validator->errors()->first());
        }
        $userId = Auth::user()->id;
        $hashedPassword = UserModel::where('id', $userId)->value('password');
        if (!Hash::check($password, $hashedPassword)) {
            return $this->setError('Wrong Password!');
        }

        //发送邮箱验证码
        $redisKey = 'sem_email_code_change_email' . '_' . $email;
        $cachedEmailCode = Redis::get($redisKey);
        if ($cachedEmailCode != $request->input('email_code')) {
            return $this->setError('Email code invalid');
        }

杨树贤's avatar
地址接口不分页
e42b1a4e
 
杨树贤 committed 2 years ago
105 106 107 108 109 110 111
        //先判断下邮箱是否存在
        $existEmail = UserModel::where('email', $email)->exists();
        if ($existEmail) {
            return $this->setError('Email has been registered');
        }

        $result = UserModel::where('id', $userId)->update(['email' => $email,'update_time' => time()]);
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
112 113 114
        if (!$result) {
            return $this->setError('Change email failed');
        }
杨树贤's avatar
新增忘记密码流程接口
d664f163
 
杨树贤 committed 2 years ago
115
        Redis::del($redisKey);
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
116 117 118 119 120
        return $this->setSuccess('Change email success');
    }


}