Skip to content

Commit 04c82c8

Browse files
committed
增加通知功能
1 parent b040668 commit 04c82c8

File tree

12 files changed

+483
-95
lines changed

12 files changed

+483
-95
lines changed

api/app/Http/Controllers/v1/Admin/IndentController.php

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Models\v1\GoodIndentCommodity;
99
use App\Models\v1\MoneyLog;
1010
use App\Models\v1\User;
11+
use App\Notifications\InvoicePaid;
1112
use Illuminate\Http\Request;
1213
use App\Http\Controllers\Controller;
1314
use Carbon\Carbon;
@@ -51,41 +52,76 @@ public function show($id){
5152

5253
// 发货
5354
public function shipment(Request $request){
54-
$GoodIndent=GoodIndent::with(['User'])->find($request->id);
55-
$GoodIndent->dhl_id = $request->dhl_id;
56-
$GoodIndent->odd = $request->odd;
57-
$GoodIndent->state = GoodIndent::GOOD_INDENT_STATE_TAKE;
58-
$GoodIndent->shipping_time = Carbon::now()->toDateTimeString();
59-
$GoodIndent->save();
60-
$Dhl=Dhl::find($request->dhl_id);
61-
//通知
62-
$config = config('wechat.mini_program.default');
63-
$delivery_release = config('wechat.subscription_information.delivery_release');
64-
$app = Factory::miniProgram($config); // 小程序
65-
$data = [
66-
'template_id' => $delivery_release,
67-
'touser' => $GoodIndent->User->wechat_applet_openid,
68-
'page' => 'pages/order/showOrder?id='.$GoodIndent->id,
69-
'data' => [
70-
'character_string1' => [
71-
'value' => $GoodIndent->identification,
55+
$return=DB::transaction(function ()use($request){
56+
$GoodIndent=GoodIndent::with(['User'])->find($request->id);
57+
$GoodIndent->dhl_id = $request->dhl_id;
58+
$GoodIndent->odd = $request->odd;
59+
$GoodIndent->state = GoodIndent::GOOD_INDENT_STATE_TAKE;
60+
$GoodIndent->shipping_time = Carbon::now()->toDateTimeString();
61+
$GoodIndent->save();
62+
$Dhl=Dhl::find($request->dhl_id);
63+
//通知
64+
$config = config('wechat.mini_program.default');
65+
$delivery_release = config('wechat.subscription_information.delivery_release');
66+
$app = Factory::miniProgram($config); // 小程序
67+
$data = [
68+
'template_id' => $delivery_release,
69+
'touser' => $GoodIndent->User->wechat_applet_openid,
70+
'page' => 'pages/order/showOrder?id='.$GoodIndent->id,
71+
'data' => [
72+
'character_string1' => [
73+
'value' => $GoodIndent->identification,
74+
],
75+
'thing3' => [
76+
'value' => $Dhl->name,
77+
],
78+
'character_string4' => [
79+
'value' => $request->odd,
80+
],
81+
'amount9' => [
82+
'value' => $GoodIndent->total/100,
83+
],
84+
'date6' => [
85+
'value' => Carbon::now()->toDateTimeString(),
86+
]
7287
],
73-
'thing3' => [
74-
'value' => $Dhl->name,
88+
];
89+
// $app->subscribe_message->send($data);
90+
// 通知
91+
$invoice=[
92+
'type'=> InvoicePaid::NOTIFICATION_TYPE_SYSTEM_MESSAGES,
93+
'title'=>'您购买的商品已发货,请到订单详情查看',
94+
'list'=>[
95+
[
96+
'keyword'=>'订单编号',
97+
'data'=>$GoodIndent->identification
98+
],
99+
[
100+
'keyword'=>'物流公司',
101+
'data'=>$Dhl->name
102+
],
103+
[
104+
'keyword'=>'快递单号',
105+
'data'=>$request->odd
106+
],
107+
[
108+
'keyword'=>'发货时间',
109+
'data'=> Carbon::now()->toDateTimeString()
110+
]
75111
],
76-
'character_string4' => [
77-
'value' => $request->odd,
78-
],
79-
'amount9' => [
80-
'value' => $GoodIndent->total/100,
81-
],
82-
'date6' => [
83-
'value' => Carbon::now()->toDateTimeString(),
84-
]
85-
],
86-
];
87-
$app->subscribe_message->send($data);
88-
return resReturn(1,'发货成功');
112+
'remark'=>'感谢您的支持。',
113+
'url'=>'/pages/order/showOrder?id='.$GoodIndent->id,
114+
'prefers'=>['database']
115+
];
116+
$user = User::find($GoodIndent->User->id);
117+
$user->notify(new InvoicePaid($invoice));
118+
return array(1,'发货成功');
119+
});
120+
if($return[0] == 1){
121+
return resReturn(1,$return[1]);
122+
}else{
123+
return resReturn(0,$return[0],$return[1]);
124+
}
89125
}
90126
// 退款
91127
public function refund($id,Request $request){
@@ -120,6 +156,21 @@ public function refund($id,Request $request){
120156
$Money->money = $GoodIndent->refund_money;
121157
$Money->remark = '订单:'.$GoodIndent->identification.'的退款';
122158
$Money->save();
159+
// 通知
160+
$invoice=collect([]);
161+
$invoice->type = InvoicePaid::NOTIFICATION_TYPE_DEAL;
162+
$invoice->title = '对订单:'.$GoodIndent->identification.'的退款';
163+
$invoice->list = [
164+
[
165+
'keyword'=>'支付方式',
166+
'data'=>'余额支付'
167+
]
168+
];
169+
$invoice->price = $GoodIndent->refund_money;
170+
$invoice->url ='pages/finance/bill_show?id='.$Money->id;
171+
$invoice->prefers = ['database'];
172+
$user = User::find(auth('web')->user()->id);
173+
$user->notify(new InvoicePaid($invoice));
123174
}
124175
return array(1,'退款成功');
125176
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\v1\Element;
4+
5+
use App\Code;
6+
use App\common\RedisLock;
7+
use App\Http\Requests\v1\SubmitLiveCodeRequest;
8+
use App\Models\v1\Common;
9+
use App\Models\v1\ApplyUser;
10+
use App\Models\v1\LiveCode;
11+
use App\Models\v1\LiveCodeLog;
12+
use App\Models\v1\LiveThirdCode;
13+
use App\Models\v1\Notification;
14+
use App\Models\v1\User;
15+
use EasyWeChat\Kernel\Http\StreamResponse;
16+
use Illuminate\Http\Request;
17+
use App\Http\Controllers\Controller;
18+
use Illuminate\Support\Facades\Cache;
19+
use Illuminate\Support\Facades\DB;
20+
use Illuminate\Support\Facades\Redis;
21+
use EasyWeChat\Factory;
22+
use Illuminate\Support\Facades\Storage;
23+
use SimpleSoftwareIO\QrCode\Facades\QrCode;
24+
25+
class NoticeController extends Controller
26+
{
27+
/**
28+
* 通知列表
29+
*
30+
* @param Request $request
31+
* @return string
32+
*/
33+
public function index(Request $request)
34+
{
35+
Notification::$withoutAppends = false;
36+
$q = Notification::query();
37+
$limit=$request->limit;
38+
$q->where('notifiable_id',auth('web')->user()->id);
39+
$q->orderBy('created_at', 'DESC');
40+
$paginate=$q->paginate($limit);
41+
// 标记为已读
42+
$user = User::find(auth('web')->user()->id);
43+
$user->unreadNotifications()->update(['read_at' => now()]);
44+
return resReturn(1,$paginate);
45+
}
46+
47+
public function count(){
48+
$count = Notification::where('read_at',null)->where('notifiable_id',auth('web')->user()->id)->count();
49+
return resReturn(1,$count);
50+
}
51+
52+
/**
53+
* 删除通知
54+
*
55+
* @param $id
56+
* @param Request $request
57+
*/
58+
public function destroy($id, Request $request)
59+
{
60+
$user = User::find(auth('web')->user()->id);
61+
$user->notifications()->where('id',$id)->delete();
62+
}
63+
64+
}

api/app/Http/Controllers/v1/Element/WeChatController.php

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Models\v1\SmsLog;
1616
use App\Models\v1\User;
1717
use App\Models\v1\UserLog;
18+
use App\Notifications\InvoicePaid;
1819
use Carbon\Carbon;
1920
use EasyWeChat\Factory;
2021
use App\Http\Controllers\Controller;
@@ -293,17 +294,41 @@ public function balancePay(Request $request){
293294
}
294295
}
295296
}
296-
User::where('id',auth('web')->user()->id)->decrement('money');
297-
$GoodIndent->state = GoodIndent::GOOD_INDENT_STATE_DELIVER;
298-
$GoodIndent->pay_time= Carbon::now()->toDateTimeString();
299-
$GoodIndent->save();
300-
$Money=new MoneyLog();
301-
$Money->user_id = auth('web')->user()->id;
302-
$Money->type = MoneyLog::MONEY_LOG_TYPE_EXPEND;
303-
$Money->money = $GoodIndent->total;
304-
$Money->remark = '对订单:'.$GoodIndent->identification.'的付款';
305-
$Money->save();
306-
return resReturn(1,'支付成功');
297+
$return=DB::transaction(function ()use($request,$GoodIndent){
298+
User::where('id',auth('web')->user()->id)->decrement('money', $GoodIndent->total);
299+
$GoodIndent->state = GoodIndent::GOOD_INDENT_STATE_DELIVER;
300+
$GoodIndent->pay_time= Carbon::now()->toDateTimeString();
301+
$GoodIndent->save();
302+
$Money=new MoneyLog();
303+
$Money->user_id = auth('web')->user()->id;
304+
$Money->type = MoneyLog::MONEY_LOG_TYPE_EXPEND;
305+
$Money->money = $GoodIndent->total;
306+
$Money->remark = '对订单:'.$GoodIndent->identification.'的付款';
307+
$Money->save();
308+
// 通知
309+
$invoice=[
310+
'type'=> InvoicePaid::NOTIFICATION_TYPE_DEAL,
311+
'title'=>'对订单:'.$GoodIndent->identification.'的付款',
312+
'list'=>[
313+
[
314+
'keyword'=>'支付方式',
315+
'data'=>'余额支付'
316+
]
317+
],
318+
'price'=>$GoodIndent->total,
319+
'url'=>'/pages/finance/bill_show?id='.$Money->id,
320+
'prefers'=>['database']
321+
];
322+
$user = User::find(auth('web')->user()->id);
323+
$user->notify(new InvoicePaid($invoice));
324+
return array(1,'支付成功');
325+
});
326+
if($return[0] == 1){
327+
return resReturn(1,$return[1]);
328+
}else{
329+
return resReturn(0,$return[0],$return[1]);
330+
}
331+
307332
}
308333

309334
/**
@@ -327,8 +352,8 @@ public function payment(Request $request){
327352
$result = $app->order->unify([
328353
'body' => $body,
329354
'out_trade_no' => $number= orderNumber(),
330-
// 'total_fee' => $GoodIndent->total,
331-
'total_fee' => 1,
355+
'total_fee' => $GoodIndent->total,
356+
// 'total_fee' => 1,
332357
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
333358
'openid' => $openid,
334359
]);
@@ -380,11 +405,10 @@ public function payment(Request $request){
380405
public function paymentNotify(Request $request){
381406
$config = config('wechat.payment.default');
382407
$config['notify_url'] = request()->root().'/api/v1/app/paymentNotify'; // 你也可以在下单时单独设置来想覆盖它
383-
Log::info(1111);
384408
$app = Factory::payment($config);
385409
$response = $app->handlePaidNotify(function ($message, $fail)
386410
{
387-
Log::info('小程序:'.json_encode($message));
411+
// Log::info('小程序:'.json_encode($message));
388412
// 根据返回的订单号查询订单数据
389413
$order = PaymentLog::where('number',$message['out_trade_no'])->first();
390414
if (!$order || $order->state == PaymentLog::PAYMENT_LOG_STATE_COMPLETE) {
@@ -410,6 +434,22 @@ public function paymentNotify(Request $request){
410434
$Money->money = $order->money;
411435
$Money->remark = '对订单:'.$GoodIndent->identification.'的付款';
412436
$Money->save();
437+
// 通知
438+
$invoice=[
439+
'type'=> InvoicePaid::NOTIFICATION_TYPE_DEAL,
440+
'title'=>'对订单:'.$GoodIndent->identification.'的付款',
441+
'list'=>[
442+
[
443+
'keyword'=>'支付方式',
444+
'data'=>'微信支付'
445+
]
446+
],
447+
'price'=>$order->money,
448+
'url'=>'/pages/finance/bill_show?id='.$Money->id,
449+
'prefers'=>['database']
450+
];
451+
$user = User::find($GoodIndent->user_id);
452+
$user->notify(new InvoicePaid($invoice));
413453
// 用户支付失败
414454
} elseif ($message['result_code'] === 'FAIL') {
415455
$order->status = 'paid_fail';

api/app/Models/v1/Notification.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Models\v1;
4+
5+
use DateTimeInterface;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
/**
9+
* @property mixed nickname
10+
* @property int admin_id
11+
* @property string path
12+
* @property string method
13+
* @property string ip
14+
* @property string input
15+
*/
16+
class Notification extends Model
17+
{
18+
public static $withoutAppends = true;
19+
/**
20+
* Prepare a date for array / JSON serialization.
21+
*
22+
* @param \DateTimeInterface $date
23+
* @return string
24+
*/
25+
protected function serializeDate(DateTimeInterface $date)
26+
{
27+
return $date->format('Y-m-d H:i:s');
28+
}
29+
30+
public function getDataAttribute()
31+
{
32+
if(isset($this->attributes['data'])){
33+
$return = $this->attributes['data'];
34+
if(self::$withoutAppends){
35+
}else{
36+
$return= json_decode($this->attributes['data']);
37+
38+
}
39+
return $return;
40+
}
41+
}
42+
}

api/app/Models/v1/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\v1\ApplyUsers;
66
use DateTimeInterface;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
8+
use Illuminate\Notifications\Notifiable;
89

910
/**
1011
* @property string password
@@ -21,6 +22,7 @@
2122
*/
2223
class User extends Authenticatable
2324
{
25+
use Notifiable;
2426
const USER_GENDER_UNKNOWN= 0; //性别:未知
2527
const USER_GENDER_MAN= 1; //性别:男
2628
const USER_GENDER_WOMAN= 2; //性别:女

0 commit comments

Comments
 (0)