Goods.php 9.7 KB

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