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']; } }