ScoreOrder.php
2.5 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
<?php
namespace app\mobile\model;
use think\Model;
use think\Db;
class ScoreOrder extends Model
{
// 表名
protected $name = 'mobile_score_order';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 错误提示
public $error = '';
// 状态码
public $code = 0;
/**
* 下单预览
*/
public function payView($user,$data)
{
if(empty($data['score_spec_id']) && empty($data['score'])){
$this->setError('缺少必要参数');
return false;
}
// 充值积分每分需多少元
$score_recharge_price = Db::name('mobile_config')->where('id',1)->value('score_recharge_price');
// 套餐ID
if(!empty($data['score_spec_id'])){
$info = ScoreSpec::get($data['score_spec_id']);
if(empty($info)){
$this->setError('套餐信息不存在');
return false;
}
$pay_price = bcmul($score_recharge_price,$info['spec_score'],2);
$score = $info['score'];
}else{
$pay_price = bcmul($score_recharge_price,$data['score'],2);
$score = $data['score'];
}
return [
'score_spec_id' => !empty($data['score_spec_id']) ? $data['score_spec_id'] : 0,
'pay_price' => $pay_price,
'score' => $score
];
}
/**
* 新增订单
*/
public function add($user, $order, $pay_type)
{
$this->save([
'user_id' => $user['id'],
'score_spec_id' => $order['score_spec_id'],
'order_sn' => get_order_sn(),
'pay_price' => $order['pay_price'],
'pay_type' => $pay_type,
'score' => $order['score']
]);
return true;
}
/**
* 设置错误信息
* @param $error
*/
private function setError($error,$code=0)
{
empty($this->error) && $this->error = $error;
$this->code = $code;
}
/**
* 是否存在错误
* @return bool
*/
private function hasError()
{
return !empty($this->error);
}
/**
* 获取错误信息
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* 获取状态码
* @return string
*/
public function getCode()
{
return $this->code;
}
}