审查视图

application/common/model/UserSize.php 4.4 KB
何书鹏 authored
1 2 3 4 5 6 7 8
<?php

namespace app\common\model;

use think\Model;

class UserSize extends Model
{
何书鹏 authored
9 10 11 12 13 14 15 16
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;
何书鹏 authored
17 18
    // 追加属性
    protected $append = [
何书鹏 authored
19
        'body_images_list',
何书鹏 authored
20 21 22
        'size_text'
    ];
何书鹏 authored
23 24 25 26 27 28 29 30 31
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'createtime',
        'updatetime'
    ];
何书鹏 authored
32 33 34 35 36 37 38 39 40 41 42 43
    /**
     * 尺寸图片
     */
    public function getBodyImagesListAttr($value, $data)
    {
        $list = !empty($data['body_images']) ? explode(',',$data['body_images']) : [];
        foreach($list as &$v){
            $v = cdnurl($v,true);
        }
        return $list;
    }
何书鹏 authored
44 45 46
	/**
     * 尺寸列表
     */
何书鹏 authored
47
    public static function sizeList($user_id)
何书鹏 authored
48 49 50 51 52 53 54
    {
        return self::all(compact('user_id'));
    }

    /**
     * 新增尺寸
     */
何书鹏 authored
55
    public function add($user, $data)
何书鹏 authored
56
    {
何书鹏 authored
57
        empty($data['isdefault']) && $data['isdefault'] = '0';
何书鹏 authored
58 59
        // 该尺寸设为默认,其他地址就设为非默认
        if($data['isdefault'] == '1'){
何书鹏 authored
60
            $this->where('user_id',$user['id'])->update(['isdefault'=>'0']);
何书鹏 authored
61
        }
1  
何书鹏 authored
62
        // 对象转json字符串
1  
何书鹏 authored
63 64 65
        if(!empty($data['body_info']) && is_array($data['body_info'])){
            $data['body_info'] = json_encode($data['body_info']);
        }
何书鹏 authored
66
        $this->allowField(true)->save(array_merge([
何书鹏 authored
67
            'user_id' => $user['id'],
何书鹏 authored
68
        ], $data));
何书鹏 authored
69
        // 没有默认收货地址,就把该收货地址设为默认
何书鹏 authored
70
        !$this->get(['user_id'=>$user['id'],'isdefault'=>'1']) && $this->update(['id'=>$this['id'],'isdefault'=>'1']);
何书鹏 authored
71 72 73 74 75 76
        return true;
    }

    /**
     * 编辑尺寸
     */
何书鹏 authored
77
    public function edit($data)
何书鹏 authored
78
    {
何书鹏 authored
79
        empty($data['isdefault']) && $data['isdefault'] = '0';
何书鹏 authored
80
        // 该地址设为默认,其他地址就设为非默认
1  
何书鹏 authored
81
        if($data['isdefault'] == '1'){
何书鹏 authored
82
            $this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
何书鹏 authored
83
        }
1  
何书鹏 authored
84
        // 对象转json字符串
1  
何书鹏 authored
85 86 87
        if(!empty($data['body_info']) && is_array($data['body_info'])){
            $data['body_info'] = json_encode($data['body_info']);
        }
何书鹏 authored
88
        return $this->save($data);
何书鹏 authored
89 90 91 92 93
    }

    /**
     * 删除尺寸
     */
何书鹏 authored
94
    public function remove($user)
何书鹏 authored
95 96 97 98
    {
        // 查询当前是否为默认地址
        if($this['isdefault'] == '1'){
            $this->delete();
何书鹏 authored
99 100 101
            $size = $this->get(['user_id'=>$user['id']]);
            $size->isdefault = '1';
            $size->save();
何书鹏 authored
102 103 104 105 106 107 108
        }else{
            $this->delete();
        }
        return true;
    }

    /**
何书鹏 authored
109 110 111 112 113 114 115 116 117 118
     * 设置为默认
     */
    public function setDefault()
    {
        $this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
        $this->update(['id'=>$this['id'],'isdefault'=>'1']);
        return true;
    }

    /**
何书鹏 authored
119 120 121 122 123 124
     * 尺寸详情
     */
    public static function detail($user_id, $id)
    {
        return self::get(compact('user_id', 'id'));
    }
何书鹏 authored
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

    /**
     * 尺寸详情
     */
    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([
1  
何书鹏 authored
151 152
            ['style_name'=>'身高','style_type'=>'1','style_value'=>$data['height'].'CM'],
            ['style_name'=>'体重','style_type'=>'1','style_value'=>$data['weight'].'KG'],
何书鹏 authored
153
            ['style_name'=>'腰围','style_type'=>'1','style_value'=>!empty($data['waistline']) ? $data['waistline'] : '未填写'],
何书鹏 authored
154 155 156
        ],$size_text);
        return $size_text;
    }
何书鹏 authored
157
}