Guild.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\Db;
  5. use fast\Random;
  6. /**
  7. * 模型
  8. */
  9. class Guild extends Model
  10. {
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. /**
  16. * 创建公会
  17. */
  18. public static function createGuild($party_id,$party_name,$user_id) {
  19. Db::startTrans();
  20. try{
  21. // 获取g_id
  22. $ids = self::column("g_id");
  23. $data = [
  24. "g_id" => self::getUinqueId(4,$ids),
  25. "user_id" => $user_id,
  26. "party_id" => $party_id,
  27. "name" => $party_name."的公会",
  28. "image" => "/assets/img/guild_image.jpeg",
  29. "desc" => "请编辑公会简介内容!",
  30. "notice" => "请编辑公会公告内容!",
  31. "createtime" => time()
  32. ];
  33. $guild_id = self::insertGetId($data);
  34. // 添加工会长
  35. $data = [
  36. "guild_id" => $guild_id,
  37. "user_id" => $user_id,
  38. "role" => 2,
  39. "sign_type" => 3,
  40. "status" => 1,
  41. "sign_time" => 2147483647,
  42. "createtime" => time()
  43. ];
  44. $res2 = \app\common\model\GuildMember::insert($data);
  45. // 更新用户公会ID
  46. $res3 = \app\common\model\User::update(["guild_id"=>$guild_id],["id"=>$user_id]);
  47. if($guild_id && $res2 && $res3) {
  48. Db::commit();
  49. }
  50. }catch (ValidateException $e) {
  51. Db::rollback();
  52. } catch (PDOException $e) {
  53. Db::rollback();
  54. } catch (Exception $e) {
  55. Db::rollback();
  56. }
  57. }
  58. /**
  59. * 生成不重复的随机数字
  60. */
  61. public static function getUinqueId($length = 4,$ids = []) {
  62. $newid = Random::build("nozero",$length);
  63. if(in_array($newid,$ids)) {
  64. self::getUinqueId(4,$ids);
  65. }
  66. return $newid;
  67. }
  68. }