Skip to content

Commit

Permalink
支持企业微信
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Jul 10, 2017
1 parent f9c17c0 commit 1e142b3
Show file tree
Hide file tree
Showing 29 changed files with 424 additions and 208 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ JWT_SECRET=
JWT_BLACKLIST_GRACE_PERIOD=3600

QYWX_ROOTID=1
QYWX_CONTACTS_SECRET=
QYWX_CORPID=
QYWX_SECRET=
QYWX_APPID=18
48 changes: 48 additions & 0 deletions app/Biz/UserBiz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Biz;

use App\Repositories\UserRepository;
use App\Exceptions\NormalException;
use Wxsdk\Qywx;

class UserBiz
{
protected $userRepo;

public function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
}

public function sendWxMsg($username, $title, $message, $url = '')
{
$qywx = new Qywx(config('qywx.app'));

$articles = [
$qywx->buildNewsItem($title, $message, $url, ''),
];

if (!is_array($username)) {
$username = [$username];
}

$username = array_filter($username, function ($name) {
return !in_array($name, ['suadmin', 'admin', 'demo']);
});

$result = $qywx->sendNewsMsg(
$articles,
['touser' => $username],
config('qywx.app.appid')
);

if (($result['invaliduser'] ?? false) or
($result['invalidparty'] ?? false) or
($result['invalidtag'] ?? false)) {
throw new NormalException('部分发送失败');
}

return true;
}
}
1 change: 0 additions & 1 deletion app/Console/Commands/Rbac/AddUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function handle()
$user->password = bcrypt($password);
$user->name = $name;
$user->email = $email;
$user->departments = '{}';
$user->info = '{}';

$user->saveOrFail();
Expand Down
17 changes: 2 additions & 15 deletions app/Department.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@

use Illuminate\Database\Eloquent\Model;

use Qywx;

class Department extends Model
{
public static function sync()
public function users()
{
if ($departments = Qywx::getDepartments(config('qywx')['rootid'])) {
self::truncate();

return self::insert(array_map(function ($row) {
return array_merge($row, [
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s")
]);
}, $departments));
} else {
return false;
}
return $this->belongsToMany(User::class);
}
}
32 changes: 32 additions & 0 deletions app/Exceptions/NormalException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Exceptions;

use Exception;

class NormalException extends Exception
{
protected $errors = [];
protected $status = 'error';
protected $data = null;

public function __construct($message = '', $status = 'error', $errors = [], $data = null, $code = 0)
{
$this->errors = $errors;
$this->status = $status;
$this->data = $data;

parent::__construct($message, $code);
}

public function toArray()
{
return [
'status' => $this->status,
'message' => $this->getMessage(),
'data' => $this->data,
'errors' => $this->errors,
'code' => $this->getCode(),
];
}
}
13 changes: 0 additions & 13 deletions app/Facades/Qywx.php

This file was deleted.

10 changes: 6 additions & 4 deletions app/Http/Controllers/Api/V1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use App\Http\Controllers\Controller;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Jobs\SyncUserFromQywx;
use App\Jobs\SyncFromQywx;
use App\User;
use Auth;
use Cache;
use Qywx;
use Wxsdk\Qywx;

/**
* 认证授权
Expand Down Expand Up @@ -215,13 +215,15 @@ public function codelogin(Request $request)
return $this->ajax('error', '未提供code');
}

if (! $username = Qywx::getUserId($code)) {
$qywx = new Qywx(config('qywx.app'));

if (! $username = $qywx->getUserId($code)) {
return $this->ajax('error', '不属于企业号,请联系管理员,或稍后再试');
}

if (! $loginResult = $this->_loginByUsername($username)) {
// 可能数据库数据数据,尝试同步
dispatch(new SyncUserFromQywx(true));
dispatch(new SyncFromQywx(true));
return $this->ajax('error', '或许您是新加入的成员,请耐心等待系统同步数据,十分钟后再来吧 :)');
}

Expand Down
16 changes: 11 additions & 5 deletions app/Http/Controllers/Api/V1/DepartmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Qywx;
use App\Http\Controllers\Controller;
use App\Department;
use App\Jobs\SyncUserFromQywx;
use App\Jobs\SyncFromQywx;
use App\Repositories\DepartmentRepository;

/**
* 部门管理
Expand All @@ -15,6 +14,13 @@
*/
class DepartmentController extends Controller
{
protected $departmentRepo;

public function __construct(DepartmentRepository $departmentRepo)
{
$this->departmentRepo = $departmentRepo;
}

/**
* 从企业号同步部门
*
Expand All @@ -23,7 +29,7 @@ class DepartmentController extends Controller
*/
public function sync()
{
dispatch(new SyncUserFromQywx);
dispatch(app(SyncFromQywx::class));

return $this->ajax('ok', "已经开始同步,请稍后刷新页面查看同步结果");
}
Expand Down Expand Up @@ -54,6 +60,6 @@ public function sync()
*/
public function list()
{
return $this->ajax('ok', '获取成功', Department::paginate(15)->toArray());
return $this->ajax('ok', '获取成功', $this->departmentRepo->list());
}
}
33 changes: 18 additions & 15 deletions app/Http/Controllers/Api/V1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Jobs\SyncUserFromQywx;
use App\User;
use App\Role;
use App\Jobs\SyncFromQywx;
use App\Biz\UserBiz;
use App\Repositories\UserRepository;

/**
* 用户
Expand All @@ -15,6 +15,15 @@
*/
class UserController extends Controller
{
protected $userRepo;
protected $userBiz;

public function __construct(UserRepository $userRepo, UserBiz $userBiz)
{
$this->userRepo = $userRepo;
$this->userBiz = $userBiz;
}

/**
* 从企业号同步用户
*
Expand All @@ -29,7 +38,7 @@ class UserController extends Controller
*/
public function sync()
{
dispatch(new SyncUserFromQywx);
dispatch(app(SyncFromQywx::class));

return $this->ajax('ok', "已经开始同步,请稍后刷新页面查看同步结果");
}
Expand Down Expand Up @@ -74,21 +83,16 @@ public function list(Request $request)
{
$search = $request->input('search');

$data = User::where('name', 'like', "%{$search}%")
->orWhere('username', 'like', "%{$search}%")
->orWhere('mobile', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%")
->paginate(15)
->toArray();
$data = $this->userRepo->search($search);

return $this->ajax('ok', '获取成功', $data);
}

/**
* 向某个用户发送微信消息
*
* @Post("sendmessage")
* @Request({"username": "testuser", "message": "测试消息"})
* @Post("sendmessage/testuser")
* @Request({"message": "测试消息"})
* @Response(200, body={
* "status": "ok|error",
* "message": "...",
Expand All @@ -97,12 +101,11 @@ public function list(Request $request)
* "code":0
* })
*/
public function sendMessage(Request $request)
public function sendMessage(Request $request, $username)
{
$username = $request->json('username');
$message = $request->json('message');

if (User::sendWxMsg($username, '管理员消息', $message)) {
if ($this->userBiz->sendWxMsg($username, '管理员消息', $message)) {
return $this->ajax('ok', '发送消息成功');
}

Expand Down
8 changes: 5 additions & 3 deletions app/Http/Controllers/Api/V1/WechatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Qywx;
use Wxsdk\Qywx;
use App\Http\Controllers\Controller;

class WechatController extends Controller
Expand All @@ -16,13 +16,15 @@ public function code(Request $request)

return redirect("/mobile.html#/login/?code={$code}&next=" . urlencode($next));
} else {
return redirect(Qywx::getJumpOAuthUrl(url()->full()));
$qywx = new Qywx(config('qywx.app'));
return redirect($qywx->getJumpOAuthUrl(url()->full()));
}
}

public function wxjs()
{
$jsApiPackage = Qywx::getJsApiPackage(url('mobile.html'));
$qywx = new Qywx(config('qywx.app'));
$jsApiPackage = $qywx->getJsApiPackage(url('mobile.html'));

$content = <<< JS
wx.config({
Expand Down
39 changes: 39 additions & 0 deletions app/Jobs/SyncFromQywx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use App\Repositories\DepartmentRepository;
use App\Repositories\UserRepository;

class SyncFromQywx implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $userRepo;
protected $departmentRepo;

public function __construct(
UserRepository $userRepo,
DepartmentRepository $departmentRepo
) {
$this->userRepo = $userRepo;
$this->departmentRepo = $departmentRepo;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->departmentRepo->sync();
$this->userRepo->sync();
}
}
Loading

0 comments on commit 1e142b3

Please sign in to comment.