Address.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php namespace Puser\Lib;
  2. use Dever;
  3. class Address
  4. {
  5. private $col = 'id,name,phone,area,address,type,province_id,city_id,county_id,town_id';
  6. # 获取默认地址
  7. public function init($uid)
  8. {
  9. $this->uid = $uid;
  10. return $this;
  11. }
  12. public function getDefault($type = 1)
  13. {
  14. $where['uid'] = $this->uid;
  15. $where['type'] = $type;
  16. $where['status'] = 1;
  17. $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
  18. if (!$info) {
  19. unset($where['type']);
  20. $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
  21. }
  22. if ($info && Dever::project('area')) {
  23. $info = $this->handleInfo($info);
  24. }
  25. return $info;
  26. }
  27. # 设置默认地址
  28. public function setDefault($id)
  29. {
  30. $where['uid'] = $this->uid;
  31. $where['id'] = $id;
  32. $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
  33. if ($info) {
  34. Dever::db('puser/address')->update(['type' => 1], ['type' => 2]);
  35. return Dever::db('puser/address')->update($info['id'], ['type' => 1]);
  36. }
  37. return false;
  38. }
  39. # 获取某个收货地址
  40. public function getInfo($id)
  41. {
  42. $where['id'] = $id;
  43. $where['uid'] = $this->uid;
  44. $info = Dever::db('puser/address')->find($where, ['col' => $this->col]);
  45. if ($info && Dever::project('area')) {
  46. $info = $this->handleInfo($info);
  47. }
  48. return $info;
  49. }
  50. # 获取地址列表
  51. public function getList()
  52. {
  53. $where['uid'] = $this->uid;
  54. $where['type'] = ['<', 3];
  55. $where['status'] = 1;
  56. $set = ['col' => $this->col, 'num' => 10];
  57. if ($id = Dever::input('id')) {
  58. $set['order'] = 'field(id, '.$id.') desc';
  59. }
  60. $data = Dever::db('puser/address')->select($where, $set);
  61. if ($data && Dever::project('area')) {
  62. foreach ($data as $k => $v) {
  63. $data[$k] = $this->handleInfo($v);
  64. }
  65. }
  66. return $data;
  67. }
  68. public function handleInfo($data)
  69. {
  70. if (is_array($data['area'])) {
  71. $data['area'] = implode(',', $data['area']);
  72. }
  73. $data['area_string'] = Dever::load(\Area\Lib\Data::class)->string($data['area'], '-');
  74. $data['full'] = $data['name'] . ',' . $data['phone'] . ',' . $data['area_string'] . ',' . $data['address'];
  75. return $data;
  76. }
  77. # 添加或者更新地址
  78. public function update($id, $type = 1, $phone, $name, $province = '', $city = '', $county = '', $town = '', $address = '')
  79. {
  80. $update['uid'] = $this->uid;
  81. if ($name) {
  82. $update['name'] = $name;
  83. }
  84. if ($phone) {
  85. $update['phone'] = $phone;
  86. } else {
  87. Dever::alert('请输入联系方式');
  88. }
  89. if ($province) {
  90. $update['province_id'] = $province;
  91. } else {
  92. Dever::alert('请选择省份');
  93. }
  94. if ($city) {
  95. $update['city_id'] = $city;
  96. } else {
  97. Dever::alert('请选择城市');
  98. }
  99. if ($county) {
  100. $update['county_id'] = $county;
  101. } else {
  102. Dever::alert('请选择区县');
  103. }
  104. if ($town) {
  105. $update['town_id'] = $town;
  106. } else {
  107. Dever::alert('请选择街镇');
  108. }
  109. if ($province && $city && $county && $town) {
  110. $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
  111. }
  112. $update['type'] = $type;
  113. if ($address) {
  114. $update['address'] = $address;
  115. }
  116. if ($type == 1) {
  117. Dever::db('puser/address')->update(['type' => 1], ['type' => 2]);
  118. }
  119. if ($id && $id > 0) {
  120. Dever::db('puser/address')->update(['id' => $id, 'uid' => $this->uid], $update);
  121. } else {
  122. $id = Dever::db('puser/address')->insert($update);
  123. }
  124. return $id;
  125. }
  126. # 删除
  127. public function delete($id)
  128. {
  129. return Dever::db('puser/address')->update(['id' => $id, 'uid' => $this->uid], ['status' => 2]);
  130. }
  131. }