User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\relation\HasOne;
  15. use think\model\relation\HasMany;
  16. use think\model\relation\BelongsTo;
  17. use app\common\model\user\PointsLog as PointsLogModel;
  18. /**
  19. * 用户模型类
  20. * Class User
  21. * @package app\common\model
  22. */
  23. class User extends BaseModel
  24. {
  25. // 定义表名
  26. protected $name = 'user';
  27. // 定义主键
  28. protected $pk = 'user_id';
  29. // 性别
  30. private $gender = [0 => '未知', 1 => '男', 2 => '女'];
  31. /**
  32. * 关联用户头像表
  33. * @return HasOne
  34. */
  35. public function avatar(): HasOne
  36. {
  37. return $this->hasOne('UploadFile', 'file_id', 'avatar_id')
  38. ->bind(['avatar_url' => 'preview_url']);
  39. }
  40. /**
  41. * 关联会员等级表
  42. * @return BelongsTo
  43. */
  44. public function grade(): BelongsTo
  45. {
  46. $module = self::getCalledModule();
  47. return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
  48. }
  49. /**
  50. * 关联收货地址表
  51. * @return HasMany
  52. */
  53. public function address(): HasMany
  54. {
  55. return $this->hasMany('UserAddress');
  56. }
  57. /**
  58. * 关联收货地址表 (默认地址)
  59. * @return BelongsTo
  60. */
  61. public function addressDefault(): BelongsTo
  62. {
  63. return $this->belongsTo('UserAddress', 'address_id');
  64. }
  65. /**
  66. * 获取器:显示性别
  67. * @param $value
  68. * @return string
  69. */
  70. public function getGenderAttr($value): string
  71. {
  72. return $this->gender[$value];
  73. }
  74. /**
  75. * 修改器:登录密码
  76. * 注意:写入时自动使用 password_hash(bcrypt) 加密,数据库中不保存明文密码
  77. * @param mixed $value 明文密码
  78. * @return string
  79. */
  80. public function setPasswordAttr($value): string
  81. {
  82. $value = (string)$value;
  83. // 空密码不做加密 (避免生成无效哈希)
  84. if ($value === '') {
  85. return '';
  86. }
  87. return password_hash($value, PASSWORD_BCRYPT);
  88. }
  89. /**
  90. * 校验登录密码
  91. * @param string $password 用户输入的明文密码
  92. * @return bool
  93. */
  94. public function verifyPassword(string $password): bool
  95. {
  96. // 未设置密码的账号一律校验失败
  97. $hash = (string)$this->getData('password');
  98. if ($hash === '') {
  99. return false;
  100. }
  101. return password_verify($password, $hash);
  102. }
  103. /**
  104. * 是否已设置登录密码
  105. * @return bool
  106. */
  107. public function hasPassword(): bool
  108. {
  109. return (string)$this->getData('password') !== '';
  110. }
  111. /**
  112. * 获取用户信息
  113. * @param $where
  114. * @param array $with
  115. * @return static|array|null
  116. */
  117. public static function detail($where, array $with = [])
  118. {
  119. $filter = ['is_delete' => 0];
  120. if (is_array($where)) {
  121. $filter = \array_merge($filter, $where);
  122. } else {
  123. $filter['user_id'] = (int)$where;
  124. }
  125. return static::get($filter, $with);
  126. }
  127. /**
  128. * 累积用户的实际消费金额
  129. * @param int $userId
  130. * @param float $expendMoney
  131. * @return mixed
  132. */
  133. public static function setIncUserExpend(int $userId, float $expendMoney)
  134. {
  135. return (new static)->myInc($userId, 'expend_money', $expendMoney);
  136. }
  137. /**
  138. * 累积用户可用余额
  139. * @param int $userId
  140. * @param float $money
  141. * @return mixed
  142. */
  143. public static function setIncBalance(int $userId, float $money)
  144. {
  145. return (new static)->myInc($userId, 'balance', $money);
  146. }
  147. /**
  148. * 消减用户可用余额
  149. * @param int $userId
  150. * @param float $money
  151. * @return mixed
  152. */
  153. public static function setDecBalance(int $userId, float $money)
  154. {
  155. return (new static)->myDec([['user_id', '=', $userId]], 'balance', $money);
  156. }
  157. /**
  158. * 指定会员等级下是否存在用户
  159. * @param int $gradeId
  160. * @return bool
  161. */
  162. public static function checkExistByGradeId(int $gradeId): bool
  163. {
  164. $model = new static;
  165. return (bool)$model->where('grade_id', '=', (int)$gradeId)
  166. ->where('is_delete', '=', 0)
  167. ->value($model->getPk());
  168. }
  169. /**
  170. * 指定的手机号是否已存在
  171. * @param string $mobile
  172. * @return bool
  173. */
  174. public static function checkExistByMobile(string $mobile): bool
  175. {
  176. $model = new static;
  177. return (bool)$model->where('mobile', '=', $mobile)
  178. ->where('is_delete', '=', 0)
  179. ->value($model->getPk());
  180. }
  181. /**
  182. * 累积用户总消费金额
  183. * @param int $userId
  184. * @param float $money
  185. * @return mixed
  186. */
  187. public static function setIncPayMoney(int $userId, float $money)
  188. {
  189. return (new static)->myInc($userId, 'pay_money', $money);
  190. }
  191. /**
  192. * 累积用户实际消费的金额 (批量)
  193. * @param array $data
  194. * @return bool
  195. */
  196. public function onBatchIncExpendMoney(array $data): bool
  197. {
  198. foreach ($data as $userId => $expendMoney) {
  199. static::setIncUserExpend($userId, (float)$expendMoney);
  200. }
  201. return true;
  202. }
  203. /**
  204. * 累积用户的可用积分数量 (批量)
  205. * @param array $data
  206. * @return bool
  207. */
  208. public function onBatchIncPoints(array $data): bool
  209. {
  210. foreach ($data as $userId => $value) {
  211. $this->myInc($userId, 'points', $value);
  212. }
  213. return true;
  214. }
  215. /**
  216. * 累积用户的可用积分
  217. * @param int $userId 用户ID
  218. * @param int $points 累计的积分
  219. * @param string $describe 积分描述
  220. * @param int|null $storeId
  221. * @return mixed
  222. */
  223. public static function setIncPoints(int $userId, int $points, string $describe, ?int $storeId = null)
  224. {
  225. // 新增积分变动明细
  226. PointsLogModel::add([
  227. 'user_id' => $userId,
  228. 'value' => $points,
  229. 'describe' => $describe,
  230. 'store_id' => $storeId ?: static::$storeId
  231. ]);
  232. // 更新用户可用积分
  233. return (new static)->myInc($userId, 'points', $points);
  234. }
  235. }