UserDetail.php 7.0 KB

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