审查视图

application/common/model/UserAddress.php 3.4 KB
何书鹏 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php

namespace app\common\model;

use think\Model;

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

	/**
     * 追加字段
     * @var array
     */
1  
何书鹏 authored
22 23 24 25 26 27
    protected $append = [
        'area',
        'user_address_id'
    ];

    /**
28
     * 新增user_address_id(目的:方便前端 待支付订单修改地址)
1  
何书鹏 authored
29 30 31 32 33
     */
    public function getUserAddressIdAttr($value, $data)
    {
        return $data['id'];
    }
何书鹏 authored
34 35 36 37 38 39

    /**
     * 地区名称
     */
    public function getAreaAttr($value, $data)
    {
何书鹏 authored
40
        return Area::getNameById($data['province_id']).','.Area::getNameById($data['city_id']).','.Area::getNameById($data['district_id']);
何书鹏 authored
41 42 43 44 45
    }

	/**
     * 地址列表
     */
何书鹏 authored
46
    public static function addressList($user_id)
何书鹏 authored
47 48 49 50 51 52 53
    {
        return self::all(compact('user_id'));
    }

    /**
     * 新增收货地址
     */
何书鹏 authored
54
    public function add($user, $data)
何书鹏 authored
55
    {
何书鹏 authored
56
        empty($data['isdefault']) && $data['isdefault'] = '0';
何书鹏 authored
57 58 59 60 61 62 63
        // 添加收货地址
        $area = explode(',', $data['area']);
        $province_id = Area::getIdByName($area[0], 1);
        $city_id = Area::getIdByName($area[1], 2, $province_id);
        $district_id = Area::getIdByName($area[2], 3, $city_id);
        // 该地址设为默认,其他地址就设为非默认
        if($data['isdefault'] == '1'){
何书鹏 authored
64
            $this->where('user_id',$user['id'])->update(['isdefault'=>'0']);
何书鹏 authored
65
        }
何书鹏 authored
66
        $this->allowField(true)->save(array_merge([
何书鹏 authored
67 68 69 70 71 72
            'user_id' => $user['id'],
            'province_id' => $province_id,
            'city_id' => $city_id,
            'district_id' => $district_id
        ], $data));
        // 没有默认收货地址,就把该收货地址设为默认
何书鹏 authored
73
        !$this->get(['user_id'=>$user['id'],'isdefault'=>'1']) && self::update(['id'=>$this['id'],'isdefault'=>'1']);
何书鹏 authored
74 75 76 77 78 79 80 81
        return true;
    }

    /**
     * 编辑收货地址
     */
    public function edit($data)
    {
何书鹏 authored
82
        empty($data['isdefault']) && $data['isdefault'] = '0';
何书鹏 authored
83 84 85 86 87 88
        // 添加收货地址
        $area = explode(',', $data['area']);
        $province_id = Area::getIdByName($area[0], 1);
        $city_id = Area::getIdByName($area[1], 2, $province_id);
        $district_id = Area::getIdByName($area[2], 3, $city_id);
        // 该地址设为默认,其他地址就设为非默认
何书鹏 authored
89 90 91
        if($data['isdefault'] == '1'){
            self::where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
        }
何书鹏 authored
92 93
        return $this->get($this['id'])->allowField(true)
            ->save(array_merge(compact('province_id', 'city_id', 'district_id'), $data));
何书鹏 authored
94 95 96 97 98 99 100 101 102 103
    }

    /**
     * 删除收货地址
     */
    public function remove($user)
    {
        // 查询当前是否为默认地址
        if($this['isdefault'] == '1'){
            $this->delete();
何书鹏 authored
104 105 106
            $address = $this->get(['user_id'=>$user['id']]);
            $address->isdefault = "1";
            $address->save();
何书鹏 authored
107 108 109 110 111 112 113
        }else{
            $this->delete();
        }
        return true;
    }

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

    /**
何书鹏 authored
124 125 126 127 128 129 130
     * 收货地址详情
     */
    public static function detail($user_id, $id)
    {
        return self::get(compact('user_id', 'id'));
    }
}