hyperf-chat/app/Service/RobotService.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2021-07-08 22:28:51 +08:00
<?php
namespace App\Service;
2021-08-31 22:33:41 +08:00
use App\Helpers\HashHelper;
2021-07-08 22:28:51 +08:00
use App\Model\Robot;
use App\Model\User;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Str;
class RobotService
{
/**
* @param array $data
* @return bool|array
*/
public function create(array $data)
{
Db::beginTransaction();
try {
$user = User::create([
'mobile' => '100' . mt_rand(1000, 9999) . mt_rand(1000, 9999),
2021-08-28 23:41:01 +08:00
'password' => HashHelper::make(Str::random(10)),
2021-07-08 22:28:51 +08:00
'is_robot' => 1
]);
$robot = Robot::create([
'user_id' => $user->id,
'robot_name' => $data['robot_name'],
'describe' => $data['describe'],
'logo' => $data['logo'],
'is_talk' => $data['is_talk'],
'status' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
Db::commit();
} catch (\Exception $e) {
Db::rollBack();
return false;
}
return $robot->toArray();
}
}