MemberAddressService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\Common;
  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. * @return array
  85. * @since 2020/11/11
  86. * @author laravel开发员
  87. */
  88. public function edit()
  89. {
  90. // 请求参数
  91. $data = request()->all();
  92. return parent::edit($data); // TODO: Change the autogenerated stub
  93. }
  94. /**
  95. * 保存数据
  96. * @param $userId
  97. * @return mixed
  98. */
  99. public function saveData($userId)
  100. {
  101. $params = request()->all();
  102. $id = isset($params['id']) ? $params['id'] : 0;
  103. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  104. $data = [
  105. 'user_id'=> $userId,
  106. 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
  107. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  108. 'province'=> isset($params['province'])? $params['province'] : '',
  109. 'city'=> isset($params['city'])? $params['city'] : '',
  110. 'district'=> isset($params['district'])? $params['district'] : '',
  111. 'codes'=> isset($params['codes'])? (is_array($params['codes'])?implode(',', $params['codes']):$params['codes']) : '',
  112. 'address'=> isset($params['address'])? $params['address'] : '',
  113. 'is_default'=> isset($params['is_default'])? $params['is_default'] : 2,
  114. 'status'=> isset($params['status'])? $params['status'] : 1,
  115. 'update_time'=> time(),
  116. 'mark'=> 1,
  117. ];
  118. if($isDefault==1){
  119. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  120. }
  121. RedisService::keyDel("caches:address:{$userId}");
  122. if(!$id){
  123. $data['create_time'] = time();
  124. return $this->model->insertGetId($data);
  125. }else{
  126. return $this->model->where(['id'=> $id])->update($data);
  127. }
  128. }
  129. /**
  130. * @param $userId
  131. * @return array|mixed
  132. */
  133. public function getBindInfo($userId)
  134. {
  135. $cacheKey = "caches:address:{$userId}";
  136. $info = RedisService::get($cacheKey);
  137. if($info){
  138. return $info;
  139. }
  140. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  141. ->orderBy('is_default','asc')
  142. ->orderBy('id','desc')
  143. ->first();
  144. $info = $info? $info->toArray() : [];
  145. if($info){
  146. RedisService::set($cacheKey, $info, rand(5, 10));
  147. }
  148. return $info;
  149. }
  150. }