Common.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\controller\api;
  3. use app\common\repositories\system\CacheRepository;
  4. use app\common\repositories\user\UserSignRepository;
  5. use ln\basic\BaseController;
  6. use app\common\repositories\store\shipping\ExpressRepository;
  7. use app\common\repositories\store\StoreCategoryRepository;
  8. use app\common\repositories\system\groupData\GroupDataRepository;
  9. use app\common\repositories\user\UserVisitRepository;
  10. use app\common\repositories\wechat\TemplateMessageRepository;
  11. use ln\services\AlipayService;
  12. use ln\services\MiniProgramService;
  13. use ln\services\UploadService;
  14. use ln\services\WechatService;
  15. use Exception;
  16. use Joypack\Tencent\Map\Bundle\Location;
  17. use Joypack\Tencent\Map\Bundle\LocationOption;
  18. use think\exception\ValidateException;
  19. use think\facade\Cache;
  20. use think\facade\Config;
  21. use think\facade\Log;
  22. use think\Response;
  23. use think\response\Html;
  24. /**
  25. * Class Common
  26. * @package app\controller\api
  27. * @author zfy
  28. * @day 2020/5/28
  29. */
  30. class Common extends BaseController
  31. {
  32. /**
  33. * @return mixed
  34. * @author zfy
  35. * @day 2020/5/28
  36. */
  37. public function hotKeyword()
  38. {
  39. $keyword = systemGroupData('hot_keyword');
  40. return app('json')->success($keyword);
  41. }
  42. public function express(ExpressRepository $repository)
  43. {
  44. return app('json')->success($repository->options());
  45. }
  46. public function menus()
  47. {
  48. return app('json')->success(['banner' => systemGroupData('my_banner'), 'menu' => systemGroupData('my_menus')]);
  49. }
  50. public function refundMessage()
  51. {
  52. return app('json')->success(explode("\n", systemConfig('refund_message')));
  53. }
  54. public function config()
  55. {
  56. $config = systemConfig(['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']);
  57. $make = app()->make(TemplateMessageRepository::class);
  58. $sys_intention_agree = app()->make(CacheRepository::class)->getResult('sys_intention_agree');
  59. if (!$sys_intention_agree) {
  60. $sys_intention_agree = systemConfig('sys_intention_agree');
  61. }
  62. $title = app()->make(UserSignRepository::class)->signConfig();
  63. if(!$title){
  64. $config['integral_status'] = 0;
  65. }
  66. $config['sys_intention_agree'] = $sys_intention_agree;
  67. $config['tempid'] = $make->getSubscribe();
  68. return app('json')->success($config);
  69. }
  70. /**
  71. * @param GroupDataRepository $repository
  72. * @return mixed
  73. * @author zfy
  74. * @day 2020/6/3
  75. */
  76. public function userRechargeQuota(GroupDataRepository $repository)
  77. {
  78. $recharge_quota = $repository->groupDataId('user_recharge_quota', 0);
  79. $recharge_attention = explode("\n", systemConfig('recharge_attention'));
  80. return app('json')->success(compact('recharge_quota', 'recharge_attention'));
  81. }
  82. /**
  83. * @param $field
  84. * @return mixed
  85. * @author zfy
  86. * @day 2020/5/28
  87. */
  88. public function uploadImage($field)
  89. {
  90. $file = $this->request->file($field);
  91. if (!$file)
  92. return app('json')->fail('请上传图片');
  93. $file = is_array($file) ? $file[0] : $file;
  94. validate(["$field|图片" => [
  95. 'fileSize' => config('upload.filesize'),
  96. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  97. 'fileMime' => 'image/jpeg,image/png,image/gif',
  98. function ($file) {
  99. $ext = $file->extension();
  100. if ($ext != strtolower($file->extension())) {
  101. return '图片后缀必须为小写';
  102. }
  103. return true;
  104. }
  105. ]])->check([$field => $file]);
  106. $upload = UploadService::create();
  107. $info = $upload->to('def')->move($field);
  108. if ($info === false) {
  109. return app('json')->fail($upload->getError());
  110. }
  111. $res = $upload->getUploadInfo();
  112. $res['dir'] = tidy_url($res['dir']);
  113. return app('json')->success(['path' => $res['dir']]);
  114. }
  115. /**
  116. * @return Response
  117. * @author zfy
  118. * @day 2020/6/3
  119. */
  120. public function wechatNotify()
  121. {
  122. try {
  123. return response(WechatService::create()->handleNotify()->getContent());
  124. } catch (Exception $e) {
  125. Log::info('支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  126. }
  127. }
  128. /**
  129. * 电商收付通合并支付回调
  130. */
  131. public function wechatCombinePayNotify($type)
  132. {
  133. if (!in_array($type, ['order', 'presell'], true))
  134. throw new ValidateException('参数错误');
  135. try {
  136. WechatService::create()->handleCombinePayNotify($type);
  137. } catch (Exception $e) {
  138. Log::info('电商收付通支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  139. }
  140. }
  141. /**
  142. * 电商收付通合并支付回调
  143. */
  144. public function routineCombinePayNotify($type)
  145. {
  146. if (!in_array($type, ['order', 'presell'], true))
  147. throw new ValidateException('参数错误');
  148. try {
  149. WechatService::create()->handleCombinePayNotify($type);
  150. } catch (Exception $e) {
  151. Log::info('小程序电商收付通支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  152. }
  153. }
  154. public function routineNotify()
  155. {
  156. try {
  157. return response(MiniProgramService::create()->handleNotify()->getContent());
  158. } catch (Exception $e) {
  159. Log::info('支付回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  160. }
  161. }
  162. public function alipayNotify($type)
  163. {
  164. if (!in_array($type, ['order', 'user_recharge', 'presell'], true))
  165. throw new ValidateException('参数错误');
  166. $post = $_POST;
  167. $get = $_GET;
  168. $_POST = $this->request->post();
  169. $_GET = $this->request->get();
  170. try {
  171. AlipayService::create()->notify($type);
  172. } catch (Exception $e) {
  173. Log::info('支付宝回调失败:' . var_export([$e->getMessage(), $e->getFile() . ':' . $e->getLine()], true));
  174. } finally {
  175. $_POST = $post;
  176. $_GET = $get;
  177. }
  178. }
  179. public function getVersion()
  180. {
  181. return app('json')->success(['version' => get_crmeb_version(), 'host' => request()->host(), 'system' => PHP_OS, 'php' => @phpversion()]);
  182. }
  183. /**
  184. * 获取图片base64
  185. * @return mixed
  186. */
  187. public function get_image_base64()
  188. {
  189. list($imageUrl, $codeUrl) = $this->request->params([
  190. ['image', ''],
  191. ['code', ''],
  192. ], true);
  193. try {
  194. $codeTmp = $code = $codeUrl ? image_to_base64($codeUrl) : '';
  195. if (!$codeTmp) {
  196. $putCodeUrl = put_image($codeUrl);
  197. $code = $putCodeUrl ? image_to_base64('./runtime/temp' . $putCodeUrl) : '';
  198. $code && unlink('./runtime/temp' . $putCodeUrl);
  199. }
  200. $imageTmp = $image = $imageUrl ? image_to_base64($imageUrl) : '';
  201. if (!$imageTmp) {
  202. $putImageUrl = put_image($imageUrl);
  203. $image = $putImageUrl ? image_to_base64('./runtime/temp' . $putImageUrl) : '';
  204. $image && unlink('./runtime/temp' . $putImageUrl);
  205. }
  206. return app('json')->success(compact('code', 'image'));
  207. } catch (Exception $e) {
  208. return app('json')->fail($e->getMessage());
  209. }
  210. }
  211. public function home()
  212. {
  213. $banner = systemGroupData('home_banner', 1, 10);
  214. $menu = systemGroupData('home_menu');
  215. $hot = systemGroupData('home_hot', 1, 4);
  216. $ad = systemConfig(['home_ad_pic', 'home_ad_url']);
  217. $category = app()->make(StoreCategoryRepository::class)->getTwoLevel();
  218. return app('json')->success(compact('banner', 'menu', 'hot', 'ad', 'category'));
  219. }
  220. public function visit()
  221. {
  222. if (!$this->request->isLogin()) return app('json')->success();
  223. [$page, $type] = $this->request->params(['page', 'type'], true);
  224. $uid = $this->request->uid();
  225. if (!$page || !$uid) return app('json')->fail();
  226. $userVisitRepository = app()->make(UserVisitRepository::class);
  227. $type == 'routine' ? $userVisitRepository->visitSmallProgram($uid, $page) : $userVisitRepository->visitPage($uid, $page);
  228. return app('json')->success();
  229. }
  230. public function hotBanner($type)
  231. {
  232. if (!in_array($type, ['new', 'hot', 'best', 'good']))
  233. $data = [];
  234. else
  235. $data = systemGroupData($type . '_home_banner');
  236. return app('json')->success($data);
  237. }
  238. public function pay_key($key)
  239. {
  240. $cache = Cache::store('file');
  241. if (!$cache->has('pay_key' . $key)) {
  242. return app('json')->fail('支付链接不存在');
  243. }
  244. return app('json')->success($cache->get('pay_key' . $key));
  245. }
  246. public function lbs_geocoder()
  247. {
  248. $data = explode(',', $this->request->param('location', ''));
  249. $locationOption = new LocationOption(systemConfig('tx_map_key'));
  250. $locationOption->setLocation($data[0] ?? '', $data[1] ?? '');
  251. $location = new Location($locationOption);
  252. $res = $location->request();
  253. if ($res->error) {
  254. return app('json')->fail($res->error);
  255. }
  256. if ($res->status) {
  257. return app('json')->fail($res->message);
  258. }
  259. if (!$res->result) {
  260. return app('json')->fail('获取失败');
  261. }
  262. return app('json')->success($res->result);
  263. }
  264. }