UserDetail.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_pay' => ['code' => "购买皮肤模板", "content" => "您使用余额支付,扣除余额:{money}。"],
  19. 'recharge_pay' => ['code' => "充值余额支付", "content" => "您使用余额充值,扣除余额:{money}。"],
  20. 'show_temp_buy' => ['code' => "购买皮肤模板消费", "content" => "{mono},用户消费:{money}。"],
  21. ];
  22. /**
  23. * 余额支付
  24. * @param type $uid
  25. * @param type $money
  26. * @param type $mono
  27. * @param type $parms
  28. */
  29. public function balancePay($uid,$money,$code,$parms=[]){
  30. $money = bcadd("0", $money."",2);
  31. $post['uid'] = $uid;
  32. $post['into'] = floatval($money);
  33. $post['code'] = $code;
  34. $post['title'] = $this->config[$code]['code'];
  35. $post['content'] = $this->TplParam($this->config[$code]['content'],compact('money'));
  36. $post['type'] = -1;//收入为1,支出为-1
  37. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);//余额
  38. $post['time'] = time();
  39. $post['tx_id'] = 0;
  40. $post['cash_uid'] = empty($parms["cash_uid"]) ? 0 : $parms["cash_uid"];
  41. $post['p_id'] = empty($parms["p_id"]) ? 0 : $parms["p_id"];
  42. $post['o_id'] = empty($parms["o_id"]) ? 0 : $parms["o_id"];
  43. $post['to_id'] = empty($parms["to_id"]) ? 0 : $parms["to_id"];//模板购买id
  44. $this->insert($post);
  45. $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
  46. if ($bool > 0) {
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. }
  52. /**
  53. * 消费记录[不影响余额进出]
  54. * @param type $uid
  55. * @param type $money
  56. * @param type $code
  57. * @param type $mono
  58. * @return bool
  59. */
  60. public function consumeLog($uid, $money,$code,$mono=""){
  61. $money = bcadd((string)0, (string)$money, 2);
  62. $post['uid'] = $uid;
  63. $post['into'] = 0;//消费现金不影响余额进出
  64. $post['code'] = $code;
  65. $post['title'] = $this->config[$code]['code'];
  66. $post['content'] = $this->TplParam($this->config[$code]['content'],compact('money','mono'));
  67. $post['type'] = -1;
  68. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money');
  69. $post['time'] = time();
  70. $post['consume'] = floatval($money);//消费现金金额
  71. $this->insert($post);
  72. return true;
  73. }
  74. /**
  75. * 后台充值余额
  76. * @param $uid
  77. * @param $money
  78. * @param $admin_id
  79. * @return bool
  80. * @throws \think\db\exception\DbException
  81. */
  82. public function adminAddMoney($uid, $money,$admin_id,$othen="")
  83. {
  84. try {
  85. self::beginTrans();
  86. $post['uid'] = $uid;
  87. $post['into'] = floatval($money);
  88. $post['code'] = 'admin_add';
  89. $post['title'] = $this->config['admin_add']['code'];
  90. $post['content'] = $this->TplParam($this->config['admin_add']['content'],compact('money'));
  91. $post['type'] = 1;
  92. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
  93. $post['time'] = time();
  94. $post['admin_id'] = $admin_id;
  95. $post['othen'] = $othen;
  96. $this->insert($post);
  97. $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
  98. self::commitTrans();
  99. if ($bool > 0) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. } catch (DbException $db) {
  105. self::rollbackTrans();
  106. return false;
  107. }
  108. }
  109. /**
  110. * 后台扣除余额
  111. * @param $uid
  112. * @param $money
  113. * @param $admin_id
  114. * @return bool
  115. * @throws \think\db\exception\DbException
  116. */
  117. public function adminCutMoney($uid, $money,$admin_id,$othen="")
  118. {
  119. try {
  120. self::beginTrans();
  121. $post['uid'] = $uid;
  122. $post['into'] = floatval($money);
  123. $post['code'] = 'admin_cut';
  124. $post['title'] = $this->config['admin_cut']['code'];
  125. $post['content'] = $this->TplParam($this->config['admin_cut']['content'],compact('money'));
  126. $post['type'] = -1;
  127. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
  128. $post['time'] = time();
  129. $post['admin_id'] = $admin_id;
  130. $post['othen'] = $othen;
  131. $this->insert($post);
  132. $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
  133. self::commitTrans();
  134. if ($bool > 0) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. } catch (DbException $db) {
  140. self::rollbackTrans();
  141. return false;
  142. }
  143. }
  144. /**
  145. * 用户提现余额
  146. * @param $uid
  147. * @param $money
  148. * @param $admin_id
  149. * @return bool
  150. * @throws \think\db\exception\DbException
  151. */
  152. public function txApplyMoney($uid, $money,$tx_id)
  153. {
  154. $post['uid'] = $uid;
  155. $post['into'] = floatval($money);
  156. $post['code'] = 'tx_apply';
  157. $post['title'] = $this->config['tx_apply']['code'];
  158. $post['content'] = $this->TplParam($this->config['tx_apply']['content'],compact('money'));
  159. $post['type'] = -1;
  160. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') - floatval($money);
  161. $post['time'] = time();
  162. $post['tx_id'] = $tx_id;
  163. $this->insert($post);
  164. $bool = Db::name("user")->where('uid', $uid)->dec('money', floatval($money))->update();
  165. if ($bool > 0) {
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. /**
  172. * 用户提现余额驳回返还
  173. * @param $uid
  174. * @param $money
  175. * @param $admin_id
  176. * @return bool
  177. * @throws \think\db\exception\DbException
  178. */
  179. public function txRefundMoney($uid, $money,$tx_id)
  180. {
  181. $post['uid'] = $uid;
  182. $post['into'] = floatval($money);
  183. $post['code'] = 'tx_refund';
  184. $post['title'] = $this->config['tx_refund']['code'];
  185. $post['content'] = $this->TplParam($this->config['tx_refund']['content'],compact('money'));
  186. $post['type'] = 1;
  187. $post['money'] = (float)(new UserModel)->where('uid', $uid)->value('money') + floatval($money);
  188. $post['time'] = time();
  189. $post['tx_id'] = $tx_id;
  190. $this->insert($post);
  191. $bool = Db::name("user")->where('uid', $uid)->inc('money', floatval($money))->update();
  192. if ($bool > 0) {
  193. return true;
  194. } else {
  195. return false;
  196. }
  197. }
  198. /**
  199. * 转义的模板信息
  200. */
  201. private function TplParam($content)
  202. {
  203. $data = func_get_args();
  204. foreach ($data[1] as $k => $v) {
  205. $content = str_replace('{' . $k . '}', $v, $content);
  206. }
  207. return $content;
  208. }
  209. }