ScratchController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace addons\Scratch\api\modules\v1\controllers;
  3. use addons\Scratch\common\logic\CommonScratchLogic;
  4. use addons\Scratch\common\logic\ScratchApiLogic;
  5. use addons\Scratch\common\logic\ScratchOrderLogic;
  6. use addons\Scratch\common\logic\ScratchPosterLogic;
  7. use addons\Scratch\common\models\ScratchLogModel;
  8. use addons\Scratch\common\models\ScratchSettingModel;
  9. use common\enums\AppEnum;
  10. use common\enums\PreviewTypeEnum;
  11. use common\enums\ShippingTypeEnum;
  12. use common\logic\order\OrderFormLogic;
  13. use common\logic\order\OrderInitDataLogic;
  14. use common\logic\order\OrderLimitLogic;
  15. use common\logic\order\OrderMarketingLogic;
  16. use common\models\order\Order;
  17. use common\models\user\UserAddress;
  18. use Yii;
  19. use api\controllers\OnAuthController;
  20. use yii\base\BaseObject;
  21. /**
  22. * Class ScratchController
  23. * @package addons\Scratch\api\modules\v1\controllers
  24. */
  25. class ScratchController extends OnAuthController
  26. {
  27. public $modelClass = '';
  28. public $from_type = ['Scratch'];
  29. /**
  30. * 不用进行登录验证的方法
  31. *
  32. * 例如: ['index', 'update', 'create', 'view', 'delete']
  33. * 默认全部需要验证
  34. *
  35. * @var array
  36. */
  37. protected $authOptional = ['get-data'];
  38. /**
  39. * 刮刮卡首页接口
  40. * @return \yii\web\Response
  41. */
  42. public function actionIndex()
  43. {
  44. try {
  45. if(!\Yii::$app->request->isGet){
  46. throw new \ErrorException('NOT IS GET!');
  47. }
  48. $page = \Yii::$app->request->get('page',1);
  49. return $this->success('ok', ScratchApiLogic::getList(
  50. $this->mall_id,
  51. $this->user,
  52. $page
  53. ));
  54. }catch (\ErrorException $errorException){
  55. $this->error($errorException->getMessage());
  56. }catch (\Exception $exception){
  57. $this->error($exception->getMessage());
  58. }
  59. }
  60. /**
  61. * 刮刮卡首页接口
  62. * @return \yii\web\Response
  63. */
  64. public function actionGetData()
  65. {
  66. try {
  67. if(!\Yii::$app->request->isGet){
  68. throw new \ErrorException('NOT IS GET!');
  69. }
  70. return $this->success('ok', ScratchApiLogic::getData(
  71. $this->mall_id,
  72. $this->user
  73. ));
  74. }catch (\ErrorException $errorException){
  75. $this->error($errorException->getMessage());
  76. }catch (\Exception $exception){
  77. $this->error($exception->getMessage());
  78. }
  79. }
  80. /**
  81. * 抽奖
  82. * @return \yii\web\Response
  83. */
  84. public function actionLottery(){
  85. try {
  86. if(!\Yii::$app->request->isGet){
  87. throw new \ErrorException('NOT IS GET!');
  88. }
  89. return $this->success('ok', ScratchApiLogic::scratchCard(
  90. $this->mall_id,
  91. $this->user
  92. ));
  93. }catch (\ErrorException $errorException){
  94. $this->error($errorException->getMessage());
  95. }catch (\Exception $exception){
  96. $this->error($exception->getMessage());
  97. }
  98. }
  99. /**
  100. * 领取奖品
  101. * @return mixed|void
  102. */
  103. public function actionReceive()
  104. {
  105. try {
  106. if(!\Yii::$app->request->isGet){
  107. throw new \ErrorException('NOT IS GET!');
  108. }
  109. $id = \Yii::$app->request->get('id', 0);
  110. return $this->success('ok', ScratchApiLogic::toReceive(
  111. $this->user,
  112. $id
  113. ));
  114. }catch (\ErrorException $errorException){
  115. $this->error($errorException->getMessage());
  116. }catch (\Exception $exception){
  117. $this->error($exception->getMessage());
  118. }
  119. }
  120. /**
  121. * 抽奖规则
  122. * @return \yii\web\Response
  123. */
  124. public function actionSetting()
  125. {
  126. try {
  127. if(!\Yii::$app->request->isGet){
  128. throw new \ErrorException('NOT IS GET!');
  129. }
  130. $setting = ScratchSettingModel::find()
  131. ->where(['mall_id' => $this->mall_id])
  132. ->asArray()
  133. ->select(["rule","start_at","end_at","payment_type","send_type"])
  134. ->one();
  135. if (!empty($setting)) {
  136. if (!empty($setting['payment_type'])) {
  137. $setting['payment_type'] = json_decode($setting['payment_type'], true);
  138. } else {
  139. $setting['payment_type'] = ['online_pay'];
  140. }
  141. $setting['send_type'] = CommonScratchLogic::sendType($setting['send_type']);
  142. $setting["start_at"] = date("Y-m-d H:i:s",$setting["start_at"]);
  143. $setting["end_at"] = date("Y-m-d H:i:s",$setting["end_at"]);
  144. }
  145. $data = [
  146. 'setting' => $setting
  147. ];
  148. return $this->success('ok', $data);
  149. }catch (\ErrorException $errorException){
  150. $this->error($errorException->getMessage());
  151. }catch (\Exception $exception){
  152. $this->error($exception->getMessage());
  153. }
  154. }
  155. /**
  156. * 中奖记录
  157. * @return \yii\web\Response
  158. */
  159. public function actionPrize()
  160. {
  161. try {
  162. if(!\Yii::$app->request->isGet){
  163. throw new \ErrorException('NOT IS GET!');
  164. }
  165. $page = \Yii::$app->request->get('page',1);
  166. return $this->success('ok', ScratchApiLogic::getLogList(
  167. $this->mall_id,
  168. $this->user,
  169. $page
  170. ));
  171. }catch (\ErrorException $errorException){
  172. $this->error($errorException->getMessage());
  173. }catch (\Exception $exception){
  174. $this->error($exception->getMessage());
  175. }
  176. }
  177. /**
  178. * 兑换奖品预览
  179. * @return \yii\web\Response
  180. */
  181. public function actionOrderPreview()
  182. {
  183. try {
  184. if(!\Yii::$app->request->isPost){
  185. throw new \ErrorException('NOT IS POST!');
  186. }
  187. $scratch_log_id = \Yii::$app->request->post('scratch_log_id',0);
  188. $scratchLog = ScratchLogModel::findOne([
  189. 'id' => $scratch_log_id,
  190. 'user_id' => $this->user_id,
  191. ]);
  192. if(empty($scratchLog)){
  193. throw new \ErrorException('中奖信息不存在');
  194. }
  195. if($scratchLog->status != ScratchLogModel::STATUS_NO_RECEIVED){
  196. $statusData = ScratchLogModel::LOG_STATUS_ARR;
  197. $t = $statusData[$scratchLog->status] ?? '未知';
  198. throw new \ErrorException('中奖信息状态是:' . $t . ',不能操作');
  199. }
  200. $attributes = [
  201. 'type' => PreviewTypeEnum::BUY_NOW,
  202. 'data' => [
  203. 'address_id' => 0,
  204. 'num' => 1,
  205. 'attr_id' => $scratchLog->attr_id
  206. ],
  207. ];
  208. $orderFormLogic = new OrderFormLogic();
  209. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  210. $form->attributes = $attributes;
  211. $form['action'] = AppEnum::ACTION_PREVIEW;
  212. !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS;
  213. // 初始化订单数据
  214. $orderInitDataLogic = new OrderInitDataLogic();
  215. $form = $orderInitDataLogic->execute($form, $form->type);
  216. if ($form === false) return $this->error($orderInitDataLogic->getError());
  217. // 购买限制验证
  218. $orderLimitLogic = new OrderLimitLogic();
  219. $form = $orderLimitLogic->run($form);
  220. if ($form === false) return $this->error($orderLimitLogic->getError());
  221. // 获取收货地址
  222. $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id);
  223. $form->address_id = $address_info['id']??0;
  224. // 营销减免活动
  225. $orderMarketingLogic = new OrderMarketingLogic();
  226. $form = $orderMarketingLogic->run($form);
  227. if ($form === false) return $this->error($orderMarketingLogic->getError());
  228. $group_order_goods = $form['group_order_goods'];
  229. $marketing = $form['marketing'];
  230. $pay_type_list = \Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
  231. $shipping_type_list = ShippingTypeEnum::getMap();
  232. $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list');
  233. //运费
  234. $shipping_fee_money = $form['marketing']['deduct']['shipping_fee']['money'] ?? 0;
  235. $form['total_pay_money'] = $shipping_fee_money;
  236. $info['total_pay_money'] = '¥' . $form['total_pay_money'];
  237. $info['is_address'] = $form['is_address'] ?? 0;
  238. return $this->success('操作成功', $info);
  239. }catch (\ErrorException $errorException){
  240. $this->error($errorException->getMessage());
  241. }catch (\Exception $exception){
  242. $this->error($exception->getMessage());
  243. }
  244. }
  245. /**
  246. * 兑换奖品下单
  247. * @return mixed|void
  248. */
  249. public function actionOrderSubmit()
  250. {
  251. try {
  252. if(!\Yii::$app->request->isPost){
  253. throw new \ErrorException('NOT IS POST!');
  254. }
  255. $params = \Yii::$app->request->post();
  256. $res = \Yii::$app->services->validate->validate($params, [
  257. [['scratch_log_id'], 'required', 'message' => 'scratch_log_id,必传'],
  258. [['address_id'], 'required', 'message' => 'address_id,必传'],
  259. [['remark'], 'safe'],
  260. ]);
  261. if ($res === false){
  262. throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage());
  263. }
  264. $scratchLog = ScratchLogModel::findOne([
  265. 'id' => $params['scratch_log_id'],
  266. 'user_id' => $this->user_id,
  267. ]);
  268. if(empty($scratchLog)){
  269. throw new \ErrorException('中奖信息不存在');
  270. }
  271. if($scratchLog->status != ScratchLogModel::STATUS_NO_RECEIVED){
  272. $statusData = ScratchLogModel::LOG_STATUS_ARR;
  273. $t = $statusData[$scratchLog->status] ?? '未知';
  274. throw new \ErrorException('中奖信息状态是:' . $t . ',不能操作');
  275. }
  276. $attributes = [
  277. 'type' => PreviewTypeEnum::BUY_NOW,
  278. 'data' => [
  279. 'address_id' => $params['address_id'],
  280. 'num' => 1,
  281. 'remark' => $params['remark'],
  282. 'attr_id' => $scratchLog->attr_id
  283. ],
  284. ];
  285. $orderFormLogic = new OrderFormLogic();
  286. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  287. $form->attributes = $attributes;
  288. $form['action'] = AppEnum::ACTION_CREATE;
  289. // 初始化订单数据
  290. $orderInitDataLogic = new OrderInitDataLogic();
  291. $form = $orderInitDataLogic->execute($form, $form->type);
  292. if ($form === false) return $this->error($orderInitDataLogic->getError());
  293. // 购买限制验证
  294. $orderLimitLogic = new OrderLimitLogic();
  295. $form = $orderLimitLogic->run($form);
  296. if ($form === false) return $this->error($orderLimitLogic->getError());
  297. // 营销减免活动
  298. $orderMarketingLogic = new OrderMarketingLogic();
  299. $form = $orderMarketingLogic->run($form, true);
  300. if ($form === false) return $this->error($orderMarketingLogic->getError());
  301. $res = ScratchOrderLogic::createOrder($form, $scratchLog);
  302. if ($res === false) return $this->error(Order::getStaticError());
  303. return $this->success('操作成功', $res);
  304. }catch (\ErrorException $errorException){
  305. $this->error($errorException->getMessage());
  306. }catch (\Exception $exception){
  307. $this->error($exception->getMessage());
  308. }
  309. }
  310. }