User.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Ceeen Wise System
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2007-2017 http://fely.cc All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Date: 2017/6/6 Time: 0:25
  8. // +----------------------------------------------------------------------
  9. // | Author: fely <fely@fely.cc>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\jarc;
  12. use think\Model;
  13. /**
  14. * Class Config 系统配置
  15. * @package common\model
  16. */
  17. class User extends Model
  18. {
  19. // 直接使用配置参数名
  20. protected $connection = 'db_jarc';
  21. /**
  22. * 更改积分
  23. */
  24. public function changeScore($score, $comment, $action = '', $union_id = '')
  25. {
  26. if (empty($union_id)) {
  27. return ['code' => 0, 'msg' => '用户不存在'];
  28. }
  29. $this->startTrans();
  30. try {
  31. $user = $this->lock(true)->where('union_id', $union_id)->find();
  32. if (empty($user)) {
  33. throw new \Exception("用户不存在");
  34. }
  35. //判断分数是否足够
  36. if ($score < 0 && $user['score'] < abs($score)) {
  37. throw new \Exception("积分不足,您的积分只有{$user['score']}!");
  38. }
  39. //记录日志
  40. UserScoreLog::create([
  41. 'user_id' => $user['id'],
  42. 'create_time' => time(),
  43. 'action' => $action,
  44. 'score' => $score,
  45. 'score_before' => $user['score'],
  46. 'score_after' => $user['score'] + $score,
  47. 'comment' => $comment,
  48. ]);
  49. $user->score = $user['score'] + $score;
  50. $user->save();
  51. $this->commit();
  52. } catch (\Exception $e) {
  53. $this->rollback();
  54. return ['code' => 0, 'msg' => $e->getMessage()];
  55. }
  56. return ['code' => 1, 'msg' => '操作成功'];
  57. }
  58. /**
  59. * 状态
  60. */
  61. public static function isEnabled($user_id = 0)
  62. {
  63. return self::where(['id' => $user_id, 'user_status' => 1])->count() > 0;
  64. }
  65. }