User.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User:林志杰
  5. * Email:[email protected]
  6. * Motto:纵有疾风起,人生不言弃!
  7. * Time:2020/1/3 0:29
  8. */
  9. namespace app\common\model\mysql;
  10. use think\Model;
  11. class User extends Model
  12. {
  13. /**
  14. * 自动生成写入时间
  15. * @var bool
  16. */
  17. protected $autoWriteTimestamp = true;
  18. /**
  19. * 根据手机号获取用户信息
  20. * @param $phoneNumber
  21. * @return array|bool|Model|null
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function getUserByPhoneNumber($phoneNumber)
  27. {
  28. if (empty($phoneNumber)) {
  29. return false;
  30. }
  31. $where = [
  32. 'phone_number' => $phoneNumber
  33. ];
  34. return $this->where($where)->find();
  35. }
  36. /**
  37. * 通过id获取用户数据
  38. * @param int $id
  39. * @return array|bool|Model|null
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getUserById(int $id)
  45. {
  46. if (!$id) {
  47. return false;
  48. }
  49. return $this->find($id);
  50. }
  51. /**
  52. * 根据username获取用户数据
  53. * @param string $username
  54. * @return array|bool|Model|null
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function getUserByUsername(string $username)
  60. {
  61. if (empty($username)) {
  62. return false;
  63. }
  64. $where = [
  65. 'username' => $username
  66. ];
  67. return $this->where($where)->find();
  68. }
  69. /**
  70. * 根据主键id更新数据表中的数据
  71. * @param $id
  72. * @param $data
  73. * @return bool
  74. */
  75. public function updateById($id, $data)
  76. {
  77. $id = (int)$id;
  78. if (empty($id) || empty($data) || !is_array($data)) {
  79. return false;
  80. }
  81. $where = [
  82. 'id' => $id,
  83. ];
  84. return $this->where($where)->save($data);
  85. }
  86. }