StoreService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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\GoodsModel;
  13. use App\Models\MemberModel;
  14. use App\Models\StoreCategoryModel;
  15. use App\Models\StoreModel;
  16. use App\Services\BaseService;
  17. use App\Services\RedisService;
  18. use Illuminate\Support\Facades\DB;
  19. /**
  20. * 商家管理-服务类
  21. * @author laravel开发员
  22. * @since 2020/11/11
  23. * @package App\Services\Api
  24. */
  25. class StoreService extends BaseService
  26. {
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new StoreModel();
  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. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $status = isset($params['status']) ? $params['status'] : 0;
  56. if ($status > 0) {
  57. $where['a.status'] = $status;
  58. }
  59. $list = $this->model->with(['user'])->from('stores as a')
  60. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  61. ->where($where)
  62. ->where(function ($query) use ($params) {
  63. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  64. if ($keyword) {
  65. $query->where('a.name', 'like', "%{$keyword}%");
  66. }
  67. })
  68. ->select(['a.*'])
  69. ->orderBy('a.create_time', '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. }
  76. }
  77. return [
  78. 'pageSize' => $pageSize,
  79. 'total' => isset($list['total']) ? $list['total'] : 0,
  80. 'list' => isset($list['data']) ? $list['data'] : []
  81. ];
  82. }
  83. /**
  84. * 商品列表
  85. * @param $params
  86. * @param int $pageSize
  87. * @return array
  88. */
  89. public function getGoodsList($params, $pageSize = 7)
  90. {
  91. $userId = isset($params['user_id'])? $params['user_id'] : 0;
  92. $cacheKey = "caches:stores:index_goods_{$userId}_{$pageSize}:".md5(json_encode($params));
  93. $datas = RedisService::get($cacheKey);
  94. if($datas){
  95. return $datas;
  96. }
  97. $storeId = $this->getStoreId($userId);
  98. if($storeId<=0){
  99. $this->error = $this->error? '没有店铺信息:'.$this->error: '没有店铺信息';
  100. return false;
  101. }
  102. $storeInfo = $this->model->where(['id'=>$storeId,'status'=>1,'mark'=>1])
  103. ->select(['id','user_id','name','logo'])
  104. ->first();
  105. if(empty($storeInfo)){
  106. $this->error = '店铺信息不存在';
  107. return ['id'=>$storeId];
  108. }
  109. $list = GoodsModel::where(['store_id'=>$storeId,'status'=>1,'mark'=>1])
  110. ->where(function ($query) use ($params) {
  111. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  112. if ($keyword) {
  113. $query->where('goods_name', 'like', "%{$keyword}%")
  114. ->orWhere('tags', 'like', "%{$keyword}%");
  115. }
  116. })
  117. ->where(function ($query) use ($params) {
  118. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  119. if ($categoryId>0) {
  120. $query->where('category_id', $categoryId);
  121. }
  122. })
  123. ->select(['id','goods_name','thumb','price','sku_type','sales','sort','status'])
  124. ->orderBy('sort', 'desc')
  125. ->orderBy('id', 'desc')
  126. ->limit($pageSize)
  127. ->get();
  128. $list = $list ? $list->toArray() : [];
  129. $datas = ['id'=>$storeId];
  130. if ($list) {
  131. $datas = [
  132. 'id'=>$storeId,
  133. 'store'=>$storeInfo,
  134. 'goods'=>$list
  135. ];
  136. RedisService::set($cacheKey, $datas, rand(10,20));
  137. }
  138. return $datas;
  139. }
  140. /**
  141. * 获取显示商家ID
  142. * @param $userId
  143. * @return array|int|mixed
  144. */
  145. public function getStoreId($userId)
  146. {
  147. $cacheKey = "caches:storeId:id_{$userId}";
  148. $data = RedisService::get($cacheKey);
  149. if($data){
  150. return $data;
  151. }
  152. $params = request()->all();
  153. $system = isset($params['system']) ? $params['system'] : [];
  154. $system = $system && !is_array($system) ? json_decode($system, true) : $system;
  155. $uuid = isset($system['uuid']) ? $system['uuid'] : '';
  156. $uuKey = "caches:storeId:uuid_{$uuid}";
  157. $ipKey = "caches:storeId:ip_".get_client_ip();
  158. if($userId<=0){
  159. if($storeId = RedisService::get($ipKey)){
  160. $this->error = "未登录缓存的店铺";
  161. return $storeId;
  162. }else if($uuid && $storeId = RedisService::get($uuKey)){
  163. $this->error = "未登录缓存的店铺";
  164. return $storeId;
  165. }
  166. }
  167. // 用户信息
  168. $userInfo = MemberModel::with(['store'])
  169. ->where(['id'=>$userId,'mark'=>1])
  170. ->select(['id', 'realname', 'nickname','parents', 'status'])
  171. ->first();
  172. $store = isset($userInfo['store'])? $userInfo['store'] : [];
  173. $parents = isset($userInfo['parents'])? $userInfo['parents'] : '';
  174. $ids = $parents? explode(',', $parents) : [];
  175. $ids = array_filter($ids);
  176. $ids = array_reverse($ids);
  177. $storeId = isset($store['id'])? $store['id'] : 0;
  178. $storeStatus = isset($store['status'])? $store['status'] : 0;
  179. $storeMark = isset($store['mark'])? $store['mark'] : 0;
  180. if($userId && empty($userInfo)){
  181. $this->error = '用户账户不存在';
  182. return 0;
  183. }
  184. // 用户自己是商家
  185. if($storeId>0 && $storeStatus==1 && $storeMark==1){
  186. $this->error = '用户自己的店铺';
  187. RedisService::set($cacheKey, $storeId, 7 * 86400);
  188. return $storeId;
  189. }
  190. // 用户上级有商家
  191. $parentStoreInfo = [];
  192. if($ids){
  193. $parentStoreInfo = $this->model->whereIn('user_id',$ids)
  194. ->where(['status'=>1,'mark'=>1])
  195. ->select(['id','user_id','name'])
  196. ->orderByRaw("FIELD(user_id,".(implode(',',$ids)).") asc")
  197. ->first();
  198. }
  199. $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0;
  200. $parentUserId = isset($parentStoreInfo['user_id'])?$parentStoreInfo['user_id'] : 0;
  201. if($parentStoreInfo && $storeId>0){
  202. RedisService::set($cacheKey, $storeId, 7 * 86400);
  203. RedisService::set($ipKey, $storeId, 7 * 86400);
  204. RedisService::set($uuKey, $storeId, 7 * 86400);
  205. $this->error = "上级[{$parentUserId}]的店铺";
  206. return $storeId;
  207. }
  208. $this->error = "";
  209. return 0;
  210. }
  211. /**
  212. * 行业分类
  213. * @return array|mixed
  214. */
  215. public function getCategoryList()
  216. {
  217. $cacheKey = "caches:stores:categoryList";
  218. $datas = RedisService::get($cacheKey);
  219. if($datas){
  220. return $datas;
  221. }
  222. $datas = StoreCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
  223. ->select(['id','name','pid','sort'])
  224. ->orderBy('sort','desc')
  225. ->orderBy('id','desc')
  226. ->get();
  227. $datas = $datas? $datas->toArray() : [];
  228. if($datas){
  229. RedisService::set($cacheKey, $datas, rand(300,600));
  230. }
  231. return $datas;
  232. }
  233. /**
  234. * 申请
  235. * @param $userId
  236. * @param $params
  237. * @return mixed
  238. */
  239. public function apply($userId, $params)
  240. {
  241. $name = isset($params['name']) ? trim($params['name']) : '';
  242. $realname = isset($params['real_name']) ? trim($params['real_name']) : '';
  243. $phone = isset($params['phone']) ? trim($params['phone']) : '';
  244. $address = isset($params['address']) ? trim($params['address']) : '';
  245. $intro = isset($params['intro']) ? trim($params['intro']) : '';
  246. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  247. $logo = isset($params['logo']) && $params['logo']? get_image_path($params['logo']) : '';
  248. $businessLicense = isset($params['business_license']) && $params['business_license']? get_image_path($params['business_license']) : '';
  249. $otherPhoto = isset($params['other_photo']) && $params['other_photo']? get_image_path($params['other_photo']) : '';
  250. $data = [
  251. 'user_id' => $userId,
  252. 'name' => $name,
  253. 'real_name' => $realname,
  254. 'phone' => $phone,
  255. 'address' => $address,
  256. 'category_id' => $categoryId,
  257. 'logo' => $logo,
  258. 'business_license' => $businessLicense,
  259. 'other_photo' => $otherPhoto,
  260. 'intro' => $intro,
  261. 'order_count' => 0,
  262. 'order_total' => 0,
  263. 'confirm_remark' => '',
  264. 'create_time' => time(),
  265. 'update_time' => time(),
  266. 'status' => 2,
  267. 'mark' => 1,
  268. ];
  269. DB::beginTransaction();
  270. if($id = $this->model->where(['user_id'=>$userId])->value('id')){
  271. $this->model->where(['id'=>$id])->update($data);
  272. }else{
  273. if (!$id = $this->model->insertGetId($data)) {
  274. DB::rollBack();
  275. $this->error = '申请入驻失败';
  276. return false;
  277. }
  278. }
  279. DB::commit();
  280. RedisService::keyDel("caches:members:info_*");
  281. RedisService::keyDel("caches:stores:info*");
  282. $this->error = '申请入驻成功,请耐心等候审核~';
  283. return ['id' => $id];
  284. }
  285. /**
  286. * 入驻信息
  287. * @param $id
  288. * @param $userId
  289. * @return array|mixed
  290. */
  291. public function getApplyInfo($userId)
  292. {
  293. $cacheKey = "caches:stores:info_u{$userId}";
  294. $info = RedisService::get($cacheKey);
  295. if($info){
  296. return $info;
  297. }
  298. $where = ['user_id'=> $userId,'mark'=>1];
  299. $info = $this->model->with(['category'])->where($where)->first();
  300. $info = $info? $info->toArray() : [];
  301. if($info){
  302. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  303. RedisService::set($cacheKey, $info, rand(5, 10));
  304. }
  305. return $info;
  306. }
  307. /**
  308. * 店铺详情
  309. * @param $id
  310. * @param $userId
  311. * @return array|mixed
  312. */
  313. public function getInfo($id)
  314. {
  315. $cacheKey = "caches:stores:info_{$id}";
  316. $info = RedisService::get($cacheKey);
  317. if($info){
  318. return $info;
  319. }
  320. $where = ['id'=> $id,'mark'=>1];
  321. $info = $this->model->with(['category'])->where($where)->first();
  322. $info = $info? $info->toArray() : [];
  323. if($info){
  324. $info['category_name'] = isset($info['category']) && $info['category']?$info['category']['name'] : '';
  325. RedisService::set($cacheKey, $info, rand(5, 10));
  326. }
  327. return $info;
  328. }
  329. }