Goods.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Goods as GoodsModel;
  4. use app\common\model\Guarantee;
  5. use app\common\model\SkuSpec;
  6. use app\common\model\Collect;
  7. use app\common\model\Comment;
  8. use app\common\model\AttributeValue;
  9. use app\common\model\Coupon;
  10. use app\common\model\CouponCondition;
  11. use app\common\model\UserCoupon;
  12. use fast\Http;
  13. use think\Log;
  14. /**
  15. * 商品接口
  16. */
  17. class Goods extends Base
  18. {
  19. protected $noNeedLogin = ['index', 'detail', 'lists', 'getWxCode'];
  20. //首页推荐商品
  21. public function index()
  22. {
  23. $hots = GoodsModel::where('status', 'normal')
  24. ->where("FIND_IN_SET('hot',`flag`)")
  25. ->order('weigh desc')
  26. ->limit(12)
  27. ->cache(false)
  28. ->select();
  29. $recommends = GoodsModel::getIndexGoodsList();
  30. foreach ($hots as $item) {
  31. $item->visible(explode(',', 'id,title,price,marketprice,sales,views,image'));
  32. }
  33. foreach ($recommends as $item) {
  34. $item->visible(explode(',', 'id,title,price,marketprice,sales,views,image'));
  35. }
  36. $this->success('获取成功', [
  37. 'hots' => $hots,
  38. 'recommends' => $recommends
  39. ]);
  40. }
  41. //详情
  42. public function detail()
  43. {
  44. $id = $this->request->param('id/d');
  45. if (!$id) {
  46. $this->error('参数错误');
  47. }
  48. $row = (new GoodsModel())->with([
  49. 'Sku',
  50. 'Comment' => function ($query) {
  51. $query->relation([
  52. 'reply' => function ($user) {
  53. $user->with([
  54. 'manage' => function ($u) {
  55. $u->field('id,nickname');
  56. }
  57. ]);
  58. }
  59. ])->where('status', 'normal')->where('pid', 0)->field('id,goods_id,content,star,user_id,images,comments,createtime')->with([
  60. 'User' => function ($u) {
  61. $u->field('id,nickname,avatar');
  62. }
  63. ])->order('createtime', 'desc')->limit(10);
  64. }
  65. ])->where('status', '<>', 'hidden')->where('id', $id)->find();
  66. if (!$row) {
  67. $this->error('未找到该商品');
  68. }
  69. $row->setInc('views');
  70. //收藏
  71. if ($this->auth->isLogin()) {
  72. $row->is_collect = !!(Collect::where('user_id', $this->auth->id)->where('goods_id', $id)->where('status', 1)->find());
  73. } else {
  74. $row->is_collect = false;
  75. }
  76. $row->sku_spec = SkuSpec::getGoodsSkuSpec($id);
  77. //服务保障
  78. $row->guarantee = $row->guarantee_ids ? Guarantee::field('id,name,intro')->where('id', 'IN', $row->guarantee_ids)->where('status', 'normal')->select() : [];
  79. //属性
  80. $row->attributes = AttributeValue::getAttributeList($row->attribute_ids);
  81. //好评度
  82. $row->favor_rate = Comment::degree($id);
  83. //评论
  84. $comment = collection($row->comment)->toArray();
  85. foreach ($comment as &$item) {
  86. if ($item['user']) {
  87. $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
  88. }
  89. }
  90. $row->setRelation('comment', $comment);
  91. unset($item);
  92. //优惠券
  93. $conditions = CouponCondition::getGoodsCondition($id, $row->category_id, $row->brand_id);
  94. $sql = "condition_ids IS NULL OR condition_ids=''";
  95. foreach ($conditions as $key => $item) {
  96. $sql .= " OR FIND_IN_SET('{$item['id']}',condition_ids)";
  97. }
  98. $couponList = Coupon::field('id,name,result,result_data,allow_num,begintime,endtime,use_times,received_num,give_num,mode,createtime')
  99. ->where($sql)
  100. ->where('is_open', 1)
  101. ->where('is_private', 'no')
  102. ->where('endtime', '>', time())
  103. ->select();
  104. //已经登录,渲染已领的优惠券
  105. $coupon_ids = [];
  106. if ($this->auth->isLogin()) {
  107. $coupon_ids = UserCoupon::where('user_id', $this->auth->id)->column('coupon_id');
  108. }
  109. foreach ($couponList as $key => &$item) {
  110. Coupon::render($item, $coupon_ids);
  111. $item->hidden(['received_num', 'give_num', 'condition_ids']);
  112. }
  113. $row->coupon = $couponList;
  114. $row->visible(explode(',', 'id,title,subtitle,category_id,price,marketprice,sales,views,image,content,images,sku_spec,sku,comment,is_collect,guarantee,attributes,favor_rate,coupon'));
  115. $row = $row->toArray();
  116. $row['content'] = \addons\shop\library\Service::formatTplToUniapp($row['content']);
  117. $this->success('获取成功', $row);
  118. }
  119. //列表
  120. public function lists()
  121. {
  122. $param = $this->request->param();
  123. $pageNum = (int)$this->request->param('pageNum', 10);
  124. $orderby = $this->request->param('orderby', 'weigh');
  125. $orderway = $this->request->param('orderway', 'desc');
  126. $list = GoodsModel::where(function ($query) use ($param) {
  127. $query->where('status', 'normal');
  128. //关键词
  129. if (isset($param['keyword']) && !empty($param['keyword'])) {
  130. $query->where('title|keywords', 'like', '%' . $param['keyword'] . '%');
  131. $log = \addons\shop\model\SearchLog::getByKeywords($param['keyword']);
  132. if ($log) {
  133. $log->setInc("nums");
  134. } else {
  135. \addons\shop\model\SearchLog::create(['keywords' => $param['keyword'], 'nums' => 1, 'status' => 'hidden']);
  136. }
  137. }
  138. //分类
  139. if (isset($param['category_id']) && !empty($param['category_id'])) {
  140. $query->where('category_id', 'IN', \addons\shop\model\Category::getCategoryChildrenIds($param['category_id']));
  141. }
  142. //属性
  143. if (isset($param['attributes']) && !empty($param['attributes'])) {
  144. $query->where('id', 'IN', \addons\shop\model\GoodsAttr::getGoodsIds($param['attributes']));
  145. }
  146. //品牌
  147. if (isset($param['brand_id']) && !empty($param['brand_id'])) {
  148. $query->where('brand_id', 'IN', $param['brand_id']);
  149. }
  150. //价格
  151. if (isset($param['price']) && !empty($param['price'])) {
  152. $priceArr = explode('-', $param['price']);
  153. if (count($priceArr) == 2) {
  154. if (isset($priceArr[0])) {
  155. $priceArr[0] = (float)$priceArr[0];
  156. }
  157. if (isset($priceArr[1])) {
  158. $priceArr[1] = (float)$priceArr[1];
  159. }
  160. $query->where('price', 'BETWEEN', $priceArr);
  161. }
  162. }
  163. })->order("{$orderby} {$orderway}")->paginate($pageNum);
  164. foreach ($list as $item) {
  165. $item->visible(explode(',', 'id,title,image,price,sales,views,description,marketprice,createtime'));
  166. }
  167. $this->success('', $list);
  168. }
  169. //获取小程序码
  170. public function getWxCode()
  171. {
  172. $goods_id = $this->request->post('goods_id');
  173. $version = $this->request->post('version', 'release');
  174. if (empty($goods_id)) {
  175. $this->error('参数错误');
  176. }
  177. $user_id = '';
  178. if ($this->auth->isLogin()) {
  179. $user_id = $this->auth->id;
  180. }
  181. $resource = '';
  182. $fileStream = (new \addons\shop\library\message\Mini)->getWxCodeUnlimited([
  183. 'scene' => "invite_id={$user_id}&goods_id={$goods_id}",
  184. 'env_version' => $version, //要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
  185. 'page' => 'pages/goods/detail',
  186. 'check_path' => false
  187. ]);
  188. if (is_null(json_decode($fileStream))) {
  189. try {
  190. $img = imagecreatefromstring($fileStream);
  191. ob_start();
  192. imagepng($img);
  193. $resource = ob_get_clean();
  194. } catch (\Exception $e) {
  195. \think\Log::write($e->getMessage());
  196. $this->error("获取微信二维码失败!");
  197. }
  198. } else {
  199. $config = get_addon_config('shop');
  200. if ($config['wxapp']) {
  201. $localFile = ROOT_PATH . 'public' . $config['wxapp'];
  202. if (is_file($localFile)) {
  203. $resource = file_get_contents($localFile);
  204. } else {
  205. $resource = Http::get(cdnurl($config['wxapp'], true));
  206. }
  207. }
  208. if (config('app_debug')) {
  209. Log::write($fileStream);
  210. }
  211. }
  212. if (!$resource) {
  213. Log::write($fileStream);
  214. $this->error("获取二维码失败!");
  215. }
  216. $base64_data = base64_encode($resource);
  217. $base64_file = 'data:image/jpg;base64,' . $base64_data;
  218. $this->success('获取成功', $base64_file);
  219. }
  220. }