Common.php 8.8 KB

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