Index.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\BaseController;
  4. use app\model\api\Product;
  5. use app\model\system\Category as CategoryModel;
  6. use app\model\system\ProductAttr;
  7. use app\model\system\Attachment;
  8. use app\Request;
  9. use EasyWeChat\Factory;
  10. use library\services\UtilService;
  11. use library\services\QrcodeService;
  12. use library\services\upload\Upload as Upload;
  13. use think\facade\Db;
  14. class Index extends BaseController
  15. {
  16. /**
  17. * 获取产品分类
  18. */
  19. public function getCategory(){
  20. $category = new CategoryModel();
  21. $menus = $category->getArMenu('', 0, 1);
  22. return app('json')->success($menus);
  23. }
  24. /**
  25. * 获取产品标签
  26. */
  27. public function getKeyword($cid){
  28. $data = Product::where('is_del', 0)->where('is_show', 1)->where('cate_id',$cid)->where('keyword','<>',"")->group('keyword')->column('keyword');
  29. $keyword = [];
  30. foreach ($data as $k => $v) {
  31. $arr = explode('、',$v);
  32. $keyword = array_merge($keyword, $arr);
  33. }
  34. $keyword = array_unique($keyword);
  35. return app('json')->success($keyword);
  36. }
  37. /**
  38. * 获取产品列表
  39. */
  40. public function productList(Request $request){
  41. $where = UtilService::getMore([
  42. ['cid', 0],
  43. ['sort', ''],
  44. ['keyword', ''],
  45. ['page', 1],
  46. ['limit', 24]
  47. ], $request);
  48. $data = Product::getProductList($where);
  49. foreach ($data['list'] as $k => $v) {
  50. if($v['price'] == 0) {
  51. $data['list'][$k]['price'] = round($v['ot_price'] * 1.05, 2);
  52. }
  53. $data['list'][$k]['market_price'] = round($data['list'][$k]['price'] * 1.2, 2);
  54. $score = Db::name("product_score")->where('product_id', $v['id'])->find();
  55. if($score) {
  56. $attr = json_decode($score['attribute'], true);
  57. $max = $attr[2]['taobao'];
  58. if($attr[2]['t1688'] > $max)
  59. $max = $attr[2]['t1688'];
  60. if($attr[2]['pdd'] > $max)
  61. $max = $attr[2]['pdd'];
  62. if($attr[2]['dy'] > $max)
  63. $max = $attr[2]['dy'];
  64. if($attr[2]['ks'] > $max)
  65. $max = $attr[2]['ks'];
  66. if($attr[2]['other'] > $max)
  67. $max = $attr[2]['other'];
  68. if($max && $max > $data['list'][$k]['market_price'])
  69. $data['list'][$k]['market_price'] = $max;
  70. }
  71. if($v['label'] != ""){
  72. $data['list'][$k]['label'] = explode('、',$v['label']);
  73. }else{
  74. $data['list'][$k]['label'] = [];
  75. }
  76. $data['list'][$k]['saler'] = random_int(1, 100);
  77. }
  78. return app('json')->success($data);
  79. }
  80. /**
  81. * 商品详情
  82. * @param Request $request
  83. * @param $id
  84. * @param int $type
  85. * @return mixed
  86. */
  87. public function productDetail(Request $request, $id)
  88. {
  89. if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
  90. $storeInfo['saler'] = random_int(1, 100);
  91. $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
  92. $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
  93. $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
  94. //替换windows服务器下正反斜杠问题导致图片无法显示
  95. $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
  96. return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
  97. }, $storeInfo['description']);
  98. if($storeInfo['price'] == 0){
  99. $storeInfo['price'] = round($storeInfo['ot_price'] * 1.05, 2);
  100. }
  101. $storeInfo['market_price'] = round($storeInfo['price'] * 1.2, 2);
  102. $score = Db::name("product_score")->where('product_id', $id)->find();
  103. if($score) {
  104. $attr = json_decode($score['attribute'], true);
  105. $max = $attr[2]['taobao'];
  106. if($attr[2]['t1688'] > $max)
  107. $max = $attr[2]['t1688'];
  108. if($attr[2]['pdd'] > $max)
  109. $max = $attr[2]['pdd'];
  110. if($attr[2]['dy'] > $max)
  111. $max = $attr[2]['dy'];
  112. if($attr[2]['ks'] > $max)
  113. $max = $attr[2]['ks'];
  114. if($attr[2]['other'] > $max)
  115. $max = $attr[2]['other'];
  116. if($max && $max > $storeInfo['market_price'])
  117. $storeInfo['market_price'] = $max;
  118. }
  119. $data['storeInfo'] = $storeInfo;
  120. list($productAttr, $productValue, $sku) = ProductAttr::getProductAttrDetail($id, 0, 0);
  121. $data['productAttr'] = $productAttr;
  122. $prices = array_column($productValue, 'price');
  123. array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
  124. $keys = array_keys($productValue);
  125. $productValue = array_combine($keys, $productValue);
  126. $data['productValue'] = $productValue;
  127. $data['sku'] = $sku;
  128. $data['priceName'] = 0;
  129. $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
  130. return app('json')->successful($data);
  131. }
  132. /**
  133. * 商品分享二维码
  134. * @param Request $request
  135. * @param $id
  136. * @return mixed
  137. */
  138. public function code(Request $request, $id)
  139. {
  140. if (!$id || !($storeInfo = Product::getValidProduct($id, 'id'))) return app('json')->fail('商品不存在或已下架');
  141. $uid = $request->user['uid'];
  142. try {
  143. //小程序
  144. $name = $id . '_' . $uid . '_product.jpg';
  145. $imageInfo = Attachment::getInfo($name, 'name');
  146. if (!$imageInfo) {
  147. $path = 'pages/product/detail';
  148. $query = 'id=' . $id . '&uid=' . $uid;
  149. $mini = Factory::miniProgram(config('weixin')['mini_program']);
  150. $res = $mini->app_code->getUnlimit($query, [
  151. 'page' => $path,
  152. 'width' => 280
  153. ]);
  154. if (!$res) return app('json')->fail('二维码生成失败');
  155. $upload = new Upload(2,config('qiniu')['upload']);
  156. $res = $upload->to('routine/product')->validate()->stream($res, $name);
  157. if ($res === false) {
  158. return app('json')->fail($upload->getError());
  159. }
  160. $imageInfo = $upload->getUploadInfo();
  161. $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  162. if (!$remoteImage['status']) return app('json')->fail('小程序二维码未能生成');
  163. Attachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, 2, $imageInfo['time'], 2);
  164. $url = $imageInfo['dir'];
  165. } else $url = $imageInfo['att_dir'];
  166. return app('json')->successful(['code' => $url]);
  167. } catch (\Exception $e) {
  168. return app('json')->fail($e->getMessage(), [
  169. 'code' => $e->getCode(),
  170. 'line' => $e->getLine(),
  171. 'message' => $e->getMessage()
  172. ]);
  173. }
  174. }
  175. }