wechatService = $wechatService; $this->authService = $authService; } /** * 微信登录 */ public function login(Request $request) { //type:web-电脑端,mobile-手机端, $app_id = subsite_config('aix.system.oauth.wechat_official.app_id'); $redirect_uri = urlencode(route('mobile.lottery.wechat_back')); $type = $request->input('type', 1); $wechat_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$type}#wechat_redirect"; return redirect($wechat_url); } /** * 微信回调 */ public function wechat_back(Request $request) { //微信登录 $officialAccount = $this->wechatService->getOfficialAccount(); $wechatUser = $officialAccount->oauth->user()->getOriginal(); $thirdlogin = Thirdlogin::where('openid', $wechatUser['openid'])->first(); if (!$thirdlogin && $wechatUser['unionid']) { $thirdlogin = Thirdlogin::where('unionid', $wechatUser['unionid'])->first(); } if ($thirdlogin) { $member = $thirdlogin->member()->withTrashed()->first(); $this->authService->login($member, 1); } else { Session::put('open_id', $wechatUser['openid']); Session::put('union_id', $wechatUser['unionid'] ?: ''); Session::save(); } $type = $request->input('state', 1); $url = route('mobile.active.spring', ['type' => $type]); return redirect($url); } /** * 抽奖页 */ public function index(Request $request) { //是否登录 $user = auth('web-member')->user(); if (empty($user)) { $open_id = Session::get('open_id'); if (empty($open_id)) { return redirect(route('mobile.lottery.login')); } } else { $third = Thirdlogin::where('uid', $user->id)->first(); $open_id = $third['openid']; } // $open_id = '11'; //抽奖次数 $type = $request->input('type', 1); $date = date('Y-m-d'); $log = LotteryLog::where([ ['create_time', '=', $date], ['type', '=', $type], ['open_id', '=', $open_id], ])->first(); $number = 1; if (!empty($log)) { $number = 0; } //奖品列表 $prize = LotteryPrize::all(); $ids = []; $restaraunts = []; $colors = []; $images = []; foreach ($prize as $v) { $ids[] = $v->id; $restaraunts[] = $v->prize_name; $colors[] = $v->prize_bg; $images[] = upload_asset($v->prize_image); } $return_data = [ 'open_id' => $open_id, 'current_url' => \Illuminate\Support\Facades\Request::getRequestUri(), 'number' => $number, 'type' => $type, 'prize' => $prize, 'ids' => $ids, 'restaraunts' => $restaraunts, 'colors' => $colors, 'images' => $images, 'wap_title' => '抽奖', 'share_title' => '抽奖', 'share_desc' => '找工作还可抽奖', 'share_link' => route('mobile.lottery.login'), ]; return view('mobile.app.lottery.index', $return_data); } /** * 抽奖 */ public function lottery_post(Request $request) { //抽奖次数 $data = $request->all(); $date = date('Y-m-d'); $log = LotteryLog::where([ ['create_time', '=', $date], ['type', '=', $data['type']], ['open_id', '=', $data['open_id']], ])->first(); if (!empty($log)) { return response()->json(['status' => 0, 'msg' => '您今天已抽过奖,请明天再来!']); } LotteryLog::create([ 'create_time' => $date, 'type' => $data['type'], 'number' => 1, 'open_id' => $data['open_id'], ]); //奖品列表 $model = new LotteryPrize(); $prize_arr = $model->where('prize_number', '>', 0)->get(); $arr = []; foreach ($prize_arr as $key => $val) { $arr[$val['id']] = $val['prize_odds'] * 100; } $rid = $this->_get_rand($arr); //根据概率获取奖项id //增加奖品 $prize_win = $model->where('id', $rid)->first(); $prize_win->prize_number--; $prize_win->save(); $user = auth('web-member')->user(); if (empty($user)) { Session::put('prize_id', $rid); Session::save(); } else { $prize = LotteryPrize::where('id', $rid)->first(); $win = new LotteryWin(); $win->member_id = $user->id; $win->prize_id = $rid; $win->prize_name = $prize->prize_name; $win->created_at = date('Y-m-d H:i:s'); $win->save(); } return response()->json(['status' => 1, 'msg' => '恭喜你,获得' . $prize_win->prize_name, 'id' => $rid]); } private function _get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); $randNum = mt_rand(1, $proSum); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } return $result; } }