Withdraw.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use app\common\model\User;
  5. use app\common\Enum\WithdrawEnum;
  6. class Withdraw extends Model
  7. {
  8. protected $name = 'shop_withdraw';
  9. // 开启自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $type = [
  15. 'withdraw_info' => 'json'
  16. ];
  17. // 追加属性
  18. protected $append = [
  19. 'status_text',
  20. 'status_style',
  21. 'charge_rate_format',
  22. 'withdraw_info_hidden',
  23. 'withdraw_type_text',
  24. 'wechat_transfer_state_text',
  25. ];
  26. // 注:微信转账状态和可退还佣金状态已移至 WithdrawEnum 枚举类
  27. public function statusList()
  28. {
  29. return WithdrawEnum::getStatusList();
  30. }
  31. public function withdrawTypeList()
  32. {
  33. return WithdrawEnum::getTypeList();
  34. }
  35. /**
  36. * 获取微信转账状态列表
  37. */
  38. public function wechatTransferStateList()
  39. {
  40. return WithdrawEnum::getWechatTransferStateList();
  41. }
  42. /**
  43. * 获取可以安全退还佣金的状态列表
  44. */
  45. public function canCancelStates()
  46. {
  47. return WithdrawEnum::getCanCancelStates();
  48. }
  49. /**
  50. * 检查是否可以退还佣金
  51. */
  52. public function canRefundCommission()
  53. {
  54. return in_array($this->wechat_transfer_state, WithdrawEnum::getCanCancelStates());
  55. }
  56. /**
  57. * 状态获取器
  58. */
  59. public function getStatusTextAttr($value, $data)
  60. {
  61. $value = $value ?: ($data['status'] ?? null);
  62. return WithdrawEnum::getStatusName($value);
  63. }
  64. /**
  65. * 状态样式获取器
  66. */
  67. public function getStatusStyleAttr($value, $data)
  68. {
  69. $value = $value ?: ($data['status'] ?? null);
  70. return WithdrawEnum::getStatusStyle($value);
  71. }
  72. /**
  73. * 类型获取器
  74. */
  75. public function getWithdrawTypeTextAttr($value, $data)
  76. {
  77. $value = $value ?: ($data['withdraw_type'] ?? null);
  78. return WithdrawEnum::getTypeName($value);
  79. }
  80. public function getWechatTransferStateTextAttr($value, $data)
  81. {
  82. $value = $value ?: ($data['wechat_transfer_state'] ?? null);
  83. return WithdrawEnum::getWechatTransferStateName($value);
  84. }
  85. public function getChargeRateFormatAttr($value, $data)
  86. {
  87. $value = $value ?: ($data['charge_rate'] ?? null);
  88. // 安全处理手续费率格式化
  89. if (is_numeric($value) && $value !== null && $value !== '') {
  90. return bcmul((string)$value, '100', 1) . '%';
  91. } else {
  92. return '0%';
  93. }
  94. }
  95. public function getWithdrawInfoHiddenAttr($value, $data)
  96. {
  97. $withdraw_info = $value ?: ($this->withdraw_info ?? null);
  98. foreach ($withdraw_info as $key => &$info) {
  99. if (in_array($key, ['微信用户', '真实姓名'])) {
  100. $info = string_hide($info, 2);
  101. } elseif (in_array($key, ['银行卡号', '支付宝账户', '微信ID'])) {
  102. $info = account_hide($info);
  103. }
  104. }
  105. return $withdraw_info;
  106. }
  107. public function user()
  108. {
  109. return $this->belongsTo(User::class, 'user_id', 'id')->field('id, username, nickname, avatar, total_consume');
  110. }
  111. }