Address.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 数据层模型
  10. */
  11. class Address extends BaseModel
  12. {
  13. /**
  14. * 取得买家默认收货地址
  15. * @author csdeshang
  16. * @param array $condition 获取条件
  17. * @param string $order 排序
  18. * @return array
  19. */
  20. public function getDefaultAddressInfo($condition = array(), $order = 'address_is_default desc,chain_id asc,address_id desc')
  21. {
  22. return $this->getAddressInfo($condition, $order);
  23. }
  24. /**
  25. * 取得单条地址信息
  26. * @author csdeshang
  27. * @param array $condition 条件
  28. * @param type $order 排序
  29. * @return string
  30. */
  31. public function getAddressInfo($condition, $order = '')
  32. {
  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. {
  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. {
  123. return Db::name('address')->where($condition)->count();
  124. }
  125. /**
  126. * 新增地址
  127. * @author csdeshang
  128. * @param array $data 参数内容
  129. * @return bool 布尔类型的返回结果
  130. */
  131. public function addAddress($data)
  132. {
  133. //当设置为默认地址,则此用户其他的地址设置为非默认地址
  134. if ($data['address_is_default'] == 1) {
  135. Db::name('address')->where('member_id', $data['member_id'])->update(array('address_is_default' => 0));
  136. }
  137. return Db::name('address')->insertGetId($data);
  138. }
  139. /**
  140. * 取单个地址
  141. * @author csdeshang
  142. * @param int $id 地址ID
  143. * @return array 数组类型的返回结果
  144. */
  145. public function getOneAddress($id)
  146. {
  147. if (intval($id) > 0) {
  148. $result = Db::name('address')->where('address_id', intval($id))->find();
  149. return $result;
  150. } else {
  151. return false;
  152. }
  153. }
  154. /**
  155. * 更新地址信息
  156. * @author csdeshang
  157. * @param array $update 更新数据
  158. * @param array $condition 更新条件
  159. * @return bool 布尔类型的返回结果
  160. */
  161. public function editAddress($update, $condition)
  162. {
  163. return Db::name('address')->where($condition)->update($update);
  164. }
  165. /**
  166. * 验证地址是否属于当前用户
  167. * @author csdeshang
  168. * @param array $member_id 会员id
  169. * @param array $address_id 地址id
  170. * @return bool 布尔类型的返回结果
  171. */
  172. public function checkAddress($member_id, $address_id)
  173. {
  174. /**
  175. * 验证地址是否属于当前用户
  176. */
  177. $check_array = self::getOneAddress($address_id);
  178. if ($check_array['member_id'] == $member_id) {
  179. unset($check_array);
  180. return true;
  181. }
  182. unset($check_array);
  183. return false;
  184. }
  185. /**
  186. * 删除地址
  187. * @author csdeshang
  188. * @param array $condition记录ID
  189. * @return bool 布尔类型的返回结果
  190. */
  191. public function delAddress($condition)
  192. {
  193. return Db::name('address')->where($condition)->delete();
  194. }
  195. }