MdGoodsController.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use AopClient;
  9. use app\constants\OptionSetting;
  10. use app\models\AggregateQrcode;
  11. use app\models\Banner;
  12. use app\models\Cat;
  13. use app\models\FoodFlag;
  14. use app\models\Goods;
  15. use app\models\GoodsCat;
  16. use app\models\MchCat;
  17. use app\models\MchGoodsCat;
  18. use app\models\Md;
  19. use app\models\MdFoodsQrcode;
  20. use app\models\MdGoods;
  21. use app\models\Option;
  22. use app\models\Store;
  23. use app\models\StoreAliMini;
  24. use app\models\StoreMini;
  25. use app\models\WechatConfig;
  26. use app\modules\admin\models\AlipayThirdForm;
  27. use app\modules\admin\models\BannerForm;
  28. use app\modules\admin\models\GoodsForm;
  29. use app\models\FoodExtGoods;
  30. use app\modules\admin\models\MdGoodsForm;
  31. use app\models\FoodCat;
  32. use app\models\FoodTableNumber;
  33. use app\modules\admin\models\WechatThirdErrorMsgForm as ErrorMsg;
  34. use app\modules\admin\models\WechatThirdForm;
  35. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppQrcodeCreateRequest;
  36. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
  37. use app\utils\QrCode;
  38. use app\utils\ShareQrcode;
  39. use EasyWeChat\Factory;
  40. use EasyWeChat\Kernel\BaseClient;
  41. use yii\base\BaseObject;
  42. use yii\helpers\Json;
  43. use ZipArchive;
  44. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  45. class MdGoodsController extends BaseController
  46. {
  47. /**
  48. * 平台获取商品列表
  49. * @return \yii\web\Response
  50. */
  51. public function actionList()
  52. {
  53. return $this->asJson(MdGoodsForm::getList(get_params()));
  54. }
  55. /**
  56. * 修改商品库存
  57. * @return \yii\web\Response
  58. */
  59. public function actionSetAttrNum()
  60. {
  61. $form = new MdGoodsForm();
  62. return $this->asJson($form->setAttrNum());
  63. }
  64. /**
  65. * 编辑商品数据
  66. */
  67. public function actionSetAttribute()
  68. {
  69. $form = new MdGoodsForm();
  70. return $this->asJson($form->setType());
  71. }
  72. /**
  73. * 编辑商品数据
  74. */
  75. public function actionSetAttr()
  76. {
  77. $status = post_params('status', -1);
  78. $delivery_type = post_params('delivery_type', null);
  79. $md_id = get_md_id();
  80. $goods_id = post_params('id');
  81. $md_goods = MdGoods::findOne([
  82. 'md_id' => $md_id,
  83. 'goods_id' => $goods_id
  84. ]);
  85. $is_new_record = false;
  86. if (!$md_goods) {
  87. $md_goods = new MdGoods();
  88. $md_goods->md_id = $md_id;
  89. $md_goods->goods_id = $goods_id;
  90. $is_new_record = true;
  91. }
  92. $goods = Goods::findOne($goods_id);
  93. if ($status > -1) {
  94. if ($md_goods->goods_num <= 0 && $status == Goods::STATUS_NORMAL) {
  95. return $this->asJson(['code' => 1, 'msg' => '请先添加商品库存之后再进行上架操作']);
  96. }
  97. $md_goods->status = $status;
  98. if ($is_new_record) {
  99. $md_goods->attr = $goods->attr;
  100. $md_goods->virtual_sales = $goods->virtual_sales;
  101. $md_goods->price = $goods->price;
  102. $md_goods->delivery_type = Md::findOne(get_md_id())->self_delivery_type;
  103. $md_goods->goods_num = $goods->goods_num;
  104. }
  105. }
  106. if (isset($delivery_type)) {
  107. $md_goods->delivery_type = Json::encode($delivery_type);
  108. if ($is_new_record) {
  109. $md_goods->attr = $goods->attr;
  110. $md_goods->virtual_sales = $goods->virtual_sales;
  111. $md_goods->price = $goods->price;
  112. $md_goods->goods_num = $goods->goods_num;
  113. }
  114. }
  115. if ($md_goods->save()) {
  116. return $this->asJson(['code' => 0, 'msg' => '保存成功']);
  117. } else {
  118. return $this->asJson(['code' => 1, 'msg' => $md_goods->errors[0]]);
  119. }
  120. }
  121. /**
  122. * 批量修改商品
  123. * @return \yii\web\Response
  124. */
  125. public function actionUpdateAll()
  126. {
  127. $status = post_params('status');
  128. $md_id = get_md_id();
  129. $goods_id = post_params('goods_id');
  130. if (!is_array($goods_id) || empty($goods_id)) {
  131. return $this->asJson([
  132. 'code' => 1,
  133. 'msg' => 'goods_id为空'
  134. ]);
  135. }
  136. $count = 0;
  137. foreach ($goods_id as $id) {
  138. $goods = Goods::findOne($id);
  139. $md_goods = MdGoods::findOne([
  140. 'md_id' => $md_id,
  141. 'goods_id' => $id
  142. ]);
  143. if (!$md_goods) {
  144. $md_goods = new MdGoods();
  145. $md_goods->md_id = $md_id;
  146. $md_goods->goods_id = $id;
  147. $md_goods->attr = $goods->attr;
  148. $md_goods->delivery_type = Md::findOne(get_md_id())->self_delivery_type;
  149. $md_goods->virtual_sales = $goods->virtual_sales;
  150. $md_goods->price = $goods->price;
  151. $md_goods->goods_num = $goods->goods_num;
  152. }
  153. $md_goods->status = $status;
  154. if ($md_goods->save()) {
  155. $count++;
  156. }
  157. }
  158. if ($count) {
  159. return $this->asJson([
  160. 'code' => 0,
  161. 'msg' => '共修改' . $count . '条数据'
  162. ]);
  163. } else {
  164. return $this->asJson([
  165. 'code' => 1,
  166. 'msg' => '修改数据为0条'
  167. ]);
  168. }
  169. }
  170. /**
  171. * 门店点餐商品
  172. * @return \yii\web\Response
  173. */
  174. public function actionFood() {
  175. $arr = get_params();
  176. $md_id = intval(get_md_id());
  177. $mch_id = intval(get_mch_id());
  178. $query = Goods::find()->alias('g')
  179. ->leftJoin(['fc' => FoodCat::tableName()], 'fc.id=g.cat_id')
  180. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0])->andWhere(['not like', 'g.name', '当面付'])
  181. ->andWhere(['!=', 'g.md_food_id', 0]);
  182. if ($md_id <= 0) {
  183. $query->andWhere(['md_food_id' => -1]);
  184. } else {
  185. $query->andWhere(['md_food_id' => $md_id]);
  186. }
  187. $query->andWhere(['g.mch_id' => $mch_id]);
  188. if (isset($arr['status']) && $arr['status'] > -1) {
  189. $query->andWhere([
  190. 'g.status' => $arr['status']
  191. ]);
  192. }
  193. if (!empty($arr['name'])) {
  194. $query->andWhere([
  195. 'like', 'g.name', $arr['name']
  196. ]);
  197. }
  198. if (!empty($arr['cat_id'])) {
  199. $query->andWhere([
  200. 'fc.id' => $arr['cat_id']
  201. ]);
  202. }
  203. if (isset($arr['name']) && !empty($arr['name'])) {
  204. $query->andWhere([
  205. 'like',
  206. 'g.name',
  207. $arr['name']
  208. ]);
  209. }
  210. if (!empty($arr['select'])) {
  211. $select = $arr['select'];
  212. } else {
  213. $select = ['g.id', 'g.name', 'g.status', 'g.service',
  214. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  215. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  216. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend',
  217. 'g.delivery_type', 'fc.name cat_name', 'g.md_food_id'];
  218. }
  219. $query->select($select)->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
  220. $pagination = pagination_make($query);
  221. $pagination['data'] = $pagination['list'];
  222. $cat_query = FoodCat::find()->where([
  223. 'is_delete' => 0,
  224. 'mch_id' => intval(get_mch_id()),
  225. 'store_id' => get_store_id()
  226. ]);
  227. if (get_md_id()) {
  228. $cat_query->andWhere(['md_id' => get_md_id()]);
  229. }
  230. $cat = $cat_query->asArray()->all();
  231. foreach($pagination['data'] as $k => $v) {
  232. $pagination['data'][$k]['delivery_type'] = Json::decode($v['delivery_type']);
  233. $pagination['data'][$k]['md_name'] = Md::findOne($v['md_food_id'])->name;
  234. }
  235. $pagination['cat'] = $cat;
  236. unset($pagination['list']);
  237. return $this->asJson([
  238. 'q' => $query->createCommand()->getRawSql(),
  239. 'code' => 0,
  240. 'msg' => 'success',
  241. 'data' => $pagination
  242. ]);
  243. }
  244. /**
  245. * 保存商品
  246. */
  247. public function actionFoodSave()
  248. {
  249. $arr = [];
  250. if(get_mch_id()){
  251. $arr['mch'] = 1;
  252. $arr['mch_id'] = get_mch_id();
  253. }
  254. $form = new GoodsForm();
  255. $form->store_id = get_store_id();
  256. $form->attributes = post_params();
  257. return $this->asJson($form->saveFood($arr));
  258. }
  259. //附加商品区保存
  260. public function actionFoodExtGoodsSave()
  261. {
  262. try{
  263. $store_id = get_store_id();
  264. $id = input_params('id');
  265. $md_id = input_params('md_id', 0);
  266. $name = input_params('name');
  267. $desc = input_params('desc');
  268. $cover_pic = input_params('cover_pic');
  269. $attr = input_params('attr');
  270. $save = FoodExtGoods::saveOne($store_id, $name, $desc, $cover_pic, $attr, $id, $md_id, intval(get_mch_id()));
  271. return $this->asJson([
  272. 'code' => $save ? 0 : 1,
  273. 'msg' => $save ? 'ok' : 'faild',
  274. ]);
  275. } catch (\Exception $e) {
  276. return $this->asJson([
  277. 'code' => 1,
  278. 'msg' => $e->getMessage(),
  279. ]);
  280. }
  281. }
  282. //附加商品区列表
  283. public function actionFoodExtGoodsList()
  284. {
  285. $store_id = get_store_id();
  286. $name = input_params('name');
  287. $md_id = input_params('md_id', 0);
  288. $list = FoodExtGoods::list([
  289. 'mch_id' => intval(get_mch_id()),
  290. 'name' => $name,
  291. 'md_id' => $md_id,
  292. ], $store_id);
  293. return $this->asJson([
  294. 'code' => 0,
  295. 'msg' => 'ok',
  296. 'data' => $list,
  297. ]);
  298. }
  299. //附加商品区删除
  300. public function actionFoodExtGoodsDel()
  301. {
  302. try{
  303. $store_id = get_store_id();
  304. $id = input_params('id');
  305. $save = FoodExtGoods::del($id, $store_id);
  306. return $this->asJson([
  307. 'code' => $save ? 0 : 1,
  308. 'msg' => $save ? 'ok' : 'faild',
  309. ]);
  310. } catch (\Exception $e) {
  311. return $this->asJson([
  312. 'code' => 1,
  313. 'msg' => $e->getMessage(),
  314. ]);
  315. }
  316. }
  317. //附加商品区设置默认
  318. public function actionFoodExtGoodsDefault()
  319. {
  320. try{
  321. $store_id = get_store_id();
  322. $id = input_params('id');
  323. $md_id = input_params('md_id', 0);
  324. $save = FoodExtGoods::setDefault($id, $store_id, $md_id, intval(get_mch_id()));
  325. return $this->asJson([
  326. 'code' => $save ? 0 : 1,
  327. 'msg' => $save ? 'ok' : 'faild',
  328. ]);
  329. } catch (\Exception $e) {
  330. return $this->asJson([
  331. 'code' => 1,
  332. 'msg' => $e->getMessage(),
  333. ]);
  334. }
  335. }
  336. /**
  337. * 保存商品
  338. */
  339. public function actionFoodCat()
  340. {
  341. $where = [
  342. 'store_id' => get_store_id(),
  343. 'mch_id' => intval(get_mch_id()),
  344. 'is_delete' => 0,
  345. 'is_show' => 1
  346. ];
  347. $where['md_id'] = get_md_id() > 0 ? get_md_id() : [0, -1];
  348. $cat = FoodCat::findAll($where);
  349. return $this->asJson([
  350. 'code' => 0,
  351. 'msg' => 'success',
  352. 'data' => $cat
  353. ]);
  354. }
  355. // 幻灯片列表
  356. public function actionBannerList()
  357. {
  358. $form = new BannerForm();
  359. $form->type = Banner::TYPE_FOOD;
  360. $form->md_id = get_md_id();
  361. return $this->asJson($form->getList(get_store_id(), intval(get_mch_id())));
  362. }
  363. // 幻灯片编辑
  364. public function actionBannerEdit()
  365. {
  366. $arr = [];
  367. if(get_mch_id()){
  368. $arr['mch'] = 1;
  369. $arr['mch_id'] = intval(get_mch_id());
  370. }
  371. $id = input_params('id');
  372. $banner = Banner::findOne(['id' => $id, 'type' => Banner::TYPE_FOOD, 'md_id' => get_md_id()]);
  373. if (!$banner) {
  374. $banner = new Banner();
  375. }
  376. if (\Yii::$app->request->isPost) {
  377. $form = new BannerForm();
  378. $form->attributes = post_params();
  379. $form->store_id = get_store_id();
  380. $form->banner = $banner;
  381. $form->md_id = get_md_id();
  382. $form->type = Banner::TYPE_FOOD;
  383. return $this->asJson($form->save($arr));
  384. }
  385. foreach ($banner as $index => $value) {
  386. $banner[$index] = str_replace("\"", "&quot;", $value);
  387. }
  388. return $this->asJson([
  389. 'code' => 0,
  390. 'msg' => 'success',
  391. 'data' => $banner
  392. ]);
  393. }
  394. // 幻灯片删除
  395. public function actionBannerDelete()
  396. {
  397. $id = input_params('id');
  398. $banner = Banner::findOne(['id' => $id, 'is_delete' => 0, 'type' => Banner::TYPE_FOOD, 'md_id' => get_md_id()]);
  399. if (!$banner) {
  400. return $this->asJson([
  401. 'code' => 1,
  402. 'msg' => '幻灯片不存在或已经删除',
  403. ]);
  404. }
  405. $banner->is_delete = 1;
  406. if ($banner->save()) {
  407. return $this->asJson([
  408. 'code' => 0,
  409. 'msg' => '成功',
  410. ]);
  411. } else {
  412. foreach ($banner->errors as $errors) {
  413. return $this->asJson([
  414. 'code' => 1,
  415. 'msg' => $errors[0],
  416. ]);
  417. }
  418. }
  419. }
  420. /**
  421. * 商品编辑
  422. * @param $id
  423. * @return \yii\web\Response
  424. */
  425. public function actionEdit()
  426. {
  427. $id = get_params('id') ?: 0;
  428. $goodsFrom = new GoodsForm();
  429. return $this->asJson($goodsFrom->getFoodEdit($id));
  430. }
  431. /**
  432. * 编辑商品数据
  433. */
  434. public function actionUpdateAttribute()
  435. {
  436. $form = new GoodsForm();
  437. return $this->asJson($form->updateAttribute());
  438. }
  439. /**
  440. * 修改商品库存
  441. * @return \yii\web\Response
  442. */
  443. public function actionUpdateAttrNum()
  444. {
  445. $form = new GoodsForm();
  446. return $this->asJson($form->updateAttrNum());
  447. }
  448. /**
  449. * @return \yii\web\Response
  450. */
  451. public function actionTableList()
  452. {
  453. $num = get_params('num');
  454. $md_id = get_md_id();
  455. if ($md_id <= 0) {
  456. $md_id = [0, -1];
  457. }
  458. $mch_id = get_mch_id();
  459. $query = FoodTableNumber::find()->where([
  460. 'is_delete' => 0,
  461. 'mch_id' => intval($mch_id),
  462. 'store_id' => get_store_id(),
  463. ]);
  464. if (get_md_id()) {
  465. $query->andWhere(['md_id' => get_md_id()]);
  466. }
  467. if ($num) {
  468. $query->andWhere(['num' => $num]);
  469. }
  470. $pagination = pagination_make($query);
  471. $list = $pagination['list'];
  472. foreach ($list as &$item) {
  473. $food_flag = FoodFlag::find()->where([
  474. 'store_id' => get_store_id(),
  475. 'mch_id' => intval(get_mch_id()),
  476. 'md_id' => $md_id,
  477. 'table_num' => $item['num'],
  478. 'status' => 0,
  479. 'type' => 1
  480. ])->asArray()->one();
  481. $item['is_can_clear'] = 0;
  482. if ($food_flag) {
  483. $item['is_can_clear'] = 1;
  484. }
  485. }
  486. return $this->asJson([
  487. 'code' => 0,
  488. 'msg' => 'success',
  489. 'data' => [
  490. 'data' => $list,
  491. 'pageNo' => $pagination['pageNo'],
  492. 'totalCount' => $pagination['totalCount'],
  493. ],
  494. ]);
  495. }
  496. public function actionClearTable() {
  497. $store_id = get_store_id();
  498. $md_id = get_md_id();
  499. if ($md_id <= 0) {
  500. $md_id = [0, -1];
  501. }
  502. $mch_id = get_mch_id();
  503. $id = post_params('id');
  504. $foodTable = FoodTableNumber::findOne($id);
  505. if (!$foodTable) {
  506. return [
  507. 'code' => 1,
  508. 'msg' => '记录不存在!'
  509. ];
  510. }
  511. $num = $foodTable->num;
  512. if (empty($num)) {
  513. return $this->asJson([
  514. 'code' => 1,
  515. 'msg' => '桌号错误'
  516. ]);
  517. }
  518. FoodFlag::updateAll(['status' => 1], [
  519. 'store_id' => $store_id,
  520. 'mch_id' => intval($mch_id),
  521. 'md_id' => $md_id,
  522. 'table_num' => $num,
  523. 'status' => 0,
  524. 'type' => 1
  525. ]);
  526. return $this->asJson([
  527. 'code' => 0,
  528. 'msg' => '处理完成'
  529. ]);
  530. }
  531. public function actionTableAdd()
  532. {
  533. $store_id = post_params('storeId', get_store_id());
  534. $params = [
  535. 'num' => post_params('num', ''),
  536. 'remark' => post_params('remark'),
  537. ];
  538. if (!is_numeric($params['num'])) {
  539. return $this->asJson([
  540. 'code' => 1,
  541. 'msg' => '桌号必须为数字!'
  542. ]);
  543. }
  544. $is = FoodTableNumber::find()->where([
  545. 'store_id' => $store_id,
  546. 'mch_id' => intval(get_mch_id()),
  547. 'md_id' => get_md_id(),
  548. 'num' => $params['num'],
  549. 'is_delete' => 0,
  550. ])->one();
  551. if ($is) {
  552. return $this->asJson([
  553. 'code' => 1,
  554. 'msg' => '桌号已经存在!'
  555. ]);
  556. }
  557. $form = new FoodTableNumber();
  558. $form->store_id = $store_id;
  559. get_mch_id() && $form->mch_id = intval(get_mch_id());
  560. $form->num = $params['num'];
  561. $form->md_id = get_md_id();
  562. $form->remark = $params['remark'];
  563. if ($form->save()) {
  564. return $this->asJson([
  565. 'code' => 0,
  566. 'msg' => '创建成功!'
  567. ]);
  568. }
  569. return $this->asJson([
  570. 'code' => 1,
  571. 'msg' => '创建失败!'
  572. ]);
  573. }
  574. public function actionTableEdit()
  575. {
  576. $store_id = post_params('storeId', get_store_id());
  577. $params = [
  578. 'id' => post_params('id'),
  579. 'num' => post_params('num', ''),
  580. 'remark' => post_params('remark'),
  581. ];
  582. if (!is_numeric($params['num'])) {
  583. return $this->asJson([
  584. 'code' => 1,
  585. 'msg' => '桌号必须为数字!'
  586. ]);
  587. }
  588. $is = FoodTableNumber::find()->where([
  589. 'store_id' => $store_id,
  590. 'mch_id' => intval(get_mch_id()),
  591. 'num' => $params['num'],
  592. 'is_delete' => 0,
  593. 'md_id' => get_md_id(),
  594. ])->andWhere(['<>', 'id', $params['id']])->one();
  595. if ($is) {
  596. return $this->asJson([
  597. 'code' => 1,
  598. 'msg' => '桌号已经存在!'
  599. ]);
  600. }
  601. $form = FoodTableNumber::findOne($params['id']);
  602. if (!$form) {
  603. return $this->asJson([
  604. 'code' => 1,
  605. 'msg' => '记录不存在!'
  606. ]);
  607. }
  608. $form->num = $params['num'];
  609. $form->remark = $params['remark'];
  610. if ($form->save()) {
  611. return $this->asJson([
  612. 'code' => 0,
  613. 'msg' => '创建成功!'
  614. ]);
  615. }
  616. return $this->asJson([
  617. 'code' => 1,
  618. 'msg' => '创建失败!'
  619. ]);
  620. }
  621. public function actionTableDel()
  622. {
  623. $id = post_params('id');
  624. $form = FoodTableNumber::findOne($id);
  625. if (!$form) {
  626. return [
  627. 'code' => 1,
  628. 'msg' => '记录不存在!'
  629. ];
  630. }
  631. $form->is_delete = FoodTableNumber::DELETE_STATUS_TRUE;
  632. if ($form->save()) {
  633. return $this->asJson([
  634. 'code' => 0,
  635. 'msg' => '删除成功!'
  636. ]);
  637. }
  638. return $this->asJson([
  639. 'code' => 1,
  640. 'msg' => '删除失败!'
  641. ]);
  642. }
  643. // 批量下载桌号二维码
  644. public function actionTableQrcodes(){
  645. $nums = post_params('nums');
  646. $store_id = get_store_id();
  647. $storeInfo = Store::findOne($store_id);
  648. $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
  649. $mch_id = intval(get_mch_id());
  650. $md_id = -1;
  651. // 必须是数组
  652. if(!is_array($nums) || empty($nums)){
  653. return $this->asJson([
  654. 'code'=>1,
  655. 'msg'=>"请选择要下载的桌号",
  656. ]);
  657. }
  658. $paths = [];
  659. foreach($nums as $item){
  660. $table = FoodTableNumber::findOne([
  661. 'is_delete' => 0,
  662. 'store_id' => $store_id,
  663. 'mch_id' => intval(get_mch_id()),
  664. 'id' => $item,
  665. 'md_id' => $md_id <= 0 ? [0, -1] : $md_id
  666. ]);
  667. if(empty($table)){
  668. continue;
  669. }
  670. if((!\Yii::$app->prod_is_dandianpu() && $storeInfo->business_model == 1) || (\Yii::$app->prod_is_dandianpu() && (int)$self_mini === 1)){
  671. $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$table->num}";
  672. }else{
  673. $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$table->num},f_num:{$item},store_id:{$store_id}";
  674. }
  675. $paths[] = $this->makeZipFoodQr($scene, $table->num,$table->remark);
  676. }
  677. if(empty($paths)){
  678. return $this->asJson([
  679. 'code'=>1,
  680. 'msg'=>"二维码生成失败",
  681. ]);
  682. }
  683. // 打包压缩包 开始下载
  684. $zipPath = \Yii::$app->runtimePath . '/zip/food-table/';
  685. if(!file_exists($zipPath)){
  686. mkdir($zipPath, 0777, true);
  687. }
  688. $zipFileName = md5(time().implode(',',$nums)).'.zip';
  689. $zip = new ZipArchive();
  690. if ($zip->open($zipPath.$zipFileName, ZipArchive::CREATE) === TRUE) {
  691. // 将每个图片文件添加到压缩包中
  692. foreach ($paths as $image) {
  693. if (file_exists($image)) {
  694. $zip->addFile($image, basename($image));
  695. }
  696. }
  697. // 关闭压缩包
  698. $zip->close();
  699. $returnPath = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/zip/food-table/' . $zipFileName );
  700. return $this->asJson(['code' => 0, 'data' => $returnPath]);
  701. } else {
  702. echo '无法创建压缩包。';
  703. }
  704. }
  705. private function makeZipFoodQr($scene, $num,$remark){
  706. $store_id = get_store_id();
  707. $mch_id = intval(get_mch_id());
  708. $md_id = get_md_id();
  709. $type_ = get_params('type', 0);
  710. if ($type_) {
  711. $type = 2;
  712. }
  713. $scene = $scene ? $scene : ("md_id:{$md_id},mch_id:{$mch_id},store:{$store_id},store_id:{$store_id}");
  714. $store = Store::findOne(['id' => $store_id]);
  715. if ($store) {
  716. if ($md_id <= 0) {
  717. $md_id = [0, 1];
  718. }
  719. $qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'mch_id' => intval(get_mch_id()), 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
  720. if ($qrcode && !$type) {
  721. $filename = $num .'+'.$remark;
  722. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  723. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  724. if(!empty($qrcode->url_path)){
  725. $text = $qrcode->url_path;
  726. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  727. $qrcode->qrcode_url = $pic_url;
  728. $qrcode->save();
  729. }
  730. } else {
  731. $scene = 'scene=' . $scene;
  732. if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
  733. if (intval($type) === 2) {
  734. //设置二维码规则
  735. $text = \Yii::$app->request->hostInfo . '/web/home/store/' . $store_id . '/?'. $scene;
  736. } else {
  737. //设置二维码规则
  738. $text = \Yii::$app->request->hostInfo . '/web/food/store/' . $store_id . '/?'. $scene;
  739. if (!\Yii::$app->isSaas()) {
  740. $text = \Yii::$app->request->hostInfo . '/web/food/' . $store_id . '?'. $scene;
  741. }
  742. }
  743. } else {
  744. $text = \Yii::$app->request->hostInfo . '/web/food/saas?' . $scene;
  745. }
  746. $filename = $num ;
  747. if(!empty($remark)){
  748. $filename .= '+'.$remark;
  749. }
  750. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  751. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  752. }
  753. return $path;
  754. } else {
  755. return [
  756. 'code' => 1,
  757. 'msg' => "商城信息获取失败"
  758. ];
  759. }
  760. }
  761. public function actionDownQr()
  762. {
  763. $num = post_params('num', 0);
  764. $store_id = get_store_id();
  765. $mch_id = intval(get_mch_id());
  766. $md_id = get_md_id();
  767. $storeInfo = Store::findOne($store_id);
  768. $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
  769. $food_table_number = FoodTableNumber::findOne([
  770. 'is_delete' => 0,
  771. 'store_id' => $store_id,
  772. 'mch_id' => intval(get_mch_id()),
  773. 'num' => $num,
  774. 'md_id' => $md_id <= 0 ? [0, -1] : $md_id
  775. ]);
  776. $table_num = 0;
  777. if ($food_table_number) {
  778. $table_num = $food_table_number->id;
  779. }
  780. $qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'mch_id' => intval(get_mch_id()), 'md_id' => $md_id <= 0 ? [0, -1] : $md_id, 'table_num' => $table_num, 'is_delete' => 0]);
  781. $pic_url = '';
  782. if ($qrcode) {
  783. $filename = md5(date('Ym') . $qrcode->id . 'md_foods_qrcode');
  784. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  785. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  786. if(!empty($qrcode->url_path)){
  787. $text = $qrcode->url_path;
  788. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  789. $qrcode->qrcode_url = $pic_url;
  790. $qrcode->save();
  791. }
  792. $is_saas = false;
  793. $business_model = $storeInfo->business_model;
  794. if (\Yii::$app->isSaas()) {
  795. $is_saas = true;
  796. }
  797. if (\Yii::$app->prod_is_dandianpu()) {
  798. $business_model = 0;
  799. if ($self_mini) {
  800. $business_model = 1;
  801. }
  802. }
  803. return $this->asJson([
  804. 'code'=>0,
  805. 'msg'=>"成功",
  806. 'data'=>[
  807. $pic_url
  808. ],
  809. 'business_model' => $business_model,
  810. 'is_saas' => $is_saas
  811. ]);
  812. } else {
  813. if((!\Yii::$app->prod_is_dandianpu() && $storeInfo->business_model == 1) || (\Yii::$app->prod_is_dandianpu() && (int)$self_mini === 1)){
  814. $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$num}";
  815. }else{
  816. $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$num},f_num:{$num},store_id:{$store_id}";
  817. }
  818. return $this->actionFoodQr($scene, 1);
  819. }
  820. // $filename = md5($store_id . '-' . $md_id . '-' . $num);
  821. // $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  822. // $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  823. // if (file_exists($path)) {
  824. // return $this->asJson([
  825. // 'code' => 0,
  826. // 'data' => [
  827. // 'qr_url' => $pic_url,
  828. // ]
  829. // ]);
  830. // }
  831. // $id = FoodTableNumber::findOne(['num' => $num, 'md_id' => $md_id, 'is_delete' => 0])->id;
  832. // $config = WechatConfig::findOne(['store_id' => $store_id]);
  833. // if (empty($config->app_id)) {
  834. // return $this->asJson([
  835. // 'code' => 1,
  836. // 'msg' => "数据查找失败"
  837. // ]);
  838. // }
  839. // $wx_mini = StoreMini::findOne(['appid' => $config->app_id])->where(['store_id' => $store_id])->orderBy('id desc')->one();
  840. // $form = new WechatThirdForm();
  841. // $result = $form->downQrcodeRules($wx_mini->id, $wx_mini->appid);
  842. // if ($result['code'] !== 0) {
  843. // return $this->asJson([
  844. // 'code' => 1,
  845. // 'msg' => $result['msg']
  846. // ]);
  847. // }
  848. // $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/food/' . $store_id . '?md_id='. $md_id . '&food_table_num=' . $id);
  849. // QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  850. // $goods_qrcode = new \claviska\SimpleImage($path);
  851. // $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
  852. // $bg = new \claviska\SimpleImage($bgPath);
  853. // $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
  854. // $bg->toFile($path, 'image/jpeg', 85);
  855. return $this->asJson([
  856. 'code' => 0,
  857. 'data' => [
  858. 'qr_url' => $pic_url,
  859. ]
  860. ]);
  861. }
  862. public function aliConfigQr($request, $data = [])
  863. {
  864. try {
  865. $third_appid = Option::get("alipay_appid", 0, 'saas')['value'];
  866. $third_private_key = Option::get("alipay_app_private_key", 0, 'saas')['value'];
  867. $third_public_key = Option::get("alipay_public_key", 0, 'saas')['value'];
  868. $data = json_encode($data);
  869. $config_ali = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value'];
  870. $config_ali = json_decode($config_ali, true);
  871. $aop = new AopClient();
  872. //如果商城的支付宝小程序信息存在,则使用支付宝小程序的配置信息
  873. if (!empty($config_ali['app_id']) && !empty($config_ali['app_private_key']) && !empty($config_ali['app_public_key'])) {
  874. $aop->appId = $config_ali['app_id'];
  875. $aop->rsaPrivateKey = $config_ali['app_private_key'];
  876. $aop->alipayrsaPublicKey = $config_ali['app_public_key'];
  877. } else {
  878. //使用三方的配置信息
  879. $aop->appId = $third_appid;
  880. $aop->rsaPrivateKey = $third_private_key;
  881. $aop->alipayrsaPublicKey = $third_public_key;
  882. $ali_mini = StoreAliMini::find()->where(['auth_app_id' => $config_ali['app_id'], 'is_cancel' => 0, 'store_id' => get_store_id()])->asArray()->one();
  883. if (empty($ali_mini)) {
  884. throw new \Exception("查找小程序信息失败");
  885. }
  886. $token = $ali_mini['auth_token'];
  887. }
  888. $aop->encryptType = "AES";
  889. $aop->apiVersion = '1.0';
  890. $aop->signType = 'RSA2';
  891. $aop->postCharset = 'utf-8';
  892. $aop->format = 'json';
  893. $request->setBizContent($data);
  894. if (!empty($token)) {
  895. $result = $aop->execute ($request, null, $token);
  896. } else {
  897. $result = $aop->execute ($request);
  898. }
  899. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  900. return $result->$responseNode;
  901. } catch (\Exception $e) {
  902. return [
  903. 'code' => 1,
  904. 'msg' => $e->getMessage()
  905. ];
  906. }
  907. }
  908. public function actionFoodSettingInfo() {
  909. $md_id = get_md_id();
  910. $md = Md::findOne($md_id);
  911. if (!$md) {
  912. return $this->asJson([
  913. 'code' => 1,
  914. 'msg' => '门店数据不存在'
  915. ]);
  916. }
  917. if (!$md->food_payment) {
  918. $payment = [
  919. [
  920. 'key' => 'alipay',
  921. 'value' => 0,
  922. 'name' => ''
  923. ],
  924. [
  925. 'key' => 'wechat',
  926. 'value' => 1,
  927. 'name' => ''
  928. ],
  929. [
  930. 'key' => 'huodao',
  931. 'value' => 0,
  932. 'name' => ''
  933. ],
  934. [
  935. 'key' => 'friend',
  936. 'value' => 0,
  937. 'name' => ''
  938. ],
  939. ];
  940. $md->food_payment = Json::encode($payment);
  941. $md->save();
  942. }
  943. $food_page_title = Option::get('food_page_title', $md_id, 'md', '')['value'];
  944. return $this->asJson([
  945. 'code' => 0,
  946. 'msg' => 'success',
  947. 'data' => [
  948. 'food_payment' => Json::decode($md->food_payment),
  949. 'food_pay_type' => $md->food_pay_type,
  950. 'food_page_title' => $food_page_title ?: '手机点餐'
  951. ]
  952. ]);
  953. }
  954. public function actionFoodSetting() {
  955. $md_id = get_md_id();
  956. $md = Md::findOne($md_id);
  957. $payment = post_params('food_payment');
  958. $pay_type = post_params('food_pay_type', 0);
  959. $food_page_title = post_params('food_page_title', '');
  960. if (!$md) {
  961. return $this->asJson([
  962. 'code' => 1,
  963. 'msg' => '门店数据不存在'
  964. ]);
  965. }
  966. $md->food_pay_type = $pay_type;
  967. $md->food_payment = $payment;
  968. if (!$md->save()) {
  969. return $this->asJson([
  970. 'code' => 1,
  971. 'msg' => $md->errors
  972. ]);
  973. }
  974. Option::set('food_page_title', $food_page_title, $md->id, 'md');
  975. return $this->asJson([
  976. 'code' => 0,
  977. 'msg' => '保存成功'
  978. ]);
  979. }
  980. public function actionFoodQr($scene = "", $type = 0)
  981. {
  982. //微信小程序码 start
  983. // $wx_url_path = "";
  984. // $ali_url_path = "";
  985. $store_id = get_store_id();
  986. $mch_id = intval(get_mch_id());
  987. $md_id = get_md_id();
  988. $type_ = get_params('type', 0);
  989. if ($type_) {
  990. $type = 2;
  991. }
  992. $scene = $scene ? $scene : ("md_id:{$md_id},mch_id:{$mch_id},store:{$store_id},store_id:{$store_id}");
  993. $store = Store::findOne(['id' => $store_id]);
  994. if ($store) {
  995. // $scene = 'scene=' . $scene;
  996. // if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
  997. // //设置二维码规则
  998. // $text = \Yii::$app->request->hostInfo . '/web/food/store/' . $store_id . '/?'. $scene;
  999. // if (!\Yii::$app->isSaas()) {
  1000. // $text = \Yii::$app->request->hostInfo . '/web/food/' . $store_id . '?'. $scene;
  1001. // }
  1002. // } else {
  1003. // $text = \Yii::$app->request->hostInfo . '/web/food/saas?' . $scene;
  1004. // }
  1005. // $filename = md5(date('Ym') . 'ali' . $store_id . '_' . $scene);
  1006. // $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  1007. // $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  1008. // QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  1009. // $is_saas = false;
  1010. // $business_model = $store->business_model;
  1011. // if (\Yii::$app->isSaas()) {
  1012. // $is_saas = true;
  1013. // }
  1014. //
  1015. // if (\Yii::$app->prod_is_dandianpu()) {
  1016. // $business_model = 0;
  1017. // $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
  1018. // if ($self_mini) {
  1019. // $business_model = 1;
  1020. // }
  1021. // }
  1022. if ($md_id <= 0) {
  1023. $md_id = [0, 1];
  1024. }
  1025. $qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'mch_id' => intval(get_mch_id()), 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
  1026. if ($qrcode && !$type) {
  1027. $filename = md5(date('Ym') . $qrcode->id . 'md_foods_qrcode');
  1028. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  1029. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  1030. if(!empty($qrcode->url_path)){
  1031. $text = $qrcode->url_path;
  1032. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  1033. $qrcode->qrcode_url = $pic_url;
  1034. $qrcode->save();
  1035. }
  1036. } else {
  1037. $scene = 'scene=' . $scene;
  1038. if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
  1039. if (intval($type) === 2) {
  1040. //设置二维码规则
  1041. $text = 'https://' . \Yii::$app->request->hostName . '/web/home/store/' . $store_id . '/?'. $scene;
  1042. } else {
  1043. //设置二维码规则
  1044. $text = 'https://' . \Yii::$app->request->hostName . '/web/food/store/' . $store_id . '/?'. $scene;
  1045. if (!\Yii::$app->isSaas()) {
  1046. $text = 'https://' . \Yii::$app->request->hostName . '/web/food/' . $store_id . '?'. $scene;
  1047. }
  1048. }
  1049. } else {
  1050. $text = 'https://' . \Yii::$app->request->hostName . '/web/food/saas?' . $scene;
  1051. }
  1052. $filename = md5(date('Ym') . 'ali' . $store_id . '_' . $scene);
  1053. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  1054. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  1055. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  1056. }
  1057. $is_saas = false;
  1058. $business_model = $store->business_model;
  1059. if (\Yii::$app->isSaas()) {
  1060. $is_saas = true;
  1061. }
  1062. if (\Yii::$app->prod_is_dandianpu()) {
  1063. $business_model = 0;
  1064. $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
  1065. if ($self_mini) {
  1066. $business_model = 1;
  1067. }
  1068. }
  1069. return $this->asJson([
  1070. 'code'=>0,
  1071. 'msg'=>"成功",
  1072. 'data'=>[
  1073. $pic_url ?? ""
  1074. ],
  1075. 'mch_id' => $mch_id,
  1076. 'business_model' => $business_model,
  1077. 'is_saas' => $is_saas
  1078. ]);
  1079. } else {
  1080. return $this->asJson([
  1081. 'code' => 1,
  1082. 'msg' => "商城信息获取失败"
  1083. ]);
  1084. }
  1085. }
  1086. public function actionCatList()
  1087. {
  1088. $name = get_params('name');
  1089. $query = FoodCat::find()->where([
  1090. 'is_delete' => 0,
  1091. 'mch_id' => intval(get_mch_id()),
  1092. 'store_id' => get_store_id()
  1093. ]);
  1094. if (!empty($name)) {
  1095. $query->andWhere(['like', 'name', $name]);
  1096. }
  1097. if (get_md_id()) {
  1098. $query->andWhere(['md_id' => get_md_id()]);
  1099. }
  1100. $query->orderBy(['sort' => SORT_DESC]);
  1101. $list = pagination_make($query);
  1102. return $this->asJson([
  1103. 'code' => 0,
  1104. 'msg' => 'success',
  1105. 'data' => [
  1106. 'data' => $list['list'],
  1107. 'pageNo' => $list['pageNo'],
  1108. 'totalCount' => $list['totalCount'],
  1109. ],
  1110. ]);
  1111. }
  1112. public function actionCatStatus()
  1113. {
  1114. $id = post_params('id');
  1115. $cat = FoodCat::findOne($id);
  1116. $cat->is_show = $cat->is_show === 1 ? 0 : 1;
  1117. if ($cat->save()) {
  1118. return $this->asJson([
  1119. 'code' => 0,
  1120. 'msg' => '修改成功'
  1121. ]);
  1122. }
  1123. return $this->asJson([
  1124. 'code' => 1,
  1125. 'msg' => '修改失败',
  1126. ]);
  1127. }
  1128. public function actionCatDel()
  1129. {
  1130. $id = post_params('id');
  1131. $cat = FoodCat::findOne($id);
  1132. $cat->is_delete = 1;
  1133. if ($cat->save()) {
  1134. return $this->asJson([
  1135. 'code' => 0,
  1136. 'msg' => '删除成功'
  1137. ]);
  1138. }
  1139. return $this->asJson([
  1140. 'code' => 1,
  1141. 'msg' => '删除失败',
  1142. ]);
  1143. }
  1144. public function actionCatEdit()
  1145. {
  1146. $store_id = get_store_id();
  1147. $id = post_params('id', 0);
  1148. $name = post_params('name');
  1149. $desc = post_params('desc', '');
  1150. $pic_url = post_params('pic_url', '');
  1151. $sort = post_params('sort');
  1152. $is_show = post_params('is_show', 1);
  1153. if ($id > 0) {
  1154. $cat = FoodCat::findOne($id);
  1155. } else {
  1156. $cat = new FoodCat();
  1157. get_mch_id() && $cat->mch_id = intval(get_mch_id());
  1158. }
  1159. $cat->store_id = $store_id;
  1160. $cat->name = $name;
  1161. $cat->desc = $desc;
  1162. $cat->pic_url = $pic_url;
  1163. $cat->sort = $sort;
  1164. $cat->is_show = $is_show;
  1165. $cat->md_id = get_md_id();
  1166. if ($cat->save()) {
  1167. return $this->asJson([
  1168. 'code' => 0,
  1169. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  1170. ]);
  1171. }
  1172. return $this->asJson([
  1173. 'code' => 1,
  1174. 'msg' => $id > 0 ? '编辑失败' : '添加失败',
  1175. ]);
  1176. }
  1177. //获取是否绑定小程序码
  1178. public function actionIsBind()
  1179. {
  1180. try {
  1181. $type = get_params('type', 0);
  1182. if ($type) {
  1183. $is_wx_bind = (int)Option::get('store_home_is_bind_wx', get_store_id(), 'store', 0)['value'];
  1184. $is_ali_bind = (int)Option::get('store_home_is_bind_ali', get_store_id(), 'store', 0)['value'];
  1185. } else {
  1186. //支付宝
  1187. $md_qrcode = MdFoodsQrcode::findOne(['store_id' => get_store_id(), 'mch_id' => intval(get_mch_id()), 'md_id' => [0, -1], 'table_num' => 0, 'is_delete' => 0]);
  1188. if (!$md_qrcode) {
  1189. $is_ali_bind = 0;
  1190. $StoreAliMini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancel' => 0])->select('id, bind_food_qr')->orderBy('id desc')->one();
  1191. if (!empty($StoreAliMini)) {
  1192. if ((int)$StoreAliMini->bind_food_qr === 1) {
  1193. $is_ali_bind = 1;
  1194. }
  1195. }
  1196. $is_wx_bind = 0;
  1197. $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
  1198. if (!empty($store)) {
  1199. if ((int)$store->bind_food_qr === 1) {
  1200. $is_wx_bind = 1;
  1201. }
  1202. }
  1203. } else {
  1204. $is_wx_bind = $md_qrcode->wx_status;
  1205. $is_ali_bind = $md_qrcode->ali_status;
  1206. }
  1207. }
  1208. return $this->asJson([
  1209. 'code' => 0,
  1210. 'data' => [
  1211. 'is_wx_bind' => $is_wx_bind,
  1212. 'is_ali_bind' => $is_ali_bind
  1213. ]
  1214. ]);
  1215. } catch (\Exception $e) {
  1216. return $this->asJson([
  1217. 'code' => 1,
  1218. 'msg' => $e->getMessage()
  1219. ]);
  1220. }
  1221. }
  1222. //关联普通二维码
  1223. public function actionAliBindQrcode()
  1224. {
  1225. $type = get_params('type', 0);
  1226. if ($type) {
  1227. $param_url = \Yii::$app->request->hostInfo . '/web/home/store/' . get_store_id() . '/';
  1228. $wx_url = '/pages/home/home';
  1229. } else {
  1230. $param_url = \Yii::$app->request->hostInfo . '/web/food/store/' . get_store_id() . '/';
  1231. $wx_url = '/alipay-order/orderMeal/orderMeal';
  1232. }
  1233. $params = [
  1234. 'param_url' => $param_url,
  1235. 'wx_url' => $wx_url
  1236. ];
  1237. try {
  1238. //换取token
  1239. $aop = new AopClient();
  1240. $aop->appId = Option::get("alipay_appid", 0, 'saas')['value'];
  1241. $aop->rsaPrivateKey = Option::get("alipay_app_private_key", 0, 'saas')['value'];
  1242. $aop->alipayrsaPublicKey = Option::get("alipay_public_key", 0, 'saas')['value'];
  1243. $aop->encryptKey = Option::get("alipay_aes_key", 0, 'saas')['value'];
  1244. $aop->encryptType = "AES";
  1245. $aop->apiVersion = '1.0';
  1246. $aop->signType = 'RSA2';
  1247. $aop->postCharset = 'utf-8';
  1248. $StoreAliMini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancel' => 0])->select('id, auth_token, bind_food_qr')->orderBy('id desc')->one();
  1249. if (empty($StoreAliMini)) {
  1250. throw new \Exception("数据错误");
  1251. }
  1252. $data = [
  1253. "route_url" => $params['param_url'],
  1254. 'mode' => "FUZZY",
  1255. "page_redirection" => $params['wx_url']
  1256. ];
  1257. \Yii::error($data);
  1258. $biz_content = json_encode($data);
  1259. $request = new AlipayOpenMiniQrcodeBindRequest();
  1260. $request->setBizContent($biz_content);
  1261. $result = $aop->execute($request, null, $StoreAliMini->auth_token);
  1262. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  1263. $result = $result->$responseNode;
  1264. if (!empty($result->code) && $result->code == 10000) {
  1265. if ($type) {
  1266. Option::set('store_home_is_bind_ali', 1, get_store_id(), 'store');
  1267. } else {
  1268. $StoreAliMini->bind_food_qr = 1;
  1269. if (!$StoreAliMini->save()) {
  1270. throw new \Exception(json_encode($StoreAliMini->errors));
  1271. };
  1272. }
  1273. return $this->asJson(
  1274. [
  1275. 'code' => 0,
  1276. 'msg' => "成功",
  1277. 'data' => json_decode(json_encode($result), true)
  1278. ]
  1279. );
  1280. } else {
  1281. throw new \Exception($result->sub_msg);
  1282. }
  1283. } catch (\Exception $e) {
  1284. return $this->asJson([
  1285. 'code' => 1,
  1286. 'msg' => $e->getMessage()
  1287. ]);
  1288. }
  1289. }
  1290. public function initMiniProgram ()
  1291. {
  1292. try {
  1293. $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
  1294. if (empty($verify_ticket)) {
  1295. $openPlatform = null;
  1296. }else {
  1297. $config = [
  1298. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  1299. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  1300. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  1301. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  1302. ];
  1303. $openPlatform = Factory::openPlatform($config);
  1304. }
  1305. if (!$openPlatform) {
  1306. throw new \Exception("配置信息错误");
  1307. }
  1308. $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
  1309. if (empty($store)) {
  1310. throw new \Exception("应用信息未找到");
  1311. }
  1312. return [
  1313. 'code' => 0,
  1314. 'data' => [
  1315. 'miniProgram' => $openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token),
  1316. 'openPlatform' => $openPlatform
  1317. ]
  1318. ];
  1319. } catch (\Exception $e) {
  1320. return [
  1321. 'code' => 1,
  1322. 'msg' => $e->getMessage()
  1323. ];
  1324. }
  1325. }
  1326. //添加二维码规则
  1327. public function setQrcodeRules($params)
  1328. {
  1329. try {
  1330. $result = $this->initMiniProgram();
  1331. if ($result['code'] === 1) {
  1332. return $result;
  1333. }
  1334. $miniProgram = $result['data']['miniProgram'];
  1335. $data = [
  1336. 'prefix' => $params['param_url'],
  1337. 'permit_sub_rule' => 1,
  1338. 'path' => $params['wx_url'],
  1339. 'open_version' => 2,
  1340. 'is_edit' => 0
  1341. ];
  1342. $client = new BaseClient($miniProgram);
  1343. $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
  1344. if ((empty($result['errcode']) && !empty($result)) || ($result['errcode'] == "85071")) {
  1345. return [
  1346. 'code' => 0,
  1347. 'msg' => "成功",
  1348. 'data' => $result
  1349. ];
  1350. } elseif (empty($result)) {
  1351. throw new \Exception("数据错误");
  1352. } else {
  1353. throw new \Exception($this->getZnMsg($result));
  1354. }
  1355. } catch (\Exception $e) {
  1356. return [
  1357. 'code' => 1,
  1358. 'msg' => $e->getMessage()
  1359. ];
  1360. }
  1361. }
  1362. //发布二维码规则
  1363. public function submitQrcodeRules($params)
  1364. {
  1365. try {
  1366. $result = $this->initMiniProgram();
  1367. if ($result['code'] === 1) {
  1368. return $result;
  1369. }
  1370. $miniProgram = $result['data']['miniProgram'];
  1371. $data = [
  1372. 'prefix' => $params['param_url']
  1373. ];
  1374. $client = new BaseClient($miniProgram);
  1375. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
  1376. if (empty($res->errcode) && !empty($res)) {
  1377. return [
  1378. 'code' => 0,
  1379. 'msg' => "成功",
  1380. 'data' => $res
  1381. ];
  1382. } elseif (empty($res)) {
  1383. return [
  1384. 'code' => 1,
  1385. 'msg' => "数据错误"
  1386. ];
  1387. } else {
  1388. $res = json_decode($res);
  1389. return [
  1390. 'code' => $res['errcode'],
  1391. 'msg' => $this->getZnMsg($res),
  1392. 'data' => $res
  1393. ];
  1394. }
  1395. } catch (\Exception $e) {
  1396. return [
  1397. 'code' => 1,
  1398. 'msg' => $e->getMessage()
  1399. ];
  1400. }
  1401. }
  1402. //发布二维码规则
  1403. public function delQrcodeRules()
  1404. {
  1405. try {
  1406. $result = $this->initMiniProgram();
  1407. if ($result['code'] === 1) {
  1408. return $result;
  1409. }
  1410. $miniProgram = $result['data']['miniProgram'];
  1411. $data = [
  1412. 'prefix' => 'https://chidian.cyyvip.com/web/food/store/193/scene=md_id:125,store:193,store_id:193'
  1413. ];
  1414. $client = new BaseClient($miniProgram);
  1415. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdelete', $data);
  1416. if (empty($res->errcode) && !empty($res)) {
  1417. return [
  1418. 'code' => 0,
  1419. 'msg' => "成功",
  1420. 'data' => $res
  1421. ];
  1422. } elseif (empty($res)) {
  1423. return [
  1424. 'code' => 1,
  1425. 'msg' => "数据错误"
  1426. ];
  1427. } else {
  1428. $res = json_decode($res);
  1429. return [
  1430. 'code' => $res['errcode'],
  1431. 'msg' => $this->getZnMsg($res),
  1432. 'data' => $res
  1433. ];
  1434. }
  1435. } catch (\Exception $e) {
  1436. return [
  1437. 'code' => 1,
  1438. 'msg' => $e->getMessage()
  1439. ];
  1440. }
  1441. }
  1442. //获取二维码规则
  1443. public function getQrcodeRules()
  1444. {
  1445. try {
  1446. $result = $this->initMiniProgram();
  1447. if ($result['code'] === 1) {
  1448. return $result;
  1449. }
  1450. $miniProgram = $result['data']['miniProgram'];
  1451. $client = new BaseClient($miniProgram);
  1452. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
  1453. if (empty($res->errcode) && !empty($res)) {
  1454. return [
  1455. 'code' => 0,
  1456. 'msg' => "成功",
  1457. 'data' => $res
  1458. ];
  1459. } elseif (empty($res)) {
  1460. return [
  1461. 'code' => 1,
  1462. 'msg' => "数据错误"
  1463. ];
  1464. } else {
  1465. $res = json_decode($res);
  1466. return [
  1467. 'code' => $res['errcode'],
  1468. 'msg' => $this->getZnMsg($res),
  1469. 'data' => $res
  1470. ];
  1471. }
  1472. } catch (\Exception $e) {
  1473. return [
  1474. 'code' => 1,
  1475. 'msg' => $e->getMessage()
  1476. ];
  1477. }
  1478. }
  1479. //下载文件
  1480. public function actionDownQrcodeRules()
  1481. {
  1482. $store_id = get_store_id();
  1483. $md_id = get_md_id();
  1484. $type = get_params('type', 0);
  1485. if ($type) {
  1486. $url_path = 'https://'. \Yii::$app->request->hostName . '/web/home/store/' . get_store_id() . '/';
  1487. $wx_url = "/pages/home/home";
  1488. } else {
  1489. if ($md_id <= 0) {
  1490. $md_id = [0, -1];
  1491. }
  1492. $md_qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
  1493. $url_path = 'https://'. \Yii::$app->request->hostName . '/web/food/store/' . get_store_id() . '/';
  1494. if ($md_qrcode) {
  1495. $url_path = $md_qrcode->url_path;
  1496. }
  1497. $wx_url = "/alipay-order/orderMeal/orderMeal";
  1498. }
  1499. $params = [
  1500. 'param_url' => $url_path,
  1501. 'wx_url' => $wx_url
  1502. ];
  1503. try {
  1504. $result = $this->initMiniProgram();
  1505. if ($result['code'] === 1) {
  1506. return $this->asJson($result);
  1507. }
  1508. $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
  1509. $miniProgram = $result['data']['miniProgram'];
  1510. $client = new BaseClient($miniProgram);
  1511. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
  1512. if (empty($res['errcode']) && !empty($res)) {
  1513. if ($type) {
  1514. if (!is_dir(\Yii::$app->basePath . '/web/home/store/' . get_store_id())) {
  1515. mkdir(\Yii::$app->basePath . '/web/home/store/' . get_store_id(), 0777, true);
  1516. }
  1517. $file_result = file_put_contents(\Yii::$app->basePath . '/web/home/store/' . get_store_id() . '/' . $res['file_name'], $res['file_content']);
  1518. } else {
  1519. if (!is_dir(\Yii::$app->basePath . '/web/food/store/' . get_store_id())) {
  1520. mkdir(\Yii::$app->basePath . '/web/food/store/' . get_store_id(), 0777, true);
  1521. }
  1522. $file_result = file_put_contents(\Yii::$app->basePath . '/web/food/store/' . get_store_id() . '/' . $res['file_name'], $res['file_content']);
  1523. }
  1524. if ($file_result) {
  1525. $resRules = $this->setQrcodeRules($params);
  1526. if ($resRules['code'] === 0) {
  1527. $ress = $this->submitQrcodeRules($params);
  1528. \Yii::error($ress);
  1529. if ($ress['code'] === 0) {
  1530. $resGet = $this->getQrcodeRules();
  1531. \Yii::error($resGet);
  1532. } else {
  1533. throw new \Exception($ress['msg']);
  1534. }
  1535. } else {
  1536. throw new \Exception($resRules['msg']);
  1537. }
  1538. } else {
  1539. throw new \Exception("创建文件失败");
  1540. }
  1541. if ($type) {
  1542. Option::set('store_home_is_bind_wx', 1, get_store_id(), 'store');
  1543. } else {
  1544. $store->bind_food_qr = 1;
  1545. if (!$store->save()) {
  1546. throw new \Exception(json_encode($store->errors));
  1547. }
  1548. }
  1549. return $this->asJson(
  1550. [
  1551. 'code' => 0,
  1552. 'msg' => "成功",
  1553. 'data' => $res,
  1554. 'file' => $file_result
  1555. ]
  1556. );
  1557. } elseif (empty($res)) {
  1558. throw new \Exception("数据错误");
  1559. } else {
  1560. //$res = json_decode($res);
  1561. throw new \Exception($res['errmsg']);
  1562. }
  1563. } catch (\Exception $e) {
  1564. return $this->asJson([
  1565. 'code' => 1,
  1566. 'msg' => $e->getMessage()
  1567. ]);
  1568. }
  1569. }
  1570. //返回中文错误信息
  1571. public function getZnMsg($result)
  1572. {
  1573. $ErrorMsg = new ErrorMsg();
  1574. $arr = $ErrorMsg->getArray();
  1575. return !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
  1576. }
  1577. }