Users.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. /**
  6. * 会员模型
  7. */
  8. class Users extends Model
  9. {
  10. // 表名
  11. protected $name = 'users';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'create_time';
  16. protected $updateTime = false;
  17. // 追加属性
  18. protected $append = [
  19. ];
  20. /**
  21. * @param $code //国际区号
  22. * @param $mobile //手机号
  23. * @return array
  24. */
  25. public static function getByCodeAndMobile($code, $mobile)
  26. {
  27. $info = self::where('code', $code)
  28. ->where('mobile', $mobile)
  29. ->find();
  30. return $info;
  31. }
  32. /**
  33. * 根据邀请码获取会员信息
  34. * @param $invitation_code
  35. * @return array|bool|\PDOStatement|string|Model|null
  36. */
  37. public static function getByInvitationCode($invitation_code)
  38. {
  39. $info = self::where('invitation_code', $invitation_code)
  40. ->find();
  41. return $info;
  42. }
  43. /**
  44. * 获取代理信息
  45. * @param $invitation_code
  46. * @return array|null
  47. */
  48. public static function getAgentRechargeInfoByAgentId($agent_id)
  49. {
  50. $data['usdt'] = '';
  51. $data['bank'] = [];
  52. $data['agent_id'] = $agent_id;
  53. $info = self::where('id', $agent_id)
  54. ->where('is_agent', 1)
  55. ->where('is_lock', 0)
  56. ->where('is_delete', 0)
  57. ->find();
  58. if(empty($info)){
  59. //没找到代理时,默认显示系统配置
  60. if((new Config())->getValue('recharge_usdt_switch')){
  61. $data['usdt'] = (new Config())->getValue('recharge_usdt_address');
  62. }
  63. if((new Config())->getValue('recharge_bank_switch')){
  64. $data['bank']['bank_name'] = (new Config())->getValue('recharge_bank_name');
  65. $data['bank']['bank_card'] = (new Config())->getValue('recharge_bank_card');
  66. $data['bank']['account_name'] = (new Config())->getValue('recharge_account_name');
  67. }
  68. $data['agent_id'] = 0;
  69. return $data;
  70. }
  71. if($info['agent_in_usdt']){
  72. $data['usdt'] = $info['agent_usdt_address'];
  73. }
  74. if($info['agent_in_bank']){
  75. $bank_info = $info['agent_bank_info'];
  76. $data['bank']['bank_name'] = $bank_info['bank_name'];
  77. $data['bank']['bank_card'] = $bank_info['bank_card'];
  78. $data['bank']['real_name'] = $bank_info['real_name'];
  79. }
  80. if(empty($data['usdt']) && empty($data['bank'])){
  81. return [];
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 获取代理信息
  87. * @param $invitation_code
  88. * @return array|null
  89. */
  90. public static function getAgentWithdrawInfoByAgentId($agent_id)
  91. {
  92. $data['agent_id'] = $agent_id;
  93. $info = self::where('id', $agent_id)
  94. ->where('is_agent', 1)
  95. ->where('is_lock', 0)
  96. ->where('is_delete', 0)
  97. ->find();
  98. if(empty($info)){
  99. //没找到代理时,默认显示系统配置
  100. $data['usdt'] = (new Config())->getValue('withdraw_usdt_switch');
  101. $data['bank'] = (new Config())->getValue('withdraw_bank_switch');
  102. $data['agent_id'] = 0;
  103. return $data;
  104. }
  105. $data['usdt'] = $info['agent_out_usdt'];
  106. $data['bank'] = $info['agent_out_bank'];
  107. return $data;
  108. }
  109. /**
  110. * 获取头像
  111. * @param string $value
  112. * @param array $data
  113. * @return string
  114. */
  115. public function getAvatarAttr($value, $data)
  116. {
  117. if (!$value) {
  118. //如果不需要启用首字母头像,请使用
  119. //$value = '/assets/img/avatar.png';
  120. $value = letter_avatar($data['nickname']);
  121. }
  122. return $value;
  123. }
  124. /**
  125. * 获取验证字段数组值
  126. * @param string $value
  127. * @param array $data
  128. * @return object
  129. */
  130. public function getVerificationAttr($value, $data)
  131. {
  132. $value = array_filter((array)json_decode($value, true));
  133. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  134. return (object)$value;
  135. }
  136. /**
  137. * 设置验证字段
  138. * @param mixed $value
  139. * @return string
  140. */
  141. public function setVerificationAttr($value)
  142. {
  143. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  144. return $value;
  145. }
  146. /**
  147. * 变更会员余额
  148. * @param int $money 余额
  149. * @param int $user_id 会员ID
  150. * @param string $memo 备注
  151. */
  152. public static function money($money, $user_id, $memo)
  153. {
  154. Db::startTrans();
  155. try {
  156. $user = self::lock(true)->find($user_id);
  157. if ($user && $money != 0) {
  158. $before = $user->money;
  159. //$after = $user->money + $money;
  160. $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
  161. //更新会员信息
  162. $user->save(['money' => $after]);
  163. //写入日志
  164. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  165. }
  166. Db::commit();
  167. } catch (\Exception $e) {
  168. Db::rollback();
  169. }
  170. }
  171. /**
  172. * 变更会员积分
  173. * @param int $score 积分
  174. * @param int $user_id 会员ID
  175. * @param string $memo 备注
  176. */
  177. public static function score($score, $user_id, $memo)
  178. {
  179. Db::startTrans();
  180. try {
  181. $user = self::lock(true)->find($user_id);
  182. if ($user && $score != 0) {
  183. $before = $user->score;
  184. $after = $user->score + $score;
  185. $level = self::nextlevel($after);
  186. //更新会员信息
  187. $user->save(['score' => $after, 'level' => $level]);
  188. //写入日志
  189. ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
  190. }
  191. Db::commit();
  192. } catch (\Exception $e) {
  193. Db::rollback();
  194. }
  195. }
  196. /**
  197. * 根据积分获取等级
  198. * @param int $score 积分
  199. * @return int
  200. */
  201. public static function nextlevel($score = 0)
  202. {
  203. $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
  204. $level = 1;
  205. foreach ($lv as $key => $value) {
  206. if ($score >= $value) {
  207. $level = $key;
  208. }
  209. }
  210. return $level;
  211. }
  212. }