Index.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. ->limit(3)->select();
  99. $list = list_domain_image($list,['image','sku_image']);
  100. foreach($list as $key => $val){
  101. //sku图片代替主图
  102. if(!empty($val['sku_image'])){
  103. $val['image'] = $val['sku_image'];
  104. }
  105. unset($val['sku_image']);
  106. $list[$key] = $val;
  107. }
  108. return $list;
  109. }
  110. //团购商品列表
  111. private function groupon_productsku_list($type){
  112. $list = Db::name('shopro_activity_sku_price')->alias('asp')
  113. ->field([
  114. 'asp.activity_id','asp.goods_sku_price_id','asp.goods_id','asp.price',
  115. 'g.title','g.image',
  116. 'gsp.image as sku_image']
  117. )
  118. ->join('shopro_activity a','asp.activity_id = a.id','LEFT')
  119. ->join('shopro_goods g' ,'asp.goods_id = g.id','LEFT')
  120. ->join('shopro_goods_sku_price gsp','asp.goods_sku_price_id = gsp.id','LEFT')
  121. ->where('asp.status','up')
  122. ->where('a.deletetime',NULL)->where('a.type',$type)
  123. ->where('g.deletetime',NULL)->whereIn('g.status', ['up', 'hidden'])
  124. ->where('gsp.status','up')
  125. ->order('a.end_time asc')
  126. ->limit(2)->select();
  127. $list = list_domain_image($list,['image','sku_image']);
  128. foreach($list as $key => $val){
  129. //sku图片代替主图
  130. if(!empty($val['sku_image'])){
  131. $val['image'] = $val['sku_image'];
  132. }
  133. unset($val['sku_image']);
  134. $list[$key] = $val;
  135. }
  136. return $list;
  137. }
  138. public function init()
  139. {
  140. $platform = $this->request->header('platform');
  141. $templateId = $this->request->param('templateId', 0);
  142. $platformConfig = sheep_config("shop.platform.$platform");
  143. if (empty($platformConfig['status']) || !$platformConfig['status']) {
  144. $this->error('暂不支持该平台,请前往商城配置启用对应平台');
  145. }
  146. $template = Decorate::template()->whereRaw("find_in_set('$platform', platform)");
  147. if ($templateId) {
  148. $template->where('id', $templateId);
  149. } else {
  150. $template->where('status', 'enable');
  151. }
  152. $template = $template->find();
  153. if ($template) {
  154. $template = Page::where('decorate_id', $template->id)->select();
  155. $template = collection($template)->column('page', 'type');
  156. }
  157. $shopConfig = sheep_config('shop.basic');
  158. // 客服配置
  159. $chatSystem = sheep_config('chat.system');
  160. // 客服应用配置
  161. $chatConfig = sheep_config('chat.application.shop');
  162. // 初始化 socket ssl 类型, 默认 none
  163. $ssl = $chatSystem['ssl'] ?? 'none';
  164. $chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';
  165. $chatConfig['chat_domain'] = $chat_domain;
  166. $data = [
  167. 'app' => [
  168. 'name' => $shopConfig['name'],
  169. 'logo' => $shopConfig['logo'],
  170. 'cdnurl' => cdnurl('', true),
  171. 'version' => $shopConfig['version'],
  172. 'user_protocol' => $shopConfig['user_protocol'],
  173. 'privacy_protocol' => $shopConfig['privacy_protocol'],
  174. 'about_us' => $shopConfig['about_us'],
  175. 'copyright' => $shopConfig['copyright'],
  176. 'copytime' => $shopConfig['copytime'],
  177. ],
  178. 'platform' => [
  179. 'auto_login' => $platformConfig['auto_login'] ?? 0,
  180. 'bind_mobile' => $platformConfig['bind_mobile'] ?? 0,
  181. 'payment' => $platformConfig['payment']['methods'],
  182. 'recharge_payment' => sheep_config('shop.recharge_withdraw.recharge.methods'), // 充值支持的支付方式
  183. 'share' => $platformConfig['share'],
  184. ],
  185. 'template' => $template,
  186. 'chat' => $chatConfig
  187. ];
  188. if ($platform == 'WechatMiniProgram') {
  189. $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram());
  190. $data['has_wechat_trade_managed'] = intval($uploadshoppingInfo->isTradeManaged());
  191. }
  192. $this->success('初始化', $data);
  193. }
  194. public function pageSync()
  195. {
  196. $pages = $this->request->post('pages/a');
  197. foreach ($pages as $page) {
  198. if (!empty($page['meta']['sync']) && $page['meta']['sync']) {
  199. $data = \app\admin\model\shopro\data\Page::getByPath($page['path']);
  200. $name = $page['meta']['title'] ?? '未命名';
  201. $group = $page['meta']['group'] ?? '其它';
  202. if ($data) {
  203. $data->name = $name;
  204. $data->group = $group;
  205. $data->save();
  206. } else {
  207. \app\admin\model\shopro\data\Page::create([
  208. 'name' => $name,
  209. 'group' => $group,
  210. 'path' => $page['path']
  211. ]);
  212. }
  213. }
  214. }
  215. $this->success();
  216. }
  217. public function page()
  218. {
  219. $id = $this->request->param('id');
  220. $template = \app\admin\model\shopro\decorate\Decorate::typeDiypage()->with('diypage')->where('id', $id)->find();
  221. if (!$template) {
  222. $this->error(__('No Results were found'));
  223. }
  224. $this->success('', $template);
  225. }
  226. public function test()
  227. {
  228. }
  229. public function feedback()
  230. {
  231. $user = auth_user();
  232. $params = $this->request->only(['type', 'content', 'images', 'phone']);
  233. if ($user) {
  234. $params['user_id'] = $user->id;
  235. }
  236. $result = \app\admin\model\shopro\Feedback::create($params);
  237. if ($result) {
  238. $this->success('感谢您的反馈');
  239. }
  240. }
  241. /**
  242. * 发送验证码
  243. *
  244. * @param string $mobile 手机号
  245. * @param string $event 事件名称
  246. */
  247. public function send()
  248. {
  249. $mobile = $this->request->post("mobile");
  250. $event = $this->request->post("event");
  251. $event = $event ? strtolower($event) : 'register';
  252. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  253. $this->error(__('手机号不正确'));
  254. }
  255. $last = Smslib::get($mobile, $event);
  256. if ($last && time() - $last['createtime'] < 60) {
  257. $this->error(__('发送频繁'));
  258. }
  259. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  260. if ($ipSendTotal >= 5) {
  261. $this->error(__('发送频繁'));
  262. }
  263. if ($event) {
  264. $userinfo = UserModel::getByMobile($mobile);
  265. if ($event == 'register' && $userinfo) {
  266. //已被注册
  267. $this->error(__('手机号已经被注册'));
  268. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  269. //被占用
  270. $this->error(__('手机号已经被占用'));
  271. } elseif (in_array($event, ['changepwd', 'resetpwd', 'mobilelogin']) && !$userinfo) {
  272. //未注册
  273. $this->error(__('手机号未注册'));
  274. }
  275. }
  276. if (!Hook::get('sms_send')) {
  277. $this->error(__('请在后台插件管理安装短信验证插件'));
  278. }
  279. $ret = Smslib::send($mobile, null, $event);
  280. if ($ret) {
  281. $this->success(__('发送成功'));
  282. } else {
  283. $this->error(__('发送失败,请检查短信配置是否正确'));
  284. }
  285. }
  286. /**
  287. * 获取统一验证 token
  288. *
  289. * @return void
  290. */
  291. public function unifiedToken()
  292. {
  293. $user = auth_user();
  294. $token = $this->getUnifiedToken('user:' . $user->id);
  295. $this->success('获取成功', [
  296. 'token' => $token
  297. ]);
  298. }
  299. }