Prize.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\api\controller;
  3. use EasyWeChat\Factory;
  4. use think\facade\Db;
  5. class Prize extends BaseApi
  6. {
  7. /**
  8. * 奖品列表
  9. * */
  10. public function prizeList(){
  11. $host = "https://".request()->host().'/';
  12. //echo "<pre>";print_r(request()->host());exit;
  13. $list = Db::table('v4prize')->field('id,prize_name,img')->order('id desc')->select()->toArray();
  14. foreach ($list as $k=>$v){
  15. $list[$k]['img'] = $host.$v['img'];
  16. }
  17. return $this->response($this->success($list));
  18. }
  19. /**
  20. * 抽奖
  21. * */
  22. public function luckDraw(){
  23. $token = $this->checkToken();
  24. if ($token['code'] < 0) return $this->response($token);
  25. $uid = $this->member_id;
  26. $draw_num = 3;
  27. //echo "<pre>";print_r($uid);exit;
  28. //可抽奖次数
  29. $sign_days_series = Db::table('v4member')->where(['member_id'=>$uid])->value('sign_days_series');
  30. if ($sign_days_series > 0){
  31. $end_log_id = Db::table('v4prize_user')->where(['uid'=>$uid])->order('id desc')->value('end_log_id');
  32. $end_log_id = $end_log_id?$end_log_id:0;
  33. $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();
  34. $last_names = array_column($signin_list,'id');
  35. array_multisort($last_names,SORT_ASC,$signin_list);
  36. if(count($signin_list) < $draw_num){
  37. return $this->response($this->error('','暂无抽奖次数'));
  38. }
  39. }else{
  40. return $this->response($this->error('','暂无抽奖次数'));
  41. }
  42. $prize_arr = Db::table('v4prize')->select()->toArray();
  43. foreach ($prize_arr as $key => $val) {
  44. $arr[$val['id']] = $val['probability'];//概率数组
  45. }
  46. $rid = $this->get_rand($arr); //根据概率获取奖项id
  47. $prize_info = Db::table('v4prize')->where(['id'=>$rid])->find();
  48. $end_log_id = $signin_list[$draw_num-1]['id'];
  49. $data['uid'] = $uid;
  50. $data['pid'] = $rid;
  51. $data['prize_name'] = $prize_info['prize_name'];
  52. $data['type'] = $prize_info['type'];
  53. $data['num'] = $prize_info['num'];
  54. $data['end_log_id'] = $end_log_id;
  55. $data['add_time'] = date('Y-m-d H:i:s',time());
  56. //echo "<pre>";print_r($data);exit;
  57. Db::table('v4prize_user')->insert($data);
  58. if($prize_info['type'] == 1){
  59. Db::table('v4member')->where(['member_id'=>$uid])->inc('point',$prize_info['num'])->update();
  60. }elseif ($prize_info['type'] == 2){
  61. Db::table('v4member')->where(['member_id'=>$uid])->inc('currency',$prize_info['num'])->update();
  62. }
  63. return $this->response($this->success(['id'=>$rid],'成功'));
  64. }
  65. //计算中奖概率
  66. function get_rand($proArr) {
  67. $result = '';
  68. //概率数组的总概率精度
  69. $proSum = array_sum($proArr);
  70. // var_dump($proSum);
  71. //概率数组循环
  72. foreach ($proArr as $key => $proCur) {
  73. $randNum = mt_rand(1, $proSum); //返回随机整数
  74. if ($randNum <= $proCur) {
  75. $result = $key;
  76. break;
  77. } else {
  78. $proSum -= $proCur;
  79. }
  80. }
  81. unset ($proArr);
  82. return $result;
  83. }
  84. /**
  85. * 抽奖次数
  86. * */
  87. public function drawNum(){
  88. $token = $this->checkToken();
  89. if ($token['code'] < 0) return $this->response($token);
  90. $uid = $this->member_id;
  91. $draw_num = 3;
  92. $sign_days_series = Db::table('v4member')->where(['member_id'=>$uid])->value('sign_days_series');
  93. if ($sign_days_series > 0){
  94. $end_log_id = Db::table('v4prize_user')->where(['uid'=>$uid])->order('id desc')->value('end_log_id');
  95. $end_log_id = $end_log_id?$end_log_id:0;
  96. $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();
  97. $last_names = array_column($signin_list,'id');
  98. array_multisort($last_names,SORT_ASC,$signin_list);
  99. $num = floor(count($signin_list)/$draw_num);
  100. }else{
  101. $num = 0;
  102. }
  103. return $this->response($this->success(['num'=>$num],'成功'));
  104. }
  105. /**
  106. * 抽奖记录
  107. * */
  108. public function drawList(){
  109. $token = $this->checkToken();
  110. if ($token['code'] < 0) return $this->response($token);
  111. $uid = $this->member_id;
  112. $list = Db::table('v4prize_user')->field('id,prize_name,type,num,add_time')->where(['uid'=>$uid])->select()->toArray();
  113. return $this->response($this->success($list,'成功'));
  114. }
  115. /**
  116. * 抽奖配置
  117. * */
  118. public function rule(){
  119. $rule = Db::table('v4config_md')->where(['type'=>'prize','name'=>'rule'])->value('value');
  120. $data['rule'] = $rule;
  121. return $this->response($this->success($data,'成功'));
  122. }
  123. /**
  124. * 补签卡信息
  125. * */
  126. public function signCardInfo(){
  127. $info = Db::table('v4config_md')->where(['type'=>'prize'])->select();
  128. foreach ($info as $v){
  129. $info[$v['name']] = $v['value'];
  130. }
  131. $data['sign_card_money'] = $info['sign_card_money'];
  132. $data['sign_card_rule'] = $info['sign_card_rule'];
  133. return $this->response($this->success($data,'成功'));
  134. }
  135. /**
  136. * 购买补签卡
  137. * */
  138. public function buySignCard(){
  139. $token = $this->checkToken();
  140. if ($token['code'] < 0) return $this->response($token);
  141. $uid = $this->member_id;
  142. $user_info = Db::table('v4member')->field('weapp_openid')->where(['member_id'=>$uid])->find();
  143. $sign_card_money = Db::table('v4config_md')->where(['type'=>'prize','name'=>'sign_card_money'])->value('value');
  144. if ($sign_card_money <= 0){
  145. return $this->response($this->error('','暂时无法购买'));
  146. }
  147. $app = Factory::payment(Config('mini_program'));
  148. $jssdk = $app->jssdk;
  149. $result = $app->order->unify([
  150. 'body' => '入驻商家',
  151. 'out_trade_no' => $this->create_order_no(),
  152. 'total_fee' => $sign_card_money*100,
  153. 'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/prize/sign_notify',
  154. 'trade_type' => 'JSAPI',
  155. 'openid' => $user_info['weapp_openid'],
  156. ]);
  157. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  158. $prepayId = $result['prepay_id'];
  159. $json = $jssdk->sdkConfig($prepayId);
  160. return $this->response($this->error(['config'=>$json],'暂时无法购买'));
  161. } else {
  162. return $this->response($this->error('','支付失败'));
  163. }
  164. }
  165. /**
  166. * 订单号
  167. * */
  168. function create_order_no() {
  169. $order_no = date('Ymd').substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(1000, 9999));
  170. return $order_no;
  171. }
  172. }