AdoptController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\adopt;
  8. use app\models\Goods;
  9. use app\models\Md;
  10. use app\models\MdGoods;
  11. use app\models\Store;
  12. use app\modules\admin\controllers\BaseController;
  13. use app\modules\admin\models\adopt\AdoptGoodsForm;
  14. use app\modules\admin\models\adopt\DeviceForm;
  15. use app\modules\admin\models\GoodsForm;
  16. use app\modules\admin\models\MdGoodsForm;
  17. use app\plugins\adopt\models\AdoptCat;
  18. use app\plugins\adopt\models\AdoptGoods;
  19. use app\plugins\adopt\models\AdoptSetting;
  20. use app\utils\QrCode;
  21. use yii\base\BaseObject;
  22. use yii\helpers\Json;
  23. class AdoptController extends BaseController
  24. {
  25. /**
  26. * 平台获取商品列表
  27. * @return \yii\web\Response
  28. */
  29. public function actionList()
  30. {
  31. return $this->asJson(AdoptGoodsForm::getList(get_params()));
  32. }
  33. /**
  34. * 修改商品库存
  35. * @return \yii\web\Response
  36. */
  37. public function actionSetAttrNum()
  38. {
  39. $form = new AdoptGoodsForm();
  40. return $this->asJson($form->setAttrNum());
  41. }
  42. /**
  43. * 编辑商品数据
  44. */
  45. public function actionSetAttribute()
  46. {
  47. $form = new AdoptGoodsForm();
  48. return $this->asJson($form->setType());
  49. }
  50. /**
  51. * 编辑商品数据
  52. */
  53. public function actionSetAttr()
  54. {
  55. $status = post_params('status', -1);
  56. $delivery_type = post_params('delivery_type', null);
  57. $md_id = get_md_id();
  58. $goods_id = post_params('id');
  59. $md_goods = MdGoods::findOne([
  60. 'md_id' => $md_id,
  61. 'goods_id' => $goods_id
  62. ]);
  63. $is_new_record = false;
  64. if (!$md_goods) {
  65. $md_goods = new MdGoods();
  66. $md_goods->md_id = $md_id;
  67. $md_goods->goods_id = $goods_id;
  68. $is_new_record = true;
  69. }
  70. $goods = Goods::findOne($goods_id);
  71. if ($status > -1) {
  72. if ($md_goods->goods_num <= 0 && $status == Goods::STATUS_NORMAL) {
  73. return $this->asJson(['code' => 1, 'msg' => '请先添加商品库存之后再进行上架操作']);
  74. }
  75. $md_goods->status = $status;
  76. if ($is_new_record) {
  77. $md_goods->attr = $goods->attr;
  78. $md_goods->virtual_sales = $goods->virtual_sales;
  79. $md_goods->price = $goods->price;
  80. $md_goods->delivery_type = Md::findOne(get_md_id())->self_delivery_type;
  81. $md_goods->goods_num = $goods->goods_num;
  82. }
  83. }
  84. if (isset($delivery_type)) {
  85. $md_goods->delivery_type = Json::encode($delivery_type);
  86. if ($is_new_record) {
  87. $md_goods->attr = $goods->attr;
  88. $md_goods->virtual_sales = $goods->virtual_sales;
  89. $md_goods->price = $goods->price;
  90. $md_goods->goods_num = $goods->goods_num;
  91. }
  92. }
  93. if ($md_goods->save()) {
  94. return $this->asJson(['code' => 0, 'msg' => '保存成功']);
  95. } else {
  96. return $this->asJson(['code' => 1, 'msg' => $md_goods->errors[0]]);
  97. }
  98. }
  99. /**
  100. * 修改商品状态
  101. * @return \yii\web\Response
  102. */
  103. public function actionStatus()
  104. {
  105. $goods = Goods::findOne(get_params('id'));
  106. if (empty($goods)) {
  107. return $this->asJson([
  108. 'code' => 1,
  109. 'msg' => '查无此商品'
  110. ]);
  111. }
  112. $type = get_params('type','status');
  113. if ($goods->product_type == Goods::GOODS_TYPE_NORMAL) {
  114. if ($type == 'status' && empty($goods->goods_num) && $goods->status == 0) {
  115. return $this->asJson([
  116. 'code' => 1,
  117. 'msg' => '请添加库存'
  118. ]);
  119. }
  120. }
  121. $value = get_params('value',-1);
  122. $goods->$type = $value == -1 ? ($goods->$type ? 0 : 1) : $value;
  123. if ($goods->save()) {
  124. return $this->asJson([
  125. 'code' => 0,
  126. 'msg' => '编辑成功'
  127. ]);
  128. }else {
  129. return $this->asJson([
  130. 'code' => 1,
  131. 'msg' => '编辑失败'
  132. ]);
  133. }
  134. }
  135. /**
  136. * 批量修改认养
  137. * @return \yii\web\Response
  138. */
  139. public function actionUpdateAll()
  140. {
  141. $status = get_params('status');
  142. $type = get_params('type');
  143. // $updateData = [
  144. // get_params('type') => get_params('status')
  145. // ];
  146. $goods_id = get_params('goods_id');
  147. if (!is_array($goods_id) || empty($goods_id)) {
  148. return $this->asJson([
  149. 'code' => 1,
  150. 'msg' => 'goods_id为空'
  151. ]);
  152. }
  153. $count = 0;
  154. foreach ($goods_id as $id) {
  155. $goods = Goods::findOne($id);
  156. $adopt_goods = AdoptGoods::findOne([
  157. 'goods_id' => $id,
  158. 'is_delete' => 0
  159. ]);
  160. $goods->$type = $status;
  161. if($type == 'is_delete' && $adopt_goods){
  162. $adopt_goods->is_delete = $status;
  163. $adopt_goods->save();
  164. }
  165. if ($goods->save()) {
  166. $count++;
  167. }
  168. }
  169. if ($count) {
  170. return $this->asJson([
  171. 'code' => 0,
  172. 'msg' => '共修改' . $count . '条数据'
  173. ]);
  174. } else {
  175. return $this->asJson([
  176. 'code' => 1,
  177. 'msg' => '修改数据为0条'
  178. ]);
  179. }
  180. }
  181. /**
  182. * 土地地块
  183. * @return \yii\web\Response
  184. */
  185. public function actionLand() {
  186. $arr = get_params();
  187. $query = Land::find()->alias('l')
  188. ->leftJoin(['lc' => LandCat::tableName()], 'lc.id=l.land_cat_id')
  189. ->where(['l.store_id' => get_store_id(), 'l.is_delete' => 0]);
  190. if (isset($arr['status']) && $arr['status'] > -1) {
  191. $query->andWhere([
  192. 'g.status' => $arr['status']
  193. ]);
  194. }
  195. if (isset($arr['land_name']) && !empty($arr['land_name'])) {
  196. $query->andWhere([
  197. 'like',
  198. 'l.land_name',
  199. $arr['land_name']
  200. ]);
  201. }
  202. if (!empty($arr['select'])) {
  203. $select = $arr['select'];
  204. } else {
  205. $select = ['l.id', 'l.land_name', 'l.status', 'l.land_desc',
  206. 'l.updated_at', 'l.land_pic', 'l.price', 'l.land_coordinate', 'l.sort' ,
  207. 'l.land_coordinate', 'l.plant_seed', 'l.lease_cycle', 'l.lease_area', 'g.detail',
  208. 'l.monitoring_equipment_id', 'lc.name land_cat_name',];
  209. }
  210. $query->select($select)->orderBy(['l.sort' => SORT_ASC, 'l.id' => SORT_DESC]);
  211. $pagination = pagination_make($query);
  212. $pagination['data'] = $pagination['list'];
  213. foreach($pagination['data'] as $k => $v) {
  214. $pagination['data'][$k]['plant_seed'] = Json::decode($v['plant_seed']);
  215. $pagination['data'][$k]['lease_cycle'] = Json::decode($v['lease_cycle']);
  216. $pagination['data'][$k]['lease_area'] = Json::decode($v['lease_area']);
  217. }
  218. unset($pagination['list']);
  219. return $this->asJson([
  220. 'code' => 0,
  221. 'msg' => 'success',
  222. 'data' => $pagination
  223. ]);
  224. }
  225. /**
  226. * 保存地块
  227. */
  228. public function actionAdoptSave()
  229. {
  230. $form = new AdoptGoodsForm();
  231. $form->store_id = get_store_id();
  232. $form->attributes = post_params();
  233. return $this->asJson($form->saveAdopt());
  234. }
  235. /**
  236. * 商品编辑
  237. * @param $id
  238. * @return \yii\web\Response
  239. */
  240. public function actionAdoptEdit()
  241. {
  242. $id = get_params('id') ?: 0;
  243. $goodsFrom = new AdoptGoodsForm();
  244. return $this->asJson($goodsFrom->getAdoptEdit($id));
  245. }
  246. /**
  247. * 编辑商品数据
  248. */
  249. public function actionUpdateAttribute()
  250. {
  251. $form = new GoodsForm();
  252. return $this->asJson($form->updateAttribute());
  253. }
  254. /**
  255. * 修改商品库存
  256. * @return \yii\web\Response
  257. */
  258. public function actionUpdateAttrNum()
  259. {
  260. $form = new GoodsForm();
  261. return $this->asJson($form->updateAttrNum());
  262. }
  263. public function actionDownQr()
  264. {
  265. $num = post_params('num', 0);
  266. $store_id = get_store_id();
  267. $md_id = get_md_id();
  268. $filename = md5($store_id . '-' . $md_id . '-' . $num);
  269. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  270. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  271. if (file_exists($path)) {
  272. return $this->asJson([
  273. 'code' => 0,
  274. 'data' => [
  275. 'qr_url' => $pic_url,
  276. ]
  277. ]);
  278. }
  279. $id = FoodTableNumber::findOne(['num' => $num, 'md_id' => $md_id, 'is_delete' => 0])->id;
  280. $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/food/' . $store_id . '?md_id='. $md_id . '&food_table_num=' . $id);
  281. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  282. $goods_qrcode = new \claviska\SimpleImage($path);
  283. $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
  284. $bg = new \claviska\SimpleImage($bgPath);
  285. $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
  286. $bg->toFile($path, 'image/jpeg', 85);
  287. return $this->asJson([
  288. 'code' => 0,
  289. 'data' => [
  290. 'qr_url' => $pic_url,
  291. ]
  292. ]);
  293. }
  294. public function actionFoodQr()
  295. {
  296. $store_id = get_store_id();
  297. $md_id = get_md_id();
  298. $filename = md5($store_id . '-' . $md_id);
  299. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  300. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  301. if (file_exists($path)) {
  302. return $this->asJson([
  303. 'code' => 0,
  304. 'data' => [
  305. 'qr_url' => $pic_url,
  306. ]
  307. ]);
  308. }
  309. $text = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/food/' . $store_id . '?md_id='. $md_id);
  310. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  311. $goods_qrcode = new \claviska\SimpleImage($path);
  312. $bgPath = \Yii::$app->basePath . '/web/v1/statics/images/alipay-bg.png';
  313. $bg = new \claviska\SimpleImage($bgPath);
  314. $bg->overlay($goods_qrcode, 'top left', 1, 200, 350);
  315. $bg->toFile($path, 'image/jpeg', 85);
  316. return $this->asJson([
  317. 'code' => 0,
  318. 'data' => [
  319. 'qr_url' => $pic_url,
  320. ]
  321. ]);
  322. }
  323. /**
  324. * 认养分类
  325. */
  326. public function actionAdoptCat()
  327. {
  328. $cat = AdoptCat::findAll(['store_id' => get_store_id(), 'is_delete' => 0, 'is_show' => 1]);
  329. return $this->asJson([
  330. 'code' => 0,
  331. 'msg' => 'success',
  332. 'data' => [
  333. 'list' => $cat
  334. ]
  335. ]);
  336. }
  337. public function actionAdoptCatList()
  338. {
  339. $name = get_params('name');
  340. $query = AdoptCat::find()->where([
  341. 'is_delete' => 0,
  342. 'store_id' => get_store_id()
  343. ]);
  344. if (!empty($name)) {
  345. $query->andWhere(['like', 'name', $name]);
  346. }
  347. $query->orderBy(['sort' => SORT_DESC]);
  348. $list = pagination_make($query);
  349. return $this->asJson([
  350. 'code' => 0,
  351. 'msg' => 'success',
  352. 'data' => [
  353. 'data' => $list['list'],
  354. 'pageNo' => $list['pageNo'],
  355. 'totalCount' => $list['totalCount'],
  356. ],
  357. ]);
  358. }
  359. public function actionAdoptCatStatus()
  360. {
  361. $id = post_params('id');
  362. $cat = AdoptCat::findOne($id);
  363. $cat->is_show = $cat->is_show === 1 ? 0 : 1;
  364. if ($cat->save()) {
  365. return $this->asJson([
  366. 'code' => 0,
  367. 'msg' => '修改成功'
  368. ]);
  369. }
  370. return $this->asJson([
  371. 'code' => 1,
  372. 'msg' => '修改失败',
  373. ]);
  374. }
  375. public function actionAdoptCatDel()
  376. {
  377. $id = post_params('id');
  378. $cat = AdoptCat::findOne($id);
  379. $cat->is_delete = 1;
  380. if ($cat->save()) {
  381. return $this->asJson([
  382. 'code' => 0,
  383. 'msg' => '删除成功'
  384. ]);
  385. }
  386. return $this->asJson([
  387. 'code' => 1,
  388. 'msg' => '删除失败',
  389. ]);
  390. }
  391. public function actionAdoptCatEdit()
  392. {
  393. $store_id = get_store_id();
  394. $id = post_params('id', 0);
  395. $name = post_params('name');
  396. $pic_url = post_params('pic_url', '');
  397. $sort = post_params('sort');
  398. $is_show = post_params('is_show', 1);
  399. if ($id > 0) {
  400. $cat = AdoptCat::findOne($id);
  401. } else {
  402. $cat = new AdoptCat();
  403. }
  404. $cat->store_id = $store_id;
  405. $cat->name = $name;
  406. $cat->pic_url = $pic_url;
  407. $cat->sort = $sort;
  408. $cat->is_show = $is_show;
  409. if ($cat->save()) {
  410. return $this->asJson([
  411. 'code' => 0,
  412. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  413. ]);
  414. }
  415. return $this->asJson([
  416. 'code' => 1,
  417. 'msg' => $id > 0 ? '编辑失败' : '添加失败',
  418. ]);
  419. }
  420. public function actionGetAdoptSetting() {
  421. $store_id = get_store_id();
  422. $adopt_setting = AdoptSetting::findOne(['store_id' => $store_id]);
  423. if (!$adopt_setting->adopt_payment) {
  424. $adopt_setting = new AdoptSetting();
  425. $payment = [
  426. [
  427. 'key' => 'alipay',
  428. 'value' => 0,
  429. 'name' => ''
  430. ],
  431. [
  432. 'key' => 'wechat',
  433. 'value' => 1,
  434. 'name' => ''
  435. ],
  436. // [
  437. // 'key' => 'huodao',
  438. // 'value' => 0,
  439. // 'name' => ''
  440. // ],
  441. // [
  442. // 'key' => 'friend',
  443. // 'value' => 0,
  444. // 'name' => ''
  445. // ],
  446. ];
  447. $adopt_setting->store_id = $store_id;
  448. $adopt_setting->adopt_payment = Json::encode($payment);
  449. $adopt_setting->adopt_pay_type = 0;
  450. $adopt_setting->agreement_id = 0;
  451. $adopt_setting->created_at = time();
  452. $adopt_setting->updated_at = time();
  453. $adopt_setting->save();
  454. }
  455. return $this->asJson([
  456. 'code' => 0,
  457. 'msg' => 'success',
  458. 'data' => [
  459. 'adopt_payment' => Json::decode($adopt_setting->adopt_payment),
  460. 'adopt_pay_type' => $adopt_setting->adopt_pay_type,
  461. 'agreement_id' => $adopt_setting->agreement_id,
  462. 'ys_app_key' => $adopt_setting->ys_app_key,
  463. 'ys_secret' => $adopt_setting->ys_secret,
  464. 'mature_overtime' => $adopt_setting->mature_overtime,
  465. 'storage_overtime' => $adopt_setting->storage_overtime,
  466. 'free_preserve_time' => $adopt_setting->free_preserve_time,
  467. 'mature_overtime_tips' => $adopt_setting->mature_overtime_tips,
  468. 'storage_overtime_tips' => $adopt_setting->storage_overtime_tips,
  469. 'free_preserve_time_tips' => $adopt_setting->free_preserve_time_tips,
  470. ]
  471. ]);
  472. }
  473. public function actionSaveAdoptSetting() {
  474. $store_id = get_store_id();
  475. $adopt_setting = AdoptSetting::findOne(['store_id' => $store_id]);
  476. $payment = post_params('adopt_payment');
  477. $pay_type = post_params('adopt_pay_type', 0);
  478. $agreement_id = post_params('agreement_id', 0);
  479. $mature_overtime = post_params('mature_overtime', 0);
  480. $storage_overtime = post_params('storage_overtime', 0);
  481. $free_preserve_time = post_params('free_preserve_time', 0);
  482. $mature_overtime_tips = post_params('mature_overtime_tips', 0);
  483. $storage_overtime_tips = post_params('storage_overtime_tips', 0);
  484. $free_preserve_time_tips = post_params('free_preserve_time_tips', 0);
  485. $adopt_setting->adopt_pay_type = $pay_type;
  486. $adopt_setting->adopt_payment = $payment;
  487. $adopt_setting->agreement_id = $agreement_id;
  488. $adopt_setting->mature_overtime = $mature_overtime;
  489. $adopt_setting->mature_overtime_tips = $mature_overtime_tips;
  490. $adopt_setting->storage_overtime = $storage_overtime;
  491. $adopt_setting->storage_overtime_tips = $storage_overtime_tips;
  492. $adopt_setting->free_preserve_time = $free_preserve_time;
  493. $adopt_setting->free_preserve_time_tips = $free_preserve_time_tips;
  494. $adopt_setting->updated_at = time();
  495. $adopt_setting->ys_app_key = post_params('ys_app_key',0);
  496. $adopt_setting->ys_secret = post_params('ys_secret',0);
  497. if (!$adopt_setting->save()) {
  498. return $this->asJson([
  499. 'code' => 1,
  500. 'msg' => array_values($adopt_setting->errors)[0],
  501. ]);
  502. }
  503. return $this->asJson([
  504. 'code' => 0,
  505. 'msg' => '保存成功'
  506. ]);
  507. }
  508. // 获取设备列表
  509. public function actionDeviceList(){
  510. return $this->asJson(DeviceForm::getList(get_params()));
  511. }
  512. // 添加设备
  513. public function actionAddDevice(){
  514. return $this->asJson(DeviceForm::addItem(post_params()));
  515. }
  516. // 添加设备
  517. public function actionDelDevice(){
  518. return $this->asJson(DeviceForm::delItem(get_params()));
  519. }
  520. // 刷新设备
  521. public function actionRefresh(){
  522. return $this->asJson(DeviceForm::refresh());
  523. }
  524. // 获取设备选择列表
  525. public function actionGetSelectList(){
  526. return $this->asJson(DeviceForm::getSelectList());
  527. }
  528. }