StoreOperationsForm.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\modules\alliance\models;
  3. use app\models\Admin;
  4. use app\models\District;
  5. use app\models\Option;
  6. use app\models\Store;
  7. use app\models\WechatConfig;
  8. use yii\base\Model;
  9. class StoreOperationsForm extends Model
  10. {
  11. public $saas_id;
  12. public $latitude;
  13. public $longitude;
  14. public $pageSize;
  15. public $pageNo;
  16. public $store_name;
  17. public function rules()
  18. {
  19. return [
  20. [['saas_id'], 'integer'],
  21. [['store_name'], 'string'],
  22. [['latitude', 'longitude', 'pageSize', 'offset'], 'number']
  23. ];
  24. }
  25. public function storeList() {
  26. $latitude = $this->latitude ?: 1;
  27. $longitude = $this->longitude ?: 1;
  28. $store_name = $this->store_name;
  29. $pageSize = $this->pageSize;
  30. $pageNo = $this->pageNo;
  31. $store_form = Store::tableName();
  32. $admin_form = Admin::tableName();
  33. $sql = "SELECT s.id, s.name, s.logo, s.address, s.province_id, s.city_id, s.district_id, s.contact_tel, s.latitude, s.longitude, s.business_model, acos(cos({$latitude}*pi()/180 )*cos(s.latitude*pi()/180)*cos({$longitude}*pi()/180 -s.longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(s.latitude*pi()/180))*6370996.81 as
  34. distance FROM {$store_form} s LEFT JOIN {$admin_form} as a ON s.id = a.type_id AND a.type = 'store' WHERE `a`.`is_delete` = 0 AND `s`.`is_delete` = 0 ";
  35. if ($store_name) {
  36. $sql .= ' AND `s`.`name` LIKE "%'.$store_name.'%"';
  37. }
  38. $sql .= ' ORDER BY distance ASC';
  39. $count = \Yii::$app->db->createCommand($sql)->query()->count();
  40. $offset = ($pageNo - 1) * $pageSize;
  41. $sql .= " LIMIT {$pageSize} OFFSET {$offset}";
  42. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  43. foreach ($list as &$item) {
  44. $item['province'] = District::findOne($item['province_id'])->name ?: '';
  45. $item['city'] = District::findOne($item['city_id'])->name ?: '';
  46. $item['district'] = District::findOne($item['district_id'])->name ?: '';
  47. $item['address'] = $item['province'] . $item['city'] . $item['district'] . $item['address'] ?: '暂未设置';
  48. $item['contact_tel'] = $item['contact_tel'] ?: '暂未设置';
  49. $item['distance'] = self::distance($item['distance']);
  50. $item['path'] = "pages/home/home?store_id={$item['id']}";
  51. $is_dandianpu = \Yii::$app->prod_is_dandianpu();
  52. //判断是单店铺模式
  53. if ($is_dandianpu) {
  54. $item['appid'] = Option::get('one_store_wechat_appid', 0, 'saas')['value'];
  55. //如果跳转店铺是没有独立小程序店铺,就跳转
  56. $self_mini = \app\models\Option::get('self_mini', $item['id'], 'store', 0)['value'];
  57. if (intval($self_mini)) {
  58. $item['appid'] = WechatConfig::findOne(['store_id' => $item['id'], 'type' => 1])->app_id;
  59. }
  60. } else {
  61. $item['appid'] = Option::get('platform_appid', 0, 'saas')['value'];
  62. if (intval($item['business_model']) === 1) {
  63. $wechatConfig = WechatConfig::findOne(['store_id' => $item['id'], 'type' => 1]);
  64. if ($wechatConfig) {
  65. $item['appid'] = $wechatConfig->app_id;
  66. }
  67. } elseif (intval($item['business_model']) === 2) {
  68. $item['path'] = "pages/shop/shopIndex?store_id={$item['id']}";
  69. } elseif (intval($item['business_model']) === 3) {
  70. $item['path'] = "pages/shop/shopDetail?store_id={$item['id']}";
  71. } elseif (intval($item['business_model']) === 4) {
  72. $item['path'] = "alipay-order/orderMeal/orderMeal?store_id={$item['id']}";
  73. }
  74. }
  75. }
  76. return [
  77. 'code' => 0,
  78. 'data' => [
  79. 'totalCount' => (int)$count,
  80. 'list' => $list,
  81. 'pageNo' => (int)$pageNo,
  82. 'pageSize' => (int)$pageSize,
  83. ]
  84. ];
  85. }
  86. public static function distance($distance)
  87. {
  88. if ($distance == -1) {
  89. return -1;
  90. }
  91. if ($distance > 1000) {
  92. $distance = round($distance / 1000, 2) . 'km';
  93. } else {
  94. $distance = round($distance, 2);
  95. $distance .= 'm';
  96. }
  97. return $distance;
  98. }
  99. }