MemberAddressService.php 9.1 KB

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