123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php namespace Puser\Lib;
- use Dever;
- class Address
- {
- private $col = 'id,name,phone,area,address,type,province_id,city_id,county_id,town_id';
- # 获取默认地址
- public function init($uid)
- {
- $this->uid = $uid;
- return $this;
- }
- public function getDefault($type = 1)
- {
- $where['uid'] = $this->uid;
- $where['type'] = $type;
- $where['status'] = 1;
- $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
- if (!$info) {
- unset($where['type']);
- $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
- }
- if ($info && Dever::project('area')) {
- $info = $this->handleInfo($info);
- }
- return $info;
- }
- # 设置默认地址
- public function setDefault($id)
- {
- $where['uid'] = $this->uid;
- $where['id'] = $id;
- $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
- if ($info) {
- Dever::db('puser/address')->update(['type' => 1], ['type' => 2]);
- return Dever::db('puser/address')->update($info['id'], ['type' => 1]);
- }
- return false;
- }
- # 获取某个收货地址
- public function getInfo($id)
- {
- $where['id'] = $id;
- $where['uid'] = $this->uid;
- $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
- if ($info && Dever::project('area')) {
- $info = $this->handleInfo($info);
- }
- return $info;
- }
- # 获取地址列表
- public function getList()
- {
- $where['uid'] = $this->uid;
- $where['type'] = ['<', 3];
- $where['status'] = 1;
- $set = ['col' => $this->col, 'num' => 10];
- if ($id = Dever::input('id')) {
- $set['order'] = 'field(id, '.$id.') desc';
- }
- $data = Dever::db('puser/address')->select($where, $set);
- if ($data && Dever::project('area')) {
- foreach ($data as $k => $v) {
- $data[$k] = $this->handleInfo($v);
- }
- }
- return $data;
- }
- public function handleInfo($data)
- {
- if (is_array($data['area'])) {
- $data['area'] = implode(',', $data['area']);
- }
- $data['area_string'] = Dever::load(\Area\Lib\Data::class)->string($data['area'], '-');
- $data['full'] = $data['name'] . ',' . $data['phone'] . ',' . $data['area_string'] . ',' . $data['address'];
- return $data;
- }
- # 添加或者更新地址
- public function update($id, $type = 1, $phone, $name, $province = '', $city = '', $county = '', $town = '', $address = '')
- {
- $update['uid'] = $this->uid;
- if ($name) {
- $update['name'] = $name;
- }
- if ($phone) {
- $update['phone'] = $phone;
- } else {
- Dever::alert('请输入联系方式');
- }
- if ($province) {
- $update['province_id'] = $province;
- } else {
- Dever::alert('请选择省份');
- }
- if ($city) {
- $update['city_id'] = $city;
- } else {
- Dever::alert('请选择城市');
- }
- if ($county) {
- $update['county_id'] = $county;
- } else {
- Dever::alert('请选择区县');
- }
- if ($town) {
- $update['town_id'] = $town;
- } else {
- Dever::alert('请选择街镇');
- }
- if ($province && $city && $county && $town) {
- $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
- }
- $update['type'] = $type;
- if ($address) {
- $update['address'] = $address;
- }
- if ($type == 1) {
- Dever::db('puser/address')->update(['type' => 1], ['type' => 2]);
- }
- if ($id && $id > 0) {
- Dever::db('puser/address')->update(['id' => $id, 'uid' => $this->uid], $update);
- } else {
- $id = Dever::db('puser/address')->insert($update);
- }
- return $id;
- }
- # 删除
- public function delete($id)
- {
- return Dever::db('puser/address')->update(['id' => $id, 'uid' => $this->uid], ['status' => 2]);
- }
- }
|