Commit 6ba7e310 authored by xiabin's avatar xiabin

Merge remote-tracking branch 'origin/dev' into dev

parents a79f222a b40522f5
<?php
/**
* 微信 API
*
* $Id$
*/
namespace common\libs;
class ApiWeixin extends OAuth2 {
private $token;
protected $_type = 'weixin';
/**
* Set up the API params.
*
* @ignore
*/
protected $host = 'https://api.weixin.qq.com/';
protected $format = 'json';
public $debug = false;
public function __construct($accessToken=null, $refreshToken=null, $openid=null) {
$config = \Yii::$app->params['weixinpay'];
//$this->token = $config['token'];
parent::__construct($config['appid'], $config['appsecret'], $accessToken, $refreshToken, $openid);
//不传递 AccessToken 时,默认使用 API AccessToken
if ($accessToken === null) {
$accessToken = $this->getClientAccessToken();
parent::__construct($config['appid'], $config['appsecret'], $accessToken, $refreshToken, $openid);
}
$this->useragent = 'PHP/'.PHP_VERSION;
}
/**
* 设置 OAuth 认证需要的 Urls
*
* @ignore
*/
public function accessTokenURL($type) {
if ($type == 'client') {
return $this->host.'cgi-bin/token';
} else {
return $this->host.'sns/oauth2/access_token';
}
}
public function authorizeURL() { return 'https://open.weixin.qq.com/connect/oauth2/authorize'; }
public function getAuthorizeURLWrapped($url, $state='') {
return $this->getAuthorizeURL($url, 'code', 'snsapi_userinfo', $state).'#wechat_redirect';
}
/**
* 获取微信API客户端 AccessToken,已进行相应的缓存处理
*
* @throws OAuthException
* @return string
*/
public function getClientAccessToken() {
$k = 'yy_wxapi_access_token';
$token = \Yii::$app->redis->get($k);
if ($token !== null) {
return $token;
}
$ret = $this->getAccessToken('client');
if (isset($ret['access_token'])) {
\Yii::$app->redis->setex($k, $ret['expires_in']-8, $ret['access_token']);
} else {
throw new OAuthException('get client access token failed because: '.$ret['errmsg'].'('.$ret['errcode'].')');
}
return $ret['access_token'];
}
/**
* 获取用于某些接口使用的 ticket,已作缓存处理
*
* @param string $type
* @throws OAuthException
* @return string
*/
public function getTicket($type='jsapi') {
$k = 'yy_wxapi_ticket_'.$type;
$ticket = \Yii::$app->redis->get('get', $k);
if ($ticket !== null) {
return $ticket;
}
$accessToken = $this->getClientAccessToken();
$ret = $this->get('cgi-bin/ticket/getticket', array('access_token'=>$accessToken,'type'=>$type));
if ($ret['errcode'] == 0) {
\Yii::$app->redis->setex($k, $ret['expires_in']-8, $ret['ticket']);
} else {
throw new OAuthException("get $type ticket failed because: {$ret['errmsg']}({$ret['errcode']})");
}
return $ret['ticket'];
}
/**
* 获取关注者 openid 列表
*
* @param string $nextOpenId
* @return array
*/
public function getUser($nextOpenId='') {
return $this->get('cgi-bin/user/get', array('next_openid'=>$nextOpenId));
}
/**
* 通过 openid 获取指定关注者信息
*
* @param $openId
* @return mixed
*/
public function userInfo($openId) {
//snsapi_userinfo 方式获取信息
return $this->get('sns/userinfo', array('openid'=>$openId, 'lang'=>'zh_CN'));
//snsapi_base 方式获取信息
//return $this->get('cgi-bin/user/info', array('openid'=>$openId));
}
public function sendTemplateMessage($openId, $templateId, $data, $url='', $topColor='#FF0000') {
$data = array(
'touser' => $openId,
'template_id' => $templateId,
'url' => $url,
'topcolor' => $topColor,
'data' => $data
);
return $this->post('cgi-bin/message/template/send', $data);
}
/**
* 创建二维码 Ticket
*
* @param int|string $sceneIdOrStr 当使用永久字符串参数二维码时,此处为字符串 scene_str 值,否则是 scene_id 值
* @param string $actionName QR_SCENE:临时二维码,QR_LIMIT_SCENE:永久二维码,QR_LIMIT_STR_SCENE:永久字符串参数二维码
* @param int $expireSeconds 临时有效期,最长7天,604800秒
* @return mixed
*/
public function createQRCode($sceneIdOrStr, $actionName='QR_SCENE', $expireSeconds=1800) {
$data = array(
'expire_seconds' => $expireSeconds,
'action_name' => $actionName,
'action_info' => array()
);
if ($actionName == 'QR_LIMIT_STR_SCENE') {
$data['action_info']['scene_str'] = $sceneIdOrStr;
} else {
$data['action_info']['scene_id'] = $sceneIdOrStr;
}
return $this->post('cgi-bin/qrcode/create', $data);
}
/**
* 通过 Ticket 换取二维码
*
* @param $ticket
* @return mixed
*/
public function showQRCode($ticket) {
return 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
}
/**
* 发送 POST 请求
*
* 微信接口 POST 包体为 json 数据,此处进行封装
*
* @param string $url
* @param array $parameters
* @param bool $multi
* @return mixed
*/
protected function post($url, $parameters=array(), $multi=false) {
!$multi && $parameters = json_encode($parameters);
return parent::post($url, $parameters, $multi);
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -8,7 +8,7 @@
namespace common\libs;
use yii\base\ExitException;
use yii\web\Response;
class Utils
......@@ -76,7 +76,22 @@ class Utils
return '';
}
/**
* 显示提示信息页面
*
* @param string $message
* @throws ExitException
*/
public static function showWarn($message)
{
\Yii::$app->controller->layout = 'blank';
\Yii::$app->response->data = \Yii::$app->controller->render('@app/views/site/warn', [
'message' => $message
]);
\Yii::$app->response->send();
throw new ExitException();
}
/**
* 生成4-6位随机数
......
......@@ -157,6 +157,12 @@ class Member extends \yii\db\ActiveRecord
}
}
/**
* 获取不同类型会员的数量统计
*
* @param int $userId
* @return array
*/
public static function typeCount($userId)
{
$data = self::find()->select('COUNT(1) AS count, type')->where(['user_id'=>$userId])->groupBy('type')
......
......@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "4454b16ac08c05c643766291ce0b6335",
"content-hash": "7cd14e3280f6fcccf735c7641fab4a28",
"hash": "479a584a432c765bda14b8e45493291a",
"content-hash": "b2a9e1b6aa19ba113948022d2525b40b",
"packages": [
{
"name": "2amigos/yii2-qrcode-helper",
......@@ -572,60 +572,6 @@
],
"time": "2015-08-15 16:07:34"
},
{
"name": "mobiledetect/mobiledetectlib",
"version": "2.8.22",
"source": {
"type": "git",
"url": "https://github.com/serbanghita/Mobile-Detect.git",
"reference": "53cddae0c272a478b24a4b5fb60d0f838caf70b6"
},
"dist": {
"type": "zip",
"url": "https://packagist.phpcomposer.com/files/serbanghita/Mobile-Detect/53cddae0c272a478b24a4b5fb60d0f838caf70b6.zip",
"reference": "53cddae0c272a478b24a4b5fb60d0f838caf70b6",
"shasum": ""
},
"require": {
"php": ">=5.0.0"
},
"require-dev": {
"codeclimate/php-test-reporter": "dev-master",
"johnkary/phpunit-speedtrap": "~1.0@dev",
"phpunit/phpunit": "*"
},
"type": "library",
"autoload": {
"classmap": [
"Mobile_Detect.php"
],
"psr-0": {
"Detection": "namespaced/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Serban Ghita",
"email": "serbanghita@gmail.com",
"homepage": "http://mobiledetect.net",
"role": "Developer"
}
],
"description": "Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.",
"homepage": "https://github.com/serbanghita/Mobile-Detect",
"keywords": [
"detect mobile devices",
"mobile",
"mobile detect",
"mobile detector",
"php mobile detect"
],
"time": "2016-04-24 09:47:16"
},
{
"name": "psr/http-message",
"version": "1.0.1",
......
......@@ -8,6 +8,7 @@
namespace frontend\components;
use common\libs\Utils;
use common\models\Classes;
use Yii;
use yii\base\ActionFilter;
......@@ -40,31 +41,25 @@ class ClassesFilter extends ActionFilter
public function beforeAction($action) {
$uid = Yii::$app->user->getId();
if (!$uid) {
return parent::beforeAction($action);
}
$class = Classes::getUserClass($uid);
if (!$class) {
$this->showAlert(0);
return false;
Utils::showWarn('您还没有创建课堂,无法访问此页面!');
}
if ($this->_checkStatus && $class['status'] != Classes::STATUS_OK) {
$this->showAlert(1);
return false;
Utils::showWarn('您提交的课堂资料尚未通过审核,无法访问此页面!');
}
if (property_exists(Yii::$app->controller, 'classStatus')) {
Yii::$app->controller->classStatus = $class;
}
return parent::beforeAction($action);
}
public function showAlert($exists)
{
Yii::$app->response->data = Yii::$app->view->renderFile('@app/views/site/error.php', [
'name' => '提示消息',
'message' => $exists ? '您提交的课堂资料尚未通过审核,无法访问此页面!' : '您还没有创建课堂,无法访问此页面!'
]);
return parent::beforeAction($action);
}
}
\ No newline at end of file
......@@ -2,10 +2,14 @@
namespace frontend\controllers;
use common\libs\Utils;
use common\models\Income;
use dosamigos\qrcode\lib\Enum;
use frontend\components\ClassesFilter;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Response;
class MyIncomeController extends \yii\web\Controller
{
......@@ -26,9 +30,12 @@ class MyIncomeController extends \yii\web\Controller
'allow' => true,
'roles' => ['@']
]
]
],
'except' => ['weixin-bind', 'weixin-callback']
],
'classes' => ClassesFilter::className()
'classes' => [
'class' => ClassesFilter::className()
]
];
}
......@@ -47,4 +54,51 @@ class MyIncomeController extends \yii\web\Controller
return $this->render('index', $ret + compact('settled', 'notSettled'));
}
public function actionWxbindQrcode()
{
//生成随机的 key 防止被猜测到
$key = Yii::$app->security->generateRandomString();
//记录绑定信息
Yii::$app->redis->setex('yy_wxbind_'.$key, 7200, serialize([
'uid' => Yii::$app->user->getId()
]));
$url = Url::to(['my-income/weixin-bind', 'key'=>$key], true);
//输出二维码图像
$response = Yii::$app->response;
$response->format = Response::FORMAT_RAW;
$response->headers->set('Content-Type', 'image/jpeg');
\dosamigos\qrcode\QrCode::encode($url, false, Enum::QR_ECLEVEL_L,
7, 2, false, Enum::QR_FORMAT_JPG);
}
public function actionWeixinBind()
{
$key = Yii::$app->request->get('key', '');
if (!$key) {
Utils::showWarn('参数缺失!');
}
$data = Yii::$app->redis->get('yy_wxbind_'.$key);
if (!$data) {
Utils::showWarn('该地址已失效,请刷新页面后重新扫描!');
}
$data = unserialize($data);
$callbackUrl = Url::to(['my-income/weixin-callback'], true);
$wx = new \common\libs\ApiWeixin();
echo $wx->getAuthorizeURLWrapped($callbackUrl, $key);
}
public function actionWeixinCallback()
{
}
}
<?php
/**
* @var $this \yii\web\View
* @var $content string
*/
use yii\helpers\Html;
$this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?=\Yii::$app->language?>">
<head>
<meta charset="<?=\Yii::$app->charset?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title><?=($this->title ? Html::encode($this->title).'-' : '')?>最课堂</title>
<?php $this->head() ?>
</head>
<body>
<?php
$this->beginBody();
echo $content;
$this->endBody();
?>
</body>
</html>
<?php $this->endPage();
\ No newline at end of file
<?php
/* @var $this yii\web\View */
/**
* @var $this yii\web\View
* @var $data array
* @var $pagination \yii\data\Pagination
*/
use frontend\assets\AppAsset;
$this->title = '知识有价';
AppAsset::addScript($this, '@web/assets/my_income.js');
?>
<div class="buy-recode-top">
<div class="col-md-4">
<h3>结算微信</h3>
<button class="btn btn-save">绑定</button>
<button type="button" class="btn btn-save" @click="bindWeixinClick">绑定</button>
</div>
<div class="col-md-4">
<h3>已结算</h3>
......@@ -40,7 +48,7 @@ $this->title = '知识有价';
<td><?=$row['order']['user']['telephone']?> / <?=$row['order']['user']['username']?></td>
<td>《<?=$row['order']['course']['name']?>》</td>
<td>¥<?=$row['order']['credit']?></td>
<td><?=($row['status'] ? '未结算' : '已结算')?></td>
<td><?=($row['status'] ? '已结算' : '未结算')?></td>
</tr>
<?php } ?>
</tbody>
......
var Vue = require('vue');
var layer = require('layer-js');
var vm = new Vue({
el: ".buy-recode-top",
methods: {
bindWeixinClick: function() {
var idx = layer.open({
type: 1,
title: "绑定微信",
content: '<div class="qrcode-img"><img src="' + __BASE_URL__ + 'my-income/wxbind-qrcode" style="margin:0;height:auto;"><p>扫码后,请在手机端“确认”绑定</p></div>',
area: ['320px', '380px'],
cancel: function(idx) {
layer.close(idx);
this.content.show();
}
});
}
}
});
\ No newline at end of file
......@@ -24,7 +24,8 @@ var entry = {
home_index: [js_dir + '/home/index.js'],
home_video: [js_dir + '/home/video.js'],
course_publish: [js_dir + '/my-course/publish.js'],
teacher_form: [js_dir + '/teacher-manager/form.js']
teacher_form: [js_dir + '/teacher-manager/form.js'],
my_income: [js_dir + '/my-income/index.js']
};
var config = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment