Address.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Address extends BaseModel {
  15. /**
  16. * 取得买家默认收货地址
  17. * @author csdeshang
  18. * @param array $condition 获取条件
  19. * @param string $order 排序
  20. * @return array
  21. */
  22. public function getDefaultAddressInfo($condition = array(), $order = 'address_is_default desc,chain_id asc,address_id desc') {
  23. return $this->getAddressInfo($condition, $order);
  24. }
  25. /**
  26. * 取得单条地址信息
  27. * @author csdeshang
  28. * @param array $condition 条件
  29. * @param type $order 排序
  30. * @return string
  31. */
  32. public function getAddressInfo($condition, $order = '') {
  33. $addr_info = Db::name('address')->where($condition)->order($order)->find();
  34. if ($addr_info['chain_id']) {
  35. if(config('ds_config.chain_isuse')){
  36. $chain_model = model('chain');
  37. $chain_info = $chain_model->getChainOpenInfo(array(array('chain_id' ,'=', $addr_info['chain_id'])));
  38. if (!empty($chain_info)) {
  39. if($addr_info['city_id'] !== $chain_info['chain_area_2'] || $addr_info['area_id'] !== $chain_info['chain_area_3']){
  40. $addr_info['cityerror'] = true;
  41. }else{
  42. $addr_info['cityerror'] = false;
  43. }
  44. $addr_info['chain_if_pickup'] = $chain_info['chain_if_pickup'];
  45. $addr_info['chain_if_collect'] = $chain_info['chain_if_collect'];
  46. $addr_info['chain_mobile'] = $chain_info['chain_mobile'];
  47. $addr_info['chain_telephony'] = $chain_info['chain_telephony'];
  48. $addr_info['chain_addressname'] = $chain_info['chain_addressname'];
  49. $addr_info['chain_area_info'] = $chain_info['chain_area_info'];
  50. $addr_info['chain_address'] = $chain_info['chain_address'];
  51. $addr_info['chain_mobile'] = $chain_info['chain_mobile'];
  52. $addr_info['area_id'] = $chain_info['chain_area_3'];
  53. $addr_info['area_info'] = $chain_info['chain_area_info'];
  54. $addr_info['address_detail'] = '(' . $chain_info['chain_addressname'] . ') ' . $chain_info['chain_address']
  55. . ',电话:' . trim($chain_info['chain_mobile'] . ',' . $chain_info['chain_telephony'], ',');
  56. }else{
  57. $addr_info=false;
  58. }
  59. }else{
  60. $addr_info=false;
  61. }
  62. }
  63. if(!empty($addr_info && $addr_info['chain_id'] == 0)){
  64. $addr_info['cityerror'] = false;
  65. }
  66. return $addr_info;
  67. }
  68. /**
  69. * 读取地址列表
  70. * @author csdeshang
  71. * @param array $condition 查询条件
  72. * @param type $order 排序
  73. * @return array 数组格式的返回结果
  74. */
  75. public function getAddressList($condition, $order = 'address_id desc') {
  76. $address_list = Db::name('address')->where($condition)->order($order)->select()->toArray();
  77. if (empty($address_list))
  78. return array();
  79. if (config('ds_config.chain_isuse')) {
  80. $chain_ids = array();
  81. $chain_new_list = array();
  82. foreach ($address_list as $k => $v) {
  83. if ($v['chain_id']) {
  84. $chain_ids[] = $v['chain_id'];
  85. }
  86. }
  87. if (!empty($chain_ids)) {
  88. $chain_model = model('chain');
  89. $condition = array();
  90. $condition[] = array('chain_id','in', $chain_ids);
  91. $chain_list = $chain_model->getChainOpenList($condition);
  92. foreach ($chain_list as $k => $v) {
  93. $chain_new_list[$v['chain_id']] = $v;
  94. }
  95. }
  96. if (!empty($chain_new_list)) {
  97. foreach ($address_list as $k => $v) {
  98. if (!$v['chain_id'])
  99. continue;
  100. if($v['chain_id'] && !isset($chain_new_list[$v['chain_id']])){
  101. unset($address_list[$k]);
  102. continue;
  103. }
  104. $chain_info = $chain_new_list[$v['chain_id']];
  105. $address_list[$k]['area_info'] = $chain_info['chain_area_info'];
  106. $address_list[$k]['address_detail'] = '(' . $chain_info['chain_addressname'] . ') ' . $chain_info['chain_address']
  107. . ',电话:' . trim($chain_info['chain_mobile'] . ',' . $chain_info['chain_telephony'], ',');
  108. }
  109. }
  110. }
  111. $address_list=array_values($address_list);
  112. return $address_list;
  113. }
  114. /**
  115. * 取数量
  116. * @author csdeshang
  117. * @param array $condition 条件
  118. * @return int
  119. */
  120. public function getAddressCount($condition = array()) {
  121. return Db::name('address')->where($condition)->count();
  122. }
  123. /**
  124. * 新增地址
  125. * @author csdeshang
  126. * @param array $data 参数内容
  127. * @return bool 布尔类型的返回结果
  128. */
  129. public function addAddress($data) {
  130. //当设置为默认地址,则此用户其他的地址设置为非默认地址
  131. if($data['address_is_default']==1){
  132. Db::name('address')->where('member_id',$data['member_id'])->update(array('address_is_default'=>0));
  133. }
  134. return Db::name('address')->insertGetId($data);
  135. }
  136. /**
  137. * 取单个地址
  138. * @author csdeshang
  139. * @param int $id 地址ID
  140. * @return array 数组类型的返回结果
  141. */
  142. public function getOneAddress($id) {
  143. if (intval($id) > 0) {
  144. $result = Db::name('address')->where('address_id',intval($id))->find();
  145. return $result;
  146. } else {
  147. return false;
  148. }
  149. }
  150. /**
  151. * 更新地址信息
  152. * @author csdeshang
  153. * @param array $update 更新数据
  154. * @param array $condition 更新条件
  155. * @return bool 布尔类型的返回结果
  156. */
  157. public function editAddress($update, $condition) {
  158. return Db::name('address')->where($condition)->update($update);
  159. }
  160. /**
  161. * 验证地址是否属于当前用户
  162. * @author csdeshang
  163. * @param array $member_id 会员id
  164. * @param array $address_id 地址id
  165. * @return bool 布尔类型的返回结果
  166. */
  167. public function checkAddress($member_id, $address_id) {
  168. /**
  169. * 验证地址是否属于当前用户
  170. */
  171. $check_array = self::getOneAddress($address_id);
  172. if ($check_array['member_id'] == $member_id) {
  173. unset($check_array);
  174. return true;
  175. }
  176. unset($check_array);
  177. return false;
  178. }
  179. /**
  180. * 删除地址
  181. * @author csdeshang
  182. * @param array $condition记录ID
  183. * @return bool 布尔类型的返回结果
  184. */
  185. public function delAddress($condition) {
  186. return Db::name('address')->where($condition)->delete();
  187. }
  188. }