123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace app\api\controller;
- use app\common\library\AliPay;
- use EasyWeChat\Factory;
- use think\Db;
- /**
- * @title 盲盒
- * @controller Box
- * @package app\api\controller
- */
- class Box extends Base
- {
- public function initialize(){
- parent::check_login();
- }
- /**
- * @title 抽奖
- * @desc 抽奖
- * @author Gavin
- * @url /api/Box/lucky_draw
- * @method POST
- * @header name:Authorization require:1 desc:Token
- *
- */
- public function lucky_draw(){
- $user = getMemberInfoHash($this->uid); //获取用户信息
- //获取每日免费次数
- $free_number = getConfigValue('free_lucky_number');
- //获取当日免费抽奖记录
- $date = date('Y-m-d');
- $now_date_count = Db::name('store_blind_box_log')
- ->where('m_id',$this->uid)
- ->where('date',$date)
- ->where('is_free',0)
- ->count();
- $free = $free_number-$now_date_count; //剩余免费次数
- $number = $user['lottery_number'] + $free;
- if ($number<=0) $this->error('抽奖次数不足');
- checkBox();
- $prize_arr = Db::name('store_blind_box')
- ->where('status',1)
- ->where('is_del',1)
- ->field('id,rate')
- ->select();
- foreach ($prize_arr as $key => $val) {
- $arr[$val['id']] = $val['rate'];//概率数组
- }
- $rid = get_rand($arr); //根据概率获取奖项id
- $info = Db::name('store_blind_box')->where('id',$rid)->field('id,title,prize,is_prize')->find();
- $return = [
- 'is_prize'=>$info['is_prize']
- ];
- $coll_info = getCollectionInfoHash($info['prize']);
- if ($info['is_prize']==1){
- $json_info = json_encode($coll_info,true);
- $return['name'] = $info['title'];
- $return['cover'] = $coll_info['cover'];
- }else{
- $json_info = '';
- $return['name'] = '未中奖';
- $return['cover'] = '';
- }
- $com = true;
- Db::startTrans();
- try {
- $data = [
- 'm_id'=>$this->uid,
- 'b_id'=>$rid,
- 'is_prize'=>$info['is_prize'],
- 'name'=>$return['name'],
- 'cover'=>$return['cover'],
- 'info'=>$json_info,
- 'date'=>$date,
- 'is_free'=>$free>0 ? 0 : 1
- ];
- Db::name('store_blind_box_log')->insert($data);
- if (!$free){
- Db::name('store_member')->where('id',$this->uid)->setDec('lottery_number');
- }
- //减少数据库库存
- if ($info['is_prize']==1){
- Db::name('store_collection')->where('id',$info['prize'])->setDec('now_inventory');
- //获取排名
- $rank = getRanking($info['prize'])+1;
- $tag = getTag($info['prize'],$rank,$coll_info['inventory']);
- saveRanking($info['prize']);
- $company = '山东梵易网络科技有限公司';
- $hash = getCompanyHash($info['prize']);
- $company_hash = $hash['hash'];
- $company_hash_time = $hash['create_at'];
- Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
- $collectors_hash = '';
- $date = [
- 'order_id'=>0,
- 'order_no'=>get_order_sn(),
- 'tag'=>$tag,
- 'mid'=>$this->uid,
- 'c_id'=>$info['prize'],
- 'name'=>$coll_info['name'],
- 'cover'=>$coll_info['cover'],
- 'pro_info'=>$json_info,
- 'company'=>$company,
- 'company_hash'=>$company_hash,
- 'company_hash_time'=>$company_hash_time,
- 'collectors_hash'=>$collectors_hash,
- 'collectors_hash_time'=>'',
- 'status'=>4
- ];
- Db::name('store_order_info')->insert($date);
- }
- Db::commit();
- }catch (\Exception $e){
- $com=false;
- Db::rollback();
- }
- if ($com){
- setMemberInfoHash($this->uid);
- if ($info['is_prize']==1){
- //减掉库存
- loseCollectionInventory($info['prize'],1);
- }
- $this->success('成功',$return);
- }
- $this->error('系统错误,请稍后重试');
- }
- /**
- * @title 充值次数
- * @desc 充值次数
- * @author Gavin
- * @url /api/Box/recharge
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:num type:int require:1 default:1 desc:充值次数
- * @param name:pay_type type:string require:1 default:wx desc:wx:微信zfb:支付宝
- * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页
- *
- * @return name:order_no type:int require:0 default:0 desc:订单号
- * @return name:pay type:string require:0 default:0 desc:支付信息
- */
- public function recharge(){
- $num = input('num',1);
- $pay_type = input('pay_type','wx');
- $from = input('from','wx');
- $user = getMemberInfoHash($this->uid); //获取用户信息
- if ($num<1) $this->error('数量错误');
- $com = true;
- Db::startTrans();
- try {
- $order_no = get_order_sn();
- //获取价格
- $price = getConfigValue('lucky_recharge_price');
- $total_fee = bcmul($price,$num,2);
- $data = [
- 'order_no'=>$order_no,
- 'm_id'=>$this->uid,
- 'num'=>$num,
- 'price'=>$price,
- 'pay_price'=>$total_fee,
- 'pay_type'=>$pay_type
- ];
- Db::name('store_blind_recharge')->insert($data);
- $body = '梵易数藏充值盲盒次数';
- switch ($pay_type){
- case 'wx':
- $config = retrunWxConfig();
- $total_fee = $total_fee * 100;
- $config['notify_url'] = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/BlindRechargeNotify';
- $app = Factory::payment($config);
- $post_data = [
- 'body' => $body,
- 'out_trade_no' => $order_no,
- 'total_fee' => $total_fee,
- 'attach'=>$this->uid, //自定义传值
- ];
- //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
- if ($from=='wx'){
- $post_data['openid'] = $user['openid'];
- $post_data['trade_type'] = 'JSAPI';
- }elseif ($from=='h5'){
- $post_data['trade_type'] = 'MWEB';
- }
- $result = $app->order->unify($post_data);
- if ($result['return_msg']=='OK'){
- if ($result['result_code']=='FAIL'){
- $com = false;
- Db::rollback();
- }else{
- $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = json_decode($order1,true);
- Db::commit();
- }
- }else{
- $com = false;
- Db::rollback();
- }
- break;
- case 'zfb':
- $zfb = new AliPay();
- $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/alipayBlindRechargeNotify';//回调地址
- $order = $zfb->ali_pay_pc($body, $total_fee, $order_no, $notify_url,'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/manghe/manghe');//调用支付宝支付的方法
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order;
- Db::commit();
- break;
- }
- }catch (\Exception $e){
- $com=false;
- Db::rollback();
- }
- if ($com){
- $this->success('成功',$retrun_data);
- }
- $this->error('失败,请稍后重试');
- }
- /**
- * @title 用户中奖纪录
- * @desc 用户中奖纪录
- * @author Gavin
- * @method POST
- * @tag 编辑信息
- * @url /api/Box/boxLog
- * @header name:Authorization require:1 desc:Token
- *
- * @return name:id type:int require:0 default:0 desc:ID
- * @return name:name type:string require:0 default:0 desc:奖品名称
- * @return name:cover type:string require:0 default:0 desc:藏品图片
- * @return name:member_name type:string require:0 default:0 desc:用户昵称
- * @return name:create_at type:string require:0 default:0 desc:时间
- */
- public function boxLog(){
- $list = Db::name('store_blind_box_log')
- ->whereIn('is_prize',1)
- ->field('id,m_id,name,cover,create_at')
- ->order('id desc')
- ->limit(10)
- ->select();
- foreach ($list as &$v){
- $v['member_name'] = Db::name('store_member')->where('id',$v['m_id'])->value('name');
- }
- $this->success('成功',$list);
- }
- }
|