MemberAddressService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 Illuminate\Support\Facades\DB;
  16. /**
  17. * 用户地址管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class MemberAddressService
  21. * @package App\Services\Api
  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. $address = [];
  76. if($item['province']){
  77. $address[] = $item['province'];
  78. }
  79. if($item['city']){
  80. $address[] = $item['city'];
  81. }
  82. if($item['district']){
  83. $address[] = $item['district'];
  84. }
  85. if($item['address']){
  86. $address[] = $item['address'];
  87. }
  88. $item['address_text'] = $address? implode(' ',$address) : '';
  89. }
  90. }
  91. return [
  92. 'pageSize' => $pageSize,
  93. 'total' => isset($list['total']) ? $list['total'] : 0,
  94. 'list' => isset($list['data']) ? $list['data'] : []
  95. ];
  96. }
  97. /**
  98. * 保存数据
  99. * @param $userId
  100. * @param $params
  101. * @return mixed
  102. */
  103. public function saveData($userId, $params)
  104. {
  105. $id = isset($params['id']) ? $params['id'] : 0;
  106. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  107. $data = [
  108. 'user_id'=> $userId,
  109. 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
  110. 'realname'=> isset($params['realname'])? $params['realname'] : '',
  111. 'province'=> isset($params['province'])? $params['province'] : '',
  112. 'city'=> isset($params['city'])? $params['city'] : '',
  113. 'district'=> isset($params['district'])? $params['district'] : '',
  114. 'codes'=> isset($params['codes'])? $params['codes'] : '',
  115. 'address'=> isset($params['address'])? $params['address'] : '',
  116. 'is_default'=> $isDefault==1? 1: 2,
  117. 'status'=> isset($params['status'])? $params['status'] : 1,
  118. 'update_time'=> time(),
  119. 'mark'=> 1,
  120. ];
  121. DB::beginTransaction();
  122. if($isDefault==1){
  123. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  124. }
  125. RedisService::keyDel("caches:members:address:{$userId}*");
  126. if($id && $id = $this->model->where(['id'=> $id])->value('id')){
  127. $this->model->where(['id'=> $id])->update($data);
  128. $this->error = '保存成功';
  129. DB::commit();
  130. return ['id'=>$id];
  131. }else{
  132. $data['create_time'] = time();
  133. if(!$id = $this->model->insertGetId($data)){
  134. DB::rollBack();
  135. $this->error = '添加失败';
  136. return false;
  137. }
  138. DB::commit();
  139. $this->error = '添加成功';
  140. return ['id'=>$id];
  141. }
  142. }
  143. /**
  144. * 默认
  145. * @param $userId
  146. * @param $params
  147. * @return mixed
  148. */
  149. public function setDefault($userId, $params)
  150. {
  151. $id = isset($params['id']) ? $params['id'] : 0;
  152. $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
  153. $data = [
  154. 'user_id'=> $userId,
  155. 'is_default'=> $isDefault==1? 1: 2,
  156. 'update_time'=> time(),
  157. ];
  158. if($isDefault==1){
  159. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
  160. }
  161. $this->model->where(['id'=> $id])->update($data);
  162. $this->error = '保存成功';
  163. RedisService::keyDel("caches:members:address:{$userId}*");
  164. return ['id'=>$id];
  165. }
  166. /**
  167. * @param $id
  168. * @param $userId
  169. * @return array|mixed
  170. */
  171. public function getInfo($id, $userId)
  172. {
  173. $cacheKey = "caches:members:address:{$userId}_info_".($id?'_'.$id:'');
  174. $info = RedisService::get($cacheKey);
  175. if($info){
  176. return $info;
  177. }
  178. $where = ['id'=> $id,'user_id'=> $userId,'mark'=>1,'status'=>1];
  179. if($id<=0){
  180. unset($where['id']);
  181. }
  182. $info = $this->model->where($where)->first();
  183. $info = $info? $info->toArray() : [];
  184. if($info){
  185. $address = [];
  186. $info['mobile_text'] = format_mobile($info['mobile']);
  187. if(isset($info['province']) && $info['province']){
  188. $address[] = $info['province'];
  189. }
  190. if(isset($info['city']) && $info['city']){
  191. $address[] = $info['city'];
  192. }
  193. if(isset($info['district']) && $info['district']){
  194. $address[] = $info['district'];
  195. }
  196. $info['area'] = $address? implode('/', $address) : '';
  197. if($info['address']){
  198. $address[] = $info['address'];
  199. }
  200. $info['address_text'] = $address? implode(' ', $address) : '';
  201. RedisService::set($cacheKey, $info, rand(5, 10));
  202. }
  203. return $info;
  204. }
  205. /**
  206. * @param $userId
  207. * @return array|mixed
  208. */
  209. public function getBindInfo($userId, $addressId=0)
  210. {
  211. $cacheKey = "caches:members:address:{$userId}".($addressId?'_'.$addressId:'');
  212. $info = RedisService::get($cacheKey);
  213. if($info){
  214. return $info;
  215. }
  216. $where = ['id'=> $addressId,'user_id'=> $userId,'mark'=>1,'status'=>1];
  217. if($addressId<=0){
  218. unset($where['id']);
  219. }
  220. $info = $this->model->where($where)
  221. ->orderBy('is_default','asc')
  222. ->orderBy('id','desc')
  223. ->first();
  224. $info = $info? $info->toArray() : [];
  225. if($info){
  226. $address = [];
  227. $info['mobile_text'] = format_mobile($info['mobile']);
  228. if(isset($info['province']) && $info['province']){
  229. $address[] = $info['province'];
  230. }
  231. if(isset($info['city']) && $info['city']){
  232. $address[] = $info['city'];
  233. }
  234. if(isset($info['district']) && $info['district']){
  235. $address[] = $info['district'];
  236. }
  237. $info['area'] = $address? implode('/', $address) : '';
  238. if($info['address']){
  239. $address[] = $info['address'];
  240. }
  241. $info['address_text'] = $address? implode(' ', $address) : '';
  242. RedisService::set($cacheKey, $info, rand(5, 10));
  243. }
  244. return $info;
  245. }
  246. /**
  247. * @return array|false
  248. */
  249. public function delete()
  250. {
  251. // 参数
  252. $id = request()->post('id');
  253. if (empty($id)) {
  254. $this->error = 2014;
  255. return false;
  256. }
  257. $this->error = 1002;
  258. $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
  259. return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
  260. }
  261. }