Skip to content

Add wechat api for dev-master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/Http/Controllers/Api/TimelineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ public function postDestroy(Request $request)

$Timeline = Timeline::findOrFail($request->id);

if ($Timeline->user_id === Auth::user()->id) {
return $Timeline->delete() ? ['success'] : response('fail', 500);
} elseif (Auth::user()->id === Auth::user()->id) {
if ($Timeline->user_id === Auth::user()->id || Auth::user()->is_admin) {
return $Timeline->delete() ? ['success'] : response('fail', 500);
}

Expand Down
126 changes: 126 additions & 0 deletions app/Http/Controllers/Api/WeChatController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use GuzzleHttp\Client;
use EasyWeChat\Foundation\Application;

use Auth;
use App\User;

class WeChatController extends Controller
{
/**
*
*
*/
public function getOAuth(Request $request)
{
if (Auth::guest()) {
$options = [
'debug' => true,
'app_id' => env('WECHAT_APPID'),
'secret' => env('WECHAT_SECRET'),
];

$domain = $request->header()['host'][0];
$redirect = 'http://cloud.hey-community.com/api/wechat/get-wechat-user?domain=' . $domain;

$app = new Application($options);
$response = $app->oauth->scopes(['snsapi_userinfo'])
->setRequest($request)
->redirect($redirect);

return $response;
} else {
return back();
}

}

/**
*
*/
public function getCheckSignature(Request $request)
{
$signature = $request->signature;
$timestamp = $request->timestamp;
$nonce = $request->nonce;

$token = 'protobiatechanddev4livingteam';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if($tmpStr == $signature){
return response($request->echostr, 200);
}else{
return response('false', 400);
}
}

/**
*
*/
public function getGetWechatUser(Request $request)
{
$options = [
'debug' => true,
'app_id' => env('WECHAT_APPID'),
'secret' => env('WECHAT_SECRET'),
];

$app = new Application($options);
$user = $app->oauth->setRequest($request)->user();

if ($user) {
$User = User::where('wx_open_id', $user->getId())->first();
if (!$User) {
$User = new User;
$User->wx_open_id = $user->getId();
$User->nickname = $user->getNickname();
$User->avatar = $user->getAvatar();

$number = random_int(0, 3);
if ($number === 0) {
$User->bio = 'My name is ' . $user->getNickname();
} else if ($number === 1) {
$User->bio = 'I\'m ' . $user->getNickname();
} else if ($number === 2) {
$User->bio = $user->getNickname() . ' is me';
} else if ($number === 3) {
$User->bio = 'I love there';
}

$User->save();
}

$redirect = 'http://' . $request->domain . '/api/wechat/login?wx_open_id=' . $user->getId();
return redirect($redirect);
} else {
abort(500, 'wechat login fail');
}
}

/**
*
*/
public function getLogin(Request $request)
{
$this->validate($request, [
'wx_open_id' => 'required',
]);

$User = User::where('wx_open_id', $request->wx_open_id)->first();
if ($User) {
Auth::login($User);
}

return redirect()->to('/?noWeChatOAuth=true');
}
}
1 change: 1 addition & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
Route::controller('user', 'Api\UserController');
Route::controller('timeline', 'Api\TimelineController');
Route::controller('notice', 'Api\NoticeController');
Route::controller('wechat', 'Api\WeChatController');
Route::controller('system', 'Api\SystemController');
});
64 changes: 63 additions & 1 deletion app/Listeners/TriggerNoticeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use EasyWeChat\Foundation\Application;

use Auth;
use App\Notice;
use App\NoticeType;
Expand All @@ -32,6 +34,7 @@ public function handle(TriggerNoticeEvent $event)
{
$this->event = $event;

// save notice in db
$Notice = new Notice();
$Notice->user_id = $this->event->target->user_id;
$Notice->initiator_user_id = Auth::user()->id;
Expand All @@ -44,7 +47,66 @@ public function handle(TriggerNoticeEvent $event)

// send wechat notice
if ($this->event->target->author->wx_open_id) {
//@todo $this->sendWechatNotice();
$this->sendWechatNotice();
}
}

/**
*
*/
public function sendWechatNotice()
{
$options = [
'debug' => true,
'app_id' => env('WECHAT_APPID'),
'secret' => env('WECHAT_SECRET'),
];
$app = new Application($options);
$notice = $app->notice;
$userId = $this->event->target->author->wx_open_id;
$templateId = env('WECHAT_TEMP_NOTICE_ID');
$url = 'http://' . request()->header()['host'][0];
$color = '#FF0000';
$data = [
'first' => $this->getWechatNoticeFirst(),
'subject' => $this->getWechatNoticeSubject(),
'sender' => "HEY社区 机器人",
'remark' => '这是来自 ' . System::findOrFail(1)->community_name . ' 的消息,点击了解详情',
];
$result = $notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($userId)->send();
}

/**
*
*/
public function getWechatNoticeFirst()
{
$str = Auth::user()->nickname . ': ';
$noticeType = NoticeType::getIdByName($this->event->noticeTypeName);
if ($noticeType == 10) {
$str .= '喜欢 ❤️';
} else if ($noticeType == 11) {
$str .= $this->event->entity->content;
} else if ($noticeType == 12) {
$str .= $this->event->entity->content;
}
return $str;
}

/**
*
*/
public function getWechatNoticeSubject()
{
$str = Auth::user()->nickname . ' ';
$noticeType = NoticeType::getIdByName($this->event->noticeTypeName);
if ($noticeType == 10) {
$str .= '喜欢你的公园动态';
} else if ($noticeType == 11) {
$str .= '评论了你的公园动态';
} else if ($noticeType == 12) {
$str .= '在公园动态中回复了你';
}
return $str;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"intervention/image": "^2.3",
"overtrue/laravel-wechat": "~3.0",
"php-ffmpeg/php-ffmpeg": "^0.6.1"
},
"require-dev": {
Expand Down
Loading