| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\modules\alliance\models;
- use app\models\Admin;
- use app\models\District;
- use app\models\Option;
- use app\models\Store;
- use app\models\WechatConfig;
- use yii\base\Model;
- class StoreOperationsForm extends Model
- {
- public $saas_id;
- public $latitude;
- public $longitude;
- public $pageSize;
- public $pageNo;
- public $store_name;
- public function rules()
- {
- return [
- [['saas_id'], 'integer'],
- [['store_name'], 'string'],
- [['latitude', 'longitude', 'pageSize', 'offset'], 'number']
- ];
- }
- public function storeList() {
- $latitude = $this->latitude ?: 1;
- $longitude = $this->longitude ?: 1;
- $store_name = $this->store_name;
- $pageSize = $this->pageSize;
- $pageNo = $this->pageNo;
- $store_form = Store::tableName();
- $admin_form = Admin::tableName();
- $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
- 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 ";
- if ($store_name) {
- $sql .= ' AND `s`.`name` LIKE "%'.$store_name.'%"';
- }
- $sql .= ' ORDER BY distance ASC';
- $count = \Yii::$app->db->createCommand($sql)->query()->count();
- $offset = ($pageNo - 1) * $pageSize;
- $sql .= " LIMIT {$pageSize} OFFSET {$offset}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- foreach ($list as &$item) {
- $item['province'] = District::findOne($item['province_id'])->name ?: '';
- $item['city'] = District::findOne($item['city_id'])->name ?: '';
- $item['district'] = District::findOne($item['district_id'])->name ?: '';
- $item['address'] = $item['province'] . $item['city'] . $item['district'] . $item['address'] ?: '暂未设置';
- $item['contact_tel'] = $item['contact_tel'] ?: '暂未设置';
- $item['distance'] = self::distance($item['distance']);
- $item['path'] = "pages/home/home?store_id={$item['id']}";
- $is_dandianpu = \Yii::$app->prod_is_dandianpu();
- //判断是单店铺模式
- if ($is_dandianpu) {
- $item['appid'] = Option::get('one_store_wechat_appid', 0, 'saas')['value'];
- //如果跳转店铺是没有独立小程序店铺,就跳转
- $self_mini = \app\models\Option::get('self_mini', $item['id'], 'store', 0)['value'];
- if (intval($self_mini)) {
- $item['appid'] = WechatConfig::findOne(['store_id' => $item['id'], 'type' => 1])->app_id;
- }
- } else {
- $item['appid'] = Option::get('platform_appid', 0, 'saas')['value'];
- if (intval($item['business_model']) === 1) {
- $wechatConfig = WechatConfig::findOne(['store_id' => $item['id'], 'type' => 1]);
- if ($wechatConfig) {
- $item['appid'] = $wechatConfig->app_id;
- }
- } elseif (intval($item['business_model']) === 2) {
- $item['path'] = "pages/shop/shopIndex?store_id={$item['id']}";
- } elseif (intval($item['business_model']) === 3) {
- $item['path'] = "pages/shop/shopDetail?store_id={$item['id']}";
- } elseif (intval($item['business_model']) === 4) {
- $item['path'] = "alipay-order/orderMeal/orderMeal?store_id={$item['id']}";
- }
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'totalCount' => (int)$count,
- 'list' => $list,
- 'pageNo' => (int)$pageNo,
- 'pageSize' => (int)$pageSize,
- ]
- ];
- }
- public static function distance($distance)
- {
- if ($distance == -1) {
- return -1;
- }
- if ($distance > 1000) {
- $distance = round($distance / 1000, 2) . 'km';
- } else {
- $distance = round($distance, 2);
- $distance .= 'm';
- }
- return $distance;
- }
- }
|