ScratchController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace addons\Scratch\backend\controllers;
  3. use addons\Scratch\common\logic\ScratchLogic;
  4. use addons\Scratch\common\models\ScratchModel;
  5. use Yii;
  6. /**
  7. * Class ScratchController
  8. * @package addons\Scratch\backend\controllers
  9. */
  10. class ScratchController extends BaseController
  11. {
  12. public $enableCsrfValidation = false;
  13. /**
  14. * @return string
  15. */
  16. public function actionIndex()
  17. {
  18. return $this->render('index');
  19. }
  20. /**
  21. * @return string
  22. */
  23. public function actionEdit()
  24. {
  25. return $this->render('edit');
  26. }
  27. /**
  28. * @return string
  29. */
  30. public function actionLogIndex()
  31. {
  32. return $this->render('log-index');
  33. }
  34. /**
  35. * @return string
  36. */
  37. public function actionOrderIndex()
  38. {
  39. return $this->render('order-index');
  40. }
  41. /**
  42. * 获取列表信息
  43. * @return mixed|void
  44. */
  45. public function actionGetList()
  46. {
  47. try {
  48. $postData = \Yii::$app->request->get();
  49. $userId = \Yii::$app->request->get('user_id', '');
  50. $res = \Yii::$app->services->validate
  51. ->validate(
  52. $postData,
  53. [
  54. [['page', 'type', 'keyword'], 'safe'],
  55. [['page'], 'default', 'value' => 1],
  56. [['type'], 'default', 'value' => 0],
  57. ]
  58. );
  59. if ($res === false) {
  60. throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage());
  61. }
  62. return $this->success('ok', ScratchLogic::getList(
  63. $this->mall_id,
  64. $userId,
  65. $postData
  66. ));
  67. } catch (\ErrorException $errorException) {
  68. return $this->error($errorException->getMessage());
  69. } catch (\Exception $exception) {
  70. throw new \Exception($exception->getMessage());
  71. }
  72. }
  73. /**
  74. * 获取列表信息
  75. * @return mixed|void
  76. */
  77. public function actionGetLogList()
  78. {
  79. try {
  80. $postData = \Yii::$app->request->get();
  81. $userId = \Yii::$app->request->get('user_id', '');
  82. $res = \Yii::$app->services->validate
  83. ->validate(
  84. $postData,
  85. [
  86. [['page', 'type', 'keyword'], 'safe'],
  87. [['page'], 'default', 'value' => 1],
  88. [['type'], 'default', 'value' => 0],
  89. ]
  90. );
  91. if ($res === false) {
  92. throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage());
  93. }
  94. return $this->success('ok', ScratchLogic::getLogList(
  95. $this->mall_id,
  96. $userId,
  97. $postData
  98. ));
  99. } catch (\ErrorException $errorException) {
  100. return $this->error($errorException->getMessage());
  101. } catch (\Exception $exception) {
  102. throw new \Exception($exception->getMessage());
  103. }
  104. }
  105. /**
  106. * @return mixed|void
  107. * @throws \Exception
  108. */
  109. public function actionGetEdit()
  110. {
  111. try {
  112. $id = \Yii::$app->request->get('id', 0);
  113. return $this->success('ok', ScratchLogic::getEdit($id, $this->mall_id));
  114. } catch (\ErrorException $errorException) {
  115. return $this->error($errorException->getMessage());
  116. } catch (\Exception $exception) {
  117. throw new \Exception($exception->getMessage());
  118. }
  119. }
  120. /**
  121. * @return mixed|void
  122. * @throws \Exception
  123. */
  124. public function actionSearchGoods()
  125. {
  126. try {
  127. $getData = \Yii::$app->request->get();
  128. return $this->success('ok', ScratchLogic::searchGoods($this->mall_id, $getData));
  129. } catch (\ErrorException $errorException) {
  130. return $this->error($errorException->getMessage());
  131. } catch (\Exception $exception) {
  132. throw new \Exception($exception->getMessage());
  133. }
  134. }
  135. /**
  136. * @return mixed|void
  137. * @throws \Exception
  138. */
  139. public function actionSaveEdit()
  140. {
  141. try {
  142. if (!\Yii::$app->request->isPost) {
  143. throw new \ErrorException('NOT IS POST!');
  144. }
  145. $params = \Yii::$app->request->post();
  146. $res = \Yii::$app->services->validate
  147. ->validate(
  148. $params,
  149. [
  150. [['name'], 'required'],
  151. [['name', 'pic', 'type', 'goods_id', 'attr_id', 'num', 'price', 'coupon_id', 'stock', 'balance', 'status'], 'safe'],
  152. ]
  153. );
  154. if ($res === false) {
  155. throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage());
  156. }
  157. return $this->success('保存成功', ScratchLogic::saveEdit($this->mall_id, $params));
  158. } catch (\ErrorException $errorException) {
  159. return $this->error($errorException->getMessage());
  160. } catch (\Exception $exception) {
  161. throw new \Exception($exception->getMessage());
  162. }
  163. }
  164. /**
  165. * @return \yii\web\Response
  166. */
  167. public function actionEditStatus()
  168. {
  169. try {
  170. if (!\Yii::$app->request->isPost) {
  171. throw new \ErrorException('NOT IS POST!');
  172. }
  173. $params = \Yii::$app->request->post();
  174. $res = \Yii::$app->services->validate
  175. ->validate(
  176. $params,
  177. [
  178. [['id'], 'required'],
  179. [['status'], 'safe'],
  180. ]
  181. );
  182. if ($res === false) {
  183. throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage());
  184. }
  185. return $this->success('操作成功', ScratchLogic::saveEditStatus($this->mall_id, $params));
  186. } catch (\ErrorException $errorException) {
  187. return $this->error($errorException->getMessage());
  188. } catch (\Exception $exception) {
  189. throw new \Exception($exception->getMessage());
  190. }
  191. }
  192. /**
  193. * @return mixed|void|\yii\web\Response
  194. * @throws \Exception
  195. */
  196. public function actionDelete()
  197. {
  198. try {
  199. if (!\Yii::$app->request->isPost) {
  200. throw new \ErrorException('NOT IS POST!');
  201. }
  202. $id = \Yii::$app->request->post('id');
  203. $ScratchModel = ScratchModel::findOne($id);
  204. if (!$ScratchModel) {
  205. throw new \ErrorException('信息不存在或已被删除');
  206. }
  207. $ScratchModel->status = -1;
  208. if (!$ScratchModel->save()) {
  209. throw new \ErrorException('删除失败');
  210. }
  211. return $this->success('删除成功');
  212. }catch (\ErrorException $errorException){
  213. return $this->error($errorException->getMessage());
  214. }catch (\Exception $exception){
  215. throw new \Exception($exception->getMessage());
  216. }
  217. }
  218. }