123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace app\api\controller\store;
- use app\admin\model\store\StoreDescription;
- use app\admin\model\system\SystemAttachment;
- use app\models\routine\RoutineCode;
- use app\models\store\StoreOrder;
- use app\models\store\StoreVisit;
- use app\models\system\SystemStore;
- use app\models\store\StoreProduct;
- use app\models\store\StoreProductAttr;
- use app\models\store\StoreProductRelation;
- use app\models\store\StoreProductReply;
- use app\models\user\User;
- use app\Request;
- use crmeb\services\GroupDataService;
- use crmeb\services\QrcodeService;
- use crmeb\services\SystemConfigService;
- use crmeb\services\UploadService;
- use crmeb\services\UtilService;
- use crmeb\services\upload\Upload;
- use Overtrue\Pinyin\Pinyin;
- use think\Exception;
- use think\exception\DbException;
- /**
- * 商品类
- * Class StoreProductController
- * @package app\api\controller\store
- */
- class StoreProductController
- {
- /**
- * 商品列表
- * @param Request $request
- * @return mixed
- */
- public function lst(Request $request)
- {
- $data = UtilService::getMore([
- ['sid', 0],
- ['cid', 0],
- ['keyword', ''],
- ['priceOrder', ''],
- ['salesOrder', ''],
- ['news', 0],
- ['page', 0],
- ['limit', 0],
- ['type', 0]
- ], $request);
- return app('json')->successful(StoreProduct::getProductList($data, $request->uid()));
- }
- /**
- * 产品分享二维码 推广员
- * @param Request $request
- * @param $id
- * @return mixed
- */
- public function code(Request $request, $id)
- {
- $user = $request->user();
- if (!$id || !($storeInfo = StoreProduct::getValidProduct($id, $user['sp_user'], 'id'))) return app('json')->fail('商品不存在或已下架');
- $userType = $request->get('user_type', 'wechat');
- $user = $request->user();
- try {
- switch ($userType) {
- case 'wechat':
- //公众号
- $name = $id . '_product_detail_' . $user['uid'] . '_is_promoter_' . $user['is_promoter'] . '_wap.jpg';
- $url = QrcodeService::getWechatQrcodePath($name, '/detail/' . $id . '?spread=' . $user['uid']);
- if ($url === false)
- return app('json')->fail('二维码生成失败');
- else
- return app('json')->successful(['code' => image_to_base64($url)]);
- break;
- case 'routine':
- //小程序
- $name = $id . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product.jpg';
- $imageInfo = SystemAttachment::getInfo($name, 'name');
- $siteUrl = sys_config('site_url');
- if (!$imageInfo) {
- $data = 'id=' . $id;
- if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $data .= '&pid=' . $user['uid'];
- $res = \app\models\routine\RoutineCode::getPageCode('pages/product/product', $data, 280);
- if (!$res) return app('json')->fail('二维码生成失败');
- $uploadType = (int)sys_config('upload_type', 1);
- $upload = new Upload($uploadType, [
- 'accessKey' => sys_config('accessKey'),
- 'secretKey' => sys_config('secretKey'),
- 'uploadUrl' => sys_config('uploadUrl'),
- 'storageName' => sys_config('storage_name'),
- 'storageRegion' => sys_config('storage_region'),
- ]);
- $res = $upload->to('routine/product')->validate()->stream($res, $name);
- if ($res === false) {
- return app('json')->fail($upload->getError());
- }
- $imageInfo = $upload->getUploadInfo();
- $imageInfo['image_type'] = $uploadType;
- if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
- else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
- if (!$remoteImage['status']) return app('json')->fail('小程序二维码未能生成');
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
- $url = $imageInfo['dir'];
- } else $url = $imageInfo['att_dir'];
- if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
- return app('json')->successful(['code' => $url]);
- }
- } catch (\Exception $e) {
- return app('json')->fail($e->getMessage(), [
- 'code' => $e->getCode(),
- 'line' => $e->getLine(),
- 'message' => $e->getMessage()
- ]);
- }
- }
- /**
- * 产品详情
- * @param Request $request
- * @param $id
- * @param int $type
- * @return mixed
- */
- public function detail(Request $request, $id, $type = 0)
- {
- if ($request->isLogin())
- $user = $request->user();
- if (!$id || !($storeInfo = StoreProduct::getValidProduct($id, $user['sp_user'] ?? 0))) return app('json')->fail('商品不存在或已下架');
- $siteUrl = sys_config('site_url');
- $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
- $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
- $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/detail/' . $id);
- $uid = $request->uid();
- $data['uid'] = $uid;
- $storeInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
- //替换windows服务器下正反斜杠问题导致图片无法显示
- $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
- return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
- }, $storeInfo['description']);
- $storeInfo['userCollect'] = StoreProductRelation::isProductRelation($id, $uid, 'collect');
- $storeInfo['userLike'] = StoreProductRelation::isProductRelation($id, $uid, 'like');
- list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $uid, $type);
- StoreVisit::setView($uid, $id, $storeInfo['cate_id'], 'viwe');
- $data['storeInfo'] = StoreProduct::setLevelPrice($storeInfo, $uid, true);
- $data['similarity'] = StoreProduct::cateIdBySimilarityProduct($storeInfo['cate_id'], 'id,store_name,image,price,sales,ficti', 4, $uid);
- $data['productAttr'] = $productAttr;
- $data['productValue'] = $productValue;
- $data['priceName'] = 0;
- if ($uid) {
- $user = $request->user();
- if (!$user->is_promoter) {
- $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
- $status = is_brokerage_statu($price);
- if ($status) {
- User::where('uid', $uid)->update(['is_promoter' => 1]);
- $user->is_promoter = 1;
- }
- }
- if ($user->is_promoter) {
- $data['priceName'] = StoreProduct::getPacketPrice($storeInfo, $productValue);
- }
- if (!strlen(trim($data['priceName'])))
- $data['priceName'] = 0;
- }
- $data['reply'] = StoreProductReply::getRecProductReply($storeInfo['id']);
- $data['replyCount'] = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->count();
- if ($data['replyCount']) {
- $goodReply = StoreProductReply::productValidWhere()->where('product_id', $storeInfo['id'])->where('product_score', 5)->count();
- $data['replyChance'] = $goodReply;
- if ($goodReply) {
- $data['replyChance'] = bcdiv($goodReply, $data['replyCount'], 2);
- $data['replyChance'] = bcmul($data['replyChance'], 100, 2);
- }
- } else $data['replyChance'] = 0;
- $data['mer_id'] = $storeInfo['mer_id'];
- $data['system_store'] = ($res = SystemStore::getStoreDispose()) ? $res : [];
- $data['good_list'] = StoreProduct::getGoodList(18, $user['sp_user'] ?? 0, 'image,store_name,price,id,ot_price');
- $data['mapKey'] = sys_config('tengxun_map_key');
- $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
- $data['activity'] = StoreProduct::activity($data['storeInfo']['id'], false);
- return app('json')->successful($data);
- }
- /**
- * 为你推荐
- *
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws DbException
- */
- public function product_hot(Request $request)
- {
- list($page, $limit) = UtilService::getMore([
- ['page', 0],
- ['limit', 0]
- ], $request, true);
- if (!$limit) return app('json')->successful([]);
- $productHot = StoreProduct::getHotProductLoading('id,image,store_name,cate_id,price,unit_name,ot_price', (int)$page, (int)$limit, $request->uid());
- if (!empty($productHot)) {
- foreach ($productHot as $k => $v) {
- $productHot[$k]['activity'] = StoreProduct::activity($v['id']);
- }
- }
- return app('json')->successful($productHot);
- }
- /**
- * 获取首页推荐不同类型产品的轮播图和产品
- * @param Request $request
- * @param $type
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws DbException
- */
- public function groom_list(Request $request, $type)
- {
- $info['banner'] = [];
- $info['list'] = [];
- if ($type == 1) {//TODO 精品推荐
- $info['banner'] = sys_data('routine_home_bast_banner') ?: [];//TODO 首页精品推荐图片
- $info['list'] = StoreProduct::getBestProduct('id,image,store_name,cate_id,price,ot_price,IFNULL(sales,0) + IFNULL(ficti,0) as sales,unit_name,sort');//TODO 精品推荐个数
- } else if ($type == 2) {//TODO 热门榜单
- $info['banner'] = sys_data('routine_home_hot_banner') ?: [];//TODO 热门榜单 猜你喜欢推荐图片
- $info['list'] = StoreProduct::getHotProduct('id,image,store_name,cate_id,price,ot_price,unit_name,sort,IFNULL(sales,0) + IFNULL(ficti,0) as sales', 0, $request->uid());//TODO 热门榜单 猜你喜欢
- } else if ($type == 3) {//TODO 首发新品
- $info['banner'] = sys_data('routine_home_new_banner') ?: [];//TODO 首发新品推荐图片
- $info['list'] = StoreProduct::getNewProduct('id,image,store_name,cate_id,price,ot_price,unit_name,sort,IFNULL(sales,0) + IFNULL(ficti,0) as sales', 0, $request->uid());//TODO 首发新品
- } else if ($type == 4) {//TODO 促销单品
- $info['banner'] = sys_data('routine_home_benefit_banner') ?: [];//TODO 促销单品推荐图片
- $info['list'] = StoreProduct::getBenefitProduct('id,image,store_name,cate_id,price,ot_price,stock,unit_name,sort', 0, $request->uid());//TODO 促销单品
- }
- return app('json')->successful($info);
- }
- /**
- * 产品评价数量和好评度
- * @param $id
- * @return mixed
- */
- public function reply_config($id)
- {
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
- return app('json')->successful(StoreProductReply::productReplyCount($id));
- }
- /**
- * 获取产品评论
- * @param Request $request
- * @param $id
- * @param $type
- * @return mixed
- */
- public function reply_list(Request $request, $id)
- {
- list($page, $limit, $type) = UtilService::getMore([
- ['page', 0], ['limit', 0], ['type', 0]
- ], $request, true);
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
- $list = StoreProductReply::getProductReplyList($id, (int)$type, $page, $limit);
- return app('json')->successful($list);
- }
- /**
- * 砍价海报
- * @param Request $request
- * @return mixed
- */
- public function poster(Request $request)
- {
- list($bargainId, $from) = UtilService::postMore([['productId', 0], ['from', 'wechat']], $request, true);
- if (!$bargainId) return app('json')->fail('参数错误');
- $user = $request->user();
- // var_dump($user);
- $storeBargainInfo = StoreProduct::getValidProduct($bargainId, $user['sp_user']);
- $price = $storeBargainInfo['price'];//TODO 获取砍价剩余金额
- try {
- $siteUrl = sys_config('site_url');
- $data['title'] = $storeBargainInfo['store_name'];
- $data['image'] = $storeBargainInfo['image'];
- $data['price'] = $price;
- $data['user_nickname'] = $user['nickname'];
- $data['user_avatar'] = $user['avatar'];
- $pinyin = new Pinyin();
- $data['user_nickname_pinyin'] = $pinyin->convert($user['nickname']);
- $data['ot_price'] = $storeBargainInfo['ot_price'];
- if ($from == 'routine') {
- //小程序
- $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_routine.jpg';
- $imageInfo = SystemAttachment::getInfo($name, 'name');
- if (!$imageInfo) {
- $valueData = 'id=' . $bargainId;
- if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
- $res = RoutineCode::getPageCode('pages/product/product', $valueData, 280);
- if (!$res) return app('json')->fail('二维码生成失败');
- $uploadType = (int)sys_config('upload_type', 1);
- $upload = new Upload($uploadType, [
- 'accessKey' => sys_config('accessKey'),
- 'secretKey' => sys_config('secretKey'),
- 'uploadUrl' => sys_config('uploadUrl'),
- 'storageName' => sys_config('storage_name'),
- 'storageRegion' => sys_config('storage_region'),
- ]);
- $res = $upload->to('routine/product/code')->validate()->stream($res, $name);
- if ($res === false) {
- return app('json')->fail($upload->getError());
- }
- $imageInfo = $upload->getUploadInfo();
- $imageInfo['image_type'] = $uploadType;
- if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
- else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
- if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
- $url = $imageInfo['dir'];
- } else $url = $imageInfo['att_dir'];
- $data['url'] = $url;
- if ($imageInfo['image_type'] == 1)
- $data['url'] = $siteUrl . $url;
- $posterImage = UtilService::setShareProductPoster($data, 'routine/product/poster');
- if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
- SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
- if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
- $routinePosterImage = UtilService::setHttpType($posterImage['dir'], 0);//小程序推广海报
- return app('json')->successful(['url' => $routinePosterImage]);
- } else if ($from == 'wechat') {
- //公众号
- $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_product_share_wap.jpg';
- $imageInfo = SystemAttachment::getInfo($name, 'name');
- if (!$imageInfo) {
- $codeUrl = UtilService::setHttpType($siteUrl . '/index/pages/product/product?id=' . $bargainId . '&spread=' . $user['uid'], 0);//二维码链接
- $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
- if (!$imageInfo) return app('json')->fail('二维码生成失败');
- SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
- $url = $imageInfo['dir'];
- } else $url = $imageInfo['att_dir'];
- $data['url'] = $url;
- if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
- $posterImage = UtilService::setShareProductPoster($data, 'wap/product/poster');
- if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
- SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
- if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
- $wapPosterImage = UtilService::setHttpType($posterImage['dir'], 1);//公众号推广海报
- return app('json')->successful(['url' => $wapPosterImage]);
- }
- return app('json')->fail('参数错误');
- } catch (\Exception $e) {
- return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
- }
- }
- }
|