StoreBargainController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. namespace app\api\controller\activity;
  3. use app\admin\model\store\StoreDescription;
  4. use app\admin\model\store\StoreProductAttrValue;
  5. use app\Request;
  6. use think\facade\Route;
  7. use app\models\user\{
  8. User, WechatUser
  9. };
  10. use app\admin\model\system\SystemAttachment;
  11. use app\models\routine\{
  12. RoutineCode, RoutineTemplate
  13. };
  14. use app\models\store\{StoreBargain, StoreBargainUser, StoreBargainUserHelp, StoreOrder, StoreProductAttr};
  15. use crmeb\services\{
  16. GroupDataService, UtilService, WechatTemplateService
  17. };
  18. use crmeb\services\upload\Upload;
  19. /**
  20. * 砍价产品类
  21. * Class StoreBargainController
  22. * @package app\api\controller\activity
  23. */
  24. class StoreBargainController
  25. {
  26. /**
  27. * 砍价列表顶部图
  28. * @return mixed
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public function config()
  34. {
  35. $lovely = sys_data('routine_lovely') ?? [];//banner图
  36. $info = isset($lovely[2]) ? $lovely[2] : [];
  37. return app('json')->successful($info);
  38. }
  39. /**
  40. * 砍价产品列表
  41. * @param Request $request
  42. * @return mixed
  43. */
  44. public function lst(Request $request)
  45. {
  46. list($page, $limit) = UtilService::getMore([
  47. ['page', 0],
  48. ['limit', 0],
  49. ], $request, true);
  50. $bargainList = StoreBargain::getList($page, $limit);
  51. return app('json')->successful($bargainList);
  52. }
  53. /**
  54. * 砍价详情和当前登录人信息
  55. * @param Request $request
  56. * @param $id
  57. * @return mixed
  58. * @throws \think\Exception
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @throws \think\exception\DbException
  62. */
  63. public function detail(Request $request, $id)
  64. {
  65. if (!$id) return app('json')->fail('参数错误');
  66. if (!($bargain = StoreBargain::getBargainTerm($id))) return app('json')->fail('砍价已结束');
  67. StoreBargain::addBargainLook($id);
  68. $bargain['time'] = time();
  69. $user = $request->user();
  70. $data['userInfo']['uid'] = $user['uid'];
  71. $data['userInfo']['nickname'] = $user['nickname'];
  72. $data['userInfo']['avatar'] = $user['avatar'];
  73. $bargain['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id,2));
  74. list($productAttr, $productValue) = StoreProductAttr::getProductAttrDetail($id, $user['uid'], 0, 2);
  75. foreach ($productValue as $k => $v) {
  76. $v['product_stock'] = StoreProductAttrValue::where('product_id',$bargain['product_id'])->where('suk',$v['suk'])->where('type',0)->value('stock');
  77. $bargain['attr'] = $v;
  78. }
  79. $data['bargain'] = $bargain;
  80. $data['bargainSumCount'] = StoreOrder::getBargainPayCount($id);
  81. $data['userBargainStatus'] = StoreBargainUser::isBargainUser($id, $user['uid']);
  82. return app('json')->successful($data);
  83. }
  84. /**
  85. * 砍价 观看/分享/参与次数
  86. * @param Request $request
  87. * @return mixed
  88. */
  89. public function share(Request $request)
  90. {
  91. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  92. $data['lookCount'] = StoreBargain::getBargainLook();//TODO 观看人数
  93. $data['shareCount'] = StoreBargain::getBargainShare();//TODO 分享人数
  94. $data['userCount'] = StoreBargainUser::count();//TODO 参与人数
  95. if (!$bargainId) return app('json')->successful($data);
  96. StoreBargain::addBargainShare($bargainId);
  97. $data['shareCount'] = StoreBargain::getBargainShare();//TODO 分享人数
  98. return app('json')->successful($data);
  99. }
  100. /**
  101. * 砍价开启
  102. * @param Request $request
  103. * @return mixed
  104. * @throws \think\Exception
  105. */
  106. public function start(Request $request)
  107. {
  108. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  109. if (!($bargain = StoreBargain::getBargainTerm($bargainId))) return app('json')->fail('砍价已结束');
  110. if (!$bargainId) return app('json')->fail('参数错误');
  111. $count = StoreBargainUser::isBargainUser($bargainId, $request->uid());
  112. if ($count === false) return app('json')->fail('参数错误');
  113. else if ($count) return app('json')->status('SUCCESSFUL', '参与成功');
  114. else $res = StoreBargainUser::setBargain($bargainId, $request->uid());
  115. if (!$res) return app('json')->fail('参与失败');
  116. else return app('json')->status('SUCCESS', '参与成功');
  117. }
  118. /**
  119. * 砍价 帮助好友砍价
  120. * @param Request $request
  121. * @return mixed
  122. * @throws \think\Exception
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public function help(Request $request)
  128. {
  129. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  130. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  131. $count = StoreBargainUserHelp::isBargainUserHelpCount($bargainId, $bargainUserUid, $request->uid());
  132. if (!$count) return app('json')->status('SUCCESSFUL', '砍价成功');
  133. $res = StoreBargainUserHelp::setBargainUserHelp($bargainId, $bargainUserUid, $request->uid());
  134. if ($res) {
  135. if (!StoreBargainUserHelp::getSurplusPrice($bargainId, $bargainUserUid)) {
  136. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);// TODO 获取用户参与砍价表编号
  137. $bargainInfo = StoreBargain::get($bargainId);//TODO 获取砍价产品信息
  138. $bargainUserInfo = StoreBargainUser::get($bargainUserTableId);// TODO 获取用户参与砍价信息
  139. //TODO 砍价成功给开启砍价用户发送模板消息
  140. $openid = WechatUser::uidToOpenid($bargainUserUid, 'openid');
  141. $routineOpenid = WechatUser::uidToOpenid($bargainUserUid, 'routine_openid');
  142. if ($openid) {//公众号
  143. $urlWeChat = Route::buildUrl('/activity/dargain_detail/' . $bargainId . '/' . $bargainUserUid)->suffix('')->domain(true)->build();
  144. WechatTemplateService::sendTemplate($openid, WechatTemplateService::BARGAIN_SUCCESS, [
  145. 'first' => '好腻害!你的朋友们已经帮你砍到底价了!',
  146. 'keyword1' => $bargainInfo['title'],
  147. 'keyword2' => $bargainInfo['min_price'],
  148. 'remark' => '点击查看订单详情'
  149. ], $urlWeChat);
  150. } else if ($routineOpenid) { //小程序
  151. RoutineTemplate::sendBargainSuccess($bargainInfo, $bargainUserInfo, $bargainUserUid);
  152. }
  153. }
  154. return app('json')->status('SUCCESS', '砍价成功');
  155. } else return app('json')->fail('砍价失败');
  156. }
  157. /**
  158. * 砍价 砍掉金额
  159. * @param Request $request
  160. * @return mixed
  161. */
  162. public function help_price(Request $request)
  163. {
  164. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  165. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  166. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);//TODO 获取用户参与砍价表编号
  167. $price = StoreBargainUserHelp::getBargainUserBargainPrice($bargainId, $bargainUserTableId, $request->uid(), 'price');// TODO 获取用户砍掉的金额
  168. if ($price) return app('json')->successful(['price' => $price]);
  169. else return app('json')->fail('获取失败');
  170. }
  171. /**
  172. * 砍价 砍价帮总人数、剩余金额、进度条、已经砍掉的价格
  173. * @param Request $request
  174. * @return mixed
  175. * @throws \think\Exception
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @throws \think\exception\DbException
  179. */
  180. public function help_count(Request $request)
  181. {
  182. list($bargainId, $bargainUserUid) = UtilService::postMore([['bargainId', 0], ['bargainUserUid', 0]], $request, true);
  183. $data['userBargainStatus'] = StoreBargainUserHelp::isBargainUserHelpCount($bargainId, $bargainUserUid, $request->uid());
  184. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  185. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid);//TODO 获取用户参与砍价表编号
  186. if ($bargainUserTableId){
  187. $count = StoreBargainUserHelp::getBargainUserHelpPeopleCount($bargainId, $bargainUserUid);//TODO 获取砍价帮总人数
  188. $price = StoreBargainUserHelp::getSurplusPrice($bargainId, $bargainUserUid);//TODO 获取砍价剩余金额
  189. $alreadyPrice = StoreBargainUser::getBargainUserPrice($bargainUserTableId);//TODO 用户已经砍掉的价格 好友砍价之后获取用户已经砍掉的价格
  190. $pricePercent = StoreBargainUserHelp::getSurplusPricePercent($bargainId, $bargainUserUid);//TODO 获取砍价进度条
  191. $data['count'] = $count;
  192. $data['price'] = $price;
  193. $data['status'] = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  194. $data['alreadyPrice'] = $alreadyPrice;
  195. $data['pricePercent'] = $pricePercent > 10 ? $pricePercent : 10;
  196. }else{
  197. $data['count'] = 0;
  198. $data['price'] = StoreBargain::where('id',$bargainId)->value('price - min_price');
  199. $data['status'] = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  200. $data['alreadyPrice'] = 0;
  201. $data['pricePercent'] = 0;
  202. }
  203. return app('json')->successful($data);
  204. }
  205. /**
  206. * 砍价 砍价帮
  207. * @param Request $request
  208. * @return mixed
  209. * @throws \think\Exception
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. * @throws \think\exception\DbException
  213. */
  214. public function help_list(Request $request)
  215. {
  216. list($bargainId, $bargainUserUid, $page, $limit) = UtilService::postMore([
  217. ['bargainId', 0],
  218. ['bargainUserUid', 0],
  219. ['page', 0],
  220. ['limit', 20]
  221. ], $request, true);
  222. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  223. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $bargainUserUid); //TODO 砍价帮获取参与砍价表编号
  224. $storeBargainUserHelp = StoreBargainUserHelp::getList($bargainUserTableId, $page, $limit);
  225. return app('json')->successful($storeBargainUserHelp);
  226. }
  227. /**
  228. * 砍价 开启砍价用户信息
  229. * @param Request $request
  230. * @return mixed
  231. * @throws \think\Exception
  232. * @throws \think\db\exception\DataNotFoundException
  233. * @throws \think\db\exception\ModelNotFoundException
  234. * @throws \think\exception\DbException
  235. */
  236. public function start_user(Request $request)
  237. {
  238. list($bargainId, $bargainUserUid) = UtilService::postMore([
  239. ['bargainId', 0],
  240. ['bargainUserUid', 0],
  241. ], $request, true);
  242. if (!$bargainId || !$bargainUserUid) return app('json')->fail('参数错误');
  243. // $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId,$bargainUserUid);//TODO 获取用户参与砍价表编号
  244. // if(!$bargainUserTableId) return app('json')->fail('参数错误');
  245. $userInfo = User::getUserInfo($bargainUserUid, 'nickname,avatar');
  246. return app('json')->successful($userInfo);
  247. }
  248. /**
  249. * 砍价列表(已参与)
  250. * @param Request $request
  251. * @return mixed
  252. */
  253. public function user_list(Request $request)
  254. {
  255. list($page, $limit) = UtilService::getMore([
  256. ['page', 0],
  257. ['limit', 0],
  258. ], $request, true);
  259. $uid = $request->uid();
  260. StoreBargainUser::editBargainUserStatus($uid);// TODO 判断过期砍价活动
  261. $list = StoreBargainUser::getBargainUserAll($uid, $page, $limit);
  262. if (count($list)) return app('json')->successful($list);
  263. else return app('json')->fail('暂无参与砍价');
  264. }
  265. /**
  266. * 砍价取消
  267. * @param Request $request
  268. * @return mixed
  269. */
  270. public function user_cancel(Request $request)
  271. {
  272. list($bargainId) = UtilService::postMore([['bargainId', 0]], $request, true);
  273. if (!$bargainId) return app('json')->fail('参数错误');
  274. $status = StoreBargainUser::getBargainUserStatus($bargainId, $request->uid());
  275. if ($status != 1) return app('json')->fail('状态错误');
  276. $id = StoreBargainUser::getBargainUserTableId($bargainId, $request->uid());
  277. $res = StoreBargainUser::edit(['is_del' => 1], $id);
  278. if ($res) return app('json')->successful('取消成功');
  279. else return app('json')->successful('取消失败');
  280. }
  281. /**
  282. * 砍价海报
  283. * @param Request $request
  284. * @return mixed
  285. * @throws \think\db\exception\DataNotFoundException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. * @throws \think\exception\DbException
  288. */
  289. public function poster(Request $request)
  290. {
  291. list($bargainId, $from) = UtilService::postMore([['bargainId', 0], ['from', 'wechat']], $request, true);
  292. if (!$bargainId) return app('json')->fail('参数错误');
  293. $user = $request->user();
  294. $storeBargainInfo = StoreBargain::getBargain($bargainId);
  295. $price = StoreBargainUserHelp::getSurplusPrice($bargainId, $user['uid']);//TODO 获取砍价剩余金额
  296. $alreadyPrice = StoreBargainUser::getBargainUserPrice(StoreBargainUser::getBargainUserTableId($bargainId, $user['uid']));
  297. try {
  298. $siteUrl = sys_config('site_url');
  299. $data['title'] = $storeBargainInfo['title'];
  300. $data['image'] = $storeBargainInfo['image'];
  301. $data['price'] = bcsub($storeBargainInfo['price'], $alreadyPrice, 2);
  302. $data['label'] = '已砍至';
  303. $data['msg'] = '还差' . $price . '元即可砍价成功';
  304. if ($from == 'routine') {
  305. //小程序
  306. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_routine.jpg';
  307. $imageInfo = SystemAttachment::getInfo($name, 'name');
  308. if (!$imageInfo) {
  309. $valueData = 'id=' . $bargainId . '&bargain=' . $user['uid'];
  310. if ($user['is_promoter'] || sys_config('store_brokerage_statu') == 2) $valueData .= '&pid=' . $user['uid'];
  311. $res = RoutineCode::getPageCode('pages/activity/goods_bargain_details/index', $valueData, 280);
  312. if (!$res) return app('json')->fail('二维码生成失败');
  313. $uploadType = (int)sys_config('upload_type', 1);
  314. $upload = new Upload($uploadType, [
  315. 'accessKey' => sys_config('accessKey'),
  316. 'secretKey' => sys_config('secretKey'),
  317. 'uploadUrl' => sys_config('uploadUrl'),
  318. 'storageName' => sys_config('storage_name'),
  319. 'storageRegion' => sys_config('storage_region'),
  320. ]);
  321. $res = $upload->to('routine/activity/bargain/code')->validate()->stream($res, $name);
  322. if ($res === false) {
  323. return app('json')->fail($upload->getError());
  324. }
  325. $imageInfo = $upload->getUploadInfo();
  326. $imageInfo['image_type'] = $uploadType;
  327. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  328. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  329. if (!$remoteImage['status']) return app('json')->fail($remoteImage['msg']);
  330. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  331. $url = $imageInfo['dir'];
  332. } else $url = $imageInfo['att_dir'];
  333. $data['url'] = $url;
  334. if ($imageInfo['image_type'] == 1)
  335. $data['url'] = $siteUrl . $url;
  336. $posterImage = UtilService::setShareMarketingPoster($data, 'routine/activity/bargain/poster');
  337. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  338. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  339. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  340. $routinePosterImage = set_http_type($posterImage['dir'], 0);//小程序推广海报
  341. return app('json')->successful(['url' => $routinePosterImage]);
  342. } else if ($from == 'wechat') {
  343. //公众号
  344. $name = $bargainId . '_' . $user['uid'] . '_' . $user['is_promoter'] . '_bargain_share_wap.jpg';
  345. $imageInfo = SystemAttachment::getInfo($name, 'name');
  346. if (!$imageInfo) {
  347. $codeUrl = set_http_type($siteUrl . '/activity/dargain_detail/' . $bargainId . '/' . $user['uid'] . '?spread=' . $user['uid'], 1);//二维码链接
  348. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  349. if (is_string($imageInfo)) {
  350. return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  351. }
  352. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  353. $url = $imageInfo['dir'];
  354. } else $url = $imageInfo['att_dir'];
  355. $data['url'] = $url;
  356. if ($imageInfo['image_type'] == 1) $data['url'] = $siteUrl . $url;
  357. $posterImage = UtilService::setShareMarketingPoster($data, 'wap/activity/bargain/poster');
  358. if (!is_array($posterImage)) return app('json')->fail('海报生成失败');
  359. SystemAttachment::attachmentAdd($posterImage['name'], $posterImage['size'], $posterImage['type'], $posterImage['dir'], $posterImage['thumb_path'], 1, $posterImage['image_type'], $posterImage['time'], 2);
  360. if ($posterImage['image_type'] == 1) $posterImage['dir'] = $siteUrl . $posterImage['dir'];
  361. $wapPosterImage = set_http_type($posterImage['dir'], 1);//公众号推广海报
  362. return app('json')->successful(['url' => $wapPosterImage]);
  363. }
  364. return app('json')->fail('参数错误');
  365. } catch (\Exception $e) {
  366. return app('json')->fail(['line' => $e->getLine(), 'message' => $e->getMessage()]);
  367. }
  368. }
  369. }