RechargeOrder.php 2.0 KB
<?php
namespace app\mobile\model;

use think\Model;
use think\Db;

class RechargeOrder extends Model
{
    // 表名
    protected $name = 'mobile_recharge_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 错误提示
    public $error = '';
    // 状态码
    public $code = 0;

    /**
     * 下单预览
     */
    public function payView($data)
    {
        if(empty($data['recharge_id'])){
            $this->setError('缺少必要参数');
            return false;
        }
        $info = Recharge::get($data['recharge_id']);
        if(empty($info)){
            $this->setError('充值产品信息不存在');
            return false;
        }
        return [
            'recharge_id' => $data['recharge_id'],
            'pay_price' => $info['price'],
            'money' => $info['price']
        ];
    }

    /**
     * 新增订单
     */
    public function add($user, $order, $pay_type)
    {
        $this->save([
            'user_id' => $user['id'],
            'recharge_id' => $order['recharge_id'],
            'order_sn' => get_order_sn(),
            'pay_price' => $order['pay_price'],
            'pay_type' => $pay_type,
            'money' => $order['money']
        ]);
        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;
    }
}