/** * 用户通过手机号来注册 */ public function registerAction() { $view = new ViewModel(); $request = $this->getRequest(); $user = $this->authentication()->getIdentity(); if ($user) { return $this->redirect()->toUrl('/test_qoros'); } else { //get user info if (parent::isWeixin() && !$request->isPost()) { $code = $this->getParam('code', false); if (!$code) { $redirectUrl = $this->wechat->getOauthRedirect('http://' . $_SERVER['SERVER_NAME'] . '/test_qoros/user/completeInfo'); return $this->redirect()->toUrl($redirectUrl); } $tokenData = $this->wechat->getOauthData(); $user = $this->userModel->getByOpenId($tokenData['openid']); if ($user) { $result = $this->login($user->mobile, null, false); if ($result->getCode() == Result::SUCCESS && $user->username != $user->openid) { return $this->redirect()->toUrl('/test_qoros'); } } $view->setVariables(array('openid' => $tokenData['openid'])); } } if ($request->isPost()) { $mobile = $this->getParam('mobile'); $openid = $this->getParam('openid', false); $exsit = $this->userModel->getUserByMobile($mobile); if ($exsit) { //to login $this->login($mobile, false, false); $user = $this->authentication()->getIdentity(); if (isset($openid) && $openid) { $userInfo = $this->wechat->getUserInfo($openid); if (isset($userInfo['headimgurl'])) { $user->avatar = $userInfo['headimgurl']; } $user->open_id = $openid; $this->userModel->updateUserInfo($user); } return new UnifyJsonModel(); } else { return new ErrorJsonModel(1001, '手机号码不存在'); } } return $view; }
/** * */ public function huanyingxinAction() { //自动登陆 $request = $this->getRequest(); if (parent::isWeixin() && !$request->isPost()) { $code = $this->getParam('code', false); if (!$code) { $redirectUrl = $this->wechat->getOauthRedirect('http://' . $_SERVER['SERVER_NAME'] . '/test_qoros/index/huanyingxin'); return $this->redirect()->toUrl($redirectUrl); } $tokenData = $this->wechat->getOauthData(); $user = $this->userModel->getByOpenId($tokenData['openid']); if ($user) { $result = $this->login($user->mobile, null, false); if ($result->getCode() == Result::SUCCESS && $user->username != $user->openid) { // return $this->redirect()->toUrl('/test_qoros'); } } } $view = new ViewModel(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $href = "{$protocol}{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; $jsSign = $this->wechat->getJsSign($href); $url = 'http://' . $_SERVER['HTTP_HOST'] . '/test_qoros/img/share_image.png'; $view->setVariables(array('jsSign' => $jsSign, 'url' => $url)); return $view; }
/** * 获取临时素材 */ public function getMediaAction() { $view = new ViewModel(); $list = $this->wechat->getMedia('Yyb1Jbwbq3unQYQXbIH7xpvkixydVnhSRbLD1MpiGXvXU0inb2ywsjtu_qgwB5LL'); print_r($list); return $view; }
/** * ajax获取随机的抽奖奖项 * 从长度为100的数组中随机一个值 */ public function RandAwardAction() { $activityId = $this->getParam('activityId', null); $activity = $this->activityModel->getById($activityId); $now = time(); if ($activity->endTime->getTimestamp() < $now || $now < $activity->startTime->getTimestamp()) { return new UnifyJsonModel(array(0)); } $user = $this->userModel->getById($this->identity()->id); $is_lucky = 0; switch ($activityId) { case 2: $is_lucky = $user->is_luck_draw_1; break; case 3: $is_lucky = $user->is_luck_draw_2; break; case 4: $is_lucky = $user->is_luck_draw_3; break; case 6: $is_lucky = $user->is_luck_draw_4; break; } if ($is_lucky == 0) { return new ErrorJsonModel(1022, '抱歉!您不满足参与该活动的条件'); } $existed = $this->awardModel->isWin($activityId, $this->identity()->id); if (!empty($existed)) { return new ErrorJsonModel(1021, '对不起!您已经参与过该活动'); } //随机奖项 $items = $this->awardModel->getList($activityId); $randArr = array(); foreach ($items as $item) { if ($item->count != 0) { $randArr[] = $item->goods->id; } } if (count($randArr) == 0) { //没有中奖 $randArr[] = -1; $randArr[] = -2; $randArr[] = -3; } $r = rand(0, count($randArr) - 1); $goodsId = $randArr[$r]; if ($goodsId > 0) { //奖项数量-1 $item = $this->awardModel->getByActivityIdAndGoodsId($activityId, $goodsId); $item->count -= 1; $this->awardModel->save($item); //添加到中奖列表 $win = new WinEntity(); $win->item = $item; $win->user = $this->identity()->id; $win->activityId = $activityId; $this->awardModel->saveWin($win); $data = array('first' => '恭喜您参与的活动中奖了!', 'keyword1' => '幸运转盘大抽奖', 'keyword2' => $item->goods->name, 'remark' => '感谢您的参与。'); $tplId = WechatMessageEntity::TEMPLATE_LUCKY_DRAW; $this->wechat->sendTemplateMessage($user->open_id, $tplId, $data); } else { //添加到中奖列表 $win = new WinEntity(); $win->item = null; $win->activityId = $activityId; $win->user = $this->identity()->id; $this->awardModel->saveWin($win); } return new UnifyJsonModel(array($goodsId)); }