Skip to content

Commit

Permalink
主动发送消息
Browse files Browse the repository at this point in the history
  • Loading branch information
callmez committed Jun 19, 2015
1 parent c5d5806 commit 7c301fb
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 158 deletions.
17 changes: 16 additions & 1 deletion components/MpWechat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class MpWechat extends \callmez\wechat\sdk\MpWechat
{
use WechatTrait;
/**
* 绑定的公众号存储类
* @var Wechat
Expand All @@ -32,4 +31,20 @@ public function __construct(Wechat $wechat, $config = [])

parent::__construct($config);
}

/**
* 跳转微信网页获取用户授权信息
* @param string $state
* @return mixed
*/
public function getAuthorizeUserInfo($state = 'authorize', $scope = 'snsapi_base')
{
$request = Yii::$app->request;
if (($code = $request->getQueryParam('code')) && $request->getQueryParam('state') == $state) {
return $this->getOauth2AccessToken($code);
} else {
Yii::$app->getResponse()->redirect($this->getOauth2AuthorizeUrl($request->getAbsoluteUrl(), $state, $scope));
Yii::$app->end();
}
}
}
81 changes: 0 additions & 81 deletions components/Wechat.php

This file was deleted.

22 changes: 0 additions & 22 deletions components/WechatTrait.php

This file was deleted.

31 changes: 14 additions & 17 deletions controllers/FansController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,25 @@ public function actionIndex()
]);
}

/**
* 用户查看
* @param $id
* @return string
* @throws NotFoundHttpException
*/
public function actionMessage($id)
{
$model = $this->findModel($id);
$message = new Message();
$message = new Message($this->getWechat());

if ($message->load(Yii::$app->request->post()) && $message->send()) {
$this->flash('消息发送成功!', 'success');
$message = new Message();
if ($message->load(Yii::$app->request->post())) {
$message->toUser = $model->open_id;
if ($message->send()) {
$this->flash('消息发送成功!', 'success');
$message = new Message($this->getWechat());
}
}
$message->toUser = $model->open_id;

$searchModel = new MessageHistorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
Expand Down Expand Up @@ -77,19 +87,6 @@ public function actionUpdate($id)
}
}

/**
* Deletes an existing Fans model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Finds the Fans model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
Expand Down
37 changes: 35 additions & 2 deletions models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,25 @@ class Message extends Model
* @var string
*/
public $hqMusicUrl;
/**
* 微信公众号
* @var Wechat
*/
protected $wechat;

public function __construct(Wechat $wechat, $config = [])
{
$this->wechat = $wechat; // 需设置微信公众号交互主体
parent::__construct($config);
}
/**
* @inhertdoc
*/
public function rules()
{
return [
[['toUser', 'content'], 'required', 'when' => function ($model, $attribute) {
[['toUser'], 'required'],
[['content'], 'required', 'when' => function ($model, $attribute) {
return $model->msgType = Message::TYPE_TEXT;
}, 'whenClient' => "function (attribute, value) {
return $('#message-msgtype input[type=radio]:checked').val() == '" . Message::TYPE_TEXT . "';
Expand Down Expand Up @@ -135,7 +146,29 @@ public function send($runValidation = true)
if ($runValidation && !$this->validate()) {
return false;
}
$method = 'send' . $this->msgType;
if (!method_exists($this, $method)) {
$this->addError('msgType', '未找到指定发送方法');
return false;
}
$result = call_user_func([$this, $method]);
if (!$result) {
$this->addError('msgType', json_encode($this->wechat->getSdk()->lastError));
}
return $result;
}

return true;
/**
* @return mixed
*/
protected function sendText()
{
return $this->wechat->getSdk()->sendMessage([
'touser' => $this->toUser,
'msgtype' => $this->msgType,
$this->msgType => [
'content' => $this->content
]
]);
}
}
1 change: 1 addition & 0 deletions views/fans/_messageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'id' => 'messageForm',
'layout' => 'horizontal'
]); ?>
<?= Html::activeHiddenInput($model, 'toUser') ?>

<?= $form->field($model, 'msgType')->inline()->radioList(Message::$types) ?>

Expand Down
54 changes: 22 additions & 32 deletions views/fans/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@
'width' => 75
]
],
[
'attribute' => 'user.avatar',
'format' => 'html',
'value' => function ($model) use ($wechatAsset) {
return Html::img($model->user ? $model->user->avatar : $wechatAsset->baseUrl . '/images/anonymous_avatar.jpg', [
'width' => 40,
'class' => 'img-rounded'
]);
},
'options' => [
'width' => 70
]
],
[
'attribute' => 'user.nickname',
'value' => function ($model) {
return $model->user ? $model->user->nickname : '';
}
],
[
'attribute' => 'open_id',
'options' => [
Expand All @@ -44,47 +63,18 @@
'width' => 120
]
],
[
'attribute' => 'user.avatar',
'format' => 'html',
'value' => function ($model) use ($wechatAsset) {
return Html::img($model->user ? $model->user->avatar : $wechatAsset->baseUrl . '/images/anonymous_avatar.jpg', [
'width' => 40,
'class' => 'img-rounded'
]);
}
],
[
'attribute' => 'user.nickname',
'value' => function ($model) {
return $model->user ? $model->user->nickname : '';
}
],
[
'attribute' => 'created_at',
'format' => 'datetime',
'options' => [
'width' => 160
]
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{history}',
'class' => 'callmez\wechat\widgets\ActionColumn',
'template' => '{history} {update}',
'buttons' => [
'history' => function ($url, $model, $key) {
return Html::a('发送消息', ['message', 'id' => $key]);
return Html::a('发送消息', ['message', 'id' => $key]);
}
],
'options' => [
'width' => 80
]
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'options' => [
'width' => 55
]
],
],
]); ?>
Expand Down
2 changes: 1 addition & 1 deletion widgets/ActiveField.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function createLayoutConfig($instanceConfig)
'offset' => 'col-sm-offset-3',
'label' => 'col-sm-3',
'wrapper' => 'col-sm-6',
'error' => 'col-sm-3',
'error' => '',
'hint' => '',
];
if (isset($instanceConfig['horizontalCssClasses'])) {
Expand Down
2 changes: 0 additions & 2 deletions widgets/MessageList.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,5 @@ public function init()
$totalCount = $this->dataProvider->getTotalCount();
$pagination->setPage((int) (($totalCount + $pageSize - 1) / $pageSize) - 1);
}


}
}

0 comments on commit 7c301fb

Please sign in to comment.