GoodsController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-03-02 09:50:20
  10. * @LastEditTime: 2021-04-28 10:52:45
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\controllers\v1\GoodsController.php
  14. */
  15. namespace app\modules\client\controllers\v1;
  16. use app\models\Goods;
  17. use app\models\GoodsLoweringPrice;
  18. use app\models\Md;
  19. use app\models\OrderDetail;
  20. use app\models\UserViews;
  21. use app\modules\client\controllers\BaseController;
  22. use app\modules\client\models\v1\common\CommonGoods;
  23. use app\modules\client\models\v1\GoodsForm;
  24. use app\modules\client\models\v1\GoodsListForm;
  25. use app\modules\client\models\v1\MdListForm;
  26. use yii\base\BaseObject;
  27. use app\modules\admin\models\mochat\MochatForm;
  28. /**
  29. * 测试
  30. */
  31. class GoodsController extends BaseController
  32. {
  33. /**
  34. * 商品推荐
  35. */
  36. public function actionGoodsRecommend()
  37. {
  38. $form = new GoodsListForm();
  39. $form->attributes = get_params();
  40. $form->goods_id = get_params('goods_id');
  41. $form->recommend_count = 6;
  42. $form->limit = 6;
  43. $form->store_id = $this->store_id;
  44. return $this->asJson($form->recommend());
  45. }
  46. /**
  47. * 商品推荐
  48. */
  49. public function actionNewGoodsRecommend()
  50. {
  51. $store_id = get_store_id();
  52. $size = intval(get_params('size', 10));
  53. if ($size < 1) {
  54. return $this->asJson([
  55. 'code' => 0,
  56. 'msg' => 'success',
  57. 'data' => []
  58. ]);
  59. }
  60. $goods = Goods::find()->where(['store_id' => $store_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0, 'md_food_id' => 0, 'is_recommend' => 1])
  61. ->andWhere(['!=', 'product_type',Goods::GOODS_TYPE_INTEGRAL])
  62. ->orderBy('sort desc')->limit($size)->asArray()->all();
  63. foreach ($goods as &$item) {
  64. $goods_ = Goods::findOne($item['id']);
  65. $item['virtual_sales'] = $goods_->getSalesVolume() + $item['virtual_sales'];
  66. $res = CommonGoods::getMemberPrice([
  67. 'attr' => $item['attr'],
  68. 'price' => $item['price'],
  69. 'mch_id' => 0,
  70. 'is_level' => $item['is_level'],
  71. ]);
  72. $goodsMemberPrice = $res['min_member_price'] ? $res['min_member_price'] : $item['price'];
  73. $item['price'] = sprintf("%.2f", $goodsMemberPrice);
  74. }
  75. return $this->asJson([
  76. 'code' => 0,
  77. 'msg' => 'success',
  78. 'data' => $goods
  79. ]);
  80. }
  81. /**
  82. * 商品详情
  83. */
  84. public function actionGoods()
  85. {
  86. $form = new GoodsForm();
  87. $form->attributes = get_params();
  88. $form->user_id = get_user_id();
  89. $form->store_id = $this->store_id;
  90. MochatForm::sendMsg(1, get_store_id(), MochatForm::MSG_TYPE_VIEW_GOODS, get_saas_user_id(), ['goods_id' => $form->id]);
  91. return $this->asJson($form->search());
  92. }
  93. public function actionGoodsNum()
  94. {
  95. $form = new GoodsForm();
  96. $form->attributes = get_params();
  97. $form->user_id = get_user_id();
  98. $form->store_id = $this->store_id;
  99. return $this->asJson($form->getGoodsNum());
  100. }
  101. /**
  102. * 获取商品优惠详情
  103. */
  104. public function actionGetGoodsYouhui() {
  105. $form = new GoodsForm();
  106. $form->attributes =get_params();
  107. return $this->asJson($form->getYouhui());
  108. }
  109. /**
  110. * 获取商品优惠详情
  111. */
  112. public function actionAddress() {
  113. $form = new GoodsForm();
  114. $form->attributes = get_params();
  115. return $this->asJson($form->getAddress());
  116. }
  117. /**
  118. * 店铺推荐
  119. */
  120. public function actionMchRecommend() {
  121. $form = new GoodsForm();
  122. $form->attributes =get_params();
  123. return $this->asJson($form->mchRecommend());
  124. }
  125. /**
  126. * 猜你喜欢
  127. */
  128. public function actionGuessLike() {
  129. $form = new GoodsForm();
  130. $form->attributes =get_params();
  131. return $this->asJson($form->guessLike());
  132. }
  133. /**
  134. * 用户商品浏览量
  135. */
  136. public function actionUserView() {
  137. $id = post_params('id');
  138. $user_view = UserViews::findOne(['goods_id' => $id, 'user_id' => get_user_id()]);
  139. if (!$user_view) {
  140. $user_view = new UserViews();
  141. $user_view->store_id = get_store_id();
  142. $user_view->user_id = get_user_id();
  143. $user_view->goods_id = $id;
  144. $user_view->views = 1;
  145. $user_view->save();
  146. return [
  147. 'code' => 0,
  148. 'msg' => '添加成功'
  149. ];
  150. }
  151. $user_view->views += 1;
  152. $user_view->save();
  153. return [
  154. 'code' => 0,
  155. 'msg' => '添加成功'
  156. ];
  157. }
  158. /**
  159. * 商品详情页门店信息接口
  160. */
  161. public function actionMdAddress() {
  162. $latitude = get_params('latitude', 0);
  163. $longitude = get_params('longitude', 0);
  164. $md_id = get_md_id();
  165. $shop_table_name = Md::tableName();
  166. $store_id = get_store_id();
  167. $sql = "SELECT *, acos(cos({$latitude}*pi()/180 )*cos(latitude*pi()/180)*cos({$longitude}*pi()/180 -longitude*pi()/180)+sin({$latitude}*pi()/180 )*sin(latitude*pi()/180))*6370996.81 as
  168. distance FROM {$shop_table_name} WHERE `id`=:id ";
  169. $sql = \Yii::$app->db->quoteSql($sql);
  170. $md = \Yii::$app->db->createCommand($sql, [
  171. ':id' => $md_id,
  172. ])->queryOne();
  173. $md['distance'] = MdListForm::distance($md['distance']);
  174. return $this->asJson([
  175. 'code' => 0,
  176. 'data' => $md
  177. ]);
  178. }
  179. /**
  180. * 模块名:actionGoodsLoweringPrice
  181. * 代码描述:商品订阅降价通知
  182. * 作者:WPing丶
  183. * 请求方式:GET
  184. * 创建时间:2024/03/11 14:18:20
  185. * @param int id 商品id
  186. * @param float price 期望价格
  187. */
  188. public function actionGoodsLoweringPrice() {
  189. $id = get_params('id', 0);
  190. $goods = Goods::find()->where(['id' => $id, 'is_delete' => 0, 'status' => Goods::STATUS_NORMAL])->asArray()->one();
  191. if(!$goods) {
  192. return $this->asJson([
  193. 'code' => 1,
  194. 'msg' => '商品不存在或已下架'
  195. ]);
  196. }
  197. // $price = get_params('price', 0);
  198. $user_id = get_user_id();
  199. $store_id = get_store_id();
  200. $model = GoodsLoweringPrice::findOne(['goods_id' => $id, 'user_id' => $user_id, 'store_id' => $store_id, 'is_delete' => 0]);
  201. if(!$model) {
  202. $model = new GoodsLoweringPrice();
  203. $model->store_id = $store_id;
  204. $model->user_id = $user_id;
  205. $model->goods_id = $id;
  206. $model->saas_id = get_saas_user_id();
  207. }
  208. $model->created_at = time();
  209. $model->is_delete = 0;
  210. $model->type = 0;
  211. $res = CommonGoods::getMemberPrice($goods);
  212. $model->price = $res['min_member_price'] ? $res['min_member_price'] : $goods['price'];
  213. if(!$model->save()) {
  214. return $this->asJson([
  215. 'code' => 1,
  216. 'msg' => '保存GoodsLoweringPrice表失败'
  217. ]);
  218. } else {
  219. return $this->asJson([
  220. 'code' => 0,
  221. 'msg' => '订阅降价通知成功',
  222. 'is_lowering' => true,
  223. ]);
  224. }
  225. }
  226. /**
  227. * 模块名:actionGoodsLoweringPrice
  228. * 代码描述:商品订阅降价通知
  229. * 作者:WPing丶
  230. * 请求方式:GET
  231. * 创建时间:2024/03/11 14:18:20
  232. * @param int id 商品id
  233. * @param float price 期望价格
  234. */
  235. public function actionCancelGoodsLoweringPrice() {
  236. $id = get_params('id', 0);
  237. // $price = get_params('price', 0);
  238. $user_id = get_user_id();
  239. $store_id = get_store_id();
  240. $model = GoodsLoweringPrice::findOne(['goods_id' => $id, 'user_id' => $user_id, 'store_id' => $store_id, 'is_delete' => 0]);
  241. if(!$model) {
  242. return $this->asJson([
  243. 'code' => 0,
  244. 'msg' => '提醒记录不存在或已取消'
  245. ]);
  246. }
  247. $model->is_delete = 1;
  248. if(!$model->save()) {
  249. return $this->asJson([
  250. 'code' => 1,
  251. 'msg' => '保存GoodsLoweringPrice表失败'
  252. ]);
  253. } else {
  254. return $this->asJson([
  255. 'code' => 0,
  256. 'msg' => '取消降价通知成功',
  257. 'is_lowering' => false
  258. ]);
  259. }
  260. }
  261. //商品常购清单分类列表
  262. public function actionGoodsPurchaseCatList() {
  263. $form = new GoodsForm();
  264. $form->store_id = get_store_id();
  265. $form->user_id = get_user_id();
  266. $form->md_id = get_md_id();
  267. return $this->asJson($form->goodsPurchaseCatList());
  268. }
  269. //商品常购清单
  270. public function actionGoodsPurchaseList() {
  271. $form = new GoodsForm();
  272. $form->store_id = get_store_id();
  273. $form->user_id = get_user_id();
  274. $form->cat_id = get_params('cat_id', 0);
  275. $form->md_id = get_md_id();
  276. return $this->asJson($form->goodsPurchaseList());
  277. }
  278. }