Recharge.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Db;
  5. use addons\epay\library\Service;
  6. /*
  7. * 公众号搜索用户进行充值
  8. * */
  9. class Recharge extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. }
  17. //公众号支付完全放弃
  18. /*public function __construct(){
  19. echo __LINE__;
  20. exit;
  21. }*/
  22. //金币兑换配置列表
  23. public function config(){
  24. $rs = Db::name('paygold_webcon')->field('id,money,gold')->order('id asc')->select();
  25. if ($rs) {
  26. foreach ($rs as &$v) {
  27. $v['money_to_gold'] = config('site.money_to_gold');
  28. }
  29. $arr = [
  30. 'id' => -1,
  31. 'money' => 0,
  32. 'gold' => 0,
  33. 'money_to_gold' => config('site.money_to_gold')
  34. ];
  35. array_push($rs, $arr);
  36. }
  37. $this->success('success',$rs);
  38. }
  39. //根据uid检索人
  40. public function check_user()
  41. {
  42. $mobile = input('mobile');
  43. if(!$mobile){
  44. $this->error('请输入用户手机号');
  45. }
  46. //填充0
  47. $map = [
  48. 'mobile'=>$mobile,
  49. ];
  50. $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
  51. if (empty($user_info)) {
  52. $this->error('用户信息不存在');
  53. }
  54. $this->success('success',$user_info);
  55. }
  56. //创建订单,公众号
  57. public function recharge(){
  58. $rc_id = input('rc_id',0);
  59. $pay_type = input('pay_type','wechat');
  60. $username = input('username',0);
  61. if(!$rc_id){
  62. $this->error('请选择充值金额');
  63. }
  64. if(!$username){
  65. $this->error('请输入用户手机号并选择');
  66. }
  67. //查找用户
  68. $user_info = Db::name('user')->field('id')->where('username',$username)->find();
  69. if (empty($user_info)) {
  70. $this->error('用户信息不存在');
  71. }
  72. //赋值money
  73. $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
  74. $money = $recharge_config['money'];
  75. $gold = $recharge_config['gold'];
  76. //创建订单
  77. $data = [];
  78. $data['status'] = 0;
  79. $pay_no = createUniqueNo('P',$user_info['id']);
  80. $data['pay_no'] = $pay_no;
  81. $data['money'] = $money;
  82. $data['payment_class'] = $pay_type;
  83. $data['user_id'] = $user_info['id'];
  84. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  85. $data['memo'] = '充值金币支付';
  86. $data['createtime'] = time();
  87. $data['payment'] = 'mp';
  88. $orderid = Db::name('pay_order')->insertGetId($data);
  89. //创建回调
  90. $data = [];
  91. $data['event'] = 'success';
  92. $data['class'] = 'app\common\model\Recharge';
  93. $data['method'] = 'rechargepaysucc';
  94. $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
  95. $data['pay_no'] = $pay_no;
  96. Db::name('pay_event')->insertGetId($data);
  97. $return = [
  98. 'pay_no'=>$pay_no,
  99. 'title' => '充值金币支付',
  100. ];
  101. $this->success('success',$return);
  102. }
  103. //公众号
  104. public function topay(){
  105. $openid = input('openid');
  106. $pay_no = input('pay_no');
  107. $pay_type = input_post('pay_type','wechat');
  108. $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
  109. //下单
  110. $params = [
  111. 'type' => $pay_type,
  112. 'orderid' => $pay_no,
  113. 'title' => $orderInfo['memo'],
  114. 'amount' => $orderInfo['money'],
  115. // 'amount' => 0.01,
  116. 'method' => 'mp',
  117. 'openid' => $openid,
  118. 'notifyurl' => $this->request->root(true) . '/notify.php',
  119. //'returnurl' => $this->request->root(true) . '/location.php',
  120. ];
  121. $res = Service::submitOrder($params);
  122. $this->success('请求成功',json_decode($res,true));
  123. }
  124. //兑换金币
  125. public function exchangegold() {
  126. $id = input('id', 0, 'intval');
  127. $freemoney = input_post('freemoney', 0, 'intval'); //自定义
  128. $uid = $this->auth->id;
  129. if(!$id && !$freemoney){
  130. $this->error('请选择或填写兑换金额');
  131. }
  132. if ($id) {
  133. //赋值money
  134. $paygold_webcon = Db::name('paygold_webcon')->where('id', $id)->find();
  135. $money = $paygold_webcon ? $paygold_webcon['money'] : 0;
  136. $gold = $paygold_webcon ? $paygold_webcon['gold'] : 0;
  137. }
  138. if ($freemoney) {
  139. $money = $freemoney;
  140. $bili = config('site.money_to_gold') ?: 10;
  141. $gold = bcmul($money,$bili,0);
  142. }
  143. if($money<=0) {
  144. $this->error('支付金额必须大于0');
  145. }
  146. if($money > 10000){
  147. $this->error('支付金额太大');
  148. }
  149. //验证余额
  150. $user_info = model('wallet')->getWallet($this->auth->id);
  151. if ($user_info['money'] < $money) {
  152. $this->success('您的余额不足', ['code' => 2]);
  153. }
  154. //开启事务
  155. Db::startTrans();
  156. //扣费
  157. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,18,'兑换金币');
  158. if($wallet_rs['status'] === false){
  159. Db::rollback();
  160. $this->error($wallet_rs['msg']);
  161. }
  162. //赠送金币
  163. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$gold,19,'获得兑换金币');
  164. if($wallet_rs['status'] === false){
  165. Db::rollback();
  166. $this->error($wallet_rs['msg']);
  167. }
  168. //tag任务赠送金币
  169. //开通VIP 50金币
  170. // $task_rs = \app\common\model\TaskLog::tofinish($uid,9);
  171. // if($task_rs === false){
  172. // Db::rollback();
  173. // $this->error('完成任务赠送奖励失败');
  174. // }
  175. Db::commit();
  176. $this->success('兑换成功');
  177. }
  178. //兑换rmb
  179. public function exchangermb() {
  180. $freemoney = input_post('freemoney', 0, 'intval'); //自定义
  181. $uid = $this->auth->id;
  182. if(!$freemoney){
  183. $this->error('请选择或填写兑换金额');
  184. }
  185. if ($freemoney) {
  186. $money = $freemoney;
  187. $bili = config('site.jewel_money') ?: 1;
  188. $gold = bcdiv($money,$bili,2);
  189. }
  190. if($money<=0) {
  191. $this->error('兑换金额必须大于0');
  192. }
  193. //验证余额
  194. $user_info = model('wallet')->getWallet($this->auth->id);
  195. if ($user_info['money'] < $money) {
  196. $this->success('您的钻石不足', ['code' => 2]);
  197. }
  198. //开启事务
  199. Db::startTrans();
  200. //扣费
  201. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,25,'兑换人民币');
  202. if($wallet_rs['status'] === false){
  203. Db::rollback();
  204. $this->error($wallet_rs['msg']);
  205. }
  206. //兑换人民币
  207. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'rmb_money',$gold,26,'钻石兑换人民币');
  208. if($wallet_rs['status'] === false){
  209. Db::rollback();
  210. $this->error($wallet_rs['msg']);
  211. }
  212. Db::commit();
  213. $this->success('兑换成功');
  214. }
  215. }