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