Common.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api;
  12. use app\common\repositories\delivery\DeliveryOrderRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\store\product\ProductAssistSetRepository;
  15. use app\common\repositories\store\product\ProductGroupBuyingRepository;
  16. use app\common\repositories\store\product\ProductGroupRepository;
  17. use app\common\repositories\store\product\ProductPresellRepository;
  18. use app\common\repositories\store\product\ProductRepository;
  19. use app\common\repositories\store\shipping\ExpressRepository;
  20. use app\common\repositories\store\StoreCategoryRepository;
  21. use app\common\repositories\system\CacheRepository;
  22. use app\common\repositories\system\diy\DiyRepository;
  23. use app\common\repositories\system\groupData\GroupDataRepository;
  24. use app\common\repositories\system\merchant\MerchantRepository;
  25. use app\common\repositories\system\notice\SystemNoticeConfigRepository;
  26. use app\common\repositories\user\UserRepository;
  27. use app\common\repositories\user\UserSignRepository;
  28. use app\common\repositories\user\UserVisitRepository;
  29. use app\common\repositories\wechat\TemplateMessageRepository;
  30. use app\common\repositories\wechat\WechatUserRepository;
  31. use crmeb\basic\BaseController;
  32. use crmeb\services\AlipayService;
  33. use crmeb\services\CopyCommand;
  34. use crmeb\services\MiniProgramService;
  35. use crmeb\services\UploadService;
  36. use crmeb\services\WechatService;
  37. use Exception;
  38. use Joypack\Tencent\Map\Bundle\Location;
  39. use Joypack\Tencent\Map\Bundle\LocationOption;
  40. use think\exception\ValidateException;
  41. use think\facade\Cache;
  42. use think\facade\Log;
  43. use think\Response;
  44. /**
  45. * Class Common
  46. * @package app\controller\api
  47. * @author xaboy
  48. * @day 2020/5/28
  49. */
  50. class Common extends BaseController
  51. {
  52. /**
  53. * @return mixed
  54. * @author xaboy
  55. * @day 2020/5/28
  56. */
  57. public function hotKeyword()
  58. {
  59. $type = $this->request->param('type');
  60. switch ($type) {
  61. case 0:
  62. $keyword = systemGroupData('hot_keyword');
  63. break;
  64. case 1:
  65. $keyword = systemGroupData('community_hot_keyword');
  66. break;
  67. }
  68. return app('json')->success($keyword);
  69. }
  70. public function express(ExpressRepository $repository)
  71. {
  72. return app('json')->success($repository->options());
  73. }
  74. public function menus()
  75. {
  76. return app('json')->success([
  77. 'global_theme' => $this->getThemeVar(systemConfig('global_theme')),
  78. 'banner' => systemGroupData('my_banner'),
  79. 'menu' => systemGroupData('my_menus')
  80. ]);
  81. }
  82. public function refundMessage()
  83. {
  84. return app('json')->success(explode("\n", systemConfig('refund_message')));
  85. }
  86. private function getThemeVar($type)
  87. {
  88. return app()->make(DiyRepository::class)->getThemeVar($type);
  89. }
  90. public function config()
  91. {
  92. $config = systemConfig(['open_update_info', 'store_street_theme', 'is_open_service', 'is_phone_login', 'global_theme', 'integral_status', 'mer_location', 'alipay_open', 'hide_mer_status', 'mer_intention_open', 'share_info', 'share_title', 'share_pic', 'store_user_min_recharge', 'recharge_switch', 'balance_func_status', 'yue_pay_status', 'site_logo', 'routine_logo', 'site_name', 'login_logo', 'procudt_increase_status', 'sys_extension_type', 'member_status', 'copy_command_status', 'community_status','community_reply_status','community_app_switch', 'withdraw_type', 'recommend_switch', 'member_interests_status', 'beian_sn', 'community_reply_auth','hot_ranking_switch','svip_switch_status']);
  93. $make = app()->make(TemplateMessageRepository::class);
  94. $cache = app()->make(CacheRepository::class)->search(['copyright_status', 'copyright_context', 'copyright_image', 'sys_intention_agree']);
  95. if (!isset($cache['sys_intention_agree'])) {
  96. $cache['sys_intention_agree'] = systemConfig('sys_intention_agree');
  97. }
  98. $title = app()->make(UserSignRepository::class)->signConfig();
  99. if (!$title) {
  100. $config['member_status'] = 0;
  101. }
  102. if (!is_array($config['withdraw_type'])) {
  103. $config['withdraw_type'] = ['1', '2', '3'];
  104. }
  105. $config['tempid'] = app()->make(SystemNoticeConfigRepository::class)->getSubscribe();
  106. $config['global_theme'] = $this->getThemeVar($config['global_theme']);
  107. $config['navigation'] = app()->make(DiyRepository::class)->getNavigation();
  108. $config = array_merge($config, $cache);
  109. return app('json')->success($config);
  110. }
  111. /**
  112. * @param GroupDataRepository $repository
  113. * @return mixed
  114. * @author xaboy
  115. * @day 2020/6/3
  116. */
  117. public function userRechargeQuota(GroupDataRepository $repository)
  118. {
  119. $recharge_quota = $repository->groupDataId('user_recharge_quota', 0);
  120. $recharge_attention = explode("\n", systemConfig('recharge_attention'));
  121. return app('json')->success(compact('recharge_quota', 'recharge_attention'));
  122. }
  123. /**
  124. * @param $field
  125. * @return mixed
  126. * @author xaboy
  127. * @day 2020/5/28
  128. */
  129. public function uploadImage($field)
  130. {
  131. $name = $this->request->param('name');
  132. $file = $this->request->file($field);
  133. if (!$file)
  134. return app('json')->fail('请上传图片');
  135. if ($name) {
  136. $f = $this->request->getOriginFile($field);
  137. if ($f) {
  138. $f['name'] = $name;
  139. }
  140. $this->request->setOriginFile($field, $f);
  141. $file = $this->request->file($field);
  142. }
  143. $file = is_array($file) ? $file[0] : $file;
  144. validate(["$field|图片" => [
  145. 'fileSize' => config('upload.filesize'),
  146. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  147. 'fileMime' => 'image/jpeg,image/png,image/gif,application/octet-stream'
  148. ]])->check([$field => $file]);
  149. $upload = UploadService::create();
  150. $info = $upload->to('def')->move($field);
  151. if ($info === false) {
  152. return app('json')->fail($upload->getError());
  153. }
  154. $res = $upload->getUploadInfo();
  155. $res['dir'] = tidy_url($res['dir']);
  156. return app('json')->success('上传成功', ['path' => $res['dir']]);
  157. }
  158. /**
  159. * @return Response
  160. * @author xaboy
  161. * @day 2020/6/3
  162. */
  163. public function wechatNotify()
  164. {
  165. try {
  166. return response(WechatService::create()->handleNotify()->getContent());
  167. } catch (Exception $e) {
  168. Log::info('支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  169. }
  170. }
  171. /**
  172. * 电商收付通合并支付回调
  173. */
  174. public function wechatCombinePayNotify($type)
  175. {
  176. if (!in_array($type, ['order', 'presell'], true))
  177. throw new ValidateException('参数错误');
  178. try {
  179. WechatService::create()->handleCombinePayNotify($type);
  180. } catch (Exception $e) {
  181. Log::info('电商收付通支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  182. }
  183. }
  184. /**
  185. * 电商收付通合并支付回调
  186. */
  187. public function routineCombinePayNotify($type)
  188. {
  189. if (!in_array($type, ['order', 'presell'], true))
  190. throw new ValidateException('参数错误');
  191. try {
  192. WechatService::create()->handleCombinePayNotify($type);
  193. } catch (Exception $e) {
  194. Log::info('小程序电商收付通支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  195. }
  196. }
  197. public function routineNotify()
  198. {
  199. try {
  200. return response(MiniProgramService::create()->handleNotify()->getContent());
  201. } catch (Exception $e) {
  202. Log::info('支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  203. }
  204. }
  205. public function alipayNotify($type)
  206. {
  207. if (!in_array($type, ['order', 'user_recharge', 'presell'], true))
  208. throw new ValidateException('参数错误');
  209. $post = $_POST;
  210. $get = $_GET;
  211. $_POST = $this->request->post();
  212. $_GET = $this->request->get();
  213. try {
  214. AlipayService::create()->notify($type);
  215. } catch (Exception $e) {
  216. Log::info('支付宝回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  217. } finally {
  218. $_POST = $post;
  219. $_GET = $get;
  220. }
  221. }
  222. public function getVersion()
  223. {
  224. return app('json')->success(['version' => get_crmeb_version(), 'host' => request()->host(), 'system' => PHP_OS, 'php' => @phpversion()]);
  225. }
  226. /**
  227. * 获取图片base64
  228. * @return mixed
  229. */
  230. public function get_image_base64()
  231. {
  232. list($imageUrl, $codeUrl) = $this->request->params([
  233. ['image', ''],
  234. ['code', ''],
  235. ], true);
  236. checkSuffix([$imageUrl, $codeUrl]);
  237. try {
  238. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : '';
  239. if (!$codeTmp) {
  240. $putCodeUrl = put_image($codeUrl);
  241. $code = $putCodeUrl ? image_to_base64('./runtime/temp' . $putCodeUrl) : '';
  242. $code && unlink('./runtime/temp' . $putCodeUrl);
  243. }
  244. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : '';
  245. if (!$imageTmp) {
  246. $putImageUrl = put_image($imageUrl);
  247. $image = $putImageUrl ? image_to_base64('./runtime/temp' . $putImageUrl) : '';
  248. $image && unlink('./runtime/temp' . $putImageUrl);
  249. }
  250. return app('json')->success(compact('code', 'image'));
  251. } catch (Exception $e) {
  252. return app('json')->fail($e->getMessage());
  253. }
  254. }
  255. public function home()
  256. {
  257. $banner = systemGroupData('home_banner', 1, 10);
  258. $menu = systemGroupData('home_menu');
  259. $hot = systemGroupData('home_hot', 1, 4);
  260. $activity = systemGroupData('sys_activity', 1, 1)[0] ?? null;
  261. $activity_lst = systemGroupData('sys_activity', 1, 3);
  262. $ad = systemConfig(['home_ad_pic', 'home_ad_url']);
  263. $category = app()->make(StoreCategoryRepository::class)->getTwoLevel();
  264. return app('json')->success(compact('banner', 'menu', 'hot', 'ad', 'category', 'activity', 'activity_lst'));
  265. }
  266. public function activityLst($id)
  267. {
  268. $merId = (int)$id;
  269. [$page, $limit] = $this->getPage();
  270. return app('json')->success($merId ? merchantGroupData($merId, 'mer_activity', $page, $limit) : systemGroupData('sys_activity', $page, $limit));
  271. }
  272. public function activityInfo($id)
  273. {
  274. $activity = app()->make(GroupDataRepository::class)->getData((int)$id);
  275. if (!$activity) {
  276. return app('json')->fail('活动不存在');
  277. }
  278. $activity['merchant'] = $activity['group_mer_id'] ? app()->make(MerchantRepository::class)->search(['mer_id' => $activity['group_mer_id']])->field('mer_name,mer_avatar')->find() : null;
  279. return app('json')->success($activity);
  280. }
  281. public function visit()
  282. {
  283. if (!$this->request->isLogin()) return app('json')->success();
  284. [$page, $type] = $this->request->params(['page', 'type'], true);
  285. $uid = $this->request->uid();
  286. if (!$page || !$uid) return app('json')->fail();
  287. $userVisitRepository = app()->make(UserVisitRepository::class);
  288. $type == 'routine' ? $userVisitRepository->visitSmallProgram($uid, $page) : $userVisitRepository->visitPage($uid, $page);
  289. return app('json')->success();
  290. }
  291. public function hotBanner($type)
  292. {
  293. if (!in_array($type, ['new', 'hot', 'best', 'good']))
  294. $data = [];
  295. else
  296. $data = systemGroupData($type . '_home_banner');
  297. return app('json')->success($data);
  298. }
  299. public function pay_key($key)
  300. {
  301. $cache = Cache::store('file');
  302. if (!$cache->has('pay_key' . $key)) {
  303. return app('json')->fail('支付链接不存在');
  304. }
  305. return app('json')->success($cache->get('pay_key' . $key));
  306. }
  307. public function lbs_geocoder()
  308. {
  309. $data = explode(',', $this->request->param('location', ''));
  310. $locationOption = new LocationOption(systemConfig('tx_map_key'));
  311. $locationOption->setLocation($data[0] ?? '', $data[1] ?? '');
  312. $location = new Location($locationOption);
  313. $res = $location->request();
  314. if ($res->error) {
  315. return app('json')->fail($res->error);
  316. }
  317. if ($res->status) {
  318. return app('json')->fail($res->message);
  319. }
  320. if (!$res->result) {
  321. return app('json')->fail('获取失败');
  322. }
  323. return app('json')->success($res->result);
  324. }
  325. public function getCommand()
  326. {
  327. $key = $this->request->param('key');
  328. if (!preg_match('/^(\/@[1-9]{1}).*\*\//', $key)) {
  329. return app('json')->fail('无效口令');
  330. }
  331. $userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
  332. $command = app()->make(CopyCommand::class)->getMassage($key);
  333. if (empty($command)) return app('json')->fail('无效口令');
  334. $info = [];
  335. if ($command['uid']) {
  336. $user = app()->make(UserRepository::class)->get($command['uid']);
  337. $info = [
  338. 'uid' => $user['uid'],
  339. 'nickname' => $user['nickname'],
  340. 'avatar' => $user['avatar'],
  341. ];
  342. }
  343. switch ($command['type']) {
  344. case 0:
  345. $data = app()->make(ProductRepository::class)->detail($command['id'], $userInfo);
  346. $ret['product_id'] = $command['id'];
  347. break;
  348. case 1:
  349. $data = app()->make(ProductRepository::class)->detail($command['id'], $userInfo);
  350. $ret['product_id'] = $command['id'];
  351. break;
  352. case 2:
  353. $data = app()->make(ProductPresellRepository::class)->apiDetail((int)$command['id'], $userInfo);
  354. $ret['activity_id'] = $command['id'];
  355. break;
  356. case 4:
  357. $data = app()->make(ProductGroupRepository::class)->apiDetail($command['id'], $userInfo);
  358. $ret['activity_id'] = $command['id'];
  359. break;
  360. case 30:
  361. $data = app()->make(ProductAssistSetRepository::class)->detail($command['id'], $userInfo);
  362. $ret['activity_id'] = $command['id'];
  363. break;
  364. case 40:
  365. $data = app()->make(ProductGroupBuyingRepository::class)->detail($command['id'], $userInfo);
  366. $ret['activity_id'] = $command['id'];
  367. break;
  368. }
  369. if ($userInfo && $command['uid']) app()->make(UserRepository::class)->bindSpread($userInfo, $command['uid']);
  370. $ret['product_type'] = $command['type'];
  371. $ret['user'] = $info;
  372. $ret['com'] = $command['com'];
  373. $ret['data'] = $data;
  374. return app('json')->success($ret);
  375. }
  376. public function script()
  377. {
  378. return \response(systemConfig('static_script'));
  379. }
  380. public function appVersion()
  381. {
  382. return app('json')->success(systemConfig([
  383. 'appVersion',
  384. 'iosAddress',
  385. 'androidAddress',
  386. 'openUpgrade'
  387. ]));
  388. }
  389. public function deliveryNotify()
  390. {
  391. try {
  392. $params = $this->request->param();
  393. app()->make(DeliveryOrderRepository::class)->notify($params);
  394. } catch (Exception $e) {
  395. Log::info('同城配送订单回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  396. }
  397. }
  398. public function diy()
  399. {
  400. $merid = $this->request->param('id', 0);
  401. return app('json')->success(app()->make(DiyRepository::class)->getDiyInfo(0, $merid));
  402. }
  403. public function getNavigation()
  404. {
  405. return app('json')->success(app()->make(DiyRepository::class)->getNavigation());
  406. }
  407. public function micro()
  408. {
  409. $id = $this->request->param('id', 0);
  410. return app('json')->success(app()->make(DiyRepository::class)->getDiyInfo($id, 0, 0));
  411. }
  412. /**
  413. * 是否关注
  414. * @return mixed
  415. */
  416. public function subscribe()
  417. {
  418. if ($this->request->isLogin()) {
  419. $user = $this->request->userInfo();
  420. if ($user && $user['wechat_user_id']) {
  421. $wechatUserService = app()->make(WechatUserRepository::class);
  422. $subscribe = $wechatUserService->getWhereCount([
  423. 'wechat_user_id' => $user['wechat_user_id'],
  424. 'subscribe' => 1
  425. ]) > 0;
  426. return app('json')->success(['subscribe' => $subscribe]);
  427. }
  428. }
  429. return app('json')->success(['subscribe' => false, 'qrcode' => systemConfig('wechat_qrcode')]);
  430. }
  431. public function order_time()
  432. {
  433. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  434. $storeOrderRepository->expireOrder();
  435. }
  436. }