123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\api\controller;
- use EasyWeChat\Factory;
- use think\facade\Db;
- class Prize extends BaseApi
- {
-
- /**
- * 奖品列表
- * */
- public function prizeList(){
- $host = "https://".request()->host().'/';
- //echo "<pre>";print_r(request()->host());exit;
- $list = Db::table('v4prize')->field('id,prize_name,img')->order('id desc')->select()->toArray();
- foreach ($list as $k=>$v){
- $list[$k]['img'] = $host.$v['img'];
- }
- return $this->response($this->success($list));
- }
- /**
- * 抽奖
- * */
- public function luckDraw(){
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $uid = $this->member_id;
- $draw_num = 3;
- //echo "<pre>";print_r($uid);exit;
- //可抽奖次数
- $sign_days_series = Db::table('v4member')->where(['member_id'=>$uid])->value('sign_days_series');
- if ($sign_days_series > 0){
- $end_log_id = Db::table('v4prize_user')->where(['uid'=>$uid])->order('id desc')->value('end_log_id');
- $end_log_id = $end_log_id?$end_log_id:0;
- $signin_list = Db::table('v4member_log')->where(['member_id'=>$uid,'action'=>'membersignin'])->where('id','>',$end_log_id)->order('id desc')->limit(0,$sign_days_series)->select()->toArray();
- $last_names = array_column($signin_list,'id');
- array_multisort($last_names,SORT_ASC,$signin_list);
- if(count($signin_list) < $draw_num){
- return $this->response($this->error('','暂无抽奖次数'));
- }
- }else{
- return $this->response($this->error('','暂无抽奖次数'));
- }
- $prize_arr = Db::table('v4prize')->select()->toArray();
- foreach ($prize_arr as $key => $val) {
- $arr[$val['id']] = $val['probability'];//概率数组
- }
- $rid = $this->get_rand($arr); //根据概率获取奖项id
- $prize_info = Db::table('v4prize')->where(['id'=>$rid])->find();
- $end_log_id = $signin_list[$draw_num-1]['id'];
- $data['uid'] = $uid;
- $data['pid'] = $rid;
- $data['prize_name'] = $prize_info['prize_name'];
- $data['type'] = $prize_info['type'];
- $data['num'] = $prize_info['num'];
- $data['end_log_id'] = $end_log_id;
- $data['add_time'] = date('Y-m-d H:i:s',time());
- //echo "<pre>";print_r($data);exit;
- Db::table('v4prize_user')->insert($data);
- if($prize_info['type'] == 1){
- Db::table('v4member')->where(['member_id'=>$uid])->inc('point',$prize_info['num'])->update();
- }elseif ($prize_info['type'] == 2){
- Db::table('v4member')->where(['member_id'=>$uid])->inc('currency',$prize_info['num'])->update();
- }
- return $this->response($this->success(['id'=>$rid],'成功'));
- }
- //计算中奖概率
- function get_rand($proArr) {
- $result = '';
- //概率数组的总概率精度
- $proSum = array_sum($proArr);
- // var_dump($proSum);
- //概率数组循环
- foreach ($proArr as $key => $proCur) {
- $randNum = mt_rand(1, $proSum); //返回随机整数
- if ($randNum <= $proCur) {
- $result = $key;
- break;
- } else {
- $proSum -= $proCur;
- }
- }
- unset ($proArr);
- return $result;
- }
- /**
- * 抽奖次数
- * */
- public function drawNum(){
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $uid = $this->member_id;
- $draw_num = 3;
- $sign_days_series = Db::table('v4member')->where(['member_id'=>$uid])->value('sign_days_series');
- if ($sign_days_series > 0){
- $end_log_id = Db::table('v4prize_user')->where(['uid'=>$uid])->order('id desc')->value('end_log_id');
- $end_log_id = $end_log_id?$end_log_id:0;
- $signin_list = Db::table('v4member_log')->where(['member_id'=>$uid,'action'=>'membersignin'])->where('id','>',$end_log_id)->order('id desc')->limit(0,$sign_days_series)->select()->toArray();
- $last_names = array_column($signin_list,'id');
- array_multisort($last_names,SORT_ASC,$signin_list);
- $num = floor(count($signin_list)/$draw_num);
- }else{
- $num = 0;
- }
- return $this->response($this->success(['num'=>$num],'成功'));
- }
- /**
- * 抽奖记录
- * */
- public function drawList(){
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $uid = $this->member_id;
- $list = Db::table('v4prize_user')->field('id,prize_name,type,num,add_time')->where(['uid'=>$uid])->select()->toArray();
- return $this->response($this->success($list,'成功'));
- }
- /**
- * 抽奖配置
- * */
- public function rule(){
- $rule = Db::table('v4config_md')->where(['type'=>'prize','name'=>'rule'])->value('value');
- $data['rule'] = $rule;
- return $this->response($this->success($data,'成功'));
- }
- /**
- * 补签卡信息
- * */
- public function signCardInfo(){
- $info = Db::table('v4config_md')->where(['type'=>'prize'])->select();
- foreach ($info as $v){
- $info[$v['name']] = $v['value'];
- }
- $data['sign_card_money'] = $info['sign_card_money'];
- $data['sign_card_rule'] = $info['sign_card_rule'];
- return $this->response($this->success($data,'成功'));
- }
- /**
- * 购买补签卡
- * */
- public function buySignCard(){
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $uid = $this->member_id;
- $user_info = Db::table('v4member')->field('weapp_openid')->where(['member_id'=>$uid])->find();
- $sign_card_money = Db::table('v4config_md')->where(['type'=>'prize','name'=>'sign_card_money'])->value('value');
- if ($sign_card_money <= 0){
- return $this->response($this->error('','暂时无法购买'));
- }
- $app = Factory::payment(Config('mini_program'));
- $jssdk = $app->jssdk;
- $result = $app->order->unify([
- 'body' => '入驻商家',
- 'out_trade_no' => $this->create_order_no(),
- 'total_fee' => $sign_card_money*100,
- 'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/prize/sign_notify',
- 'trade_type' => 'JSAPI',
- 'openid' => $user_info['weapp_openid'],
- ]);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $prepayId = $result['prepay_id'];
- $json = $jssdk->sdkConfig($prepayId);
- return $this->response($this->error(['config'=>$json],'暂时无法购买'));
- } else {
- return $this->response($this->error('','支付失败'));
- }
- }
- /**
- * 订单号
- * */
- function create_order_no() {
- $order_no = date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
- return $order_no;
- }
- }
|