Index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace addons\shopro\controller;
  3. use addons\shopro\controller\traits\Util;
  4. use addons\shopro\library\easywechatPlus\WechatMiniProgramShop;
  5. use app\admin\model\shopro\decorate\Decorate;
  6. use app\admin\model\shopro\decorate\Page;
  7. use app\common\library\Sms as Smslib;
  8. use app\admin\model\shopro\user\User as UserModel;
  9. use addons\shopro\facade\Wechat;
  10. use Symfony\Component\Cache\Adapter\NullAdapter;
  11. use think\Hook;
  12. use think\Db;
  13. class Index extends Common
  14. {
  15. use Util;
  16. protected $noNeedLogin = ['init', 'pageSync', 'page', 'feedback', 'send', 'test'];
  17. protected $noNeedRight = ['*'];
  18. //首页:搜索,购物车数量,轮播,分类列表,3个秒杀,2个团购,2个砍价,6个精品推荐商品。
  19. public function index(){
  20. //购物车数量
  21. $cart_num = 0;
  22. if($this->auth->isLogin()){
  23. $where = [
  24. 'user_id'=>$this->auth->id
  25. ];
  26. $cart_num = Db::name('shopro_cart')->where($where)->sum('goods_num');
  27. }
  28. //轮播
  29. $where = [
  30. 'status' => 1,
  31. 'type' => 2
  32. ];
  33. $banner = Db::name('banner')->field('id, title, image, url')->where($where)->order('weigh', 'desc')->select();
  34. $banner = list_domain_image($banner, ['image']);
  35. //分类列表
  36. $where = [
  37. 'status' => 'normal',
  38. 'parent_id' => 1
  39. ];
  40. $category = Db::name('shopro_category')->field('id, name, image')->where($where)->order('weigh', 'desc')->order('id', 'desc')->select();
  41. $category = list_domain_image($category, ['image']);
  42. //3个秒杀
  43. $seckill_product = [];
  44. $where = [
  45. 'type' => 'seckill',
  46. 'deletetime' => NULL,
  47. ];
  48. $seckill = Db::name('shopro_activity')->field('id,title,start_time,end_time')->where($where)
  49. ->where('start_time', '<=', time())->where('end_time', '>=', time())->order('start_time','asc')->find();
  50. $seckill['seconds'] = $seckill['end_time'] - time();
  51. if($seckill){
  52. $seckill_product = $this->seckill_productsku_list($seckill['id']);
  53. }
  54. //2个团购
  55. $groupon_product = $this->groupon_productsku_list('groupon');
  56. //2个砍价
  57. $kan_product = $this->groupon_productsku_list('kan');
  58. //6个精品推荐商品。
  59. $where = [
  60. 'status' => 'up',
  61. 'deletetime' => Null,
  62. ];
  63. $filed = ['id','title','image','price',];
  64. $tuijian_product = Db::name('shopro_goods')->where($where)->field($filed)->order('show_sales','desc')->limit(6)->select();
  65. $tuijian_product = list_domain_image($tuijian_product,['image']);
  66. //结果
  67. $result = [
  68. 'cart_num' => $cart_num,
  69. 'banner' => $banner,
  70. 'category' => $category,
  71. 'seckill' => $seckill,
  72. 'seckill_product' => $seckill_product,
  73. 'groupon_product' => $groupon_product,
  74. 'kan_product' => $kan_product,
  75. 'tuijian_product' => $tuijian_product,
  76. ];
  77. $this->success(1,$result);
  78. }
  79. //某秒杀的商品列表
  80. private function seckill_productsku_list($activity_id){
  81. $map = [
  82. 'asp.activity_id' => $activity_id,
  83. ];
  84. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  85. ->field([
  86. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
  87. 'g.title','g.image',
  88. 'gsp.image as sku_image']
  89. )
  90. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  91. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  92. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  93. ->where('asp.status','up')
  94. ->where('a.deletetime',NULL)->where('a.type','seckill')
  95. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  96. ->where('gsp.status','up')
  97. ->where($map)
  98. ->order('a.end_time asc,asp.id desc')
  99. ->limit(3)->select();
  100. $list = list_domain_image($list,['image','sku_image']);
  101. foreach($list as $key => $val){
  102. //sku图片代替主图
  103. if(!empty($val['sku_image'])){
  104. $val['image'] = $val['sku_image'];
  105. }
  106. unset($val['sku_image']);
  107. $list[$key] = $val;
  108. }
  109. return $list;
  110. }
  111. //团购商品列表
  112. private function groupon_productsku_list($type){
  113. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  114. ->field([
  115. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
  116. 'g.title','g.image',
  117. 'gsp.image as sku_image']
  118. )
  119. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  120. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  121. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  122. ->where('asp.status','up')
  123. ->where('a.deletetime',NULL)->where('a.type',$type)
  124. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  125. ->where('gsp.status','up')
  126. ->order('a.end_time asc,asp.id desc')
  127. ->limit(2)->select();
  128. $list = list_domain_image($list,['image','sku_image']);
  129. foreach($list as $key => $val){
  130. //sku图片代替主图
  131. if(!empty($val['sku_image'])){
  132. $val['image'] = $val['sku_image'];
  133. }
  134. unset($val['sku_image']);
  135. $list[$key] = $val;
  136. }
  137. return $list;
  138. }
  139. public function init()
  140. {
  141. $platform = $this->request->header('platform');
  142. $templateId = $this->request->param('templateId', 0);
  143. $platformConfig = sheep_config("shop.platform.$platform");
  144. if (empty($platformConfig['status']) || !$platformConfig['status']) {
  145. $this->error('暂不支持该平台,请前往商城配置启用对应平台');
  146. }
  147. $template = Decorate::template()->whereRaw("find_in_set('$platform', platform)");
  148. if ($templateId) {
  149. $template->where('id', $templateId);
  150. } else {
  151. $template->where('status', 'enable');
  152. }
  153. $template = $template->find();
  154. if ($template) {
  155. $template = Page::where('decorate_id', $template->id)->select();
  156. $template = collection($template)->column('page', 'type');
  157. }
  158. $shopConfig = sheep_config('shop.basic');
  159. // 客服配置
  160. $chatSystem = sheep_config('chat.system');
  161. // 客服应用配置
  162. $chatConfig = sheep_config('chat.application.shop');
  163. // 初始化 socket ssl 类型, 默认 none
  164. $ssl = $chatSystem['ssl'] ?? 'none';
  165. $chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';
  166. $chatConfig['chat_domain'] = $chat_domain;
  167. $data = [
  168. 'app' => [
  169. 'name' => $shopConfig['name'],
  170. 'logo' => $shopConfig['logo'],
  171. 'cdnurl' => cdnurl('', true),
  172. 'version' => $shopConfig['version'],
  173. 'user_protocol' => $shopConfig['user_protocol'],
  174. 'privacy_protocol' => $shopConfig['privacy_protocol'],
  175. 'about_us' => $shopConfig['about_us'],
  176. 'copyright' => $shopConfig['copyright'],
  177. 'copytime' => $shopConfig['copytime'],
  178. ],
  179. 'platform' => [
  180. 'auto_login' => $platformConfig['auto_login'] ?? 0,
  181. 'bind_mobile' => $platformConfig['bind_mobile'] ?? 0,
  182. 'payment' => $platformConfig['payment']['methods'],
  183. 'recharge_payment' => sheep_config('shop.recharge_withdraw.recharge.methods'), // 充值支持的支付方式
  184. 'share' => $platformConfig['share'],
  185. ],
  186. 'template' => $template,
  187. 'chat' => $chatConfig
  188. ];
  189. if ($platform == 'WechatMiniProgram') {
  190. $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram());
  191. $data['has_wechat_trade_managed'] = intval($uploadshoppingInfo->isTradeManaged());
  192. }
  193. $this->success('初始化', $data);
  194. }
  195. public function pageSync()
  196. {
  197. $pages = $this->request->post('pages/a');
  198. foreach ($pages as $page) {
  199. if (!empty($page['meta']['sync']) && $page['meta']['sync']) {
  200. $data = \app\admin\model\shopro\data\Page::getByPath($page['path']);
  201. $name = $page['meta']['title'] ?? '未命名';
  202. $group = $page['meta']['group'] ?? '其它';
  203. if ($data) {
  204. $data->name = $name;
  205. $data->group = $group;
  206. $data->save();
  207. } else {
  208. \app\admin\model\shopro\data\Page::create([
  209. 'name' => $name,
  210. 'group' => $group,
  211. 'path' => $page['path']
  212. ]);
  213. }
  214. }
  215. }
  216. $this->success();
  217. }
  218. public function page()
  219. {
  220. $id = $this->request->param('id');
  221. $template = \app\admin\model\shopro\decorate\Decorate::typeDiypage()->with('diypage')->where('id', $id)->find();
  222. if (!$template) {
  223. $this->error(__('No Results were found'));
  224. }
  225. $this->success('', $template);
  226. }
  227. public function test()
  228. {
  229. }
  230. public function feedback()
  231. {
  232. $user = auth_user();
  233. $params = $this->request->only(['type', 'content', 'images', 'phone']);
  234. if ($user) {
  235. $params['user_id'] = $user->id;
  236. }
  237. $result = \app\admin\model\shopro\Feedback::create($params);
  238. if ($result) {
  239. $this->success('感谢您的反馈');
  240. }
  241. }
  242. /**
  243. * 发送验证码
  244. *
  245. * @param string $mobile 手机号
  246. * @param string $event 事件名称
  247. */
  248. public function send()
  249. {
  250. $mobile = $this->request->post("mobile");
  251. $event = $this->request->post("event");
  252. $event = $event ? strtolower($event) : 'register';
  253. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  254. $this->error(__('手机号不正确'));
  255. }
  256. $last = Smslib::get($mobile, $event);
  257. if ($last && time() - $last['createtime'] < 60) {
  258. $this->error(__('发送频繁'));
  259. }
  260. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  261. if ($ipSendTotal >= 5) {
  262. $this->error(__('发送频繁'));
  263. }
  264. if ($event) {
  265. $userinfo = UserModel::getByMobile($mobile);
  266. if ($event == 'register' && $userinfo) {
  267. //已被注册
  268. $this->error(__('手机号已经被注册'));
  269. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  270. //被占用
  271. $this->error(__('手机号已经被占用'));
  272. } elseif (in_array($event, ['changepwd', 'resetpwd', 'mobilelogin']) && !$userinfo) {
  273. //未注册
  274. $this->error(__('手机号未注册'));
  275. }
  276. }
  277. if (!Hook::get('sms_send')) {
  278. $this->error(__('请在后台插件管理安装短信验证插件'));
  279. }
  280. $ret = Smslib::send($mobile, null, $event);
  281. if ($ret) {
  282. $this->success(__('发送成功'));
  283. } else {
  284. $this->error(__('发送失败,请检查短信配置是否正确'));
  285. }
  286. }
  287. /**
  288. * 获取统一验证 token
  289. *
  290. * @return void
  291. */
  292. public function unifiedToken()
  293. {
  294. $user = auth_user();
  295. $token = $this->getUnifiedToken('user:' . $user->id);
  296. $this->success('获取成功', [
  297. 'token' => $token
  298. ]);
  299. }
  300. }