WxUser.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Models;
  3. class WxUser extends BaseModel {
  4. const CREATED_AT = 'createTime';
  5. protected $table = "user";
  6. const UPDATED_AT = 'updateTime';
  7. public $timestamps = true;
  8. protected $primaryKey = "id";
  9. /**
  10. * 检测UID是否在表中
  11. *
  12. * @param [type] $uid
  13. * @return void
  14. */
  15. function checkUser($uid) {
  16. $data = $this->where('uid', $uid)->first();
  17. if (!$data) {
  18. return 0;
  19. }
  20. return $data->id;
  21. }
  22. function getUser($user_id) {
  23. $data = $this->where('id', $user_id)->first();
  24. if (!$data) {
  25. return -1;
  26. }
  27. return $data;
  28. }
  29. function addUser($uid, $user) {
  30. if (empty($uid) || empty($user)) {
  31. return -1;
  32. }
  33. if (!$user = $this->checkUser($uid)) {
  34. $this->userName = $user;
  35. $this->uid = $uid;
  36. $this->point = 0;
  37. $this->totalPoint = 0;
  38. $this->save();
  39. return $this->id;
  40. }
  41. return $user;
  42. }
  43. function queryPoint($user_id) {
  44. $user = $this->getUser($user_id);
  45. if ($user->id < 0) {
  46. return -3200;
  47. }
  48. return array('totalPoint' => $user['point']);
  49. }
  50. function upPoint($user_id, $point) {
  51. $user = $this->getUser($user_id);
  52. if ($user->id < 0) {
  53. return -3200;
  54. }
  55. $model = $this->find($user_id);
  56. $model->point = $user->point + $point;
  57. $model->totalPoint = $user->totalPoint + $point;
  58. $model->save();
  59. return array('totalPoint' => $model->point, 'point' => $point);
  60. }
  61. function downPoint($user_id, $point) {
  62. $user = $this->getUser($user_id);
  63. if ($user->id < 0) {
  64. return -3200;
  65. }
  66. if ($user->point < $point) {
  67. return -3203;
  68. }
  69. $add_point = $point;
  70. $model = $this->find($user_id);
  71. $model->point = $user->point - $point;
  72. $model->save();
  73. return array('totalPoint' => $model->point, 'point' => $point);
  74. }
  75. }