| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\controllers\v1;
- use app\constants\OptionSetting;
- use app\models\AboutArticle;
- use app\models\Address;
- use app\models\AggregateSaasQrcode;
- use app\models\Goods;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\StoreOperations;
- use app\models\User;
- use app\models\UserLockStoreLog;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\v1\GoodsListForm;
- use app\modules\client\models\v1\StoreListForm;
- use app\modules\client\models\v1\UserForm;
- use app\utils\Wechat\Wechat;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class StoreController extends BaseController
- {
- /**
- * 关键词联想
- * @return \yii\web\Response
- */
- public function actionGoodsKeyword() {
- $store_id = get_store_id();
- $keyword = get_params('keyword');
- $query = Goods::find()->where(['store_id' => $store_id]);
- if (!empty($keyword)) {
- $query->andWhere(['like', 'key_word', $keyword]);
- }
- $query->andWhere([
- 'and',
- ['!=', 'key_word', ''],
- ['!=', 'key_word', 'null']
- ]);
- $recommend_key = array_column(Json::decode(Store::findOne($store_id)->recommend_keyword) ?: [], 'value');
- $list = $query->orderBy(['virtual_sales' => SORT_DESC, 'created_at' => SORT_DESC])->select('key_word')->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'recommend_key' => $recommend_key,
- 'data' => empty($list) ? $list : array_column($list, 'key_word')
- ]);
- }
- public function actionRecommendKey() {
- return $this->asJson(
- [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => Json::decode(Store::findOne(get_store_id())->recommend_keyword)
- ]
- );
- }
- public function actionGoodsList() {
- $form = new GoodsListForm();
- $form->store_id = get_store_id();
- $form->attributes = get_params();
- $form->type = get_params('type', -1);
- return $this->asJson($form->newSearch());
- }
- /**
- * 用户收货地址列表
- * @return \yii\web\Response
- */
- public function actionUserAddress() {
- $user_id = get_saas_user_id();
- $address = Address::find()->where(['user_id' => $user_id, 'is_delete' => 0])->select('name, mobile, province, city, district, detail, latitude, longitude')->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $address
- ]);
- }
- /**
- * 店铺列表
- */
- public function actionList() {
- $form = new StoreListForm();
- $form->attributes = get_params();
- return $this->asJson($form->list());
- }
- /**
- * 店铺列表搜索配置参数
- */
- public function actionSearchParams() {
- $form = new StoreListForm();
- $form->attributes = get_params();
- return $this->asJson($form->config());
- }
- /**
- * @return \yii\web\Response
- * 获取商城信息
- */
- public function actionGetInfo() {
- $id = get_params('store_id');
- try {
- $data = Store::find()->where(['id' => $id, 'is_delete' => 0])->select('id, name, logo')->asArray()->one();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data ?: [
- 'id' => $id,
- 'name' => '',
- 'logo' => ''
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $e->getMessage(),
- 'data' => [
- 'id' => $id,
- 'name' => '',
- 'logo' => ''
- ]
- ]);
- }
- }
- /**
- * 店铺销量
- */
- public function actionSales() {
- $form = new StoreListForm();
- return $this->asJson($form->sales());
- }
- public function actionUserInfo()
- {
- $is_open_combine = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge')['value'];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'user' => get_user(),
- 'is_open_combine' => intval($is_open_combine)
- ]
- ]);
- }
- public function actionJsConfig() {
- $api = post_params('api', []);
- $url = post_params('url', []);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => Wechat::getTicketConfig($api, $url)
- ]);
- }
- public function actionSearchHotel() {
- $form = new GoodsListForm();
- $form->store_id = get_store_id();
- $form->attributes = get_params();
- return $this->asJson($form->searchHotel());
- }
- /**
- * 小程序是否开启审核
- */
- public function actionMpAudit() {
- // $dan_audit_status = 0; //
- // if (is_platform()) {
- // $audit_status = 0;
- // $is_ali_shenhe = 0;
- // $is_component_ali_shenhe = 0;
- // } else {
- // $audit_status = Option::get('is_shenhe', get_store_id(), 'wechat', 0)['value'];
- // $is_ali_shenhe = Option::get('is_ali_shenhe', get_store_id(), 'wechat', 0)['value'];
- // $is_component_ali_shenhe = Option::get('is_component_ali_shenhe', get_store_id(), 'wechat', 0)['value'];
- // }
- // if ((int)$audit_status === 0 && (int)$is_ali_shenhe !== 0) {
- // if (is_alipay_platform()) {
- // $audit_status = 1;
- // }
- // if (is_wechat_platform()) {
- // $is_ali_shenhe = 0;
- // }
- // }
- // if ((int)$audit_status !== 0 && (int)$is_ali_shenhe === 0) {
- // if (is_alipay_platform()) {
- // $audit_status = 0;
- // }
- // }
- // if ((int)$audit_status !== 0 && (int)$is_ali_shenhe !== 0) {
- // if (is_alipay_platform()) {
- // $audit_status = 1;
- // $is_ali_shenhe = 0;
- // if ((int)$is_component_ali_shenhe === 1) {
- // $audit_status = 1;
- // $is_ali_shenhe = 1;
- // }
- // }
- // if (is_wechat_platform()) {
- // $audit_status = 1;
- // $is_ali_shenhe = 0;
- // }
- // }
- $data = Store::mpAudit(get_store_id());
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- // 'data' => [
- // 'status' => $audit_status,
- // 'is_ali_shenhe' => $is_ali_shenhe,
- // 'article' => $article
- // ]
- ]);
- }
- /**
- * 根据聚合码id获取store_id
- * @return \yii\web\Response
- * @throws \yii\base\InvalidConfigException
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-02
- */
- public function actionGetStoreByAggregate()
- {
- $id = \get_params('id');
- $data = AggregateSaasQrcode::findOne(['id' => $id]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'store_id' => $data ? $data->store_id : 0,
- ],
- ]);
- }
- public function actionAllianceAppInfo()
- {
- $data = Option::get([
- 'name',
- OptionSetting::PLATFORM_APP_STORE_ANDROID,
- OptionSetting::PLATFORM_APP_STORE_IOS,
- OptionSetting::PLATFORM_STORE_APP_LOGO,
- OptionSetting::PLATFORM_STORE_APP_BG,
- ], 0, 'saas');
- $arr = [];
- foreach ($data as $item) {
- $arr[$item['name']] = $item['value'];
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $arr,
- ]);
- }
- // 获取登录信息
- public function actionStoreAppInfo() {
- try {
- $keys = [
- OptionSetting::APP_STORE_ANDROID,
- OptionSetting::APP_STORE_ANDROID_VERSION,
- OptionSetting::APP_STORE_IOS,
- OptionSetting::STORE_APP_LOGO,
- OptionSetting::STORE_APP_BG,
- ];
- $data = Option::get($keys, get_store_id(), 'app');
- $data['one_store_app_android'] = Option::get('one_store_app_android', 0, 'saas');//供应链安卓app地址
- $data['one_store_app_android_version'] = Option::get('one_store_app_android_version', 0, 'saas');//供应链安卓app版本
- if (empty($data)) {
- $data = [
- OptionSetting::APP_STORE_ANDROID => '',
- OptionSetting::APP_STORE_ANDROID_VERSION => '',
- OptionSetting::APP_STORE_IOS => '',
- OptionSetting::STORE_APP_LOGO => '',
- OptionSetting::STORE_APP_BG => '',
- 'one_store_app_android' => '',//供应链安卓app地址
- 'one_store_app_android_version' => '',//供应链安卓app版本
- ];
- } else {
- $arr = [];
- foreach ($data as $value) {
- $index = array_search($value['name'], $keys);
- unset($keys[$index]);
- $arr[$value['name']] = $value['value'];
- }
- foreach ($keys as $key) {
- $arr[$key] = '';
- }
- $data = $arr;
- }
- $store_name = Option::get(OptionSetting::STORE_NAME, get_store_id(), 'store')['value'];
- $data = array_merge($data, [
- 'store_name' => $store_name
- ]);
- //目前供应链没有自己商城app 修改升级所需参数 故替换
- if (\Yii::$app->prod_is_dandianpu()) {
- $data[OptionSetting::APP_STORE_ANDROID] = $data['one_store_app_android'];
- $data[OptionSetting::APP_STORE_ANDROID_VERSION] = $data['one_store_app_android_version'];
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //判断是单店铺且平台设置供应链为不可切换店铺时返回店铺id
- public function actionGetStoreSwitch() {
- //is_switch_store 是否需要切换店铺
- $is_switch_store = false;
- $store_id = (int)post_params('store_id');//get_store_id不行 get_params('store_id')先获取 拿到的数据不对
- $code = post_params('code', '');
- $is_dandianpu = \Yii::$app->prod_is_dandianpu();
- $token = '';
- //判断是单店铺模式
- if ($is_dandianpu) {
- //如果当前店铺是有独立小程序店铺,就不跳转
- $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
- if (!intval($self_mini)) {
- //判断平台设置是否锁定来源店铺
- $lock_store = Option::get('one_store_lock_store', 0, 'saas', 0)['value'];
- if (intval($lock_store)) {
- //获取注册时店铺id
- $saas_user = get_saas_user();
- if ($saas_user) {
- //如果是非运营人员就做判断
- $storeOperations = StoreOperations::findOne(['saas_id' => $saas_user->id, 'is_delete' => 0]);
- if (empty($storeOperations) || intval($storeOperations->status) === 0) {
- //注册店铺id与当前店铺不一致就执行
- if ($saas_user->store_id > 0 && intval($saas_user->store_id) !== $store_id) {
- //如果跳转店铺是没有独立小程序店铺,就跳转
- $self_mini = \app\models\Option::get('self_mini', $saas_user->store_id, 'store', 0)['value'];
- if (!intval($self_mini)) {
- $store_id = $saas_user->store_id;
- $is_switch_store = true;
- }
- }
- }
- } else {
- $this->addUserLockStore($code, $store_id, $is_switch_store, $token);
- }
- }
- if ($store_id <= 0) {
- $store_id = \app\models\Option::get('one_store_default_store', 0, 'saas', 1)['value'];
- }
- }
- }
- $store = Store::findOne($store_id);
- $copyright = Option::get('copyright', $store_id, 'store', '')['value'];
- $copyright = json_decode($copyright, true) ?: [
- 'type' => 1,
- 'mini_url' => [
- 'name' => '商城首页',
- 'link' => '/pages/home/home',
- 'open_type' => "switchtab",
- 'params' => []
- ],
- 'mobile' => $store_admin->mobile ?? '',
- 'logo' => $store->logo ?: (Option::get('logo', get_store_id(), 'store')['value'] ?: Option::get('web_log', get_store_id(), 'web')['value']),
- 'copyright' => $store->name ?? ''
- ];
- $copyright['type'] = intval($copyright['type']);
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'is_switch_store' => $is_switch_store,
- 'store_id' => $store_id,
- 'copyright' => $copyright,
- 'token' => $token
- ]
- ]);
- }
- //用户锁定店铺记录 //https://docs.qq.com/doc/DWUdjZGNEQmtNaHFX第八条
- public function addUserLockStore($code, &$store_id, &$is_switch_store = false, &$token = '') {
- // $is_switch_store = false;
- $user = get_user();
- if (!$user && $code) {
- //用户未登陆时 获取openid来分辨用户
- [$wechat_open_id, $ali_user_id, $ali_open_id] = (new UserForm())->getOpenId($code, [
- 'wechat_open_id' => '',
- 'ali_user_id' => '',
- 'ali_open_id' => '',
- ]);
- //如果微信openid存在
- if (!empty($wechat_open_id)) {
- //查询当前小程序的用户是否存在
- $user = User::findOne(['wechat_open_id' => $wechat_open_id, 'is_delete' => 0]);
- //查询是否存在锁定关系
- $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'wechat_open_id' => $wechat_open_id]);
- }
- //如果支付宝userid存在
- if (!empty($ali_user_id)) {
- //查询用户是否存在
- $user = User::findOne(['alipay_open_id' => $ali_user_id, 'is_delete' => 0]);
- //查询是否存在锁定关系
- $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'ali_user_id' => $ali_user_id]);
- }
- //如果支付宝openid存在
- if (!empty($ali_open_id)) {
- //查询当前小程序的用户是否存在
- $user = User::findOne(['ali_openId' => $ali_open_id, 'is_delete' => 0]);
- //查询是否存在锁定关系
- $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'ali_openId' => $ali_open_id]);
- }
- //查询到小程序用户信息 查询saas_user绑定的store_id
- if (!empty($user)) {
- //通过
- $saasUser = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- }
- if (!empty($saasUser)) {
- $lock_store_id = $saasUser->store_id;
- } else {
- if (empty($userLockStoreLog)) {
- $userLockStoreLog = new UserLockStoreLog();
- $userLockStoreLog->store_id = $store_id;
- $userLockStoreLog->wechat_open_id = $wechat_open_id;
- $userLockStoreLog->ali_user_id = $ali_user_id;
- $userLockStoreLog->ali_openId = $ali_open_id;
- if (is_wechat_platform()) {
- $userLockStoreLog->type = UserLockStoreLog::TYPE_WX;
- }
- if (is_alipay_platform()) {
- $userLockStoreLog->type = UserLockStoreLog::TYPE_ALI;
- }
- if (is_app_platform()) {
- $userLockStoreLog->type = UserLockStoreLog::TYPE_APP;
- }
- if (is_h5()) {
- $userLockStoreLog->type = UserLockStoreLog::TYPE_H5;
- }
- $userLockStoreLog->saas_id = $saasUser->id ?? 0;
- $userLockStoreLog->save();
- }
- $lock_store_id = $userLockStoreLog->store_id;
- }
- } else {
- $saasUser = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- $lock_store_id = $saasUser->store_id;
- }
- if (!empty($saasUser)) {
- $token = $saasUser->access_token;
- $storeOperations = StoreOperations::findOne(['saas_id' => $saasUser->id, 'is_delete' => 0]);
- }
- if (empty($storeOperations) || intval($storeOperations->status) === 0) {
- //注册店铺id与当前店铺不一致就执行
- if ($lock_store_id > 0 && intval($lock_store_id) !== $store_id) {
- //如果跳转店铺是没有独立小程序店铺,就允许切换店铺
- $self_mini = \app\models\Option::get('self_mini', $lock_store_id, 'store', 0)['value'];
- if (!intval($self_mini)) {
- $is_switch_store = true;
- }
- }
- }
- if ($is_switch_store) {
- $store_id = $lock_store_id;
- }
- }
- }
|