StoreAdminController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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\jobs\StoreSyncJob;
  9. use app\models\Admin;
  10. use app\models\StoreAdmin;
  11. use app\modules\admin\models\AdminForm;
  12. use app\modules\admin\models\OrderRefundForm;
  13. use app\modules\admin\models\OrderSendForm;
  14. use app\models\StoreTemplate;
  15. use app\models\SaasCategory;
  16. use app\models\StoreCash;
  17. use app\modules\client\behaviors\StoreAdminLogin;
  18. use app\modules\client\controllers\BaseController;
  19. use app\modules\client\models\v1\ShareQrcodeForm;
  20. use app\modules\client\models\v1\store\StoreAdminCommentForm;
  21. use app\modules\client\models\v1\store\StoreAdminForm;
  22. use app\modules\client\models\v1\store\StoreAdminGoodsForm;
  23. use app\modules\client\models\v1\store\StoreAdminOrderForm;
  24. use yii\helpers\ArrayHelper;
  25. use app\modules\admin\models\MchForm;
  26. class StoreAdminController extends BaseController
  27. {
  28. public $mch = null;
  29. /**
  30. * @return array
  31. */
  32. public function behaviors()
  33. {
  34. $merge = [
  35. [
  36. 'class' => StoreAdminLogin::class,
  37. ]
  38. ];
  39. return ArrayHelper::merge(parent::behaviors(), $merge);
  40. }
  41. //手机端管理员登录
  42. public function actionLogin() {
  43. $model = new AdminForm();
  44. $model->attributes = post_params();
  45. $model->validatePassword('password');
  46. if ($model->login()) {
  47. try {
  48. $admin = $model->getAdmin();
  49. if ($admin->type !== Admin::ADMIN_TYPE_MINI_ADMIN) {
  50. throw new \Exception('账户类型错误');
  51. }
  52. $store_admin = StoreAdmin::findOne(['id' => $admin->type_id, 'is_delete' => 0, 'status' => 1, 'store_id' => get_store_id()]);
  53. if (empty($store_admin)) {
  54. throw new \Exception('账户信息错误,或被禁用');
  55. }
  56. $token = $this->setToken($store_admin->id);
  57. return $this->asJson([
  58. 'code' => 0,
  59. 'msg' => '登录成功',
  60. 'data' => [
  61. 'Authorization' => $token
  62. ]
  63. ]);
  64. } catch (\Throwable $throwable) {
  65. return $this->asJson([
  66. 'code' => 1,
  67. 'msg' => $throwable->getMessage(),
  68. ]);
  69. }
  70. }
  71. return $this->asJson([
  72. 'code' => 1,
  73. 'msg' => $model->getFirstErrors()['password']
  74. ]);
  75. }
  76. /**
  77. * 商品
  78. **/
  79. //管理中心
  80. public function actionStoreInfo(){
  81. $form = new StoreAdminForm(['mch_id' => $this->mch['id']]);
  82. $form->wechat = $this->wechat;
  83. $res = $form->storeInfo();
  84. return $this->asJson($res);
  85. }
  86. //店铺数据
  87. public function actionShopData()
  88. {
  89. $form = new StoreAdminForm(['mch_id' => $this->mch['id']]);
  90. $form->start_date = input_params('start_date');
  91. $form->end_date = input_params('end_date');
  92. $form->wechat = $this->wechat;
  93. $form->tab = input_params('tab');
  94. $form->type = input_params('type');
  95. $res = $form->shopData();
  96. return $this->asJson($res);
  97. }
  98. //修改获取商城数据
  99. public function actionGetStoreInfo()
  100. {
  101. $form = new StoreAdminForm();
  102. $form->logo = post_params('logo');
  103. $form->name = post_params('name');
  104. $form->coordinate = post_params('coordinate');
  105. $form->address = post_params('address');
  106. $res = $form->getStoreInfo();
  107. return $this->asJson($res);
  108. }
  109. //获取入驻商数据
  110. public function actionMchInfo()
  111. {
  112. $mch_id = $this->mch['id'];
  113. $form = new MchForm();
  114. $form->attributes = all_params();
  115. $form->mch_id = $mch_id;
  116. $result = $form->mchInfo();
  117. return $this->asJson($result);
  118. }
  119. //修改入驻商数据
  120. public function actionMchInfoSave()
  121. {
  122. $mch_id = $this->mch['id'];
  123. $form = new MchForm();
  124. $form->attributes = all_params();
  125. $form->id = $mch_id;
  126. $result = $form->mchSave();
  127. return $this->asJson($result);
  128. }
  129. public function actionMchQrcode()
  130. {
  131. $mch_id = $this->mch['id'];
  132. $form = new ShareQrcodeForm();
  133. $form->store_id = get_store_id();
  134. $form->type = \app\models\Qrcode::TYPE_MCH;
  135. $form->user = get_user();
  136. $form->user_id = get_user_id();
  137. $form->mch_id = $mch_id;
  138. return $this->asJson($form->search());
  139. }
  140. /**
  141. * 提现
  142. */
  143. public function actionMchCashSubmit() {
  144. $price = input_params('cash');
  145. $type = input_params('type', 0);
  146. $name = input_params('name', '');
  147. $account = input_params('account', '');
  148. $bank = input_params('bank', '');
  149. $mch_id = $this->mch['id'];
  150. $form = new MchForm();
  151. $form->attributes = all_params();
  152. $form->mch_id = $mch_id;
  153. $result = $form->mchCashSubmit($price, $type, get_user_id(), $name, $account, $bank);
  154. return $this->asJson($result);
  155. }
  156. //店铺数据
  157. public function actionGetYesData()
  158. {
  159. $form = new StoreAdminForm(['mch_id' => $this->mch['id']]);
  160. $form->start_date = input_params('start_date');
  161. $form->end_date = input_params('end_date');
  162. $form->wechat = $this->wechat;
  163. $res = $form->getYesData();
  164. return $this->asJson($res);
  165. }
  166. //商品列表
  167. public function actionGoodsList(){
  168. $form = new StoreAdminGoodsForm(['mch_id' => $this->mch['id']]);
  169. $form->status = get_params('status',1);
  170. $form->sort = get_params('sort');
  171. $res = $form->goodsList();
  172. return $this->asJson($res);
  173. }
  174. //修改标题
  175. public function actionSetGoodsName(){
  176. $form = new StoreAdminGoodsForm();
  177. $form->id = post_params('id');
  178. $form->goods_name = post_params('name');
  179. $res = $form->setGoodsName();
  180. return $this->asJson($res);
  181. }
  182. //删除商品
  183. public function actionGoodsDel(){
  184. $form = new StoreAdminGoodsForm();
  185. $form->id = post_params('id');
  186. $res = $form->goodsDel();
  187. return $this->asJson($res);
  188. }
  189. //分享商品
  190. public function actionShareGoods(){
  191. $form = new ShareQrcodeForm();
  192. $form->goods_id = post_params('id');
  193. $form->store_id = get_store_id();
  194. $form->type = 0;
  195. $form->user = get_user();
  196. $form->user_id = get_user_id();
  197. return $this->asJson($form->search());
  198. }
  199. //修改上下架状态
  200. public function actionSetGoodsStatus(){
  201. $form = new StoreAdminGoodsForm();
  202. $form->id = post_params('id');
  203. $form->status = post_params('status');
  204. $res = $form->setGoodsStatus();
  205. return $this->asJson($res);
  206. }
  207. //修改价格库存
  208. public function actionSetGoodsPrice(){
  209. $form = new StoreAdminGoodsForm();
  210. $form->id = post_params('id');
  211. $form->goods_price = post_params('goods_price', 0);
  212. $form->goods_num = post_params('goods_num', 0);
  213. $form->attr = post_params('attr', 0);
  214. $res = $form->setPrice();
  215. return $this->asJson($res);
  216. }
  217. /**
  218. * 批量修改价格
  219. */
  220. public function actionUpPriceBatch()
  221. {
  222. $goods_id = input_params('goods_id');
  223. $num = input_params('num');
  224. $form = new \app\modules\admin\models\GoodsForm();
  225. $form->store_id = get_store_id();
  226. $res = $form->upPriceBatch($goods_id, $num);
  227. return $this->asJson($res);
  228. }
  229. //获取修改商品数据
  230. public function actionGetGoodsEdit(){
  231. $form = new StoreAdminGoodsForm();
  232. $form->id = get_params('id');
  233. $res = $form->getGoodsEdit();
  234. return $this->asJson($res);
  235. }
  236. //修改订单价格
  237. public function actionModifyPrice()
  238. {
  239. $form = new StoreAdminOrderForm();
  240. $form->price = post_params("price");
  241. $form->id = post_params("id");
  242. $res = $form->ModifyPrice();
  243. return $this->asJson($res);
  244. }
  245. //修改运费价格
  246. public function actionExpressPrice()
  247. {
  248. $form = new StoreAdminOrderForm();
  249. $form->express_price = post_params("express_price");
  250. $form->id = post_params("id");
  251. $res = $form->ExpressPrice();
  252. return $this->asJson($res);
  253. }
  254. //获取运费规则
  255. public function actionGetPostageData(){
  256. $form = new StoreAdminGoodsForm();
  257. $res = $form->getPostageData();
  258. return $this->asJson($res);
  259. }
  260. //保存商品
  261. public function actionSaveGoods(){
  262. $form = new StoreAdminGoodsForm(['mch_id' => $this->mch['id']]);
  263. $form->goods_info = post_params('goods_info');
  264. $res = $form->saveGoods();
  265. return $this->asJson($res);
  266. }
  267. //商品详情
  268. public function actionGoodsDetail(){
  269. $form = new StoreAdminGoodsForm();
  270. $form->id = post_params('id');
  271. $res = $form->goodsDetail();
  272. return $this->asJson($res);
  273. }
  274. //获取商品规格信息
  275. public function actionGetGoodsAttr(){
  276. $form = new StoreAdminGoodsForm();
  277. $form->id = post_params('id');
  278. $res = $form->getGoodsAttr();
  279. return $this->asJson($res);
  280. }
  281. //分类列表
  282. public function actionCatList() {
  283. $form = new StoreAdminGoodsForm(['mch_id' => $this->mch['id']]);
  284. $res = $form->catList();
  285. return $this->asJson($res);
  286. }
  287. //分类添加保存
  288. public function actionCatSave() {
  289. $form = new StoreAdminGoodsForm(['mch_id' => $this->mch['id']]);
  290. $form->cat_name = post_params("name");
  291. $form->id = post_params("id");
  292. $form->parent_id = post_params("parent_id", 0);
  293. $form->pic_url = post_params("pic_url");
  294. $form->status = post_params("is_show", 1);
  295. $res = $form->catAdd();
  296. return $this->asJson($res);
  297. }
  298. //分类修改状态
  299. public function actionSetCatStatus() {
  300. $form = new StoreAdminGoodsForm(['mch_id' => $this->mch['id']]);
  301. $form->id = post_params("id");
  302. $form->status = post_params("status", 0);
  303. $res = $form->setCatStatus();
  304. return $this->asJson($res);
  305. }
  306. /**
  307. * 订单
  308. */
  309. //订单列表
  310. public function actionOrderList() {
  311. $form = new StoreAdminOrderForm(['mch_id' => $this->mch['id']]);
  312. $form->status = get_params("status");
  313. $form->date_range = (empty(get_params("begin_time")) || empty(get_params("end_time")))?false:[
  314. 'begin_time' => get_params("begin_time"),
  315. 'end_time' => get_params("end_time")
  316. ];
  317. $res = $form->orderList();
  318. return $this->asJson($res);
  319. }
  320. //订单商品评价
  321. public function actionOrderCommentGoodsList() {
  322. $form = new StoreAdminCommentForm(['mch_id' => $this->mch['id']]);
  323. $form->goods_name = get_params("goods_name");
  324. $form->date_range = (empty(get_params("begin_time")) || empty(get_params("end_time")))?false:[
  325. 'begin_time' => get_params("begin_time"),
  326. 'end_time' => get_params("end_time")
  327. ];
  328. $res = $form->commentGoodsList();
  329. return $this->asJson($res);
  330. }
  331. //订单评价
  332. public function actionOrderCommentList() {
  333. $form = new StoreAdminCommentForm();
  334. $form->goods_name = get_params("goods_name");
  335. $form->goods_id = get_params("goods_id");
  336. $form->status = get_params("status");
  337. $form->type = get_params("type");
  338. $res = $form->commentList();
  339. return $this->asJson($res);
  340. }
  341. //删除评论
  342. public function actionCommentDel() {
  343. $form = new StoreAdminCommentForm();
  344. $form->id = post_params("id");
  345. $res = $form->commentDel();
  346. return $this->asJson($res);
  347. }
  348. //回复评论
  349. public function actionCommentReply() {
  350. $form = new StoreAdminCommentForm();
  351. $form->id = post_params("id");
  352. $form->reply_content = post_params("reply_content");
  353. $res = $form->commentReply();
  354. return $this->asJson($res);
  355. }
  356. //取消订单
  357. public function actionOrderCancel() {
  358. $form = new StoreAdminOrderForm();
  359. $form->remark = post_params("remark");
  360. $form->id = post_params("id");
  361. $res = $form->orderCancel();
  362. return $this->asJson($res);
  363. }
  364. //订单发货
  365. public function actionOrderSend() {
  366. $form = new OrderSendForm();
  367. $post = post_params();
  368. if (($post['is_express'] == OrderSendForm::EXPRESS_TYPE_EXPRESS)) {
  369. $form->scenario = 'EXPRESS';
  370. }elseif ($post['is_express'] == OrderSendForm::EXPRESS_TYPE_DADA){
  371. $form->scenario = 'DADA';
  372. }elseif ($post['is_express'] == OrderSendForm::EXPRESS_TYPE_UU){
  373. $form->scenario = 'UU';
  374. }elseif ($post['is_express'] == OrderSendForm::EXPRESS_TYPE_PEISONG){
  375. $form->scenario = 'PEISONG';
  376. }
  377. $form->attributes = $post;
  378. $form->store_id = get_store_id();
  379. return $this->asJson($form->save());
  380. }
  381. //售后订单
  382. public function actionOrderRefund() {
  383. $form = new StoreAdminOrderForm(['mch_id' => $this->mch['id']]);
  384. $form->status = get_params("status", 0);
  385. $form->date_range = (empty(get_params("begin_time")) || empty(get_params("end_time")))?false:[
  386. 'begin_time' => get_params("begin_time"),
  387. 'end_time' => get_params("end_time")
  388. ];
  389. $res = $form->orderRefund();
  390. return $this->asJson($res);
  391. }
  392. //订单详情
  393. public function actionOrderDetail() {
  394. $form = new StoreAdminOrderForm();
  395. $form->id = post_params("id");
  396. $form->type = post_params("type", 0);
  397. $res = $form->orderDetail();
  398. return $this->asJson($res);
  399. }
  400. //获取快递公司数据
  401. public function actionGetExpress(){
  402. $form = new StoreAdminOrderForm();
  403. $res = $form->express();
  404. return $this->asJson($res);
  405. }
  406. //售后订单处理
  407. public function actionOrderRefundApply() {
  408. $form = new OrderRefundForm();
  409. $form->order_refund_id = post_params("id");
  410. $form->store_id = get_store_id();
  411. $form->action = post_params("action", 0);
  412. $form->type = post_params("type", 1);
  413. $form->refund = post_params("refund", 0);
  414. $form->address_id = post_params("address_id", 0);
  415. $res = $form->save();
  416. return $this->asJson($res);
  417. }
  418. //售后地址添加
  419. public function actionRefundAddressSave() {
  420. $form = new StoreAdminOrderForm();
  421. $form->id = post_params("id");
  422. $form->mch_name = post_params("name");
  423. $form->mch_mobile = post_params("mobile");
  424. $form->mch_address = post_params("address");
  425. $form->is_default = post_params("is_default", 0);
  426. $res = $form->refundAddressSave();
  427. return $this->asJson($res);
  428. }
  429. //商家收货地址列表
  430. public function actionRefundAddress() {
  431. $form = new StoreAdminOrderForm();
  432. $res = $form->refundAddress();
  433. return $this->asJson($res);
  434. }
  435. //商家收货地址删除
  436. public function actionRefundAddressDel() {
  437. $form = new StoreAdminOrderForm();
  438. $form->id = post_params("id");
  439. $res = $form->refundAddressDel();
  440. return $this->asJson($res);
  441. }
  442. //商家收货地址设置默认
  443. public function actionRefundAddressDefault(){
  444. $form = new StoreAdminOrderForm();
  445. $form->id = post_params("id");
  446. $res = $form->refundAddressDefault();
  447. return $this->asJson($res);
  448. }
  449. //设置订单中的收货地址
  450. public function actionSetRefundAddress(){
  451. $form = new StoreAdminOrderForm();
  452. $form->id = post_params("id");
  453. $form->mch_address = post_params("address_id");
  454. $res = $form->setRefundAddress();
  455. return $this->asJson($res);
  456. }
  457. //获取单个收货地址
  458. public function actionGetAddressInfo(){
  459. $form = new StoreAdminOrderForm();
  460. $form->id = post_params("id");
  461. $res = $form->getAddressInfo();
  462. return $this->asJson($res);
  463. }
  464. public function actionTemplateList()
  465. {
  466. $name = input_params('name', '');
  467. $category_id = input_params('category_id', 0);
  468. $cond = ['is_delete' => 0];
  469. $name && $cond['name'] = $name;
  470. $category_id && $cond['category_id'] = $category_id;
  471. $query = StoreTemplate::find()->where($cond);
  472. $pagination = pagination_make($query);
  473. $list = $pagination['list'];
  474. foreach($list as &$item){
  475. $item['cat_name'] = '-';
  476. if ($item['category_id'] > 0) {
  477. $cat = SaasCategory::findOne($item['category_id']);
  478. if ($cat) {
  479. $item['cat_name'] = $cat->name;
  480. }
  481. }
  482. $item['home_pic'] = json_decode($item['home_pic'], true);
  483. $item['center_pic'] = json_decode($item['center_pic'], true);
  484. }
  485. return $this->asJson([
  486. 'code' => 0,
  487. 'msg' => 'success',
  488. 'data' => [
  489. 'data' => $list,
  490. 'pageNo' => $pagination['pageNo'],
  491. 'totalCount' => $pagination['totalCount'],
  492. ],
  493. ]);
  494. }
  495. public function actionTemplateInfo()
  496. {
  497. $store_id = get_store_id();
  498. $id = input_params('id', 0);
  499. $model = StoreTemplate::findOne($id);
  500. $store_template_used = \app\models\Option::get('store_template_used', $store_id, 'saas', 0)['value'];
  501. $used = $store_template_used == $model->id ? 1 : 0;
  502. return $this->asJson([
  503. 'code' => 0,
  504. 'msg' => 'success',
  505. 'data' => $model,
  506. 'used' => $used,
  507. ]);
  508. }
  509. public function actionTemplateUsed()
  510. {
  511. $store_id = get_store_id();
  512. $id = input_params('id', 0);
  513. $type = input_params('type', ['home']);//home/category/product/center/all
  514. $model = StoreTemplate::findOne($id);
  515. $useTemp = $model->useTemp($store_id, $type);
  516. if($useTemp['code'] != 0){
  517. return $useTemp;
  518. }
  519. if(in_array('all', $type)){
  520. \app\models\Option::set('store_template_used', $id, $store_id, 'saas');
  521. $model->use_count++;
  522. $model->save();
  523. }
  524. return $this->asJson([
  525. 'code' => 0,
  526. 'msg' => 'success',
  527. 'data' => $model,
  528. 'copy' => $useTemp,
  529. ]);
  530. }
  531. public function actionStoreCopyStatus()
  532. {
  533. $id = input_params('id', 0);
  534. $res = \app\models\StoreCopy::doCopyStatus($id);
  535. return $this->asJson($res);
  536. }
  537. //店铺初始化
  538. public function actionStoreInit()
  539. {
  540. $id = input_params("id", 0);
  541. $default = \app\models\Option::get('store_template_default', 0, 'saas', null)['value'];
  542. if($default){
  543. $default = json_decode($default, true);
  544. }else{
  545. return $this->asJson([
  546. 'code' => 0,
  547. 'msg' => '暂无默认模板',
  548. ]);
  549. }
  550. $id = \queue_push(new StoreSyncJob(['type' => 1, 'to_id' => $id]), 0, 1);
  551. //$res = \app\models\StoreCopy::doCopy($default['store_id'], $id, $default['type']);
  552. return $this->asJson([
  553. 'code' => 0,
  554. 'msg' => '操作成功'
  555. ]);
  556. }
  557. //生成token
  558. public function setToken($id) {
  559. $store_admin = StoreAdmin::findOne($id);
  560. $admin = Admin::findOne(['type' => Admin::ADMIN_TYPE_MINI_ADMIN, 'type_id' => $id, 'is_delete' => 0]);
  561. $time = time();
  562. $payload = [
  563. 'iss' => \Yii::$app->request->hostInfo,
  564. 'iat' => $time,
  565. 'exp' => $time + 86400,
  566. 'admin_id' => $admin->id,
  567. 'username' => $admin->username,
  568. 'store_admin_id' => $store_admin->id
  569. ];
  570. return \Yii::$app->jwt->createToken($payload);
  571. }
  572. //扫码入库
  573. public function actionErpScanGetGoodsList() {
  574. $form = new StoreAdminGoodsForm();
  575. $form->attributes = get_params();
  576. $form->store_id = get_store_id();
  577. $res = $form->erpScanGetGoodsList();
  578. return $this->asJson($res);
  579. }
  580. //erp入库
  581. public function actionPurchaseInSave() {
  582. $form = new StoreAdminGoodsForm();
  583. $form->attributes = all_params();
  584. $form->store_id = get_store_id();
  585. $res = $form->purchaseInSave();
  586. return $this->asJson($res);
  587. }
  588. //单品退款列表
  589. public function actionOrderGoodsCancelList() {
  590. $form = new StoreAdminOrderForm();
  591. $form->status = get_params('status');
  592. $res = $form->orderGoodsCancelList();
  593. return $this->asJson($res);
  594. }
  595. //前端后台管理员同意打款
  596. public function actionOrderGoodsCancelHandle() {
  597. try {
  598. $id = post_params('id');
  599. $status = post_params('status');
  600. $result = \app\models\OrderGoodsCancel::orderGoodsCancelHandle($id, $status);
  601. return $this->asJson($result);
  602. } catch (\Exception $e) {
  603. return $this->asJson([
  604. 'code' => 1,
  605. 'msg' => $e->getMessage()
  606. ]);
  607. }
  608. }
  609. }