UserDetail.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use think\Model;
  5. use library\basic\BaseModel;
  6. use think\facade\Db;
  7. use app\model\api\User as UserModel;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class UserDetail extends BaseModel
  12. {
  13. private $config = [
  14. 'admin_add' => ["code" => "后台补加余额", "content" => "后台补加余额:{money}。"],
  15. 'admin_cut' => ["code" => "后台补扣余额", "content" => "后台补扣余额:{money}。"],
  16. 'tx_apply' => ['code' => "提现余额", "content" => "您发起提现,扣除余额:{money}。"],
  17. 'tx_refund' => ['code' => "提现失败", "content" => "提现失败,返还余额:{money}。"],
  18. 'show_temp_buy' => ['code' => "购买皮肤模板消费", "content" => "{mono},用户消费:{money}。"],
  19. ];
  20. /**
  21. * 消费记录[不影响余额进出]
  22. * @param type $uid
  23. * @param type $money
  24. * @param type $code
  25. * @param type $mono
  26. * @return bool
  27. */
  28. public function consumeLog($uid, $money,$code,$mono=""){
  29. $money = bcadd((string)0, (string)$money, 2);
  30. $post['uid'] = $uid;
  31. $post['into'] = 0;//消费现金不影响余额进出
  32. $post['code'] = $code;
  33. $post['title'] = $this->config[$code]['code'];
  34. $post['content'] = $this->TplParam($this->config[$code]['content'],compact('money','mono'));
  35. $post['type'] = -1;
  36. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money');
  37. $post['time'] = time();
  38. $post['consume'] = floatval($money);//消费现金金额
  39. $this->insert($post);
  40. return true;
  41. }
  42. /**
  43. * 后台充值余额
  44. * @param $uid
  45. * @param $money
  46. * @param $admin_id
  47. * @return bool
  48. * @throws \think\db\exception\DbException
  49. */
  50. public function adminAddMoney($uid, $money,$admin_id,$othen="")
  51. {
  52. try {
  53. self::beginTrans();
  54. $post['uid'] = $uid;
  55. $post['into'] = floatval($money);
  56. $post['code'] = 'admin_add';
  57. $post['title'] = $this->config['admin_add']['code'];
  58. $post['content'] = $this->TplParam($this->config['admin_add']['content'],compact('money'));
  59. $post['type'] = 1;
  60. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
  61. $post['time'] = time();
  62. $post['admin_id'] = $admin_id;
  63. $post['othen'] = $othen;
  64. $this->insert($post);
  65. $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
  66. self::commitTrans();
  67. if ($bool > 0) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. } catch (DbException $db) {
  73. self::rollbackTrans();
  74. return false;
  75. }
  76. }
  77. /**
  78. * 后台扣除余额
  79. * @param $uid
  80. * @param $money
  81. * @param $admin_id
  82. * @return bool
  83. * @throws \think\db\exception\DbException
  84. */
  85. public function adminCutMoney($uid, $money,$admin_id,$othen="")
  86. {
  87. try {
  88. self::beginTrans();
  89. $post['uid'] = $uid;
  90. $post['into'] = floatval($money);
  91. $post['code'] = 'admin_cut';
  92. $post['title'] = $this->config['admin_cut']['code'];
  93. $post['content'] = $this->TplParam($this->config['admin_cut']['content'],compact('money'));
  94. $post['type'] = -1;
  95. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
  96. $post['time'] = time();
  97. $post['admin_id'] = $admin_id;
  98. $post['othen'] = $othen;
  99. $this->insert($post);
  100. $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
  101. self::commitTrans();
  102. if ($bool > 0) {
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. } catch (DbException $db) {
  108. self::rollbackTrans();
  109. return false;
  110. }
  111. }
  112. /**
  113. * 用户提现余额
  114. * @param $uid
  115. * @param $money
  116. * @param $admin_id
  117. * @return bool
  118. * @throws \think\db\exception\DbException
  119. */
  120. public function txApplyMoney($uid, $money,$tx_id)
  121. {
  122. $post['uid'] = $uid;
  123. $post['into'] = floatval($money);
  124. $post['code'] = 'tx_apply';
  125. $post['title'] = $this->config['tx_apply']['code'];
  126. $post['content'] = $this->TplParam($this->config['tx_apply']['content'],compact('money'));
  127. $post['type'] = -1;
  128. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
  129. $post['time'] = time();
  130. $post['tx_id'] = $tx_id;
  131. $this->insert($post);
  132. $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
  133. if ($bool > 0) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. /**
  140. * 用户提现余额驳回返还
  141. * @param $uid
  142. * @param $money
  143. * @param $admin_id
  144. * @return bool
  145. * @throws \think\db\exception\DbException
  146. */
  147. public function txRefundMoney($uid, $money,$tx_id)
  148. {
  149. $post['uid'] = $uid;
  150. $post['into'] = floatval($money);
  151. $post['code'] = 'tx_refund';
  152. $post['title'] = $this->config['tx_refund']['code'];
  153. $post['content'] = $this->TplParam($this->config['tx_refund']['content'],compact('money'));
  154. $post['type'] = 1;
  155. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
  156. $post['time'] = time();
  157. $post['tx_id'] = $tx_id;
  158. $this->insert($post);
  159. $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
  160. if ($bool > 0) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. /**
  167. * 转义的模板信息
  168. */
  169. private function TplParam($content)
  170. {
  171. $data = func_get_args();
  172. foreach ($data[1] as $k => $v) {
  173. $content = str_replace('{' . $k . '}', $v, $content);
  174. }
  175. return $content;
  176. }
  177. }