MemberAddressService.php 9.0 KB

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