Common.php 17 KB

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