MemberAddressService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  16. * 用户地址管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class MemberAddressService
  20. * @package App\Services\Common
  21. */
  22. class MemberAddressService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * MemberAddressService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberAddressModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1,'a.status'=>1];
  56. $status = isset($params['status']) ? $params['status'] : 0;
  57. if ($status > 0) {
  58. $where['a.status'] = $status;
  59. }
  60. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  61. if ($userId > 0) {
  62. $where['a.user_id'] = $userId;
  63. }
  64. $list = $this->model->from('member_address as a')
  65. ->where($where)
  66. ->select(['a.*'])
  67. ->orderBy('a.is_default', 'asc')
  68. ->orderBy('a.id', 'desc')
  69. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  70. $list = $list ? $list->toArray() : [];
  71. if ($list) {
  72. foreach ($list['data'] as &$item) {
  73. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  74. }
  75. }
  76. return [
  77. 'pageSize' => $pageSize,
  78. 'total' => isset($list['total']) ? $list['total'] : 0,
  79. 'list' => isset($list['data']) ? $list['data'] : []
  80. ];
  81. }
  82. /**
  83. * 保存数据
  84. * @param $userId
  85. * @param $params
  86. * @return mixed
  87. */
  88. public function saveData($userId, $params)
  89. {
  90. $id = isset($params['id']) ? $params['id'] : 0;
  91. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  92. $data = [
  93. 'user_id'=> $userId,
  94. 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
  95. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  96. 'province'=> isset($params['province'])? $params['province'] : '',
  97. 'city'=> isset($params['city'])? $params['city'] : '',
  98. 'district'=> isset($params['district'])? $params['district'] : '',
  99. 'codes'=> isset($params['codes'])? (is_array($params['codes'])?implode(',', $params['codes']):$params['codes']) : '',
  100. 'address'=> isset($params['address'])? $params['address'] : '',
  101. 'is_default'=> $isDefault==1? 1: 2,
  102. 'status'=> isset($params['status'])? $params['status'] : 1,
  103. 'update_time'=> time(),
  104. 'mark'=> 1,
  105. ];
  106. if($isDefault==1){
  107. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  108. }
  109. RedisService::keyDel("caches:address:{$userId}");
  110. if($id && $this->model->where(['id'=> $id])->value('id')){
  111. $this->model->where(['id'=> $id])->update($data);
  112. $this->error = $id? 1008 : 1027;
  113. return true;
  114. }else{
  115. $data['create_time'] = time();
  116. $this->error = 1027;
  117. return $this->model->insertGetId($data);
  118. }
  119. }
  120. /**
  121. * @param $userId
  122. * @return array|mixed
  123. */
  124. public function getBindInfo($userId)
  125. {
  126. $cacheKey = "caches:address:{$userId}";
  127. $info = RedisService::get($cacheKey);
  128. if($info){
  129. return $info;
  130. }
  131. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  132. ->orderBy('is_default','asc')
  133. ->orderBy('id','desc')
  134. ->first();
  135. $info = $info? $info->toArray() : [];
  136. if($info){
  137. RedisService::set($cacheKey, $info, rand(5, 10));
  138. }
  139. return $info;
  140. }
  141. /**
  142. * @return array|false
  143. */
  144. public function delete()
  145. {
  146. // 参数
  147. $id = request()->post('id');
  148. if (empty($id)) {
  149. $this->error = 2014;
  150. return false;
  151. }
  152. $this->error = 1002;
  153. $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  154. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  155. }
  156. }