CanOrder.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* trait :对象可以被访问并生成访问日志trait
+-----------------------------------------------------------------------------------------------------------------------
*
* @copyright Copyright
* @author Richer <yangzi1028@163.com>
* @package App\Models\Traits
* @version 2019年11月12日,17:23:28
* @link
*/
namespace App\Models\Traits;
use App\Factories\OrderFactory;
use App\Models\Order\Order;
use Illuminate\Database\Eloquent\Model;
/**
* Trait CanOrder.
*/
trait CanOrder
{
/**
* User order given targets.
*
* @param Model $target 订单下单对象
* @return mixed
*/
public function placeOrder($target)
{
$factory = app(OrderFactory::class);
return $factory->generateOrder($target, $this);
}
/**
* Cancel a order for given targets.
*
* @param Order $order 订单对象
*
* @return bool
*/
public function cancelOrder($order)
{
if (!$order) {
return true;
}
// 实例化订单工厂类
$factory = app(OrderFactory::class);
if (!$result = $factory->cancel($order, $this)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* Pay order
*
* @param Order $order 订单对象
* @param string $type 支付方式
* @param string $notify_url 回调地址
* @return bool
*/
public function payOrder($order, $type, $notify_url = '')
{
// 实例化订单工厂类
$factory = app(OrderFactory::class);
if (!$result = $factory->pay($this, $order, $type, $notify_url)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* @param $recharge
* @param $user
* @param $type
* @return bool
*/
public function pay($recharge, $user, $type)
{
$factory = app(OrderFactory::class);
if (!$result = $factory->pay($recharge, $user, $type)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* Refund order
*
* @param Order $order 订单对象
*
* @return bool
*/
public function refundOrder($order)
{
// 实例化订单工厂类
$factory = app(OrderFactory::class);
if (!$result = $factory->refund($order, $this)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* Receive order
*
* @param Order $order 订单对象
*
* @return bool
*/
public function receiveOrder($order)
{
// 实例化订单工厂类
$factory = app(OrderFactory::class);
if (!$result = $factory->receive($order, $this)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* Evaluate order
*
* @param Order $order 订单对象
*
* @return bool
*/
public function evaluateOrder($order)
{
// 实例化订单工厂类
$factory = app(OrderFactory::class);
if (!$result = $factory->evaluate($order, $this)) {
$this->message = $factory->getMessage();
return false;
}
return $result;
}
/**
* Return orders.
*
* @param string $orderable_type 对象类型
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function orders($orderable_type = null)
{
// 不使用多态关联,因为订单的数据需要保留历史
//return $this->morphedByMany($orderable_type , 'orderable','orders');
return $this->hasMany(Order::class, 'user_id', 'id');
}
/**
* 获取错误提示
*
* @return string
*/
public function getOrderMsg()
{
return $this->message;
}
}