123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use \think\Db;
- use addons\epay\library\Service;
- /*
- * 公众号搜索用户进行充值
- * */
- class Recharge extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- }
- //金币兑换配置列表
- public function config(){
- $rs = Db::name('paygold_webcon')->field('id,money,gold')->order('id asc')->select();
- if ($rs) {
- foreach ($rs as &$v) {
- $v['money_to_gold'] = config('site.money_to_gold');
- }
- $arr = [
- 'id' => -1,
- 'money' => 0,
- 'gold' => 0,
- 'money_to_gold' => config('site.money_to_gold')
- ];
- array_push($rs, $arr);
- }
- $this->success('success',$rs);
- }
- //根据uid检索人
- public function check_user()
- {
- $mobile = input('mobile');
- if(!$mobile){
- $this->error('请输入用户手机号');
- }
- //填充0
- $map = [
- 'mobile'=>$mobile,
- ];
- $user_info = Db::name('user')->field('username,nickname')->where($map)->find();
- if (empty($user_info)) {
- $this->error('用户信息不存在');
- }
- $this->success('success',$user_info);
- }
- //创建订单,公众号
- public function recharge(){
- $rc_id = input('rc_id',0);
- $pay_type = input('pay_type','wechat');
- $username = input('username',0);
- if(!$rc_id){
- $this->error('请选择充值金额');
- }
- if(!$username){
- $this->error('请输入用户手机号并选择');
- }
- //查找用户
- $user_info = Db::name('user')->field('id')->where('username',$username)->find();
- if (empty($user_info)) {
- $this->error('用户信息不存在');
- }
- //赋值money
- $recharge_config = Db::name('paygold_webcon')->where('id',$rc_id)->find();
- $money = $recharge_config['money'];
- $gold = $recharge_config['gold'];
- //创建订单
- $data = [];
- $data['status'] = 0;
- $pay_no = createUniqueNo('P',$user_info['id']);
- $data['pay_no'] = $pay_no;
- $data['money'] = $money;
- $data['payment_class'] = $pay_type;
- $data['user_id'] = $user_info['id'];
- $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
- $data['memo'] = '充值金币支付';
- $data['createtime'] = time();
- $data['payment'] = 'mp';
- $orderid = Db::name('pay_order')->insertGetId($data);
- //创建回调
- $data = [];
- $data['event'] = 'success';
- $data['class'] = 'app\common\model\Recharge';
- $data['method'] = 'rechargepaysucc';
- $data['args'] = json_encode(['user_id'=>$user_info['id'],'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id]);
- $data['pay_no'] = $pay_no;
- Db::name('pay_event')->insertGetId($data);
- $return = [
- 'pay_no'=>$pay_no,
- 'title' => '充值金币支付',
- ];
- $this->success('success',$return);
- }
- //公众号
- public function topay(){
- $openid = input('openid');
- $pay_no = input('pay_no');
- $pay_type = input_post('pay_type','wechat');
- $orderInfo = Db::name('pay_order')->where('pay_no',$pay_no)->find();
- //下单
- $params = [
- 'type' => $pay_type,
- 'orderid' => $pay_no,
- 'title' => $orderInfo['memo'],
- 'amount' => $orderInfo['money'],
- // 'amount' => 0.01,
- 'method' => 'mp',
- 'openid' => $openid,
- 'notifyurl' => $this->request->root(true) . '/notify.php',
- //'returnurl' => $this->request->root(true) . '/location.php',
- ];
- $res = Service::submitOrder($params);
- $this->success('请求成功',json_decode($res,true));
- }
- //兑换金币
- public function exchangegold() {
- $id = input('id', 0, 'intval');
- $freemoney = input_post('freemoney', 0, 'intval'); //自定义
- $uid = $this->auth->id;
- if(!$id && !$freemoney){
- $this->error('请选择或填写兑换金额');
- }
- if ($id) {
- //赋值money
- $paygold_webcon = Db::name('paygold_webcon')->where('id', $id)->find();
- $money = $paygold_webcon ? $paygold_webcon['money'] : 0;
- $gold = $paygold_webcon ? $paygold_webcon['gold'] : 0;
- }
- if ($freemoney) {
- $money = $freemoney;
- $bili = config('site.money_to_gold') ?: 10;
- $gold = bcmul($money,$bili,0);
- }
- if($money<=0) {
- $this->error('支付金额必须大于0');
- }
- if($money > 10000){
- $this->error('支付金额太大');
- }
- //验证余额
- $user_info = model('wallet')->getWallet($this->auth->id);
- if ($user_info['money'] < $money) {
- $this->success('您的余额不足', ['code' => 2]);
- }
- //开启事务
- Db::startTrans();
- //扣费
- $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,18,'兑换金币');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- //赠送金币
- $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$gold,19,'获得兑换金币');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- //tag任务赠送金币
- //开通VIP 50金币
- // $task_rs = \app\common\model\TaskLog::tofinish($uid,9);
- // if($task_rs === false){
- // Db::rollback();
- // $this->error('完成任务赠送奖励失败');
- // }
- Db::commit();
- $this->success('兑换成功');
- }
- }
|