ShopService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\shop;
  12. use app\dict\site\SiteDict;
  13. use app\model\shop\Shop;
  14. use app\model\shop\ShopMember;
  15. use app\model\shop\ShopSite;
  16. use app\service\core\sys\CoreSysConfigService;
  17. use app\service\core\web\CoreWebConfigService;
  18. use core\base\BaseApiService;
  19. use core\util\Barcode;
  20. use think\db\exception\DbException;
  21. /**
  22. * 店铺服务层(针对站点服务层扩展实现)
  23. * Class ShopService
  24. * @package addon\mall\app\service\admin\mall\shop
  25. */
  26. class ShopService extends BaseApiService
  27. {
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->model = new ShopSite();
  32. }
  33. /**
  34. * 获取站点列表
  35. * @param array $where
  36. * @return array
  37. * @throws DbException
  38. */
  39. public function getPage(array $where = [])
  40. {
  41. $field = 'shop.site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  42. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id,is_recommend, is_self, follow_number, category_id';
  43. $condition = [
  44. [ 'app_type', '<>', 'admin' ],
  45. [ 'status', '=', 1 ]
  46. ];
  47. $search_model = $this->model->where($condition)->withSearch(['keyword', 'group_id', 'category_id', 'is_self' ], $where)
  48. ->withJoin( [
  49. 'shop' => [ 'category_id', 'is_self', 'follow_number'] ])->with(['categoryName', 'groupName'])
  50. ->field($field)->order('create_time desc');
  51. return $this->pageQuery($search_model);
  52. }
  53. /**
  54. * 获取甄选好店列表(web端)
  55. * @return array
  56. * @throws DbException
  57. */
  58. public function getSelectShopLists()
  59. {
  60. $config = (new CoreWebConfigService())->getWebConfig();
  61. $site_ids = $config['site_ids'] ?? [];
  62. $field = 'site_id,site_name,front_end_name,front_end_logo,icon';
  63. $condition = [
  64. [ 'app_type', '<>', 'admin' ],
  65. [ 'status', '=', 1 ],
  66. [ 'site_id', 'in', $site_ids ]
  67. ];
  68. $list = [];
  69. if ($config['is_show_shop']) {
  70. $list = $this->model->where($condition)->with( [ 'shop' => function($query) {
  71. $query->field('site_id,is_self');
  72. }])->field($field)->order('create_time desc')->select()->toArray();
  73. foreach ($list as &$v) {
  74. $goods_list = array_values(array_filter(event('MallShopGoodsList', [ 'site_id' => $v[ 'site_id' ], 'num' => 2 ])))[0] ?? [];
  75. $v['goods_list'] = $goods_list;
  76. }
  77. }
  78. return [
  79. 'list' => $list,
  80. 'config' => $config
  81. ];
  82. }
  83. /**
  84. * 获取站点列表(wap端)
  85. * @param array $where
  86. * @return array
  87. * @throws DbException
  88. */
  89. public function getShopPage(array $where = [])
  90. {
  91. $field = 'shop.site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  92. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id,is_recommend, is_self, follow_number, category_id';
  93. $condition = [
  94. [ 'app_type', '<>', 'admin' ],
  95. [ 'status', '=', 1 ]
  96. ];
  97. $search_model = $this->model->where($condition)->withSearch(['keyword', 'group_id', 'category_id', 'is_self' ], $where)
  98. ->withJoin( [
  99. 'shop' => [ 'category_id', 'is_self', 'follow_number'] ])->with(['categoryName', 'groupName'])
  100. ->field($field)->order('create_time desc');
  101. $res = $this->pageQuery($search_model);
  102. foreach ($res['data'] as &$v) {
  103. $goods_list = array_values(array_filter(event('MallShopGoodsList', [ 'site_id' => $v[ 'site_id' ], 'num' => 3 ])))[0] ?? [];
  104. $v['goods'] = $goods_list;
  105. }
  106. return $res;
  107. }
  108. /**
  109. * 获取站点列表
  110. * @param array $where
  111. * @return array
  112. * @throws DbException
  113. */
  114. public function getAll(int $limit = 0)
  115. {
  116. $field = 'shop.site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  117. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id,is_recommend, is_self, follow_number, category_id';
  118. $condition = [
  119. [ 'app_type', '<>', 'admin' ],
  120. [ 'status', '=', 1 ]
  121. ];
  122. $search_model = $this->model->where($condition)->withJoin( [ 'shop' => [ 'category_id', 'is_self', 'follow_number'] ])->with(['categoryName', 'groupName'])
  123. ->field($field)->order('create_time desc')->limit($limit);
  124. return $search_model->select()->toArray();
  125. }
  126. /**
  127. * 站点信息
  128. * @param int $site_id
  129. * @return array
  130. */
  131. public function getInfo(int $site_id)
  132. {
  133. $field = 'shop.site_id, site_name, front_end_name, front_end_logo, app_type, keywords, logo, icon, `desc`, status, latitude, longitude, province_id, city_id,
  134. district_id, address, full_address, phone, business_hours, create_time, expire_time, group_id,is_recommend, is_self, follow_number, category_id';
  135. $condition = [
  136. [ 'shop_site.site_id', '=', $site_id ]
  137. ];
  138. $modelResult = $this->model->where($condition)
  139. ->withJoin( [
  140. 'shop'=> ['site_id', 'category_id', 'is_self', 'follow_number', 'business_license'] ])->with(['categoryName', 'groupName'])
  141. ->field($field)->findOrEmpty()->toArray();
  142. if (!empty($this->member_id)) {
  143. $member_info = (new ShopMember())->field('is_follow')->where([['member_id', '=', $this->member_id], ['site_id', '=', $site_id]])->findOrEmpty()->toArray();
  144. $modelResult['member_info'] = $member_info ? ['is_follow' => $member_info['is_follow']] : ['is_follow' => 0];
  145. } else {
  146. $modelResult['member_info'] = ['is_follow' => 0];
  147. }
  148. return $modelResult;
  149. }
  150. /**
  151. * 更新店铺关注数
  152. * @param int $site_id
  153. * @param int $is_follow
  154. * @return void
  155. */
  156. public function editFollowNumber(int $site_id, int $is_follow)
  157. {
  158. $shop_model = new Shop();
  159. if ($is_follow == 1) {
  160. $shop_model->where(['site_id' => $site_id])->setInc('follow_number');
  161. } else if ($is_follow == 0) {
  162. $shop_model->where(['site_id' => $site_id])->setDec('follow_number');
  163. }
  164. }
  165. /**
  166. * 获取商品列表供组件调用
  167. * @param array $where
  168. * @return array
  169. */
  170. public function getShopComponents(array $where = [])
  171. {
  172. $field = 'site_id,site_name,front_end_logo,front_end_name,icon';
  173. $condition = [
  174. [ 'app_type', '<>', 'admin' ],
  175. [ 'status', '=', SiteDict::ON ]
  176. ];
  177. if (!empty($where[ 'site_ids' ])) {
  178. $condition[] = [ 'shop_site.site_id', 'in', $where[ 'site_ids' ] ];
  179. }
  180. // 参数过滤
  181. if (!empty($where[ 'order' ]) && in_array($where[ 'order' ], [ 'follow_number' ])) {
  182. $order = $where[ 'order' ] . ' desc';
  183. } else {
  184. $order = 'follow_number desc,create_time desc';
  185. }
  186. $list = $this->model
  187. ->where($condition)->withSearch([ 'category_id' ], $where)
  188. ->field($field)
  189. ->withJoin([ 'shop' => function($query) {
  190. $query->field('category_id')->with(['categoryName']);
  191. } ])
  192. ->order($order)
  193. ->limit($where[ 'num' ])
  194. ->select()->toArray();
  195. foreach ($list as $k => &$v) {
  196. $goods_list = array_values(array_filter(event('MallShopGoodsList', [ 'site_id' => $v[ 'site_id' ], 'num' => 3 ])))[0] ?? [];
  197. $v[ 'goods_list' ] = $goods_list;
  198. if (empty($goods_list)) unset($list[$k]);
  199. }
  200. return $list;
  201. }
  202. /**
  203. * 获取店铺二维码
  204. */
  205. public function getQrcode(int $site_id){
  206. $url = ( new CoreSysConfigService() )->getSceneDomain($site_id)[ 'wap_url' ];
  207. $page = 'app/pages/site/index';
  208. $data = [
  209. [
  210. 'key' => 'sid',
  211. 'value' => $site_id
  212. ]
  213. ];
  214. $dir = 'upload/qrcode/' . $site_id . '/shop';
  215. $channel = 'weapp';
  216. return qrcode($url, $page, $data, $site_id, $dir, $channel);
  217. }
  218. }