Shopnearby.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 附件店铺控制器
  10. */
  11. class Shopnearby extends MobileMall
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. }
  17. /**
  18. * @api {POST} api/Shopnearby/index 首页显示
  19. * @apiVersion 1.0.0
  20. * @apiGroup Shopnearby
  21. *
  22. * @apiHeader {String} X-DS-KEY 用户授权token
  23. *
  24. * @apiParam {Int} brand 所属品牌id
  25. * @apiParam {Int} category 所属分类id
  26. * @apiParam {String} keyword 关键字
  27. * @apiParam {String} longitude 经度
  28. * @apiParam {String} latitude 纬度
  29. * @apiParam {String} sort_key 键
  30. * @apiParam {String} sort_value 值
  31. * @apiParam {Int} page 当前第几页
  32. * @apiParam {Int} per_page 每页多少
  33. *
  34. * @apiSuccess {String} code 返回码,10000为成功
  35. * @apiSuccess {String} message 返回消息
  36. * @apiSuccess {Object} result 返回数据
  37. * @apiSuccess {Object[]} result.store_list 店铺列表 (返回字段参考store)
  38. * @apiSuccess {Float} result.store_list.distance 距离
  39. * @apiSuccess {Float} result.store_list.store_credit_percent 店铺信用评分
  40. * @apiSuccess {Int} result.page_total 总页数
  41. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  42. */
  43. public function index()
  44. {
  45. $this->_get_Own_Store_List();
  46. }
  47. private function _get_Own_Store_List()
  48. {
  49. //查询条件
  50. $condition = array();
  51. $condition[] = array('store_state', '=', 1);
  52. if (!empty(input('post.keyword'))) {
  53. $condition[] = array('store_name', 'like', '%' . input('post.keyword') . '%');
  54. }
  55. $storeclass_id = intval(input('param.storeclass_id'));
  56. if ($storeclass_id > 0) {
  57. $condition[] = array('storeclass_id', '=', $storeclass_id);
  58. }
  59. $lat = input('post.latitude', 0);
  60. $lng = input('post.longitude', 0);
  61. if (!is_numeric($lat) || !is_numeric($lng)) {
  62. ds_json_encode(10001, lang('param_error'));
  63. }
  64. $sort_key = input('post.sort_key');
  65. switch ($sort_key) {
  66. case 'sale':
  67. $order = 'store_sales desc';
  68. break;
  69. case 'distance':
  70. $order = 'distance asc';
  71. break;
  72. case 'score':
  73. $order = 'store_credit desc';
  74. break;
  75. default:
  76. $order = 'is_platform_store desc,store_recommend desc,store_sort asc,store_baozh desc,store_credit desc,store_desccredit desc';
  77. }
  78. $evaluatestore_model = model('evaluatestore');
  79. $store_list_object = Db::name('store')->where($condition)
  80. ->where('(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(' . $lat . '-store_latitude)/360),2)+COS(PI()*' . $lat . '/180)* COS(store_latitude * PI()/180)*POW(SIN(PI()*(' . $lng . '-store_longitude)/360),2)))) < 100000')
  81. ->fieldRaw('store_id,is_platform_store,store_name,area_info,store_address,storeclass_id,store_collect,store_logo,store_avatar,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(' . $lat . '-store_latitude)/360),2)+COS(PI()*' . $lat . '/180)* COS(store_latitude * PI()/180)*POW(SIN(PI()*(' . $lng . '-store_longitude)/360),2)))) as distance')
  82. ->order($order)
  83. ->paginate(['list_rows' => $this->pagesize, 'query' => request()->param()], false);
  84. $store_list = $store_list_object->items();
  85. $memberId = $this->getMemberIdIfExists();
  86. foreach ($store_list as $key => $value) {
  87. $store_list[$key]['distance'] = round($value['distance'], 2);
  88. $store_list[$key]['store_avatar'] = get_store_logo($value['store_avatar'], 'store_avatar');
  89. $store_list[$key]['store_logo'] = get_store_logo($value['store_logo']);
  90. $store_evaluate_info = $evaluatestore_model->getEvaluatestoreInfoByStoreID($value['store_id'], $value['storeclass_id']);
  91. $store_list[$key]['store_credit_percent'] = $store_evaluate_info['store_credit_percent'];
  92. $store_list[$key]['goods_list'] = model('goods')->getGoodsListByColorDistinct(array(array('store_id', '=', $value['store_id']), array('goods_commend', '=', 1)), 'goods_image,goods_id,goods_price', 'goods_id desc', 0, 4);
  93. foreach ($store_list[$key]['goods_list'] as $k => $v) {
  94. $store_list[$key]['goods_list'][$k]['goods_image_url'] = goods_cthumb($v['goods_image'], 480, $value['store_id']);
  95. }
  96. // 如果已登录 判断该店铺是否已被收藏
  97. if ($memberId) {
  98. $c = (int) model('favorites')->getStoreFavoritesCountByStoreId($value['store_id'], $memberId);
  99. $store_list[$key]['is_favorate'] = $c > 0;
  100. } else {
  101. $store_list[$key]['is_favorate'] = false;
  102. }
  103. }
  104. $result = array_merge(array('store_list' => $store_list), mobile_page($store_list_object));
  105. ds_json_encode(10000, '', $result);
  106. }
  107. }