UserMoney.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Customer;
  4. use app\admin\model\Third;
  5. use app\admin\model\Tzmoneylog;
  6. use app\admin\model\UserMoneyLog;
  7. use app\admin\model\UserMoneyRecharge;
  8. use app\admin\model\UserMoneyWithdrawal;
  9. use app\admin\model\Withdrawal;
  10. use app\common\controller\Api;
  11. use think\Db;
  12. use think\Exception;
  13. /**
  14. * 用户-我的钱包
  15. * @ApiWeigh (7)
  16. * @package app\api\controller
  17. */
  18. class UserMoney extends Api
  19. {
  20. protected $noNeedLogin = [];
  21. protected $noNeedRight = ['*'];
  22. /**
  23. * 用户余额
  24. * @ApiMethod (GET)
  25. * @ApiReturnParams (name="money", type="int", required=true, description="余额")
  26. * @ApiReturnParams (name="u_wmoney", type="string", required=true, description="累积提现")
  27. * @ApiReturnParams (name="u_cmoney", type="string", required=true, description="累积充值")
  28. * @ApiReturn ({"code":1,"msg":"用户余额","time":"1674894846","data":{"money":"985.50","u_wmoney":"0.00","u_cmoney":"0.00","prevtime_text":"","logintime_text":"","jointime_text":""}})
  29. */
  30. public function user_money()
  31. {
  32. $user_model = new \app\admin\model\User();
  33. $tanzghu = $user_model->where('id', $this->auth->id)->field('money,u_wmoney,u_cmoney')->find();
  34. $this->success('用户余额', $tanzghu);
  35. }
  36. /**
  37. * 余额明细记录
  38. * @ApiMethod (GET)
  39. * @ApiParams (name="type",type="int", required=false,description="变动类型:1=消费记录;2=充值明细;3=提现明细")
  40. * @ApiParams (name=limit,type="int", required=false,description="每页数量")
  41. * @ApiParams (name=page,type="int", required=false,description="页数")
  42. * @ApiReturnParams (name="money", type="int", required=true, description="变更余额")
  43. * @ApiReturnParams (name="before", type="string", required=true, description="变更前余额")
  44. * @ApiReturnParams (name="after", type="string", required=true, description="变更后余额")
  45. * @ApiReturnParams (name="memo", type="string", required=true, description="内容")
  46. * @ApiReturnParams (name="createtime", type="int", required=true, description="时间")
  47. * @ApiReturn ({"code":1,"msg":"余额明细记录","time":"1674896309","data":{"total":9,"per_page":"1","current_page":1,"last_page":9,"data":[{"id":9,"user_id":3,"money":"0.50","before":"985.00","after":"985.50","memo":"余额退款","createtime":1673229429,"type":1}]}})
  48. */
  49. public function money_log()
  50. {
  51. $type = input('type', 1);
  52. $page = input('page', 1);
  53. $limit = input('limit');
  54. $money_log_model = new UserMoneyLog();
  55. $list = $money_log_model->where(['user_id' => $this->auth->id, 'type' => $type])->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
  56. $this->success('余额明细记录', $list);
  57. }
  58. /**
  59. * 提现
  60. * @ApiMethod (POST)
  61. * @ApiParams (name="money",type="float", required=true,description="提现金额")
  62. * @ApiParams (name="type",type="int", required=true,description="提现类型:1=微信,2=支付宝")
  63. */
  64. public function user_withdrawal()
  65. {
  66. $withdrawal_model = new UserMoneyWithdrawal();
  67. $third_model = new Third();
  68. $money_log_model = new UserMoneyLog();
  69. $money = $this->request->post('money', 0);
  70. if ($money < 100) {
  71. $this->error('最低提现金额为100');
  72. }
  73. if ($this->auth->money < $money) {
  74. $this->error('用户可用余额不足');
  75. }
  76. $type = $this->request->post('type');
  77. //微信提现
  78. if ($type == 1) {
  79. $openID = $third_model->where('user_id', $this->auth->id)->where('platform','xcc')->value('openid');
  80. $data = [
  81. 'type' => $type,
  82. 'openid' => $openID,
  83. 'money' => $money,
  84. 'user_id' => $this->auth->id,
  85. 'user_name' => $this->auth->username,
  86. 'create_time' => time(),
  87. 'status' => 1
  88. ];
  89. } else {
  90. $data = [
  91. 'type' => $type,
  92. 'z_name' => $this->auth->z_name,
  93. 'z_phone' => $this->auth->z_phone,
  94. 'money' => $money,
  95. 'user_id' => $this->auth->id,
  96. 'user_name' => $this->auth->username,
  97. 'create_time' => time(),
  98. 'status' => 1
  99. ];
  100. }
  101. Db::startTrans();
  102. try {
  103. $user = $this->auth->getUser();
  104. $money_log = [
  105. 'user_id' => $this->auth->id,
  106. 'type' => 3,
  107. 'memo' => '余额提现',
  108. 'money' => $money,
  109. 'before' => $user->money,
  110. 'after' => $user->money - $money,
  111. 'createtime' => time(),
  112. ];
  113. $user->money -= $money;
  114. $user->save();
  115. $withdrawal_model->insert($data);
  116. $money_log_model->insert($money_log);
  117. Db::commit();
  118. $this->success('提现申请已提交');
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e);
  122. }
  123. }
  124. /**
  125. * 充值
  126. * @ApiMethod (POST)
  127. * @ApiParams (name="money",type="float", required=true,description="提现金额")
  128. * @ApiParams (name="type",type="string", required=true,description="充值类型:wechat=微信,alipay=支付宝")
  129. * @ApiParams (name="mod",type="string", required=true,description="支付方法:web、wap、app、scan、pos、mp、miniapp")
  130. */
  131. public function user_recharge()
  132. {
  133. $money = $this->request->post('money', 0);
  134. if ($money < 1) {
  135. $this->error('最低充值金额为1');
  136. }
  137. $type = $this->request->post('type');
  138. if ($type != 'wechat' && $type != 'alipay') {
  139. $this->error('请选择支付类型');
  140. }
  141. $method = $this->request->post('mod');
  142. // 订单号
  143. $orderid = 'CZ' . order_no_s($this->auth->id);
  144. $recharge_model = new UserMoneyRecharge();
  145. Db::startTrans();
  146. try {
  147. $recharge_data = [
  148. 'user_id' => $this->auth->id,
  149. 'type' => $type,
  150. 'method' => $method,
  151. 'money' => $money,
  152. 'order_sn' => $orderid,
  153. 'create_time' => time(),
  154. ];
  155. $recharge_model->insert($recharge_data);
  156. Db::commit();
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e);
  160. }
  161. $openID = '';
  162. if($type=='wechat'){
  163. $third_model = new Third();
  164. $openID = $third_model->where('user_id', $this->auth->id)->where('platform','xcc')->value('openid');
  165. }
  166. $params = [
  167. 'amount' => $money,
  168. 'orderid' => $orderid,
  169. 'type' => $type,
  170. 'title' => "用户充值",
  171. 'notifyurl' => common_url() . '/index.php/api/Notify/user_recharge_notify/type/' . $type,
  172. 'returnurl' => common_url() . '/index.php/api/Notify/user_recharge_notify/type/' . $type . '/out_trade_no/' . $orderid,
  173. 'method' => $method,
  174. 'openid' => $openID,
  175. ];
  176. $pay = \addons\epay\library\Service::submitOrder($params);
  177. $this->success('ok', $pay);
  178. }
  179. }