MemberAddressService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MemberAddressModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. use App\Services\SupplyService;
  16. /**
  17. * 用户地址管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class MemberAddressService
  21. * @package App\Services\Common
  22. */
  23. class MemberAddressService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * MemberAddressService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new MemberAddressModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 列表数据
  50. * @param $params
  51. * @param int $pageSize
  52. * @return array
  53. */
  54. public function getDataList($params, $pageSize = 15)
  55. {
  56. $where = ['a.mark' => 1,'a.status'=>1];
  57. $status = isset($params['status']) ? $params['status'] : 0;
  58. if ($status > 0) {
  59. $where['a.status'] = $status;
  60. }
  61. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  62. if ($userId > 0) {
  63. $where['a.user_id'] = $userId;
  64. }
  65. $list = $this->model->from('member_address as a')
  66. ->where($where)
  67. ->select(['a.*'])
  68. ->orderBy('a.is_default', 'asc')
  69. ->orderBy('a.id', 'desc')
  70. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  71. $list = $list ? $list->toArray() : [];
  72. if ($list) {
  73. foreach ($list['data'] as &$item) {
  74. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  75. }
  76. }
  77. return [
  78. 'pageSize' => $pageSize,
  79. 'total' => isset($list['total']) ? $list['total'] : 0,
  80. 'list' => isset($list['data']) ? $list['data'] : []
  81. ];
  82. }
  83. /**
  84. * 保存数据
  85. * @param $userId
  86. * @param $params
  87. * @return mixed
  88. */
  89. public function saveData($userId, $params)
  90. {
  91. $id = isset($params['id']) ? $params['id'] : 0;
  92. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  93. $data = [
  94. 'user_id'=> $userId,
  95. 'type'=> isset($params['type']) && $params['type']? intval($params['type']) :1,
  96. 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
  97. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  98. 'province_code'=> isset($params['province_code'])? $params['province_code'] : '',
  99. 'city_code'=> isset($params['city_code'])? $params['city_code'] : '',
  100. 'district_code'=> isset($params['district_code'])? $params['district_code'] : '',
  101. 'street_code'=> isset($params['street_code'])? $params['street_code'] : '',
  102. 'province'=> isset($params['province'])? $params['province'] : '',
  103. 'city'=> isset($params['city'])? $params['city'] : '',
  104. 'district'=> isset($params['district'])? $params['district'] : '',
  105. 'street'=> isset($params['street'])? $params['street'] : '',
  106. 'address'=> isset($params['address'])? $params['address'] : '',
  107. 'is_default'=> $isDefault==1? 1: 2,
  108. 'status'=> isset($params['status'])? $params['status'] : 1,
  109. 'update_time'=> time(),
  110. 'mark'=> 1,
  111. ];
  112. if($isDefault==1){
  113. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  114. }
  115. RedisService::clear("caches:members:address:{$userId}");
  116. if($id && $this->model->where(['id'=> $id])->value('id')){
  117. $this->model->where(['id'=> $id])->update($data);
  118. $this->error = $id? 1008 : 1027;
  119. return true;
  120. }else{
  121. $data['create_time'] = time();
  122. $this->error = 1027;
  123. return $this->model->insertGetId($data);
  124. }
  125. }
  126. /**
  127. * @param $userId
  128. * @return array|mixed
  129. */
  130. public function getBindInfo($userId, $addressId=0)
  131. {
  132. $cacheKey = "caches:members:address:{$userId}".($addressId?'_'.$addressId:'');
  133. $info = RedisService::get($cacheKey);
  134. if($info){
  135. return $info;
  136. }
  137. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  138. ->orderBy('is_default','asc')
  139. ->orderBy('id','desc')
  140. ->first();
  141. $info = $info? $info->toArray() : [];
  142. if($info){
  143. $address = [];
  144. $info['mobile_text'] = format_mobile($info['mobile']);
  145. if(isset($info['province']) && $info['province']){
  146. $address[] = $info['province'];
  147. }
  148. if(isset($info['city']) && $info['city']){
  149. $address[] = $info['city'];
  150. }
  151. if(isset($info['district']) && $info['district']){
  152. $address[] = $info['district'];
  153. }
  154. if(isset($info['street']) && $info['street']){
  155. $address[] = $info['street'];
  156. }
  157. $info['address_text'] = $address? implode('', $address) : '';
  158. RedisService::set($cacheKey, $info, rand(5, 10));
  159. }
  160. return $info;
  161. }
  162. /**
  163. * 获取可选择的地址列表
  164. * @param $code
  165. * @return array|false|mixed|string
  166. */
  167. public function getRegion($code)
  168. {
  169. $cacheKey ="caches:members:regions:{$code}";
  170. $datas = RedisService::get($cacheKey);
  171. if($datas){
  172. return $datas;
  173. }
  174. $datas = SupplyService::make()->getApiData('getRegion',['code'=> $code]);
  175. if($datas){
  176. RedisService::set($cacheKey, $datas, rand(300, 600));
  177. }
  178. return $datas;
  179. }
  180. /**
  181. * @return array|false
  182. */
  183. public function delete()
  184. {
  185. // 参数
  186. $id = request()->post('id');
  187. if (empty($id)) {
  188. $this->error = 2014;
  189. return false;
  190. }
  191. $this->error = 1002;
  192. $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  193. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  194. }
  195. }