UserDetail.php 7.8 KB

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