Address.php 7.7 KB

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