MemberAddressService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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];
  56. $status = isset($params['status']) ? $params['status'] : 0;
  57. if ($status > 0) {
  58. $where['a.status'] = $status;
  59. }
  60. $list = $this->model->from('member_address as a')
  61. ->where($where)
  62. ->select(['a.*'])
  63. ->orderBy('a.id', 'desc')
  64. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  65. $list = $list ? $list->toArray() : [];
  66. if ($list) {
  67. foreach ($list['data'] as &$item) {
  68. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  69. }
  70. }
  71. return [
  72. 'pageSize' => $pageSize,
  73. 'total' => isset($list['total']) ? $list['total'] : 0,
  74. 'list' => isset($list['data']) ? $list['data'] : []
  75. ];
  76. }
  77. /**
  78. * 添加编辑
  79. * @return array
  80. * @since 2020/11/11
  81. * @author laravel开发员
  82. */
  83. public function edit()
  84. {
  85. // 请求参数
  86. $data = request()->all();
  87. return parent::edit($data); // TODO: Change the autogenerated stub
  88. }
  89. /**
  90. * 保存数据
  91. * @param $userId
  92. * @return mixed
  93. */
  94. public function saveData($userId)
  95. {
  96. $params = request()->all();
  97. $id = isset($params['id']) ? $params['id'] : 0;
  98. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  99. $data = [
  100. 'user_id'=> $userId,
  101. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  102. 'province'=> isset($params['province'])? $params['province'] : '',
  103. 'city'=> isset($params['city'])? $params['city'] : '',
  104. 'district'=> isset($params['district'])? $params['district'] : '',
  105. 'codes'=> isset($params['codes'])? implode(',', $params['codes']) : '',
  106. 'address'=> isset($params['address'])? $params['address'] : '',
  107. 'is_default'=> isset($params['is_default'])? $params['is_default'] : 2,
  108. 'status'=> isset($params['status'])? $params['status'] : 1,
  109. 'create_time'=> time(),
  110. 'update_time'=> time(),
  111. 'mark'=> 1,
  112. ];
  113. if($isDefault==1){
  114. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  115. }
  116. RedisService::keyDel("caches:address:{$userId}");
  117. if(!$id){
  118. $data['create_time'] = time();
  119. return $this->model->insertGetId($data);
  120. }else{
  121. return $this->model->where(['id'=> $id])->update($data);
  122. }
  123. }
  124. /**
  125. * @param $userId
  126. * @return array|mixed
  127. */
  128. public function getBindInfo($userId)
  129. {
  130. $cacheKey = "caches:address:{$userId}";
  131. $info = RedisService::get($cacheKey);
  132. if($info){
  133. return $info;
  134. }
  135. $info = $this->model->where(['user_id'=> $userId,'mark'=>1,'status'=>1])
  136. ->orderBy('is_default','asc')
  137. ->orderBy('id','desc')
  138. ->first();
  139. if($info){
  140. RedisService::set($cacheKey, $info, rand(5, 10));
  141. }
  142. return $info;
  143. }
  144. }