Shop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Index.php
  4. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  5. * =========================================================
  6. * Copy right 2015-2025 山西牛酷信息科技有限公司, 保留所有权利。
  7. * ----------------------------------------------
  8. * 官方网址: http://www.niushop.com.cn
  9. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  10. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  11. * =========================================================
  12. * @author : niuteam
  13. * @date : 2015.1.17
  14. * @version : v1.0.0.0
  15. */
  16. namespace app\api\controller;
  17. use app\model\shop\Shop as ShopModel;
  18. use app\model\web\WebSite;
  19. use think\facade\Db;
  20. class Shop extends BaseApi
  21. {
  22. /**
  23. * 基础信息
  24. */
  25. public function info()
  26. {
  27. $site_id = isset($this->params['site_id']) ? $this->params['site_id'] : 0;
  28. if (empty($site_id)) {
  29. return $this->response($this->error('', 'REQUEST_SITE_ID'));
  30. }
  31. $shop = new ShopModel();
  32. $field = 'site_id,expire_time,site_name,website_id,is_own,level_name,category_name,shop_status,start_time,end_time,logo,avatar,banner,
  33. seo_description,qq,ww,telephone,shop_desccredit,shop_servicecredit,shop_deliverycredit,workingtime,shop_baozh,shop_baozhopen,shop_baozhrmb,
  34. shop_qtian,shop_zhping,shop_erxiaoshi,shop_tuihuo,shop_shiyong,shop_shiti,shop_xiaoxie,shop_free_time,shop_sales,shop_adv,work_week,address,full_address,longitude,latitude,sub_num';
  35. $info = $shop->getShopInfo([ [ 'site_id', '=', $site_id ] ], $field);
  36. $info['data']['start_time'] = time_to_date($info['data']['start_time'],'H:i');
  37. $info['data']['end_time'] = time_to_date($info['data']['end_time'],'H:i');
  38. $count_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$site_id])->count();
  39. $all_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$site_id])->sum('scores');
  40. $info['data']['evaluate'] = bcdiv($all_evaluate,$count_evaluate,1);
  41. return $this->response($info);
  42. }
  43. public function page()
  44. {
  45. $page = isset($this->params['page']) ? $this->params['page'] : 1;
  46. $page_size = isset($this->params['page_size']) ? $this->params['page_size'] : PAGE_LIST_ROWS;
  47. $keyword = isset($this->params['keyword']) ? $this->params['keyword'] : '';//关键词
  48. $order = isset($this->params['order']) ? $this->params['order'] : "site_id";//排序(综合、销量、信用)
  49. $sort = isset($this->params['sort']) ? $this->params['sort'] : "desc";//升序、降序
  50. $web_city = isset($this->params['web_city']) ? $this->params['web_city'] : "";
  51. $lat = isset($this->params['lat']) ? $this->params['lat'] : ""; // 纬度
  52. $lng = isset($this->params['lng']) ? $this->params['lng'] : ""; // 经度
  53. $is_recommend = isset($this->params['is_recommend']) ? $this->params['is_recommend'] : 0; // 是否推荐
  54. $shop = new ShopModel();
  55. $condition = [
  56. [ 'shop_status', '=', 1 ],
  57. [ 'cert_id', '<>', 0 ]
  58. ];
  59. $city_name = input('city_name/s','');
  60. if (!empty($city_name)){
  61. $condition[] = ['city_name','like','%'.$city_name.'%'];
  62. }
  63. if (!empty($keyword)) {
  64. $token = $this->checkToken();
  65. if ($this->member_id){
  66. $in_data['uid'] = $this->member_id;
  67. $in_data['keyword'] = $keyword;
  68. $is_history = Db::table('v4search_history')->where($in_data)->find();
  69. if ($is_history){
  70. Db::table('v4search_history')->where($in_data)->delete();
  71. }
  72. $in_data['create_time'] = date("Y-m-d H:i:s",time());
  73. Db::table('v4search_history')->insert($in_data);
  74. }
  75. $condition[] = [ 'site_name', 'like', '%' . $keyword . '%' ];
  76. }
  77. if (!empty($is_recommend)) {
  78. $condition[] = [ 'is_recommend', '=', $is_recommend ];
  79. }
  80. // 非法参数进行过滤
  81. if ($sort != "desc" && $sort != "asc") {
  82. $sort = "";
  83. }
  84. // 非法参数进行过滤
  85. if ($order != '') {
  86. if ($order != "shop_sales" && $order != "shop_desccredit") {
  87. $order = 'site_id';
  88. }
  89. $order_by = $order . ' ' . $sort;
  90. } else {
  91. $order_by = 'is_recommend desc,sort desc,site_id desc';
  92. }
  93. // 查询是否存在城市分站
  94. if (addon_is_exit('city') && !empty($web_city)) {
  95. $website_model = new WebSite();
  96. $website_info = $website_model->getWebSite([ [ 'site_area_id', '=', $web_city ] ], 'site_id');
  97. if (!empty($website_info['data'])) {
  98. $order_by = "INSTR('{$website_info['data']['site_id']}', website_id) desc," . $order_by;
  99. }
  100. }
  101. $list = $shop->getShopPageList($condition, $page, $page_size, $order_by, 'site_id,site_name,category_name,group_name,logo,avatar,banner,seo_description,shop_desccredit,shop_servicecredit,shop_deliverycredit,shop_sales,sub_num,is_own,longitude,latitude,telephone,address,full_address');
  102. if (!empty($list['data']['list'])) {
  103. foreach ($list['data']['list'] as $k => $item) {
  104. if ($item['longitude'] && $item['latitude'] && $lng && $lat) {
  105. $list['data']['list'][ $k ]['distance'] = round(getDistance((float) $item['longitude'], (float) $item['latitude'], (float) $lng, (float) $lat));
  106. } else {
  107. $list['data']['list'][ $k ]['distance'] = 0;
  108. }
  109. $count_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$list['data']['list'][ $k ]['site_id']])->count();
  110. $all_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$list['data']['list'][ $k ]['site_id']])->sum('scores');
  111. $list['data']['list'][ $k ]['evaluate'] = bcdiv($all_evaluate,$count_evaluate,1);
  112. $list['data']['list'][$k]['avatar'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$item['avatar'];
  113. }
  114. }
  115. return $this->response($list);
  116. }
  117. /**
  118. * 是否显示店铺相关功能,用于审核小程序
  119. */
  120. public function isShow()
  121. {
  122. $res = 1;// 0 隐藏,1 显示
  123. return $this->response($this->success($res));
  124. }
  125. /**
  126. * 活动列表
  127. * */
  128. public function activityList(){
  129. $page = isset($this->params['page']) ? $this->params['page'] : 1;
  130. $page_size = isset($this->params['page_size']) ? $this->params['page_size'] : PAGE_LIST_ROWS;
  131. $order = isset($this->params['order']) ? $this->params['order'] : "id";//排序(综合、销量、信用)
  132. $sort = isset($this->params['sort']) ? $this->params['sort'] : "desc";//升序、降序
  133. $list = Db::table('v4shop_activity')->field('id,img,title,address,time')->page($page,$page_size)->order($order.' '.$sort)->select();
  134. return $this->response($this->success($list));
  135. }
  136. /**
  137. * 活动详情
  138. * */
  139. public function activityDetails(){
  140. $id = input('id/d',0);
  141. $info = Db::table('v4shop_activity')->field('id,img,title,address,time,content')->where(['id'=>$id])->find();
  142. return $this->response($this->success($info));
  143. }
  144. /**
  145. * 店铺入驻
  146. * */
  147. public function joinShop(){
  148. $token = $this->checkToken();
  149. if ($token['code'] < 0) return $this->response($token);
  150. $uid = $this->member_id;
  151. $data['shop_name'] = input('shop_name','');
  152. $data['telephone'] = input('telephone','');
  153. $data['address'] = input('address','');
  154. $data['do_business_date'] = input('do_business_date','');
  155. $data['business_license'] = input('business_license','');
  156. $data['id_front'] = input('id_front','');
  157. $data['id_behind'] = input('id_behind','');
  158. $username = input('username','');
  159. $is_join_shop = Db::table('v4shop_apply')->where(['member_id'=>$uid])->find();
  160. if ($is_join_shop){
  161. return $this->response($this->error('','请勿重复申请'));
  162. }
  163. if (empty($data['shop_name'])){
  164. return $this->response($this->error('店铺名称不能为空'));
  165. }
  166. if (empty($data['telephone'])){
  167. return $this->response($this->error('店铺手机号不能为空'));
  168. }
  169. if (empty($username)){
  170. return $this->response($this->error('店铺账号不能为空'));
  171. }
  172. if (empty($data['address'])){
  173. return $this->response($this->error('地址不能为空'));
  174. }
  175. if (empty($data['do_business_date'])){
  176. return $this->response($this->error('营业时间不能为空'));
  177. }
  178. if (empty($data['business_license'])){
  179. return $this->response($this->error('营业执照不能为空'));
  180. }
  181. if (empty($data['id_front'])){
  182. return $this->response($this->error('身份证正面照不能为空'));
  183. }
  184. if (empty($data['id_behind'])){
  185. return $this->response($this->error('身份证反面照不能为空'));
  186. }
  187. $member_info = Db::table('v4member')->where(['member_id'=>$uid])->find();
  188. $data['member_id'] = $uid;
  189. $data['uid'] = $uid;
  190. $data['member_name'] = $member_info['nickname'];
  191. $data['create_time'] = time();
  192. $result = Db::table('v4shop_apply')->insert($data);
  193. Db::table('v4member')->where('member_id',$uid)->update(['username'=>$username]);
  194. return $this->response($this->success($result));
  195. }
  196. /**
  197. * 自动发送红包
  198. * */
  199. public function courseWatchMoney(){
  200. $standard_watch = Db::table('v4config_md')->where(['type'=>'prize','name'=>'shop_watch'])->value('value');
  201. $money = Db::table('v4config_md')->where(['type'=>'prize','name'=>'shop_watch_money'])->value('value');
  202. if ($standard_watch > 0 && $money > 0){
  203. $list = Db::table('v4curriculum')->where([['see_num','>=',$standard_watch],['uid','<>','null'],['status','=',1],['is_reward','=',0]])->select()->toArray();
  204. //echo "<pre>";print_r($list);exit;
  205. foreach ($list as $k=>$v){
  206. $user_info = Db::table('v4member')->field('username,mobile,email')->where(['member_id'=>$v['uid']])->find();
  207. Db::table('v4curriculum')->where(['id'=>$v['id']])->update(['is_reward'=>1]);
  208. Db::table('v4member')->where(['member_id'=>$v['uid']])->inc('balance',$money)->update();
  209. $log_data['member_id'] = $v['uid'];
  210. $log_data['account_type'] = 'balance';
  211. $log_data['account_data'] = $money;
  212. $log_data['from_type'] = 'curriculum';
  213. $log_data['type_name'] = '课程浏览数达标奖励';
  214. $log_data['remark'] = '课程浏览数达标奖励送'.$money.'余额';
  215. $log_data['create_time'] = time();
  216. $log_data['username'] = $user_info['username'];
  217. $log_data['mobile'] = $user_info['mobile'];
  218. $log_data['email'] = $user_info['email'];
  219. Db::table('v4member_account')->insert($log_data);
  220. }
  221. }
  222. }
  223. /**
  224. * 商家列表
  225. * */
  226. public function shopList(){
  227. $page = isset($this->params['page']) ? $this->params['page'] : 1;
  228. $page_size = isset($this->params['page_size']) ? $this->params['page_size'] : 5;
  229. $city_name = input('city_name/s','');
  230. $lng = input('longitude','');//经度
  231. $lat = input('latitude','');//纬度
  232. $min_distance = input('min_distance/d',0);
  233. $max_distance = input('max_distance/d',0);
  234. $where = [['shop_status','=',1],['city_name','like','%'.$city_name.'%']];
  235. $EARTH=6378.137; //地球半径
  236. $PI=3.1415926535898; //PI值
  237. if (!empty($min_distance) && !empty($max_distance)){
  238. $list = Db::table('v4shop')
  239. ->field("site_id,site_name,avatar,address,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$lat."-latitude)/360),2)+COS($PI*".$lat."/180)* COS(latitude * $PI/180)*POW(SIN($PI*(".$lng."-longitude)/360),2)))) as juli")
  240. ->where($where)
  241. ->having('juli >='.$min_distance)
  242. ->having('juli <='.$max_distance)
  243. ->order('juli asc')
  244. ->page($page,$page_size)->select()->toArray();
  245. }else{
  246. $list = Db::table('v4shop')
  247. ->field("site_id,site_name,avatar,address,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$lat."-latitude)/360),2)+COS($PI*".$lat."/180)* COS(latitude * $PI/180)*POW(SIN($PI*(".$lng."-longitude)/360),2)))) as juli")
  248. ->where($where)
  249. ->order('juli asc')
  250. ->page($page,$page_size)->select()->toArray();
  251. }
  252. foreach ($list as $k => &$v) {
  253. $count_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$v['site_id']])->count();
  254. $all_evaluate = Db::table('v4goods_evaluate')->where(['site_id'=>$v['site_id']])->sum('scores');
  255. $v['evaluate'] = bcdiv($all_evaluate,$count_evaluate,1);
  256. $v['avatar'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$v['avatar'];
  257. $v['good_num'] = Db::table('v4goods')->where(['site_id'=>$v['site_id'],'goods_state'=>1,'verify_state'=>1,'is_delete'=>0])->count();
  258. $v['juli'] = round($v['juli'],2);
  259. }
  260. return $this->response($this->success($list));
  261. }
  262. }