| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use AopClient;
- use app\constants\OptionSetting;
- use app\models\AggregateQrcode;
- use app\models\Banner;
- use app\models\Cat;
- use app\models\FoodFlag;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\MchCat;
- use app\models\MchGoodsCat;
- use app\models\Md;
- use app\models\MdFoodsQrcode;
- use app\models\MdGoods;
- use app\models\Option;
- use app\models\Store;
- use app\models\StoreAliMini;
- use app\models\StoreMini;
- use app\models\WechatConfig;
- use app\modules\admin\models\AlipayThirdForm;
- use app\modules\admin\models\BannerForm;
- use app\modules\admin\models\GoodsForm;
- use app\models\FoodExtGoods;
- use app\modules\admin\models\MdGoodsForm;
- use app\models\FoodCat;
- use app\models\FoodTableNumber;
- use app\modules\admin\models\WechatThirdErrorMsgForm as ErrorMsg;
- use app\modules\admin\models\WechatThirdForm;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAppQrcodeCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
- use app\utils\QrCode;
- use app\utils\ShareQrcode;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- use ZipArchive;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class MdGoodsController extends BaseController
- {
- /**
- * 平台获取商品列表
- * @return \yii\web\Response
- */
- public function actionList()
- {
- return $this->asJson(MdGoodsForm::getList(get_params()));
- }
- /**
- * 修改商品库存
- * @return \yii\web\Response
- */
- public function actionSetAttrNum()
- {
- $form = new MdGoodsForm();
- return $this->asJson($form->setAttrNum());
- }
- /**
- * 编辑商品数据
- */
- public function actionSetAttribute()
- {
- $form = new MdGoodsForm();
- return $this->asJson($form->setType());
- }
- /**
- * 编辑商品数据
- */
- public function actionSetAttr()
- {
- $status = post_params('status', -1);
- $delivery_type = post_params('delivery_type', null);
- $md_id = get_md_id();
- $goods_id = post_params('id');
- $md_goods = MdGoods::findOne([
- 'md_id' => $md_id,
- 'goods_id' => $goods_id
- ]);
- $is_new_record = false;
- if (!$md_goods) {
- $md_goods = new MdGoods();
- $md_goods->md_id = $md_id;
- $md_goods->goods_id = $goods_id;
- $is_new_record = true;
- }
- $goods = Goods::findOne($goods_id);
- if ($status > -1) {
- if ($md_goods->goods_num <= 0 && $status == Goods::STATUS_NORMAL) {
- return $this->asJson(['code' => 1, 'msg' => '请先添加商品库存之后再进行上架操作']);
- }
- $md_goods->status = $status;
- if ($is_new_record) {
- $md_goods->attr = $goods->attr;
- $md_goods->virtual_sales = $goods->virtual_sales;
- $md_goods->price = $goods->price;
- $md_goods->delivery_type = Md::findOne(get_md_id())->self_delivery_type;
- $md_goods->goods_num = $goods->goods_num;
- }
- }
- if (isset($delivery_type)) {
- $md_goods->delivery_type = Json::encode($delivery_type);
- if ($is_new_record) {
- $md_goods->attr = $goods->attr;
- $md_goods->virtual_sales = $goods->virtual_sales;
- $md_goods->price = $goods->price;
- $md_goods->goods_num = $goods->goods_num;
- }
- }
- if ($md_goods->save()) {
- return $this->asJson(['code' => 0, 'msg' => '保存成功']);
- } else {
- return $this->asJson(['code' => 1, 'msg' => $md_goods->errors[0]]);
- }
- }
- /**
- * 批量修改商品
- * @return \yii\web\Response
- */
- public function actionUpdateAll()
- {
- $status = post_params('status');
- $md_id = get_md_id();
- $goods_id = post_params('goods_id');
- if (!is_array($goods_id) || empty($goods_id)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => 'goods_id为空'
- ]);
- }
- $count = 0;
- foreach ($goods_id as $id) {
- $goods = Goods::findOne($id);
- $md_goods = MdGoods::findOne([
- 'md_id' => $md_id,
- 'goods_id' => $id
- ]);
- if (!$md_goods) {
- $md_goods = new MdGoods();
- $md_goods->md_id = $md_id;
- $md_goods->goods_id = $id;
- $md_goods->attr = $goods->attr;
- $md_goods->delivery_type = Md::findOne(get_md_id())->self_delivery_type;
- $md_goods->virtual_sales = $goods->virtual_sales;
- $md_goods->price = $goods->price;
- $md_goods->goods_num = $goods->goods_num;
- }
- $md_goods->status = $status;
- if ($md_goods->save()) {
- $count++;
- }
- }
- if ($count) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '共修改' . $count . '条数据'
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '修改数据为0条'
- ]);
- }
- }
- /**
- * 门店点餐商品
- * @return \yii\web\Response
- */
- public function actionFood() {
- $arr = get_params();
- $md_id = intval(get_md_id());
- $mch_id = intval(get_mch_id());
- $query = Goods::find()->alias('g')
- ->leftJoin(['fc' => FoodCat::tableName()], 'fc.id=g.cat_id')
- ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0])->andWhere(['not like', 'g.name', '当面付'])
- ->andWhere(['!=', 'g.md_food_id', 0]);
- if ($md_id <= 0) {
- $query->andWhere(['md_food_id' => -1]);
- } else {
- $query->andWhere(['md_food_id' => $md_id]);
- }
- $query->andWhere(['g.mch_id' => $mch_id]);
- if (isset($arr['status']) && $arr['status'] > -1) {
- $query->andWhere([
- 'g.status' => $arr['status']
- ]);
- }
- if (!empty($arr['name'])) {
- $query->andWhere([
- 'like', 'g.name', $arr['name']
- ]);
- }
- if (!empty($arr['cat_id'])) {
- $query->andWhere([
- 'fc.id' => $arr['cat_id']
- ]);
- }
- if (isset($arr['name']) && !empty($arr['name'])) {
- $query->andWhere([
- 'like',
- 'g.name',
- $arr['name']
- ]);
- }
- if (!empty($arr['select'])) {
- $select = $arr['select'];
- } else {
- $select = ['g.id', 'g.name', 'g.status', 'g.service',
- 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
- 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
- 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend',
- 'g.delivery_type', 'fc.name cat_name', 'g.md_food_id'];
- }
- $query->select($select)->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
- $pagination = pagination_make($query);
- $pagination['data'] = $pagination['list'];
- $cat_query = FoodCat::find()->where([
- 'is_delete' => 0,
- 'mch_id' => intval(get_mch_id()),
- 'store_id' => get_store_id()
- ]);
- if (get_md_id()) {
- $cat_query->andWhere(['md_id' => get_md_id()]);
- }
- $cat = $cat_query->asArray()->all();
- foreach($pagination['data'] as $k => $v) {
- $pagination['data'][$k]['delivery_type'] = Json::decode($v['delivery_type']);
- $pagination['data'][$k]['md_name'] = Md::findOne($v['md_food_id'])->name;
- }
- $pagination['cat'] = $cat;
- unset($pagination['list']);
- return $this->asJson([
- 'q' => $query->createCommand()->getRawSql(),
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination
- ]);
- }
- /**
- * 保存商品
- */
- public function actionFoodSave()
- {
- $arr = [];
- if(get_mch_id()){
- $arr['mch'] = 1;
- $arr['mch_id'] = get_mch_id();
- }
- $form = new GoodsForm();
- $form->store_id = get_store_id();
- $form->attributes = post_params();
- return $this->asJson($form->saveFood($arr));
- }
- //附加商品区保存
- public function actionFoodExtGoodsSave()
- {
- try{
- $store_id = get_store_id();
- $id = input_params('id');
- $md_id = input_params('md_id', 0);
- $name = input_params('name');
- $desc = input_params('desc');
- $cover_pic = input_params('cover_pic');
- $attr = input_params('attr');
- $save = FoodExtGoods::saveOne($store_id, $name, $desc, $cover_pic, $attr, $id, $md_id, intval(get_mch_id()));
- return $this->asJson([
- 'code' => $save ? 0 : 1,
- 'msg' => $save ? 'ok' : 'faild',
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- //附加商品区列表
- public function actionFoodExtGoodsList()
- {
- $store_id = get_store_id();
- $name = input_params('name');
- $md_id = input_params('md_id', 0);
- $list = FoodExtGoods::list([
- 'mch_id' => intval(get_mch_id()),
- 'name' => $name,
- 'md_id' => $md_id,
- ], $store_id);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $list,
- ]);
- }
- //附加商品区删除
- public function actionFoodExtGoodsDel()
- {
- try{
- $store_id = get_store_id();
- $id = input_params('id');
- $save = FoodExtGoods::del($id, $store_id);
- return $this->asJson([
- 'code' => $save ? 0 : 1,
- 'msg' => $save ? 'ok' : 'faild',
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- //附加商品区设置默认
- public function actionFoodExtGoodsDefault()
- {
- try{
- $store_id = get_store_id();
- $id = input_params('id');
- $md_id = input_params('md_id', 0);
- $save = FoodExtGoods::setDefault($id, $store_id, $md_id, intval(get_mch_id()));
- return $this->asJson([
- 'code' => $save ? 0 : 1,
- 'msg' => $save ? 'ok' : 'faild',
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- /**
- * 保存商品
- */
- public function actionFoodCat()
- {
- $where = [
- 'store_id' => get_store_id(),
- 'mch_id' => intval(get_mch_id()),
- 'is_delete' => 0,
- 'is_show' => 1
- ];
- $where['md_id'] = get_md_id() > 0 ? get_md_id() : [0, -1];
- $cat = FoodCat::findAll($where);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $cat
- ]);
- }
- // 幻灯片列表
- public function actionBannerList()
- {
- $form = new BannerForm();
- $form->type = Banner::TYPE_FOOD;
- $form->md_id = get_md_id();
- return $this->asJson($form->getList(get_store_id(), intval(get_mch_id())));
- }
- // 幻灯片编辑
- public function actionBannerEdit()
- {
- $arr = [];
- if(get_mch_id()){
- $arr['mch'] = 1;
- $arr['mch_id'] = intval(get_mch_id());
- }
- $id = input_params('id');
- $banner = Banner::findOne(['id' => $id, 'type' => Banner::TYPE_FOOD, 'md_id' => get_md_id()]);
- if (!$banner) {
- $banner = new Banner();
- }
- if (\Yii::$app->request->isPost) {
- $form = new BannerForm();
- $form->attributes = post_params();
- $form->store_id = get_store_id();
- $form->banner = $banner;
- $form->md_id = get_md_id();
- $form->type = Banner::TYPE_FOOD;
- return $this->asJson($form->save($arr));
- }
- foreach ($banner as $index => $value) {
- $banner[$index] = str_replace("\"", """, $value);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $banner
- ]);
- }
- // 幻灯片删除
- public function actionBannerDelete()
- {
- $id = input_params('id');
- $banner = Banner::findOne(['id' => $id, 'is_delete' => 0, 'type' => Banner::TYPE_FOOD, 'md_id' => get_md_id()]);
- if (!$banner) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '幻灯片不存在或已经删除',
- ]);
- }
- $banner->is_delete = 1;
- if ($banner->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '成功',
- ]);
- } else {
- foreach ($banner->errors as $errors) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $errors[0],
- ]);
- }
- }
- }
- /**
- * 商品编辑
- * @param $id
- * @return \yii\web\Response
- */
- public function actionEdit()
- {
- $id = get_params('id') ?: 0;
- $goodsFrom = new GoodsForm();
- return $this->asJson($goodsFrom->getFoodEdit($id));
- }
- /**
- * 编辑商品数据
- */
- public function actionUpdateAttribute()
- {
- $form = new GoodsForm();
- return $this->asJson($form->updateAttribute());
- }
- /**
- * 修改商品库存
- * @return \yii\web\Response
- */
- public function actionUpdateAttrNum()
- {
- $form = new GoodsForm();
- return $this->asJson($form->updateAttrNum());
- }
- /**
- * @return \yii\web\Response
- */
- public function actionTableList()
- {
- $num = get_params('num');
- $md_id = get_md_id();
- if ($md_id <= 0) {
- $md_id = [0, -1];
- }
- $mch_id = get_mch_id();
- $query = FoodTableNumber::find()->where([
- 'is_delete' => 0,
- 'mch_id' => intval($mch_id),
- 'store_id' => get_store_id(),
- ]);
- if (get_md_id()) {
- $query->andWhere(['md_id' => get_md_id()]);
- }
- if ($num) {
- $query->andWhere(['num' => $num]);
- }
- $pagination = pagination_make($query);
- $list = $pagination['list'];
- foreach ($list as &$item) {
- $food_flag = FoodFlag::find()->where([
- 'store_id' => get_store_id(),
- 'mch_id' => intval(get_mch_id()),
- 'md_id' => $md_id,
- 'table_num' => $item['num'],
- 'status' => 0,
- 'type' => 1
- ])->asArray()->one();
- $item['is_can_clear'] = 0;
- if ($food_flag) {
- $item['is_can_clear'] = 1;
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ]);
- }
- public function actionClearTable() {
- $store_id = get_store_id();
- $md_id = get_md_id();
- if ($md_id <= 0) {
- $md_id = [0, -1];
- }
- $mch_id = get_mch_id();
- $id = post_params('id');
- $foodTable = FoodTableNumber::findOne($id);
- if (!$foodTable) {
- return [
- 'code' => 1,
- 'msg' => '记录不存在!'
- ];
- }
- $num = $foodTable->num;
- if (empty($num)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '桌号错误'
- ]);
- }
- FoodFlag::updateAll(['status' => 1], [
- 'store_id' => $store_id,
- 'mch_id' => intval($mch_id),
- 'md_id' => $md_id,
- 'table_num' => $num,
- 'status' => 0,
- 'type' => 1
- ]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => '处理完成'
- ]);
- }
- public function actionTableAdd()
- {
- $store_id = post_params('storeId', get_store_id());
- $params = [
- 'num' => post_params('num', ''),
- 'remark' => post_params('remark'),
- ];
- if (!is_numeric($params['num'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '桌号必须为数字!'
- ]);
- }
- $is = FoodTableNumber::find()->where([
- 'store_id' => $store_id,
- 'mch_id' => intval(get_mch_id()),
- 'md_id' => get_md_id(),
- 'num' => $params['num'],
- 'is_delete' => 0,
- ])->one();
- if ($is) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '桌号已经存在!'
- ]);
- }
- $form = new FoodTableNumber();
- $form->store_id = $store_id;
- get_mch_id() && $form->mch_id = intval(get_mch_id());
- $form->num = $params['num'];
- $form->md_id = get_md_id();
- $form->remark = $params['remark'];
- if ($form->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '创建成功!'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '创建失败!'
- ]);
- }
- public function actionTableEdit()
- {
- $store_id = post_params('storeId', get_store_id());
- $params = [
- 'id' => post_params('id'),
- 'num' => post_params('num', ''),
- 'remark' => post_params('remark'),
- ];
- if (!is_numeric($params['num'])) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '桌号必须为数字!'
- ]);
- }
- $is = FoodTableNumber::find()->where([
- 'store_id' => $store_id,
- 'mch_id' => intval(get_mch_id()),
- 'num' => $params['num'],
- 'is_delete' => 0,
- 'md_id' => get_md_id(),
- ])->andWhere(['<>', 'id', $params['id']])->one();
- if ($is) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '桌号已经存在!'
- ]);
- }
- $form = FoodTableNumber::findOne($params['id']);
- if (!$form) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '记录不存在!'
- ]);
- }
- $form->num = $params['num'];
- $form->remark = $params['remark'];
- if ($form->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '创建成功!'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '创建失败!'
- ]);
- }
- public function actionTableDel()
- {
- $id = post_params('id');
- $form = FoodTableNumber::findOne($id);
- if (!$form) {
- return [
- 'code' => 1,
- 'msg' => '记录不存在!'
- ];
- }
- $form->is_delete = FoodTableNumber::DELETE_STATUS_TRUE;
- if ($form->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功!'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '删除失败!'
- ]);
- }
- // 批量下载桌号二维码
- public function actionTableQrcodes(){
- $nums = post_params('nums');
- $store_id = get_store_id();
- $storeInfo = Store::findOne($store_id);
- $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
- $mch_id = intval(get_mch_id());
- $md_id = -1;
- // 必须是数组
- if(!is_array($nums) || empty($nums)){
- return $this->asJson([
- 'code'=>1,
- 'msg'=>"请选择要下载的桌号",
- ]);
- }
- $paths = [];
- foreach($nums as $item){
- $table = FoodTableNumber::findOne([
- 'is_delete' => 0,
- 'store_id' => $store_id,
- 'mch_id' => intval(get_mch_id()),
- 'id' => $item,
- 'md_id' => $md_id <= 0 ? [0, -1] : $md_id
- ]);
- if(empty($table)){
- continue;
- }
- if((!\Yii::$app->prod_is_dandianpu() && $storeInfo->business_model == 1) || (\Yii::$app->prod_is_dandianpu() && (int)$self_mini === 1)){
- $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$table->num}";
- }else{
- $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$table->num},f_num:{$item},store_id:{$store_id}";
- }
- $paths[] = $this->makeZipFoodQr($scene, $table->num,$table->remark);
- }
- if(empty($paths)){
- return $this->asJson([
- 'code'=>1,
- 'msg'=>"二维码生成失败",
- ]);
- }
- // 打包压缩包 开始下载
- $zipPath = \Yii::$app->runtimePath . '/zip/food-table/';
- if(!file_exists($zipPath)){
- mkdir($zipPath, 0777, true);
- }
- $zipFileName = md5(time().implode(',',$nums)).'.zip';
- $zip = new ZipArchive();
- if ($zip->open($zipPath.$zipFileName, ZipArchive::CREATE) === TRUE) {
- // 将每个图片文件添加到压缩包中
- foreach ($paths as $image) {
- if (file_exists($image)) {
- $zip->addFile($image, basename($image));
- }
- }
- // 关闭压缩包
- $zip->close();
- $returnPath = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/zip/food-table/' . $zipFileName );
- return $this->asJson(['code' => 0, 'data' => $returnPath]);
- } else {
- echo '无法创建压缩包。';
- }
- }
- private function makeZipFoodQr($scene, $num,$remark){
- $store_id = get_store_id();
- $mch_id = intval(get_mch_id());
- $md_id = get_md_id();
- $type_ = get_params('type', 0);
- if ($type_) {
- $type = 2;
- }
- $scene = $scene ? $scene : ("md_id:{$md_id},mch_id:{$mch_id},store:{$store_id},store_id:{$store_id}");
- $store = Store::findOne(['id' => $store_id]);
- if ($store) {
- if ($md_id <= 0) {
- $md_id = [0, 1];
- }
- $qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'mch_id' => intval(get_mch_id()), 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
- if ($qrcode && !$type) {
- $filename = $num .'+'.$remark;
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- if(!empty($qrcode->url_path)){
- $text = $qrcode->url_path;
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- $qrcode->qrcode_url = $pic_url;
- $qrcode->save();
- }
- } else {
- $scene = 'scene=' . $scene;
- if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
- if (intval($type) === 2) {
- //设置二维码规则
- $text = \Yii::$app->request->hostInfo . '/web/home/store/' . $store_id . '/?'. $scene;
- } else {
- //设置二维码规则
- $text = \Yii::$app->request->hostInfo . '/web/food/store/' . $store_id . '/?'. $scene;
- if (!\Yii::$app->isSaas()) {
- $text = \Yii::$app->request->hostInfo . '/web/food/' . $store_id . '?'. $scene;
- }
- }
- } else {
- $text = \Yii::$app->request->hostInfo . '/web/food/saas?' . $scene;
- }
- $filename = $num ;
- if(!empty($remark)){
- $filename .= '+'.$remark;
- }
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- }
- return $path;
- } else {
- return [
- 'code' => 1,
- 'msg' => "商城信息获取失败"
- ];
- }
- }
- public function actionDownQr()
- {
- $num = post_params('num', 0);
- $store_id = get_store_id();
- $mch_id = intval(get_mch_id());
- $md_id = get_md_id();
- $storeInfo = Store::findOne($store_id);
- $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
- $food_table_number = FoodTableNumber::findOne([
- 'is_delete' => 0,
- 'store_id' => $store_id,
- 'mch_id' => intval(get_mch_id()),
- 'num' => $num,
- 'md_id' => $md_id <= 0 ? [0, -1] : $md_id
- ]);
- $table_num = 0;
- if ($food_table_number) {
- $table_num = $food_table_number->id;
- }
- $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]);
- $pic_url = '';
- if ($qrcode) {
- $filename = md5(date('Ym') . $qrcode->id . 'md_foods_qrcode');
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- if(!empty($qrcode->url_path)){
- $text = $qrcode->url_path;
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- $qrcode->qrcode_url = $pic_url;
- $qrcode->save();
- }
- $is_saas = false;
- $business_model = $storeInfo->business_model;
- if (\Yii::$app->isSaas()) {
- $is_saas = true;
- }
- if (\Yii::$app->prod_is_dandianpu()) {
- $business_model = 0;
- if ($self_mini) {
- $business_model = 1;
- }
- }
- return $this->asJson([
- 'code'=>0,
- 'msg'=>"成功",
- 'data'=>[
- $pic_url
- ],
- 'business_model' => $business_model,
- 'is_saas' => $is_saas
- ]);
- } else {
- if((!\Yii::$app->prod_is_dandianpu() && $storeInfo->business_model == 1) || (\Yii::$app->prod_is_dandianpu() && (int)$self_mini === 1)){
- $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$num}";
- }else{
- $scene = "md_id:{$md_id},mch_id:{$mch_id},food_table_num:{$num},f_num:{$num},store_id:{$store_id}";
- }
- return $this->actionFoodQr($scene, 1);
- }
- // $filename = md5($store_id . '-' . $md_id . '-' . $num);
- // $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- // $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- // if (file_exists($path)) {
- // return $this->asJson([
- // 'code' => 0,
- // 'data' => [
- // 'qr_url' => $pic_url,
- // ]
- // ]);
- // }
- // $id = FoodTableNumber::findOne(['num' => $num, 'md_id' => $md_id, 'is_delete' => 0])->id;
- // $config = WechatConfig::findOne(['store_id' => $store_id]);
- // if (empty($config->app_id)) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => "数据查找失败"
- // ]);
- // }
- // $wx_mini = StoreMini::findOne(['appid' => $config->app_id])->where(['store_id' => $store_id])->orderBy('id desc')->one();
- // $form = new WechatThirdForm();
- // $result = $form->downQrcodeRules($wx_mini->id, $wx_mini->appid);
- // if ($result['code'] !== 0) {
- // return $this->asJson([
- // 'code' => 1,
- // 'msg' => $result['msg']
- // ]);
- // }
- // $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/food/' . $store_id . '?md_id='. $md_id . '&food_table_num=' . $id);
- // QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- // $goods_qrcode = new \claviska\SimpleImage($path);
- // $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
- // $bg = new \claviska\SimpleImage($bgPath);
- // $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
- // $bg->toFile($path, 'image/jpeg', 85);
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'qr_url' => $pic_url,
- ]
- ]);
- }
- public function aliConfigQr($request, $data = [])
- {
- try {
- $third_appid = Option::get("alipay_appid", 0, 'saas')['value'];
- $third_private_key = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- $third_public_key = Option::get("alipay_public_key", 0, 'saas')['value'];
- $data = json_encode($data);
- $config_ali = Option::get(Option::OPTOPN_KEY, get_store_id(), 'alipay')['value'];
- $config_ali = json_decode($config_ali, true);
- $aop = new AopClient();
- //如果商城的支付宝小程序信息存在,则使用支付宝小程序的配置信息
- if (!empty($config_ali['app_id']) && !empty($config_ali['app_private_key']) && !empty($config_ali['app_public_key'])) {
- $aop->appId = $config_ali['app_id'];
- $aop->rsaPrivateKey = $config_ali['app_private_key'];
- $aop->alipayrsaPublicKey = $config_ali['app_public_key'];
- } else {
- //使用三方的配置信息
- $aop->appId = $third_appid;
- $aop->rsaPrivateKey = $third_private_key;
- $aop->alipayrsaPublicKey = $third_public_key;
- $ali_mini = StoreAliMini::find()->where(['auth_app_id' => $config_ali['app_id'], 'is_cancel' => 0, 'store_id' => get_store_id()])->asArray()->one();
- if (empty($ali_mini)) {
- throw new \Exception("查找小程序信息失败");
- }
- $token = $ali_mini['auth_token'];
- }
- $aop->encryptType = "AES";
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $aop->format = 'json';
- $request->setBizContent($data);
- if (!empty($token)) {
- $result = $aop->execute ($request, null, $token);
- } else {
- $result = $aop->execute ($request);
- }
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return $result->$responseNode;
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function actionFoodSettingInfo() {
- $md_id = get_md_id();
- $md = Md::findOne($md_id);
- if (!$md) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '门店数据不存在'
- ]);
- }
- if (!$md->food_payment) {
- $payment = [
- [
- 'key' => 'alipay',
- 'value' => 0,
- 'name' => ''
- ],
- [
- 'key' => 'wechat',
- 'value' => 1,
- 'name' => ''
- ],
- [
- 'key' => 'huodao',
- 'value' => 0,
- 'name' => ''
- ],
- [
- 'key' => 'friend',
- 'value' => 0,
- 'name' => ''
- ],
- ];
- $md->food_payment = Json::encode($payment);
- $md->save();
- }
- $food_page_title = Option::get('food_page_title', $md_id, 'md', '')['value'];
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'food_payment' => Json::decode($md->food_payment),
- 'food_pay_type' => $md->food_pay_type,
- 'food_page_title' => $food_page_title ?: '手机点餐'
- ]
- ]);
- }
- public function actionFoodSetting() {
- $md_id = get_md_id();
- $md = Md::findOne($md_id);
- $payment = post_params('food_payment');
- $pay_type = post_params('food_pay_type', 0);
- $food_page_title = post_params('food_page_title', '');
- if (!$md) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '门店数据不存在'
- ]);
- }
- $md->food_pay_type = $pay_type;
- $md->food_payment = $payment;
- if (!$md->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $md->errors
- ]);
- }
- Option::set('food_page_title', $food_page_title, $md->id, 'md');
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- }
- public function actionFoodQr($scene = "", $type = 0)
- {
- //微信小程序码 start
- // $wx_url_path = "";
- // $ali_url_path = "";
- $store_id = get_store_id();
- $mch_id = intval(get_mch_id());
- $md_id = get_md_id();
- $type_ = get_params('type', 0);
- if ($type_) {
- $type = 2;
- }
- $scene = $scene ? $scene : ("md_id:{$md_id},mch_id:{$mch_id},store:{$store_id},store_id:{$store_id}");
- $store = Store::findOne(['id' => $store_id]);
- if ($store) {
- // $scene = 'scene=' . $scene;
- // if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
- // //设置二维码规则
- // $text = \Yii::$app->request->hostInfo . '/web/food/store/' . $store_id . '/?'. $scene;
- // if (!\Yii::$app->isSaas()) {
- // $text = \Yii::$app->request->hostInfo . '/web/food/' . $store_id . '?'. $scene;
- // }
- // } else {
- // $text = \Yii::$app->request->hostInfo . '/web/food/saas?' . $scene;
- // }
- // $filename = md5(date('Ym') . 'ali' . $store_id . '_' . $scene);
- // $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- // $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- // QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- // $is_saas = false;
- // $business_model = $store->business_model;
- // if (\Yii::$app->isSaas()) {
- // $is_saas = true;
- // }
- //
- // if (\Yii::$app->prod_is_dandianpu()) {
- // $business_model = 0;
- // $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
- // if ($self_mini) {
- // $business_model = 1;
- // }
- // }
- if ($md_id <= 0) {
- $md_id = [0, 1];
- }
- $qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'mch_id' => intval(get_mch_id()), 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
- if ($qrcode && !$type) {
- $filename = md5(date('Ym') . $qrcode->id . 'md_foods_qrcode');
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- if(!empty($qrcode->url_path)){
- $text = $qrcode->url_path;
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- $qrcode->qrcode_url = $pic_url;
- $qrcode->save();
- }
- } else {
- $scene = 'scene=' . $scene;
- if ((int)$store->business_model === 1 && !\Yii::$app->prod_is_dandianpu()) {
- if (intval($type) === 2) {
- //设置二维码规则
- $text = 'https://' . \Yii::$app->request->hostName . '/web/home/store/' . $store_id . '/?'. $scene;
- } else {
- //设置二维码规则
- $text = 'https://' . \Yii::$app->request->hostName . '/web/food/store/' . $store_id . '/?'. $scene;
- if (!\Yii::$app->isSaas()) {
- $text = 'https://' . \Yii::$app->request->hostName . '/web/food/' . $store_id . '?'. $scene;
- }
- }
- } else {
- $text = 'https://' . \Yii::$app->request->hostName . '/web/food/saas?' . $scene;
- }
- $filename = md5(date('Ym') . 'ali' . $store_id . '_' . $scene);
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- }
- $is_saas = false;
- $business_model = $store->business_model;
- if (\Yii::$app->isSaas()) {
- $is_saas = true;
- }
- if (\Yii::$app->prod_is_dandianpu()) {
- $business_model = 0;
- $self_mini = Option::get('self_mini', $store_id ?: get_store_id(), 'store', 0)['value'];
- if ($self_mini) {
- $business_model = 1;
- }
- }
- return $this->asJson([
- 'code'=>0,
- 'msg'=>"成功",
- 'data'=>[
- $pic_url ?? ""
- ],
- 'mch_id' => $mch_id,
- 'business_model' => $business_model,
- 'is_saas' => $is_saas
- ]);
- } else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => "商城信息获取失败"
- ]);
- }
- }
- public function actionCatList()
- {
- $name = get_params('name');
- $query = FoodCat::find()->where([
- 'is_delete' => 0,
- 'mch_id' => intval(get_mch_id()),
- 'store_id' => get_store_id()
- ]);
- if (!empty($name)) {
- $query->andWhere(['like', 'name', $name]);
- }
- if (get_md_id()) {
- $query->andWhere(['md_id' => get_md_id()]);
- }
- $query->orderBy(['sort' => SORT_DESC]);
- $list = pagination_make($query);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ],
- ]);
- }
- public function actionCatStatus()
- {
- $id = post_params('id');
- $cat = FoodCat::findOne($id);
- $cat->is_show = $cat->is_show === 1 ? 0 : 1;
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '修改成功'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '修改失败',
- ]);
- }
- public function actionCatDel()
- {
- $id = post_params('id');
- $cat = FoodCat::findOne($id);
- $cat->is_delete = 1;
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '删除失败',
- ]);
- }
- public function actionCatEdit()
- {
- $store_id = get_store_id();
- $id = post_params('id', 0);
- $name = post_params('name');
- $desc = post_params('desc', '');
- $pic_url = post_params('pic_url', '');
- $sort = post_params('sort');
- $is_show = post_params('is_show', 1);
- if ($id > 0) {
- $cat = FoodCat::findOne($id);
- } else {
- $cat = new FoodCat();
- get_mch_id() && $cat->mch_id = intval(get_mch_id());
- }
- $cat->store_id = $store_id;
- $cat->name = $name;
- $cat->desc = $desc;
- $cat->pic_url = $pic_url;
- $cat->sort = $sort;
- $cat->is_show = $is_show;
- $cat->md_id = get_md_id();
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $id > 0 ? '编辑成功' : '添加成功',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => $id > 0 ? '编辑失败' : '添加失败',
- ]);
- }
- //获取是否绑定小程序码
- public function actionIsBind()
- {
- try {
- $type = get_params('type', 0);
- if ($type) {
- $is_wx_bind = (int)Option::get('store_home_is_bind_wx', get_store_id(), 'store', 0)['value'];
- $is_ali_bind = (int)Option::get('store_home_is_bind_ali', get_store_id(), 'store', 0)['value'];
- } else {
- //支付宝
- $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]);
- if (!$md_qrcode) {
- $is_ali_bind = 0;
- $StoreAliMini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancel' => 0])->select('id, bind_food_qr')->orderBy('id desc')->one();
- if (!empty($StoreAliMini)) {
- if ((int)$StoreAliMini->bind_food_qr === 1) {
- $is_ali_bind = 1;
- }
- }
- $is_wx_bind = 0;
- $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
- if (!empty($store)) {
- if ((int)$store->bind_food_qr === 1) {
- $is_wx_bind = 1;
- }
- }
- } else {
- $is_wx_bind = $md_qrcode->wx_status;
- $is_ali_bind = $md_qrcode->ali_status;
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'is_wx_bind' => $is_wx_bind,
- 'is_ali_bind' => $is_ali_bind
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //关联普通二维码
- public function actionAliBindQrcode()
- {
- $type = get_params('type', 0);
- if ($type) {
- $param_url = \Yii::$app->request->hostInfo . '/web/home/store/' . get_store_id() . '/';
- $wx_url = '/pages/home/home';
- } else {
- $param_url = \Yii::$app->request->hostInfo . '/web/food/store/' . get_store_id() . '/';
- $wx_url = '/alipay-order/orderMeal/orderMeal';
- }
- $params = [
- 'param_url' => $param_url,
- 'wx_url' => $wx_url
- ];
- try {
- //换取token
- $aop = new AopClient();
- $aop->appId = Option::get("alipay_appid", 0, 'saas')['value'];
- $aop->rsaPrivateKey = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- $aop->alipayrsaPublicKey = Option::get("alipay_public_key", 0, 'saas')['value'];
- $aop->encryptKey = Option::get("alipay_aes_key", 0, 'saas')['value'];
- $aop->encryptType = "AES";
- $aop->apiVersion = '1.0';
- $aop->signType = 'RSA2';
- $aop->postCharset = 'utf-8';
- $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();
- if (empty($StoreAliMini)) {
- throw new \Exception("数据错误");
- }
- $data = [
- "route_url" => $params['param_url'],
- 'mode' => "FUZZY",
- "page_redirection" => $params['wx_url']
- ];
- \Yii::error($data);
- $biz_content = json_encode($data);
- $request = new AlipayOpenMiniQrcodeBindRequest();
- $request->setBizContent($biz_content);
- $result = $aop->execute($request, null, $StoreAliMini->auth_token);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $result = $result->$responseNode;
- if (!empty($result->code) && $result->code == 10000) {
- if ($type) {
- Option::set('store_home_is_bind_ali', 1, get_store_id(), 'store');
- } else {
- $StoreAliMini->bind_food_qr = 1;
- if (!$StoreAliMini->save()) {
- throw new \Exception(json_encode($StoreAliMini->errors));
- };
- }
- return $this->asJson(
- [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => json_decode(json_encode($result), true)
- ]
- );
- } else {
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- public function initMiniProgram ()
- {
- try {
- $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
- if (empty($verify_ticket)) {
- $openPlatform = null;
- }else {
- $config = [
- 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
- 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
- 'token' => Option::get("platform_token", 0, 'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
- ];
- $openPlatform = Factory::openPlatform($config);
- }
- if (!$openPlatform) {
- throw new \Exception("配置信息错误");
- }
- $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
- if (empty($store)) {
- throw new \Exception("应用信息未找到");
- }
- return [
- 'code' => 0,
- 'data' => [
- 'miniProgram' => $openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token),
- 'openPlatform' => $openPlatform
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //添加二维码规则
- public function setQrcodeRules($params)
- {
- try {
- $result = $this->initMiniProgram();
- if ($result['code'] === 1) {
- return $result;
- }
- $miniProgram = $result['data']['miniProgram'];
- $data = [
- 'prefix' => $params['param_url'],
- 'permit_sub_rule' => 1,
- 'path' => $params['wx_url'],
- 'open_version' => 2,
- 'is_edit' => 0
- ];
- $client = new BaseClient($miniProgram);
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
- if ((empty($result['errcode']) && !empty($result)) || ($result['errcode'] == "85071")) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } elseif (empty($result)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($result));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //发布二维码规则
- public function submitQrcodeRules($params)
- {
- try {
- $result = $this->initMiniProgram();
- if ($result['code'] === 1) {
- return $result;
- }
- $miniProgram = $result['data']['miniProgram'];
- $data = [
- 'prefix' => $params['param_url']
- ];
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
- if (empty($res->errcode) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //发布二维码规则
- public function delQrcodeRules()
- {
- try {
- $result = $this->initMiniProgram();
- if ($result['code'] === 1) {
- return $result;
- }
- $miniProgram = $result['data']['miniProgram'];
- $data = [
- 'prefix' => 'https://chidian.cyyvip.com/web/food/store/193/scene=md_id:125,store:193,store_id:193'
- ];
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdelete', $data);
- if (empty($res->errcode) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取二维码规则
- public function getQrcodeRules()
- {
- try {
- $result = $this->initMiniProgram();
- if ($result['code'] === 1) {
- return $result;
- }
- $miniProgram = $result['data']['miniProgram'];
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
- if (empty($res->errcode) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //下载文件
- public function actionDownQrcodeRules()
- {
- $store_id = get_store_id();
- $md_id = get_md_id();
- $type = get_params('type', 0);
- if ($type) {
- $url_path = 'https://'. \Yii::$app->request->hostName . '/web/home/store/' . get_store_id() . '/';
- $wx_url = "/pages/home/home";
- } else {
- if ($md_id <= 0) {
- $md_id = [0, -1];
- }
- $md_qrcode = MdFoodsQrcode::findOne(['store_id' => $store_id, 'md_id' => $md_id, 'table_num' => 0, 'is_delete' => 0]);
- $url_path = 'https://'. \Yii::$app->request->hostName . '/web/food/store/' . get_store_id() . '/';
- if ($md_qrcode) {
- $url_path = $md_qrcode->url_path;
- }
- $wx_url = "/alipay-order/orderMeal/orderMeal";
- }
- $params = [
- 'param_url' => $url_path,
- 'wx_url' => $wx_url
- ];
- try {
- $result = $this->initMiniProgram();
- if ($result['code'] === 1) {
- return $this->asJson($result);
- }
- $store = StoreMini::find()->where(['store_id' => get_store_id(), 'is_use' => 1, 'is_cancle' => 0])->orderBy("id desc")->one();
- $miniProgram = $result['data']['miniProgram'];
- $client = new BaseClient($miniProgram);
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
- if (empty($res['errcode']) && !empty($res)) {
- if ($type) {
- if (!is_dir(\Yii::$app->basePath . '/web/home/store/' . get_store_id())) {
- mkdir(\Yii::$app->basePath . '/web/home/store/' . get_store_id(), 0777, true);
- }
- $file_result = file_put_contents(\Yii::$app->basePath . '/web/home/store/' . get_store_id() . '/' . $res['file_name'], $res['file_content']);
- } else {
- if (!is_dir(\Yii::$app->basePath . '/web/food/store/' . get_store_id())) {
- mkdir(\Yii::$app->basePath . '/web/food/store/' . get_store_id(), 0777, true);
- }
- $file_result = file_put_contents(\Yii::$app->basePath . '/web/food/store/' . get_store_id() . '/' . $res['file_name'], $res['file_content']);
- }
- if ($file_result) {
- $resRules = $this->setQrcodeRules($params);
- if ($resRules['code'] === 0) {
- $ress = $this->submitQrcodeRules($params);
- \Yii::error($ress);
- if ($ress['code'] === 0) {
- $resGet = $this->getQrcodeRules();
- \Yii::error($resGet);
- } else {
- throw new \Exception($ress['msg']);
- }
- } else {
- throw new \Exception($resRules['msg']);
- }
- } else {
- throw new \Exception("创建文件失败");
- }
- if ($type) {
- Option::set('store_home_is_bind_wx', 1, get_store_id(), 'store');
- } else {
- $store->bind_food_qr = 1;
- if (!$store->save()) {
- throw new \Exception(json_encode($store->errors));
- }
- }
- return $this->asJson(
- [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- 'file' => $file_result
- ]
- );
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- //$res = json_decode($res);
- throw new \Exception($res['errmsg']);
- }
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- //返回中文错误信息
- public function getZnMsg($result)
- {
- $ErrorMsg = new ErrorMsg();
- $arr = $ErrorMsg->getArray();
- return !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
- }
- }
|