ShopController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace frontendApi\modules\v1\controllers;
  9. use common\helpers\Date;
  10. use common\helpers\Form;
  11. use common\helpers\user\Info;
  12. use common\models\DecOrder;
  13. use common\models\forms\DeclarationForm;
  14. use common\models\forms\OrderForm;
  15. use common\models\Order;
  16. use common\models\ReceiveAddress;
  17. use common\models\Region;
  18. use common\models\ShopGoods;
  19. use common\models\User;
  20. use common\models\UserBonus;
  21. use common\models\UserWallet;
  22. class ShopController extends BaseController {
  23. public $modelClass = DecOrder::class;
  24. /**
  25. * 商品列表
  26. * @return mixed
  27. * @throws \yii\web\HttpException
  28. */
  29. public function actionIndex() {
  30. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0';
  31. $isStudio = User::getEnCodeInfo(\Yii::$app->user->id)['IS_STUDIO'];
  32. if($isStudio==1){
  33. $condition.= " OR FIND_IN_SET(4,GIFT_TYPE)>0";
  34. }
  35. $condition.=")";
  36. $data = ShopGoods::lists($condition, [], [
  37. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  38. 'from' => ShopGoods::tableName(),
  39. ]);
  40. foreach ($data['list'] as $key => $value) {
  41. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  42. $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  43. } else {
  44. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  45. }
  46. // $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  47. }
  48. return static::notice($data);
  49. }
  50. /**
  51. * 获取商品详情
  52. * @return mixed
  53. * @throws \yii\web\HttpException
  54. */
  55. public function actionGoodsDetail(){
  56. $id = \Yii::$app->request->get('id');
  57. $data = null;
  58. if($id){
  59. $data = ShopGoods::findOneAsArray('ID=:ID AND STATUS=1', [':ID'=>$id]);
  60. }
  61. return static::notice($data);
  62. }
  63. /**
  64. * 购物车订单展示
  65. * @throws \yii\web\HttpException
  66. */
  67. public function actionShowCart(){
  68. $userId = \Yii::$app->user->id;
  69. $payList = ShopGoods::payTypes();
  70. $allAddress = ReceiveAddress::findAllAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId]);
  71. if($allAddress) {
  72. foreach ($allAddress as $key => $row) {
  73. $allAddress[$key]['PROVINCE_NAME'] = Region::getCnName($row['PROVINCE']);
  74. $allAddress[$key]['CITY_NAME'] = Region::getCnName($row['CITY']);
  75. $allAddress[$key]['COUNTY_NAME'] = Region::getCnName($row['COUNTY']);
  76. }
  77. }
  78. $userBalance = [
  79. 'points' => 0,
  80. 'cash' => 0,
  81. 'exchange' => 0
  82. ];
  83. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  84. $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
  85. $userBalance['exchange'] = $userBonusResult['EXCHANGE_POINTS'];
  86. }
  87. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  88. $userBalance['cash'] = $userCashResult['CASH'];
  89. }
  90. return static::notice(['payList'=>$payList,'allAddress'=>$allAddress,'userBalance'=>$userBalance]);
  91. }
  92. /**
  93. * 确认订单
  94. */
  95. public function actionSureOrder(){
  96. if (\Yii::$app->request->isPost) {
  97. $formModel = new OrderForm();
  98. $formModel->scenario = 'userOrder';
  99. $formModel->remark = '复销备注';
  100. $post = \Yii::$app->request->post();
  101. $post['type'] = DeclarationForm::TYPE_FX;
  102. if ($formModel->load($post, '') && $formModel->add()) {
  103. return static::notice('购物成功');
  104. } else {
  105. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  106. }
  107. }
  108. }
  109. /**
  110. * 订单支付成功
  111. * @throws \yii\web\HttpException
  112. */
  113. public function actionPaySuccess(){
  114. $orderSn = \Yii::$app->request->get('orderSn');
  115. $data = null;
  116. if($orderSn){
  117. $data = Order::findOneAsArray('SN=:SN', [':SN'=>$orderSn]);
  118. }
  119. return static::notice($data);
  120. }
  121. /**
  122. * 我的报单
  123. * @return mixed
  124. * @throws \yii\web\HttpException
  125. */
  126. public function actionDecOrderList() {
  127. $condition = ' AND USER_ID=:USER_ID AND IS_DEL=0';
  128. $params[':USER_ID'] = \Yii::$app->user->id;
  129. $data = DecOrder::lists($condition, $params, [
  130. 'select' => 'DO.*,U.USER_NAME USER_NAME,U.REAL_NAME REAL_NAME,RU.USER_NAME REC_USER_NAME,RU.REAL_NAME REC_REAL_NAME,CU.USER_NAME CON_USER_NAME,CU.REAL_NAME CON_REAL_NAME',
  131. 'orderBy' => 'DO.CREATED_AT DESC',
  132. 'from' => DecOrder::tableName() . ' AS DO',
  133. 'join' => [
  134. ['LEFT JOIN', User::tableName() . ' AS U', 'DO.TO_USER_ID=U.ID'],
  135. ['LEFT JOIN', User::tableName() . ' AS RU', 'DO.REC_USER_ID=RU.ID'],
  136. ['LEFT JOIN', User::tableName() . ' AS CU', 'DO.CON_USER_ID=CU.ID'],
  137. ],
  138. ]);
  139. return static::notice($data);
  140. }
  141. /**
  142. * 我的订单
  143. * @return mixed
  144. * @throws \yii\web\HttpException
  145. */
  146. public function actionOrderList() {
  147. $uname = Info::getUserNameByUserId(\Yii::$app->user->id);
  148. $condition = " AND ORDER_TYPE='FX' AND (USER_ID=:USER_ID OR CREATE_USER='$uname')";
  149. $params[':USER_ID'] = \Yii::$app->user->id;
  150. $data = Order::lists($condition, $params, [
  151. 'select' => 'O.*,U.REAL_NAME',
  152. 'orderBy' => 'O.CREATED_AT DESC',
  153. 'from' => Order::tableName() . ' AS O',
  154. 'join' => [
  155. ['LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID'],
  156. ],
  157. ]);
  158. foreach ($data['list'] as $key => $value) {
  159. //$data['list'][$key]['PROVINCE_NAME'] = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  160. //$data['list'][$key]['CITY_NAME'] = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  161. //$data['list'][$key]['COUNTY_NAME'] = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  162. $data['list'][$key]['PAY_AT'] = Date::convert($value['PAY_AT'],'Y-m-d H:i:s');
  163. }
  164. return static::notice($data);
  165. }
  166. /**
  167. * 会员复消
  168. */
  169. public function actionReconsume() {
  170. $condition = ' AND STATUS=1 AND (FIND_IN_SET(2,GIFT_TYPE)>0 OR FIND_IN_SET(4,GIFT_TYPE)>0)';
  171. $data = ShopGoods::lists($condition, [], [
  172. 'orderBy' => 'SORT ASC,CREATED_AT DESC',
  173. 'from' => ShopGoods::tableName(),
  174. ]);
  175. foreach ($data['list'] as $key => $value) {
  176. if ($value['TYPE'] == 1 || $value['TYPE'] == 2) {
  177. $data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  178. } else {
  179. $data['list'][$key]['DISCOUNT'] = $value['SELL_DISCOUNT']*100;
  180. }
  181. //$data['list'][$key]['DISCOUNT'] = ShopGoods::GOODS_TYPE[$value['TYPE']]['discount'];
  182. }
  183. return static::notice($data);
  184. }
  185. /**
  186. * 帮会员复消购物车
  187. * @throws \yii\web\HttpException
  188. */
  189. public function actionReconsumeCart(){
  190. $userId = \Yii::$app->user->id;
  191. $payList = ['cash'=>['name'=>'余额支付'],];
  192. $userBalance = [
  193. 'points' => 0,
  194. 'cash' => 0
  195. ];
  196. if ($userBonusResult = UserBonus::findOneAsArray(['USER_ID' => $userId])) {
  197. $userBalance['points'] = $userBonusResult['RECONSUME_POINTS'];
  198. }
  199. if ($userCashResult = UserWallet::findOneAsArray(['USER_ID' => $userId])) {
  200. $userBalance['cash'] = $userCashResult['CASH'];
  201. }
  202. return static::notice(['payList'=>$payList,'userBalance'=>$userBalance]);
  203. }
  204. /**
  205. * 帮会员复消确认订单
  206. */
  207. public function actionReconsumeSureOrder(){
  208. if (\Yii::$app->request->isPost) {
  209. $formModel = new OrderForm();
  210. $formModel->scenario = 'reconsumeOrder';
  211. $formModel->remark = '帮会员复销';
  212. $post = \Yii::$app->request->post();
  213. $post['type'] = DeclarationForm::TYPE_FX;
  214. if ($formModel->load($post, '') && $formModel->reconsumeAdd()) {
  215. return static::notice('帮会员复消成功');
  216. } else {
  217. return static::notice(Form::formatErrorsForApi($formModel->getErrors()),400);
  218. }
  219. }
  220. }
  221. }