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.41 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 7 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
use App\Models\UserModel;
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
34
    public function update(UserUpdate $request)
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
35 36 37 38 39 40 41 42 43 44 45
    {
        $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
46
            return $this->setError('Update user info failed');
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
47 48
        }

杨树贤's avatar
地址管理相关接口以及国家数据
da4bb473
 
杨树贤 committed 2 years ago
49
        return $this->setSuccess('Update user info success');
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
50 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
    }

    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
84 85
            'password' => 'required|max:100',
            'email_code' => 'required'
杨树贤's avatar
修改用户信息相关接口
fcc22723
 
杨树贤 committed 2 years ago
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        ]);

        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');
        }

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

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


}