ScoreOrder.php 2.5 KB
<?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;
    }
}