| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers\adopt;
- use app\models\Goods;
- use app\models\Md;
- use app\models\MdGoods;
- use app\models\Store;
- use app\modules\admin\controllers\BaseController;
- use app\modules\admin\models\adopt\AdoptGoodsForm;
- use app\modules\admin\models\adopt\DeviceForm;
- use app\modules\admin\models\GoodsForm;
- use app\modules\admin\models\MdGoodsForm;
- use app\plugins\adopt\models\AdoptCat;
- use app\plugins\adopt\models\AdoptGoods;
- use app\plugins\adopt\models\AdoptSetting;
- use app\utils\QrCode;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- class AdoptController extends BaseController
- {
- /**
- * 平台获取商品列表
- * @return \yii\web\Response
- */
- public function actionList()
- {
- return $this->asJson(AdoptGoodsForm::getList(get_params()));
- }
- /**
- * 修改商品库存
- * @return \yii\web\Response
- */
- public function actionSetAttrNum()
- {
- $form = new AdoptGoodsForm();
- return $this->asJson($form->setAttrNum());
- }
- /**
- * 编辑商品数据
- */
- public function actionSetAttribute()
- {
- $form = new AdoptGoodsForm();
- 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 actionStatus()
- {
- $goods = Goods::findOne(get_params('id'));
- if (empty($goods)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '查无此商品'
- ]);
- }
- $type = get_params('type','status');
- if ($goods->product_type == Goods::GOODS_TYPE_NORMAL) {
- if ($type == 'status' && empty($goods->goods_num) && $goods->status == 0) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '请添加库存'
- ]);
- }
- }
- $value = get_params('value',-1);
- $goods->$type = $value == -1 ? ($goods->$type ? 0 : 1) : $value;
- if ($goods->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '编辑成功'
- ]);
- }else {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '编辑失败'
- ]);
- }
- }
- /**
- * 批量修改认养
- * @return \yii\web\Response
- */
- public function actionUpdateAll()
- {
- $status = get_params('status');
- $type = get_params('type');
- // $updateData = [
- // get_params('type') => get_params('status')
- // ];
- $goods_id = get_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);
- $adopt_goods = AdoptGoods::findOne([
- 'goods_id' => $id,
- 'is_delete' => 0
- ]);
- $goods->$type = $status;
- if($type == 'is_delete' && $adopt_goods){
- $adopt_goods->is_delete = $status;
- $adopt_goods->save();
- }
- if ($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 actionLand() {
- $arr = get_params();
- $query = Land::find()->alias('l')
- ->leftJoin(['lc' => LandCat::tableName()], 'lc.id=l.land_cat_id')
- ->where(['l.store_id' => get_store_id(), 'l.is_delete' => 0]);
- if (isset($arr['status']) && $arr['status'] > -1) {
- $query->andWhere([
- 'g.status' => $arr['status']
- ]);
- }
- if (isset($arr['land_name']) && !empty($arr['land_name'])) {
- $query->andWhere([
- 'like',
- 'l.land_name',
- $arr['land_name']
- ]);
- }
- if (!empty($arr['select'])) {
- $select = $arr['select'];
- } else {
- $select = ['l.id', 'l.land_name', 'l.status', 'l.land_desc',
- 'l.updated_at', 'l.land_pic', 'l.price', 'l.land_coordinate', 'l.sort' ,
- 'l.land_coordinate', 'l.plant_seed', 'l.lease_cycle', 'l.lease_area', 'g.detail',
- 'l.monitoring_equipment_id', 'lc.name land_cat_name',];
- }
- $query->select($select)->orderBy(['l.sort' => SORT_ASC, 'l.id' => SORT_DESC]);
- $pagination = pagination_make($query);
- $pagination['data'] = $pagination['list'];
- foreach($pagination['data'] as $k => $v) {
- $pagination['data'][$k]['plant_seed'] = Json::decode($v['plant_seed']);
- $pagination['data'][$k]['lease_cycle'] = Json::decode($v['lease_cycle']);
- $pagination['data'][$k]['lease_area'] = Json::decode($v['lease_area']);
- }
- unset($pagination['list']);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination
- ]);
- }
- /**
- * 保存地块
- */
- public function actionAdoptSave()
- {
- $form = new AdoptGoodsForm();
- $form->store_id = get_store_id();
- $form->attributes = post_params();
- return $this->asJson($form->saveAdopt());
- }
- /**
- * 商品编辑
- * @param $id
- * @return \yii\web\Response
- */
- public function actionAdoptEdit()
- {
- $id = get_params('id') ?: 0;
- $goodsFrom = new AdoptGoodsForm();
- return $this->asJson($goodsFrom->getAdoptEdit($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());
- }
- public function actionDownQr()
- {
- $num = post_params('num', 0);
- $store_id = get_store_id();
- $md_id = get_md_id();
- $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;
- $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 actionFoodQr()
- {
- $store_id = get_store_id();
- $md_id = get_md_id();
- $filename = md5($store_id . '-' . $md_id);
- $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,
- ]
- ]);
- }
- $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/food/' . $store_id . '?md_id='. $md_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 actionAdoptCat()
- {
- $cat = AdoptCat::findAll(['store_id' => get_store_id(), 'is_delete' => 0, 'is_show' => 1]);
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'list' => $cat
- ]
- ]);
- }
- public function actionAdoptCatList()
- {
- $name = get_params('name');
- $query = AdoptCat::find()->where([
- 'is_delete' => 0,
- 'store_id' => get_store_id()
- ]);
- if (!empty($name)) {
- $query->andWhere(['like', 'name', $name]);
- }
- $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 actionAdoptCatStatus()
- {
- $id = post_params('id');
- $cat = AdoptCat::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 actionAdoptCatDel()
- {
- $id = post_params('id');
- $cat = AdoptCat::findOne($id);
- $cat->is_delete = 1;
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功'
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '删除失败',
- ]);
- }
- public function actionAdoptCatEdit()
- {
- $store_id = get_store_id();
- $id = post_params('id', 0);
- $name = post_params('name');
- $pic_url = post_params('pic_url', '');
- $sort = post_params('sort');
- $is_show = post_params('is_show', 1);
- if ($id > 0) {
- $cat = AdoptCat::findOne($id);
- } else {
- $cat = new AdoptCat();
- }
- $cat->store_id = $store_id;
- $cat->name = $name;
- $cat->pic_url = $pic_url;
- $cat->sort = $sort;
- $cat->is_show = $is_show;
- if ($cat->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => $id > 0 ? '编辑成功' : '添加成功',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => $id > 0 ? '编辑失败' : '添加失败',
- ]);
- }
- public function actionGetAdoptSetting() {
- $store_id = get_store_id();
- $adopt_setting = AdoptSetting::findOne(['store_id' => $store_id]);
- if (!$adopt_setting->adopt_payment) {
- $adopt_setting = new AdoptSetting();
- $payment = [
- [
- 'key' => 'alipay',
- 'value' => 0,
- 'name' => ''
- ],
- [
- 'key' => 'wechat',
- 'value' => 1,
- 'name' => ''
- ],
- // [
- // 'key' => 'huodao',
- // 'value' => 0,
- // 'name' => ''
- // ],
- // [
- // 'key' => 'friend',
- // 'value' => 0,
- // 'name' => ''
- // ],
- ];
- $adopt_setting->store_id = $store_id;
- $adopt_setting->adopt_payment = Json::encode($payment);
- $adopt_setting->adopt_pay_type = 0;
- $adopt_setting->agreement_id = 0;
- $adopt_setting->created_at = time();
- $adopt_setting->updated_at = time();
- $adopt_setting->save();
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'adopt_payment' => Json::decode($adopt_setting->adopt_payment),
- 'adopt_pay_type' => $adopt_setting->adopt_pay_type,
- 'agreement_id' => $adopt_setting->agreement_id,
- 'ys_app_key' => $adopt_setting->ys_app_key,
- 'ys_secret' => $adopt_setting->ys_secret,
- 'mature_overtime' => $adopt_setting->mature_overtime,
- 'storage_overtime' => $adopt_setting->storage_overtime,
- 'free_preserve_time' => $adopt_setting->free_preserve_time,
- 'mature_overtime_tips' => $adopt_setting->mature_overtime_tips,
- 'storage_overtime_tips' => $adopt_setting->storage_overtime_tips,
- 'free_preserve_time_tips' => $adopt_setting->free_preserve_time_tips,
- ]
- ]);
- }
- public function actionSaveAdoptSetting() {
- $store_id = get_store_id();
- $adopt_setting = AdoptSetting::findOne(['store_id' => $store_id]);
- $payment = post_params('adopt_payment');
- $pay_type = post_params('adopt_pay_type', 0);
- $agreement_id = post_params('agreement_id', 0);
- $mature_overtime = post_params('mature_overtime', 0);
- $storage_overtime = post_params('storage_overtime', 0);
- $free_preserve_time = post_params('free_preserve_time', 0);
- $mature_overtime_tips = post_params('mature_overtime_tips', 0);
- $storage_overtime_tips = post_params('storage_overtime_tips', 0);
- $free_preserve_time_tips = post_params('free_preserve_time_tips', 0);
- $adopt_setting->adopt_pay_type = $pay_type;
- $adopt_setting->adopt_payment = $payment;
- $adopt_setting->agreement_id = $agreement_id;
- $adopt_setting->mature_overtime = $mature_overtime;
- $adopt_setting->mature_overtime_tips = $mature_overtime_tips;
- $adopt_setting->storage_overtime = $storage_overtime;
- $adopt_setting->storage_overtime_tips = $storage_overtime_tips;
- $adopt_setting->free_preserve_time = $free_preserve_time;
- $adopt_setting->free_preserve_time_tips = $free_preserve_time_tips;
- $adopt_setting->updated_at = time();
- $adopt_setting->ys_app_key = post_params('ys_app_key',0);
- $adopt_setting->ys_secret = post_params('ys_secret',0);
- if (!$adopt_setting->save()) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => array_values($adopt_setting->errors)[0],
- ]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功'
- ]);
- }
- // 获取设备列表
- public function actionDeviceList(){
- return $this->asJson(DeviceForm::getList(get_params()));
- }
- // 添加设备
- public function actionAddDevice(){
- return $this->asJson(DeviceForm::addItem(post_params()));
- }
- // 添加设备
- public function actionDelDevice(){
- return $this->asJson(DeviceForm::delItem(get_params()));
- }
- // 刷新设备
- public function actionRefresh(){
- return $this->asJson(DeviceForm::refresh());
- }
- // 获取设备选择列表
- public function actionGetSelectList(){
- return $this->asJson(DeviceForm::getSelectList());
- }
- }
|