This is the 30th day of my participation in the August Text Challenge.More challenges in August
Edit user information
The function is not complicated, we only do nickname and gender editing, so the front end of the parameters only nick_name and sex, directly update the user’s two fields can be.
/** * updateUserInfo function * Updates user information *@return string
*/
public function updateUserInfo(Request $request)
{
// Get all the arguments passed in
$request = $request->all();
// Check parameters
if (empty($request['nick_name']) {return Util::ajaxMsg('1'.'Incoming argument cannot be null');
}
$row = User::where('id', JWTAuth::parseToken()->touser()->id)
->update([
'nick_name'= >$request['nick_name']]);return $row= =1
? Util::ajaxMsg('0'.'Information modified successfully', User::where('id', JWTAuth::parseToken()->touser()->id)->get())
: Util::ajaxMsg('1'.'Failed to modify information');
}
Copy the code
Add to a routing group
// Update user information
$router->post('/user/updateUserInfo'.'UserController@updateUserInfo');
Copy the code
The latest user information is returned to the front end. The front end needs to update the local cache.
Interface for user to modify password
This is also an update operation, but before updating the data, we need to verify that the old password passed is correct. The code is as follows:
/** * resetPassword function * User changes password *@param Request $request
* @return void* /
public function resetPassword(Request $request)
{
// Get all the arguments passed in
$request = $request->all();
if (empty($request['old_password']) || empty($request['password']) {return Util::ajaxMsg('1'.'Old password and new password cannot be empty');
}
$acount['id'] = JWTAuth::parseToken()->touser()->id;
$acount['password'] = $request['old_password'];
if(! JWTAuth::attempt($acount)) {return Util::ajaxMsg('1'.'Old password entered incorrectly');
}
if (empty($request['password') | |! Util::isPsw($request['password']) {return Util::ajaxMsg('1'.'Passwords must be 6-10 characters, including numbers and case');
}
$row = User::where('id'.'=', $acount['id'])->update([
'password' => Hash::make($request['password']),]);return $row == 1 ? Util::ajaxMsg('0'.'Password changed successfully') : Util::ajaxMsg('1'.'Failed to change password');
}
// The user changes the password
$router->post('/user/resetPassword'.'UserController@resetPassword');
Copy the code
List module interface
With the user module interface example above, it is much easier to write the add, delete, change and check list. Again, we create a new todoController.php file and write the corresponding methods: Get to-do list (getTodoList), Add To-do list (addTodo), Delete To-do list (delTodo), update To-do list (updateTodo).
Get a to-do list
/** * getTodoList function *@return void
*/
public function getTodoList()
{
$data = todo_list::where('user_id', JWTAuth::parseToken()->touser()->id)
->select('todo_id'.'content'.'status')
->orderBy('status')
->simplePaginate(20);
return Util::ajaxMsg('0'.'Obtain success'.$data);
}
Copy the code
New matters
/** * addTodo function * New to-do list *@param Request $request
* @return void
*/
public function addTodo(Request $request)
{
// Get all the arguments passed in
$request = $request->all();
// Check parameters
if (empty($request['content']) {return Util::ajaxMsg('1'.'Incoming argument cannot be null');
}
$newTodo = new todo_list;
$newTodo->user_id = JWTAuth::parseToken()->touser()->id; // Generate an 11-bit TODO_ID
$newTodo->todo_id = Util::createId(); // Generate an 11-bit TODO_ID
$newTodo->content = $request['content']; // Incoming mailbox
$newTodo->status = 1; // Status, 1- incomplete, 2- completed
return $newTodo->save()
? Util::ajaxMsg('0'.'Added successfully')
: Util::ajaxMsg('1'.'New failure');
}
Copy the code
Update agenda
/** * updateTodo function *@param Request $request
* @return void
*/
public function updateTodo(Request $request)
{
// Get all the arguments passed in
$request = $request->all();
// Check parameters
if (empty($request['status') | |empty($request['todo_id']) {return Util::ajaxMsg('1'.'Incoming argument cannot be null');
}
$row = todo_list::where([
['user_id', JWTAuth::parseToken()->touser()->id],
['todo_id'.$request['todo_id']],
])
->update([
'status'= >$request['status']]);return $row= =1
? Util::ajaxMsg('0'.'Information modified successfully')
: Util::ajaxMsg('1'.'Failed to modify information');
}
Copy the code
Delete items
/** * delTodo function *@param Request $request
* @return void
*/
public function delTodo(Request $request)
{
// Get all the arguments passed in
$request = $request->all();
// Check parameters
if (empty($request['todo_id']) {return Util::ajaxMsg('1'.'Incoming argument cannot be null');
}
$row = todo_list::where([
['user_id', JWTAuth::parseToken()->touser()->id],
['todo_id'.$request['todo_id']],
])
->delete();
return $row= =1
? Util::ajaxMsg('0'.'Deleted successfully')
: Util::ajaxMsg('1'.'Delete failed');
}
Copy the code
Add routes corresponding to the preceding interfaces
// Get the to-do list
$router->get('/todo/getTodoList'.'TodoController@getTodoList');
// Add a to-do list
$router->post('/todo/addTodo'.'TodoController@addTodo');
// Update the list
$router->post('/todo/updateTodo'.'TodoController@updateTodo');
// Delete the to-do list
$router->post('/todo/delTodo'.'TodoController@delTodo');
Copy the code
Welcome to other articles
- Internationalize your website with Vite2+Vue3 | August Update Challenge
- Actual combat: The front-end interface request parameters are confused
- Practice: Implement a Message component with Vue3
- Actual combat: Vue3 Image components, incidentally support lazy loading
- What updates does One Piece, vue.js 3.0 bring
- An article digests the major new features of ES7, ES8, and ES9
- Common problems and solutions for technical teams
- Introduction to 10 common new features in ES6
- Vue3 script Setup syntax is really cool