User.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * 根据主键id更新数据表中的数据
  53. * @param $id
  54. * @param $data
  55. * @return bool
  56. */
  57. public function updateById($id, $data)
  58. {
  59. $id = (int)$id;
  60. if (empty($id) || empty($data) || !is_array($data)) {
  61. return false;
  62. }
  63. $where = [
  64. 'id' => $id,
  65. ];
  66. return $this->where($where)->save($data);
  67. }
  68. }