MemberAddressService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
  96. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  97. 'province'=> isset($params['province'])? $params['province'] : '',
  98. 'city'=> isset($params['city'])? $params['city'] : '',
  99. 'district'=> isset($params['district'])? $params['district'] : '',
  100. 'codes'=> isset($params['codes'])? (is_array($params['codes'])?implode(',', $params['codes']):$params['codes']) : '',
  101. 'address'=> isset($params['address'])? $params['address'] : '',
  102. 'is_default'=> $isDefault==1? 1: 2,
  103. 'status'=> isset($params['status'])? $params['status'] : 1,
  104. 'update_time'=> time(),
  105. 'mark'=> 1,
  106. ];
  107. if($isDefault==1){
  108. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  109. }
  110. RedisService::keyDel("caches:address:{$userId}");
  111. if($id && $this->model->where(['id'=> $id])->value('id')){
  112. $this->model->where(['id'=> $id])->update($data);
  113. $this->error = $id? 1008 : 1027;
  114. return true;
  115. }else{
  116. $data['create_time'] = time();
  117. $this->error = 1027;
  118. return $this->model->insertGetId($data);
  119. }
  120. }
  121. /**
  122. * @param $userId
  123. * @return array|mixed
  124. */
  125. public function getBindInfo($userId)
  126. {
  127. $cacheKey = "caches:address:{$userId}";
  128. $info = RedisService::get($cacheKey);
  129. if($info){
  130. return $info;
  131. }
  132. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  133. ->orderBy('is_default','asc')
  134. ->orderBy('id','desc')
  135. ->first();
  136. $info = $info? $info->toArray() : [];
  137. if($info){
  138. RedisService::set($cacheKey, $info, rand(5, 10));
  139. }
  140. return $info;
  141. }
  142. public function getRegion($code)
  143. {
  144. $cacheKey ="caches:members:regions:{$code}";
  145. $datas = RedisService::get($cacheKey);
  146. if($datas){
  147. return $datas;
  148. }
  149. $datas = SupplyService::make()->getApiData('getRegion',['code'=> $code]);
  150. if($datas){
  151. }
  152. }
  153. /**
  154. * @return array|false
  155. */
  156. public function delete()
  157. {
  158. // 参数
  159. $id = request()->post('id');
  160. if (empty($id)) {
  161. $this->error = 2014;
  162. return false;
  163. }
  164. $this->error = 1002;
  165. $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  166. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  167. }
  168. }