AddressService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\AddressModel;
  13. /**
  14. * 收货地址管理-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class AddressService
  18. * @package App\Services
  19. */
  20. class AddressService extends BaseService
  21. {
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * @author wesmiler
  26. * @since 2020/11/11
  27. * AddressService constructor.
  28. */
  29. public function __construct()
  30. {
  31. $this->model = new AddressModel();
  32. }
  33. /**
  34. * 静态入口
  35. * @return ArticleService|null
  36. */
  37. public static function make(){
  38. if(!self::$instance){
  39. self::$instance = new ArticleService();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * 获取列表
  45. * @return array
  46. * @since 2020/11/11
  47. * @author wesmiler
  48. */
  49. public function getDataList($params)
  50. {
  51. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  52. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  53. $dataList = $this->model::from('member_address as a')
  54. ->leftJoin('city as c1', 'c1.citycode', '=', 'a.province_id')
  55. ->leftJoin('city as c2', 'c2.citycode', '=', 'a.city_id')
  56. ->leftJoin('city as c3', 'c3.citycode', '=', 'a.district_id')
  57. ->where(function ($query) use ($params) {
  58. $query->where(['a.mark'=> 1,'a.status'=> 1]);
  59. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  60. if ($userId > 0) {
  61. $query->where('a.user_id', $userId);
  62. }
  63. })
  64. ->select(['a.id', 'a.user_id', 'c1.name as province_name','c2.name as city_name','c3.name as district_name', 'a.realname', 'a.mobile', 'a.is_default', 'a.status', 'a.create_time', 'a.update_time', 'a.address'])
  65. ->orderBy('a.is_default', 'asc')
  66. ->orderBy('a.create_time', 'desc')
  67. ->paginate($pageSize);
  68. $dataList = $dataList ? $dataList->toArray() : [];
  69. if ($dataList) {
  70. foreach ($dataList['data'] as &$item) {
  71. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  72. $item['address_text'] = $item['province_name'].$item['city_name'].$item['district_name'];
  73. }
  74. unset($item);
  75. }
  76. return [
  77. 'code' => 0,
  78. 'success'=> true,
  79. 'msg' => '操作成功',
  80. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  81. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  82. ];
  83. }
  84. /**
  85. * 添加或编辑
  86. * @return array
  87. * @since 2020/11/11
  88. * @author wesmiler
  89. */
  90. public function edit()
  91. {
  92. $data = request()->all();
  93. $data['update_time'] = time();
  94. $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
  95. return parent::edit($data); // TODO: Change the autogenerated stub
  96. }
  97. /**
  98. * 保存添加数据
  99. * @param $params
  100. * @return array
  101. */
  102. public function save($params){
  103. $cityCodes = isset($params['cityCodes'])? $params['cityCodes'] : [];
  104. $cityCodes = $cityCodes? array_filter($cityCodes) : [];
  105. if(empty($cityCodes)){
  106. return message('请选择省市', false);
  107. }
  108. $data = [
  109. 'user_id' => isset($params['user_id'])? $params['user_id'] : 0,
  110. 'realname' => isset($params['realname'])? $params['realname'] : '',
  111. 'mobile' => isset($params['mobile'])? $params['mobile'] : '',
  112. 'province_id' => isset($cityCodes[0])? $cityCodes[0] : '',
  113. 'city_id' => isset($cityCodes[1])? $cityCodes[1] : '',
  114. 'district_id' => isset($cityCodes[2])? $cityCodes[2] : '',
  115. 'address' => isset($params['address'])? trim($params['address']) : '',
  116. 'is_default'=>isset($params['is_default'])? intval($params['is_default']) : 2,
  117. 'create_time'=> time()
  118. ];
  119. $data['update_time'] = time();
  120. return parent::edit($data); // TODO: Change the autogenerated stub
  121. }
  122. /**
  123. * 获取默认地址
  124. * @param $userId
  125. * @return array
  126. */
  127. public function getDefault($userId){
  128. $info = $this->model::from('member_address as a')
  129. ->leftJoin('city as c1', 'c1.citycode', '=', 'a.province_id')
  130. ->leftJoin('city as c2', 'c2.citycode', '=', 'a.city_id')
  131. ->leftJoin('city as c3', 'c3.citycode', '=', 'a.district_id')
  132. ->where(['a.user_id'=> $userId,'a.maek'=> 1,'a.status'=> 1])
  133. ->select(['a.id', 'a.user_id', 'c1.name as province_name','c2.name as city_name','c3.name as district_name', 'a.realname', 'a.mobile', 'a.is_default', 'a.status', 'a.create_time', 'a.update_time', 'a.address'])
  134. ->orderBy('a.is_default', 'asc')
  135. ->orderBy('a.create_time', 'desc')
  136. ->first();
  137. $info = $info? $info->toArray() : [];
  138. if($info){
  139. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'],'Y-m-d H:i:s') : '';
  140. $info['address_text'] = $info['province_name'].$info['city_name'].$info['district_name'];
  141. }
  142. return $info;
  143. }
  144. }