StoreController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\constants\OptionSetting;
  9. use app\models\AboutArticle;
  10. use app\models\Address;
  11. use app\models\AggregateSaasQrcode;
  12. use app\models\Goods;
  13. use app\models\Option;
  14. use app\models\SaasUser;
  15. use app\models\Store;
  16. use app\models\StoreOperations;
  17. use app\models\User;
  18. use app\models\UserLockStoreLog;
  19. use app\modules\client\controllers\BaseController;
  20. use app\modules\client\models\v1\GoodsListForm;
  21. use app\modules\client\models\v1\StoreListForm;
  22. use app\modules\client\models\v1\UserForm;
  23. use app\utils\Wechat\Wechat;
  24. use yii\base\BaseObject;
  25. use yii\helpers\Json;
  26. class StoreController extends BaseController
  27. {
  28. /**
  29. * 关键词联想
  30. * @return \yii\web\Response
  31. */
  32. public function actionGoodsKeyword() {
  33. $store_id = get_store_id();
  34. $keyword = get_params('keyword');
  35. $query = Goods::find()->where(['store_id' => $store_id]);
  36. if (!empty($keyword)) {
  37. $query->andWhere(['like', 'key_word', $keyword]);
  38. }
  39. $query->andWhere([
  40. 'and',
  41. ['!=', 'key_word', ''],
  42. ['!=', 'key_word', 'null']
  43. ]);
  44. $recommend_key = array_column(Json::decode(Store::findOne($store_id)->recommend_keyword) ?: [], 'value');
  45. $list = $query->orderBy(['virtual_sales' => SORT_DESC, 'created_at' => SORT_DESC])->select('key_word')->asArray()->all();
  46. return $this->asJson([
  47. 'code' => 0,
  48. 'msg' => 'success',
  49. 'recommend_key' => $recommend_key,
  50. 'data' => empty($list) ? $list : array_column($list, 'key_word')
  51. ]);
  52. }
  53. public function actionRecommendKey() {
  54. return $this->asJson(
  55. [
  56. 'code' => 0,
  57. 'msg' => 'success',
  58. 'data' => Json::decode(Store::findOne(get_store_id())->recommend_keyword)
  59. ]
  60. );
  61. }
  62. public function actionGoodsList() {
  63. $form = new GoodsListForm();
  64. $form->store_id = get_store_id();
  65. $form->attributes = get_params();
  66. $form->type = get_params('type', -1);
  67. return $this->asJson($form->newSearch());
  68. }
  69. /**
  70. * 用户收货地址列表
  71. * @return \yii\web\Response
  72. */
  73. public function actionUserAddress() {
  74. $user_id = get_saas_user_id();
  75. $address = Address::find()->where(['user_id' => $user_id, 'is_delete' => 0])->select('name, mobile, province, city, district, detail, latitude, longitude')->asArray()->all();
  76. return $this->asJson([
  77. 'code' => 0,
  78. 'msg' => 'success',
  79. 'data' => $address
  80. ]);
  81. }
  82. /**
  83. * 店铺列表
  84. */
  85. public function actionList() {
  86. $form = new StoreListForm();
  87. $form->attributes = get_params();
  88. return $this->asJson($form->list());
  89. }
  90. /**
  91. * 店铺列表搜索配置参数
  92. */
  93. public function actionSearchParams() {
  94. $form = new StoreListForm();
  95. $form->attributes = get_params();
  96. return $this->asJson($form->config());
  97. }
  98. /**
  99. * @return \yii\web\Response
  100. * 获取商城信息
  101. */
  102. public function actionGetInfo() {
  103. $id = get_params('store_id');
  104. try {
  105. $data = Store::find()->where(['id' => $id, 'is_delete' => 0])->select('id, name, logo')->asArray()->one();
  106. return $this->asJson([
  107. 'code' => 0,
  108. 'msg' => '获取成功',
  109. 'data' => $data ?: [
  110. 'id' => $id,
  111. 'name' => '',
  112. 'logo' => ''
  113. ]
  114. ]);
  115. } catch (\Exception $e) {
  116. return $this->asJson([
  117. 'code' => 0,
  118. 'msg' => $e->getMessage(),
  119. 'data' => [
  120. 'id' => $id,
  121. 'name' => '',
  122. 'logo' => ''
  123. ]
  124. ]);
  125. }
  126. }
  127. /**
  128. * 店铺销量
  129. */
  130. public function actionSales() {
  131. $form = new StoreListForm();
  132. return $this->asJson($form->sales());
  133. }
  134. public function actionUserInfo()
  135. {
  136. $is_open_combine = Option::get(OptionSetting::STORE_COMBINE_PAY, get_store_id(), 'recharge')['value'];
  137. return $this->asJson([
  138. 'code' => 0,
  139. 'msg' => 'success',
  140. 'data' => [
  141. 'user' => get_user(),
  142. 'is_open_combine' => intval($is_open_combine)
  143. ]
  144. ]);
  145. }
  146. public function actionJsConfig() {
  147. $api = post_params('api', []);
  148. $url = post_params('url', []);
  149. return $this->asJson([
  150. 'code' => 0,
  151. 'msg' => 'success',
  152. 'data' => Wechat::getTicketConfig($api, $url)
  153. ]);
  154. }
  155. public function actionSearchHotel() {
  156. $form = new GoodsListForm();
  157. $form->store_id = get_store_id();
  158. $form->attributes = get_params();
  159. return $this->asJson($form->searchHotel());
  160. }
  161. /**
  162. * 小程序是否开启审核
  163. */
  164. public function actionMpAudit() {
  165. // $dan_audit_status = 0; //
  166. // if (is_platform()) {
  167. // $audit_status = 0;
  168. // $is_ali_shenhe = 0;
  169. // $is_component_ali_shenhe = 0;
  170. // } else {
  171. // $audit_status = Option::get('is_shenhe', get_store_id(), 'wechat', 0)['value'];
  172. // $is_ali_shenhe = Option::get('is_ali_shenhe', get_store_id(), 'wechat', 0)['value'];
  173. // $is_component_ali_shenhe = Option::get('is_component_ali_shenhe', get_store_id(), 'wechat', 0)['value'];
  174. // }
  175. // if ((int)$audit_status === 0 && (int)$is_ali_shenhe !== 0) {
  176. // if (is_alipay_platform()) {
  177. // $audit_status = 1;
  178. // }
  179. // if (is_wechat_platform()) {
  180. // $is_ali_shenhe = 0;
  181. // }
  182. // }
  183. // if ((int)$audit_status !== 0 && (int)$is_ali_shenhe === 0) {
  184. // if (is_alipay_platform()) {
  185. // $audit_status = 0;
  186. // }
  187. // }
  188. // if ((int)$audit_status !== 0 && (int)$is_ali_shenhe !== 0) {
  189. // if (is_alipay_platform()) {
  190. // $audit_status = 1;
  191. // $is_ali_shenhe = 0;
  192. // if ((int)$is_component_ali_shenhe === 1) {
  193. // $audit_status = 1;
  194. // $is_ali_shenhe = 1;
  195. // }
  196. // }
  197. // if (is_wechat_platform()) {
  198. // $audit_status = 1;
  199. // $is_ali_shenhe = 0;
  200. // }
  201. // }
  202. $data = Store::mpAudit(get_store_id());
  203. return $this->asJson([
  204. 'code' => 0,
  205. 'msg' => 'success',
  206. 'data' => $data,
  207. // 'data' => [
  208. // 'status' => $audit_status,
  209. // 'is_ali_shenhe' => $is_ali_shenhe,
  210. // 'article' => $article
  211. // ]
  212. ]);
  213. }
  214. /**
  215. * 根据聚合码id获取store_id
  216. * @return \yii\web\Response
  217. * @throws \yii\base\InvalidConfigException
  218. * @author Syan mzsongyan@gmail.com
  219. * @date 2022-09-02
  220. */
  221. public function actionGetStoreByAggregate()
  222. {
  223. $id = \get_params('id');
  224. $data = AggregateSaasQrcode::findOne(['id' => $id]);
  225. return $this->asJson([
  226. 'code' => 0,
  227. 'msg' => 'success',
  228. 'data' => [
  229. 'store_id' => $data ? $data->store_id : 0,
  230. ],
  231. ]);
  232. }
  233. public function actionAllianceAppInfo()
  234. {
  235. $data = Option::get([
  236. 'name',
  237. OptionSetting::PLATFORM_APP_STORE_ANDROID,
  238. OptionSetting::PLATFORM_APP_STORE_IOS,
  239. OptionSetting::PLATFORM_STORE_APP_LOGO,
  240. OptionSetting::PLATFORM_STORE_APP_BG,
  241. ], 0, 'saas');
  242. $arr = [];
  243. foreach ($data as $item) {
  244. $arr[$item['name']] = $item['value'];
  245. }
  246. return $this->asJson([
  247. 'code' => 0,
  248. 'msg' => 'success',
  249. 'data' => $arr,
  250. ]);
  251. }
  252. // 获取登录信息
  253. public function actionStoreAppInfo() {
  254. try {
  255. $keys = [
  256. OptionSetting::APP_STORE_ANDROID,
  257. OptionSetting::APP_STORE_ANDROID_VERSION,
  258. OptionSetting::APP_STORE_IOS,
  259. OptionSetting::STORE_APP_LOGO,
  260. OptionSetting::STORE_APP_BG,
  261. ];
  262. $data = Option::get($keys, get_store_id(), 'app');
  263. $data['one_store_app_android'] = Option::get('one_store_app_android', 0, 'saas');//供应链安卓app地址
  264. $data['one_store_app_android_version'] = Option::get('one_store_app_android_version', 0, 'saas');//供应链安卓app版本
  265. if (empty($data)) {
  266. $data = [
  267. OptionSetting::APP_STORE_ANDROID => '',
  268. OptionSetting::APP_STORE_ANDROID_VERSION => '',
  269. OptionSetting::APP_STORE_IOS => '',
  270. OptionSetting::STORE_APP_LOGO => '',
  271. OptionSetting::STORE_APP_BG => '',
  272. 'one_store_app_android' => '',//供应链安卓app地址
  273. 'one_store_app_android_version' => '',//供应链安卓app版本
  274. ];
  275. } else {
  276. $arr = [];
  277. foreach ($data as $value) {
  278. $index = array_search($value['name'], $keys);
  279. unset($keys[$index]);
  280. $arr[$value['name']] = $value['value'];
  281. }
  282. foreach ($keys as $key) {
  283. $arr[$key] = '';
  284. }
  285. $data = $arr;
  286. }
  287. $store_name = Option::get(OptionSetting::STORE_NAME, get_store_id(), 'store')['value'];
  288. $data = array_merge($data, [
  289. 'store_name' => $store_name
  290. ]);
  291. //目前供应链没有自己商城app 修改升级所需参数 故替换
  292. if (\Yii::$app->prod_is_dandianpu()) {
  293. $data[OptionSetting::APP_STORE_ANDROID] = $data['one_store_app_android'];
  294. $data[OptionSetting::APP_STORE_ANDROID_VERSION] = $data['one_store_app_android_version'];
  295. }
  296. return $this->asJson([
  297. 'code' => 0,
  298. 'msg' => 'success',
  299. 'data' => $data,
  300. ]);
  301. } catch (\Exception $e) {
  302. return $this->asJson([
  303. 'code' => 1,
  304. 'msg' => $e->getMessage()
  305. ]);
  306. }
  307. }
  308. //判断是单店铺且平台设置供应链为不可切换店铺时返回店铺id
  309. public function actionGetStoreSwitch() {
  310. //is_switch_store 是否需要切换店铺
  311. $is_switch_store = false;
  312. $store_id = (int)post_params('store_id');//get_store_id不行 get_params('store_id')先获取 拿到的数据不对
  313. $code = post_params('code', '');
  314. $is_dandianpu = \Yii::$app->prod_is_dandianpu();
  315. $token = '';
  316. //判断是单店铺模式
  317. if ($is_dandianpu) {
  318. //如果当前店铺是有独立小程序店铺,就不跳转
  319. $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
  320. if (!intval($self_mini)) {
  321. //判断平台设置是否锁定来源店铺
  322. $lock_store = Option::get('one_store_lock_store', 0, 'saas', 0)['value'];
  323. if (intval($lock_store)) {
  324. //获取注册时店铺id
  325. $saas_user = get_saas_user();
  326. if ($saas_user) {
  327. //如果是非运营人员就做判断
  328. $storeOperations = StoreOperations::findOne(['saas_id' => $saas_user->id, 'is_delete' => 0]);
  329. if (empty($storeOperations) || intval($storeOperations->status) === 0) {
  330. //注册店铺id与当前店铺不一致就执行
  331. if ($saas_user->store_id > 0 && intval($saas_user->store_id) !== $store_id) {
  332. //如果跳转店铺是没有独立小程序店铺,就跳转
  333. $self_mini = \app\models\Option::get('self_mini', $saas_user->store_id, 'store', 0)['value'];
  334. if (!intval($self_mini)) {
  335. $store_id = $saas_user->store_id;
  336. $is_switch_store = true;
  337. }
  338. }
  339. }
  340. } else {
  341. $this->addUserLockStore($code, $store_id, $is_switch_store, $token);
  342. }
  343. }
  344. if ($store_id <= 0) {
  345. $store_id = \app\models\Option::get('one_store_default_store', 0, 'saas', 1)['value'];
  346. }
  347. }
  348. }
  349. $store = Store::findOne($store_id);
  350. $copyright = Option::get('copyright', $store_id, 'store', '')['value'];
  351. $copyright = json_decode($copyright, true) ?: [
  352. 'type' => 1,
  353. 'mini_url' => [
  354. 'name' => '商城首页',
  355. 'link' => '/pages/home/home',
  356. 'open_type' => "switchtab",
  357. 'params' => []
  358. ],
  359. 'mobile' => $store_admin->mobile ?? '',
  360. 'logo' => $store->logo ?: (Option::get('logo', get_store_id(), 'store')['value'] ?: Option::get('web_log', get_store_id(), 'web')['value']),
  361. 'copyright' => $store->name ?? ''
  362. ];
  363. $copyright['type'] = intval($copyright['type']);
  364. return $this->asJson([
  365. 'code' => 0,
  366. 'data' => [
  367. 'is_switch_store' => $is_switch_store,
  368. 'store_id' => $store_id,
  369. 'copyright' => $copyright,
  370. 'token' => $token
  371. ]
  372. ]);
  373. }
  374. //用户锁定店铺记录 //https://docs.qq.com/doc/DWUdjZGNEQmtNaHFX第八条
  375. public function addUserLockStore($code, &$store_id, &$is_switch_store = false, &$token = '') {
  376. // $is_switch_store = false;
  377. $user = get_user();
  378. if (!$user && $code) {
  379. //用户未登陆时 获取openid来分辨用户
  380. [$wechat_open_id, $ali_user_id, $ali_open_id] = (new UserForm())->getOpenId($code, [
  381. 'wechat_open_id' => '',
  382. 'ali_user_id' => '',
  383. 'ali_open_id' => '',
  384. ]);
  385. //如果微信openid存在
  386. if (!empty($wechat_open_id)) {
  387. //查询当前小程序的用户是否存在
  388. $user = User::findOne(['wechat_open_id' => $wechat_open_id, 'is_delete' => 0]);
  389. //查询是否存在锁定关系
  390. $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'wechat_open_id' => $wechat_open_id]);
  391. }
  392. //如果支付宝userid存在
  393. if (!empty($ali_user_id)) {
  394. //查询用户是否存在
  395. $user = User::findOne(['alipay_open_id' => $ali_user_id, 'is_delete' => 0]);
  396. //查询是否存在锁定关系
  397. $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'ali_user_id' => $ali_user_id]);
  398. }
  399. //如果支付宝openid存在
  400. if (!empty($ali_open_id)) {
  401. //查询当前小程序的用户是否存在
  402. $user = User::findOne(['ali_openId' => $ali_open_id, 'is_delete' => 0]);
  403. //查询是否存在锁定关系
  404. $userLockStoreLog = UserLockStoreLog::findOne(['is_delete' => 0, 'ali_openId' => $ali_open_id]);
  405. }
  406. //查询到小程序用户信息 查询saas_user绑定的store_id
  407. if (!empty($user)) {
  408. //通过
  409. $saasUser = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  410. }
  411. if (!empty($saasUser)) {
  412. $lock_store_id = $saasUser->store_id;
  413. } else {
  414. if (empty($userLockStoreLog)) {
  415. $userLockStoreLog = new UserLockStoreLog();
  416. $userLockStoreLog->store_id = $store_id;
  417. $userLockStoreLog->wechat_open_id = $wechat_open_id;
  418. $userLockStoreLog->ali_user_id = $ali_user_id;
  419. $userLockStoreLog->ali_openId = $ali_open_id;
  420. if (is_wechat_platform()) {
  421. $userLockStoreLog->type = UserLockStoreLog::TYPE_WX;
  422. }
  423. if (is_alipay_platform()) {
  424. $userLockStoreLog->type = UserLockStoreLog::TYPE_ALI;
  425. }
  426. if (is_app_platform()) {
  427. $userLockStoreLog->type = UserLockStoreLog::TYPE_APP;
  428. }
  429. if (is_h5()) {
  430. $userLockStoreLog->type = UserLockStoreLog::TYPE_H5;
  431. }
  432. $userLockStoreLog->saas_id = $saasUser->id ?? 0;
  433. $userLockStoreLog->save();
  434. }
  435. $lock_store_id = $userLockStoreLog->store_id;
  436. }
  437. } else {
  438. $saasUser = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  439. $lock_store_id = $saasUser->store_id;
  440. }
  441. if (!empty($saasUser)) {
  442. $token = $saasUser->access_token;
  443. $storeOperations = StoreOperations::findOne(['saas_id' => $saasUser->id, 'is_delete' => 0]);
  444. }
  445. if (empty($storeOperations) || intval($storeOperations->status) === 0) {
  446. //注册店铺id与当前店铺不一致就执行
  447. if ($lock_store_id > 0 && intval($lock_store_id) !== $store_id) {
  448. //如果跳转店铺是没有独立小程序店铺,就允许切换店铺
  449. $self_mini = \app\models\Option::get('self_mini', $lock_store_id, 'store', 0)['value'];
  450. if (!intval($self_mini)) {
  451. $is_switch_store = true;
  452. }
  453. }
  454. }
  455. if ($is_switch_store) {
  456. $store_id = $lock_store_id;
  457. }
  458. }
  459. }