控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Member.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 会员模型
  13. // +----------------------------------------------------------------------
  14. namespace app\member\model;
  15. use app\member\service\User;
  16. use think\Db;
  17. use think\Model;
  18. class Member extends Model
  19. {
  20. protected $autoWriteTimestamp = true;
  21. protected $updateTime = false;
  22. protected $createTime = 'reg_time';
  23. protected $insert = ['reg_ip', 'last_login_ip', 'last_login_time'];
  24. protected function setRegIpAttr()
  25. {
  26. return request()->ip();
  27. }
  28. protected function setLastLoginIpAttr()
  29. {
  30. return request()->ip();
  31. }
  32. protected function setLastLoginTimeAttr()
  33. {
  34. return time();
  35. }
  36. protected function setBirthdayAttr($value)
  37. {
  38. return $value ? $value : null;
  39. }
  40. public function getLastLoginTimeAttr($value, $data)
  41. {
  42. return time_format($data['last_login_time']);
  43. }
  44. public function getGroupnameAttr($value, $data)
  45. {
  46. $group = cache("Member_Group");
  47. return isset($group[$data['groupid']]['name']) ? $group[$data['groupid']]['name'] : '';
  48. }
  49. /**
  50. * 变更会员余额
  51. * @param int $amount 余额
  52. * @param int $user_id 会员ID
  53. * @param string $remark 备注
  54. */
  55. public static function amount($amount, $user_id, $remark)
  56. {
  57. Db::startTrans();
  58. try {
  59. $user = self::lock(true)->find($user_id);
  60. if ($user && $amount != 0) {
  61. $before = $user->amount;
  62. //$after = $user->amount + $amount;
  63. $after = function_exists('bcadd') ? bcadd($user->amount, $amount, 2) : $user->amount + $amount;
  64. //更新会员信息
  65. $user->save(['amount' => $after]);
  66. //写入日志
  67. AmountLog::create(['user_id' => $user_id, 'amount' => $amount, 'before' => $before, 'after' => $after, 'remark' => $remark]);
  68. }
  69. Db::commit();
  70. } catch (\Exception $e) {
  71. Db::rollback();
  72. }
  73. }
  74. /**
  75. * 变更会员积分
  76. * @param int $point 积分
  77. * @param int $user_id 会员ID
  78. * @param string $remark 备注
  79. */
  80. public static function point($point, $user_id, $remark)
  81. {
  82. Db::startTrans();
  83. try {
  84. $user = self::lock(true)->find($user_id);
  85. if ($user && $point != 0) {
  86. $before = $user->point;
  87. $after = $user->point + $point;
  88. $groupid = User::instance()->get_usergroup_bypoint($after);
  89. //更新会员信息
  90. $user->save(['point' => $after, 'groupid' => $groupid]);
  91. //写入日志
  92. PointLog::create(['user_id' => $user_id, 'point' => $point, 'before' => $before, 'after' => $after, 'remark' => $remark]);
  93. }
  94. Db::commit();
  95. } catch (\Exception $e) {
  96. Db::rollback();
  97. }
  98. }
  99. /**
  100. * 获取头像
  101. * @param $value
  102. * @param $data
  103. * @return string
  104. */
  105. public function getAvatarAttr($value, $data)
  106. {
  107. $memberConfig = get_addon_config("member");
  108. if (!$value) {
  109. $value = $memberConfig['user_letter_avatar'] ? letter_avatar($data['nickname']) : config('public_url') . 'static/addons/member/img/avatar.png';
  110. }
  111. return cdnurl($value, true);
  112. }
  113. }