/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $num) { $query = ApplicationBaseInfo::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => $num]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'create_time' => $this->create_time, 'update_time' => $this->update_time, 'user_id' => $this->user_id, 'status' => $this->status]); $query->andFilterWhere(['like', 'app_name', $this->app_name])->andFilterWhere(['like', 'app_id', $this->app_id])->andFilterWhere(['like', 'app_key', $this->app_key])->andFilterWhere(['like', 'app_desc', $this->app_desc])->andFilterWhere(['like', 'provider', $this->provider])->andFilterWhere(['like', 'app_logo', $this->app_logo])->andFilterWhere(['like', 'app_url', $this->app_url]); $session = Yii::$app->getSession(); $id = $session[AdminBaseInfo::SESSION_KEY_ADMIN]['id']; $authManager = Yii::$app->authManager->getRolesByUser($id); if (AdminBaseInfo::getAdminById($id)['role_id'] != 0) { $query->andFilterWhere(['like', 'app_key', array_keys($authManager)[0]]); } return $dataProvider; }
/** * 获取管理员所有角色 */ public static function getAdminRole() { $app = new ApplicationBaseInfo(); $role[0] = 'admin'; foreach ($app->getAppKeyId() as $key => $value) { $role[$value['id']] = $value['app_key']; } return $role; }
/** * 登录设置 * * 主要针对成功登录的用户进行Session、Cookie的设置 * * @param array $user 用户信息 * @param bool $cross_cookie true表示需要JSONP请求 * @return void */ private function _loginSetting($user, $cross_cookie = false) { // SESSION设置 $session = Yii::$app->session; $is_register = $session->get('is_register'); $session->destroy(); $data = ['id' => $user['id'], 'mobile' => $user['mobile'], 'name' => $user['username'], 'email' => $user['email'], 'avatar' => $user['avatar'], 'gender' => $user['gender'], 'open_id' => $user['open_id']]; if ($cross_cookie) { $session['cross_cookie'] = 1; $session['cookie_url'] = Url::toRoute(['sso/app-cookie'], true); } if ($is_register) { $session['is_register'] = $is_register; } $session[self::SESSION_KEY_USER] = $data; // COOKIE设置 $cookies = Yii::$app->response->cookies; $expiredTime = time() + 60 * 60 * 24; unset($data['open_id']); $myCookie = []; foreach ($data as $key => $val) { $myCookie[$key] = ['name' => self::COOKIE_NAME_PREFIX . $key, 'value' => $val]; } /* $myCookie = [ 'id' => [ 'name' => self::COOKIE_KEY_ID, 'value' => $user['id'], ], 'user_id' => [ 'name' => self::COOKIE_KEY_NAME, 'value' => $user['mobile'], ], 'avatar' => [ 'name' => 'avatar', 'value' => $user['avatar'], ], 'open_id' => [ 'name' => 'open_id', 'value' => $user['open_id'], ], ]; */ Yii::$app->util->cookie($myCookie, $expiredTime); $returnUrl = $session->get('register_returnurl'); $returnUrl = $returnUrl ? rawurldecode($returnUrl) : Url::toRoute(['ucenter/index'], true); $app = Yii::$app->util->judgTransUrl($returnUrl); if (!$app) { $app = ApplicationBaseInfo::getByAppUrl(parse_url(Url::toRoute(['ucenter/index'], true))['host']); } $appId = $app['app_id']; $data = ['user_id' => $user['id'], 'login_ip' => Yii::$app->util->getClientIP()]; try { $loginLogModel = Yii::$app->sharding->getModel('UserLoginLog', $appId); $loginLogModel->store($data); } catch (\Exception $res) { echo $res->getMessage(); } }
} ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'header' => '序号'], 'app_name', 'app_id', 'app_key', 'app_desc', 'provider', 'app_url:url', ['attribute' => 'create_time', 'value' => function ($model, $row) { return date('Y-m-d H:i:s', ApplicationBaseInfo::getApplicationById($row)['create_time']); }], ['attribute' => 'update_time', 'value' => function ($model, $row) { return date('Y-m-d H:i:s', ApplicationBaseInfo::getApplicationById($row)['update_time']); }], 'user_id', ['attribute' => 'status', 'value' => function ($model, $row) { $status = ApplicationBaseInfo::getApplicationById($row)['status']; if ($status == 1) { return '正常'; } else { return '禁用'; } }, 'filter' => ApplicationBaseInfo::getType('status', ['1' => '正常', '0' => '禁用'])], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{view} {update} {delete} {binding-service-view}', 'header' => '操作', 'buttons' => ['delete' => function ($url, $model, $key) { $options = ['title' => Yii::t('yii', 'Delete'), 'aria-label' => Yii::t('yii', 'Delete'), 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post', 'data-pjax' => '0']; $status = $model->status; if ($status == 2 || $status == 0) { return; } return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options); }, 'binding-service-view' => function ($url, $model, $key) { $options = ['title' => Yii::t('yii', 'BindingService'), 'aria-label' => Yii::t('yii', 'BindingService'), 'data_method' => 'post', 'data-pjax' => '0']; return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', $url, $options); }]]]]); ?> </div>
/** * Finds the ApplicationBaseInfo model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ApplicationBaseInfo the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ApplicationBaseInfo::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 处理用户登录 */ public function actionDoLogin() { header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"'); $mUserBase = new UserBaseInfo(); $util = Yii::$app->util; $session = Yii::$app->session; $request = Yii::$app->getRequest(); if (!$request->getIsAjax()) { $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']); } $returnUrl = $session->get('register_returnurl'); $returnUrl = $returnUrl ? rawurldecode($returnUrl) : Url::toRoute(['ucenter/index'], true); $app = Yii::$app->util->judgTransUrl($returnUrl); if (!$app) { $parseUrl = parse_url(Url::toRoute(['ucenter/index'], true)); $app = ApplicationBaseInfo::getByAppUrl($parseUrl['host']); } $post = Yii::$app->request->post(); $post = array_map('htmlEntityString', $post); $post = array_map('trim', $post); $userId = $post['user_id']; $password = $post['password']; $code = $post['code']; // 用户已登录 if ($this->isLogin) { $util->formatResData(0, 'success', ['url' => $returnUrl, 'appCookieUrl' => Url::toRoute(['sso/app-cookie'], true)]); } if ($session['login-fail-count'] >= 3) { $captcha = $this->createAction('captchaUserRegister'); if (!$captcha->validate($code, false)) { $util->formatResData(1201, 'captcha_error', ['msg' => '验证码错误']); } } if (empty($userId) || empty($password)) { $util->formatResData(1401, 'not_empty_error', ['msg' => '账号或密码不能为空']); } $user = $mUserBase->login($userId, $password); // p($user);die; if ($user) { $session->destroy(); $session[UserBaseInfo::SESSION_KEY_USER] = $user; if (isset($session['login-fail-count'])) { unset($session['login-fail-count']); } $cookies = Yii::$app->response->cookies; $times = time() + 60 * 60 * 4; $id = ['name' => UserBaseInfo::COOKIE_KEY_ID, 'value' => $user['id'], 'expire' => $times]; $userId = ['name' => UserBaseInfo::COOKIE_KEY_NAME, 'value' => $user['name'], 'expire' => $times]; $userMobile = ['name' => UserBaseInfo::COOKIE_NAME_PREFIX . 'mobile', 'value' => $user['mobile'], 'expire' => $times]; $userEmail = ['name' => UserBaseInfo::COOKIE_NAME_PREFIX . 'email', 'value' => $user['email'], 'expire' => $times]; $userGender = ['name' => UserBaseInfo::COOKIE_NAME_PREFIX . 'gender', 'value' => $user['gender'], 'expire' => $times]; $avatar = ['name' => 'avatar', 'value' => $user['avatar'], 'expire' => $times]; $openId = ['name' => 'open_id', 'value' => $user['open_id'], 'expire' => $times]; $cookie = ['id' => $id, 'userId' => $userId, 'avatar' => $avatar, 'openId' => $openId, 'mobile' => $userMobile, 'email' => $userEmail, 'gender' => $userGender]; $util->cookie($cookie, $times); $appId = $app['app_id']; $data = ['user_id' => $user['id'], 'login_ip' => Yii::$app->util->getClientIP()]; try { $loginLogModel = Yii::$app->sharding->getModel('UserLoginLog', $appId); $loginLogModel->store($data); } catch (\Exception $res) { echo $res->getMessage(); } $result = ['code' => 0, 'msg' => 'success', 'data' => ['url' => $returnUrl, 'appCookieUrl' => Url::toRoute(['sso/app-cookie'], true)]]; //$util->formatResData(0,'success', ['url' => $returnUrl]); echo json_encode($result); } else { $loginFailCount = $session['login-fail-count']; if (isset($session['login-fail-count'])) { $loginFailCount++; $session['login-fail-count'] = $loginFailCount; } else { $session['login-fail-count'] = 1; } $util = Yii::$app->util; $util->formatResData(1101, 'login_fail', $loginFailCount); } }
/** * 用户支付页面 */ public function actionOrderPay() { /** * 获取第三方绑定token * $url = 'http://test-api.chuangyejia.com/v1/token?app_id=100003&app_key=f8d80e9a'; * $res = do_request($url); * p($token = $res['data']['ret']['access_token']);die; * * do_request 获取借口信息 * $request = Yii::$app->getRequest(); * $get = $request->get(); * $data = [ * 'open_id' => $open_id, * 'add_order_sn' => $get['app_order_sn'], * 'access_token' => $token, * ]; * $url = 'adfadfaf'; * $orderInfo = do_request($url, $data); * $orderInfo = json_decode($orderInfo, true); * if ($orderInfo['code'] == 1101) { * return '订单不存在'; * } * if ($orderInfo['code'] == 1201) { * return '用户不存在'; * } * if ($orderInfo['code'] == 1301) { * return ... * } * if (!isset($_SERVER['HTTP_REFERER']) || !$_SERVER['HTTP_REFERER']) { * $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=非法请求'); * return '非法请求'; * } */ $mBind = new PayBinding(); $payMethods = new PayMethod(); $mOrder = new Order(); $mAddress = new Address(); $mApp = new ApplicationBaseInfo(); $request = Yii::$app->getRequest(); $get = $request->get(); $session = Yii::$app->getSession(); $cookie = $request->cookies->getValue('open_id'); $openId = $session[UserBaseInfo::SESSION_KEY_USER]['open_id'] ?: $cookie; $order = ''; //没有第三方传值 暂时默认 app_id = 100002, app_order_sn = 554892031231322 $appId = isset($get['app_id']) && $get['app_id'] ? $get['app_id'] : '100002'; if (!$appId) { return '应用id失效'; } $appOrderSn = isset($get['app_order_sn']) && $get['app_order_sn'] ? $get['app_order_sn'] : '555892131' . mt_rand(100000, 999999); if (!$appOrderSn) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=因无订单号为空'); return '订单号为空'; } if (isset($get['order_sn']) && $get['order_sn']) { $order = Order::getByOrderSn($get['order_sn']); if (!$order) { return $this->redirect(Url::toRoute('order/index')); } $appId = $order['app_id']; $appOrderSn = $order['app_order_sn']; if (!$appId || !$appOrderSn) { $order = ''; } } if (!isset($get['order_sn']) || !$get['order_sn'] || !$order) { $order = $mOrder->getByAppOrderSn($appOrderSn, $appId); } // 伪代码 访问应用接口返回data数据 // if ($data['open_id'] != $openId) { // return '非法请求'; // } $payIds = $mBind->getByAppId($appId); if (!$payIds) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=应用未绑定任何支付方式'); return '应用未绑定任何支付方式'; } $appUrls = arrayColumn($payIds, 'app_url'); $appUrls = array_unique($appUrls); $returnUrl = array_pop($appUrls); $app = $mApp->getByAppId($appId); $app = ['app_order_sn' => $appOrderSn, 'origin' => $app['app_name']]; $time = time(); $orderSn = Yii::$app->util->orderSn(); if (!$order) { try { // TODO:: 通过接口获取应用生成的订单信息 $orderInfo = ['open_id' => $openId, 'product_id' => '1', 'product_name' => 'Young K' . mt_rand(100000, 999999), 'order_sn' => $orderSn, 'turnover' => '0.01', 'app_order_title' => '黑马大赛报名' . mt_rand(100000, 999999), 'create_time' => $time, 'expires_time' => $time + 24 * 3600, 'address_id' => 1, 'app_id' => $appId]; $data = $this->wxQrCode($orderInfo); $app['address_id'] = $orderInfo['address_id']; $app['app_id'] = $orderInfo['app_id']; $orderId = $mOrder->add(array_merge($app, $data)); if (empty($orderId)) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=生成订单失败'); return '生成订单失败'; } $address = $mAddress->getById($orderInfo['address_id']); } catch (\Exception $e) { return $e->getMessage(); } } else { if ($order['expires_time'] < $time) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=订单过期'); return '订单过期'; } if ($order['open_id'] != $openId) { return '不具备操作该订单权限'; } if ($order['pay_status'] == Order::ORDER_PAID_STATUS) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=该订单已完成交易'); return '该订单已完成交易'; } if ($order['pay_status'] == Order::ORDER_REFUND_STATUS) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=该订单已退款'); return '该订单已退款'; } if ($order['pay_status'] == Order::ORDER_INVALID_STATUS) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=该订单已失效'); return '该订单已失效'; } if ($order['pay_status'] == Order::ORDER_CANCLE_STATUS) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=已取消订单'); return '已取消订单'; } $data = $this->wxQrCode($order); $address = $mAddress->getById($order['address_id']); } $address['county'] = isset($address['county']) && $address['county'] ? District::getRegionById($address['county']) : ''; $alipayForm = $this->genAlipayForm($data); $alipayQrcode = $this->genAlipayQrcode($data); $sig = $this->wxCodeSig($data); $data['signature'] = $sig; $methods = $payMethods->getById(arrayColumn($payIds, 'pay_id'), true); if (!$methods) { // $this->redirect($_SERVER['HTTP_REFERER'] . '?fileInfo=未找到支付方式'); return '未找到支付方式'; } $this->layout = 'main'; return $this->render('order_pay', ['methods' => $methods, 'data' => $data, 'appOrderSn' => $appOrderSn, 'address' => $address, 'returnUrl' => $returnUrl, 'alipayForm' => $alipayForm, 'alipayQrcode' => $alipayQrcode]); }
/** * 判断传递的域名是否满足要求 * * @param string $url 应用传递到数据中心的域名 * @return boolean */ public function judgTransUrl($url, $time = 1800) { if (!($app = Yii::$app->cache->get($url))) { if (!$this->regularUrl($url)) { return false; } $parseUrl = parse_url($url); if (!$parseUrl) { return false; } $appUrl = $parseUrl['host']; if (empty($appUrl)) { return false; } $app = ApplicationBaseInfo::getByAppUrl($appUrl); if (!$app) { return false; } Yii::$app->cache->set($url, $app, $time); } return $app; }