UserSize.php 4.5 KB
<?php

namespace app\common\model;

use think\Model;

class UserSize extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'body_images_list',
        'size_text'
    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'createtime',
        'updatetime'
    ];

    /**
     * 尺寸图片
     */
    public function getBodyImagesListAttr($value, $data)
    {
        $list = !empty($data['body_images']) ? explode(',',$data['body_images']) : [];
        foreach($list as &$v){
            $v = cdnurl($v,true);
        }
        return $list;
    }

	/**
     * 尺寸列表
     */
    public static function sizeList($user_id)
    {
        return self::all(compact('user_id'));
    }

    /**
     * 新增尺寸
     */
    public function add($user, $data)
    {
        empty($data['isdefault']) && $data['isdefault'] = '0';
        // 该尺寸设为默认,其他地址就设为非默认
        if($data['isdefault'] == '1'){
            $this->where('user_id',$user['id'])->update(['isdefault'=>'0']);
        }
        // 对象转json字符串
        if(!empty($data['body_info']) && is_array($data['body_info'])){
            $data['body_info'] = json_encode($data['body_info']);
        }
        $this->allowField(true)->save(array_merge([
            'user_id' => $user['id'],
        ], $data));
        // 没有默认收货地址,就把该收货地址设为默认
        !$this->get(['user_id'=>$user['id'],'isdefault'=>'1']) && $this->update(['id'=>$this['id'],'isdefault'=>'1']);
        return true;
    }

    /**
     * 编辑尺寸
     */
    public function edit($data)
    {
        empty($data['isdefault']) && $data['isdefault'] = '0';
        // 该地址设为默认,其他地址就设为非默认
        if($data['isdefault'] == '1'){
            $this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
        }
        // 对象转json字符串
        if(!empty($data['body_info']) && is_array($data['body_info'])){
            $data['body_info'] = json_encode($data['body_info']);
        }
        return $this->allowField(true)->isUpdate(true)->save($data);
    }

    /**
     * 删除尺寸
     */
    public function remove($user)
    {
        // 查询当前是否为默认地址
        if($this['isdefault'] == '1'){
            $this->delete();
            $size = $this->get(['user_id'=>$user['id']]);
            $size->isdefault = '1';
            $size->save();
        }else{
            $this->delete();
        }
        return true;
    }

    /**
     * 设置为默认
     */
    public function setDefault()
    {
        $this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
        $this->update(['id'=>$this['id'],'isdefault'=>'1']);
        return true;
    }

    /**
     * 尺寸详情
     */
    public static function detail($user_id, $id)
    {
        return self::get(compact('user_id', 'id'));
    }

    /**
     * 尺寸详情
     */
    public function getSizeTextAttr($value,$data){
        $size_text = [];
        if(!empty($data['body_info'])){
            $size = json_decode(str_replace('\'','"',$data['body_info']),true);
            foreach($size as $k => $v){
                $style = Style::where('id',$k)->field('style_name,style_type')->find()->toArray();
                $style['style_value'] = '';
                switch ($style['style_type']) {
                    case '1':
                        $style['style_value'] = StyleValue::where('id',$v)->value('style_value_name');
                        break;
                    case '2':
                        $style['style_value'] = $v;
                        break;
                    case '3':
                        $style['style_value'] = cdnurl($v,true);
                        break;
                }
                $size_text[] = $style;
            }
        }
        $size_text = array_merge([
            ['style_name'=>'身高','style_type'=>'1','style_value'=>$data['height'].'CM'],
            ['style_name'=>'体重','style_type'=>'1','style_value'=>$data['weight'].'KG'],
            ['style_name'=>'腰围','style_type'=>'1','style_value'=>!empty($data['waistline']) ? $data['waistline'] : '未填写'],
        ],$size_text);
        return $size_text;
    }
}