Address.php 7.8 KB

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