Index.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 think\Hook;
  11. class Index extends Common
  12. {
  13. use Util;
  14. protected $noNeedLogin = ['init', 'pageSync', 'page', 'feedback', 'send', 'test'];
  15. protected $noNeedRight = ['*'];
  16. //首页:搜索,购物车数量,轮播,分类列表,3个秒杀,2个团购,2个砍价,6个精品推荐商品。
  17. public function index(){
  18. }
  19. public function init()
  20. {
  21. $platform = $this->request->header('platform');
  22. $templateId = $this->request->param('templateId', 0);
  23. $platformConfig = sheep_config("shop.platform.$platform");
  24. if (empty($platformConfig['status']) || !$platformConfig['status']) {
  25. $this->error('暂不支持该平台,请前往商城配置启用对应平台');
  26. }
  27. $template = Decorate::template()->whereRaw("find_in_set('$platform', platform)");
  28. if ($templateId) {
  29. $template->where('id', $templateId);
  30. } else {
  31. $template->where('status', 'enable');
  32. }
  33. $template = $template->find();
  34. if ($template) {
  35. $template = Page::where('decorate_id', $template->id)->select();
  36. $template = collection($template)->column('page', 'type');
  37. }
  38. $shopConfig = sheep_config('shop.basic');
  39. // 客服配置
  40. $chatSystem = sheep_config('chat.system');
  41. // 客服应用配置
  42. $chatConfig = sheep_config('chat.application.shop');
  43. // 初始化 socket ssl 类型, 默认 none
  44. $ssl = $chatSystem['ssl'] ?? 'none';
  45. $chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';
  46. $chatConfig['chat_domain'] = $chat_domain;
  47. $data = [
  48. 'app' => [
  49. 'name' => $shopConfig['name'],
  50. 'logo' => $shopConfig['logo'],
  51. 'cdnurl' => cdnurl('', true),
  52. 'version' => $shopConfig['version'],
  53. 'user_protocol' => $shopConfig['user_protocol'],
  54. 'privacy_protocol' => $shopConfig['privacy_protocol'],
  55. 'about_us' => $shopConfig['about_us'],
  56. 'copyright' => $shopConfig['copyright'],
  57. 'copytime' => $shopConfig['copytime'],
  58. ],
  59. 'platform' => [
  60. 'auto_login' => $platformConfig['auto_login'] ?? 0,
  61. 'bind_mobile' => $platformConfig['bind_mobile'] ?? 0,
  62. 'payment' => $platformConfig['payment']['methods'],
  63. 'recharge_payment' => sheep_config('shop.recharge_withdraw.recharge.methods'), // 充值支持的支付方式
  64. 'share' => $platformConfig['share'],
  65. ],
  66. 'template' => $template,
  67. 'chat' => $chatConfig
  68. ];
  69. if ($platform == 'WechatMiniProgram') {
  70. $uploadshoppingInfo = new WechatMiniProgramShop(Wechat::miniProgram());
  71. $data['has_wechat_trade_managed'] = intval($uploadshoppingInfo->isTradeManaged());
  72. }
  73. $this->success('初始化', $data);
  74. }
  75. public function pageSync()
  76. {
  77. $pages = $this->request->post('pages/a');
  78. foreach ($pages as $page) {
  79. if (!empty($page['meta']['sync']) && $page['meta']['sync']) {
  80. $data = \app\admin\model\shopro\data\Page::getByPath($page['path']);
  81. $name = $page['meta']['title'] ?? '未命名';
  82. $group = $page['meta']['group'] ?? '其它';
  83. if ($data) {
  84. $data->name = $name;
  85. $data->group = $group;
  86. $data->save();
  87. } else {
  88. \app\admin\model\shopro\data\Page::create([
  89. 'name' => $name,
  90. 'group' => $group,
  91. 'path' => $page['path']
  92. ]);
  93. }
  94. }
  95. }
  96. $this->success();
  97. }
  98. public function page()
  99. {
  100. $id = $this->request->param('id');
  101. $template = \app\admin\model\shopro\decorate\Decorate::typeDiypage()->with('diypage')->where('id', $id)->find();
  102. if (!$template) {
  103. $this->error(__('No Results were found'));
  104. }
  105. $this->success('', $template);
  106. }
  107. public function test()
  108. {
  109. }
  110. public function feedback()
  111. {
  112. $user = auth_user();
  113. $params = $this->request->only(['type', 'content', 'images', 'phone']);
  114. if ($user) {
  115. $params['user_id'] = $user->id;
  116. }
  117. $result = \app\admin\model\shopro\Feedback::create($params);
  118. if ($result) {
  119. $this->success('感谢您的反馈');
  120. }
  121. }
  122. /**
  123. * 发送验证码
  124. *
  125. * @param string $mobile 手机号
  126. * @param string $event 事件名称
  127. */
  128. public function send()
  129. {
  130. $mobile = $this->request->post("mobile");
  131. $event = $this->request->post("event");
  132. $event = $event ? strtolower($event) : 'register';
  133. if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
  134. $this->error(__('手机号不正确'));
  135. }
  136. $last = Smslib::get($mobile, $event);
  137. if ($last && time() - $last['createtime'] < 60) {
  138. $this->error(__('发送频繁'));
  139. }
  140. $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
  141. if ($ipSendTotal >= 5) {
  142. $this->error(__('发送频繁'));
  143. }
  144. if ($event) {
  145. $userinfo = UserModel::getByMobile($mobile);
  146. if ($event == 'register' && $userinfo) {
  147. //已被注册
  148. $this->error(__('手机号已经被注册'));
  149. } elseif (in_array($event, ['changemobile']) && $userinfo) {
  150. //被占用
  151. $this->error(__('手机号已经被占用'));
  152. } elseif (in_array($event, ['changepwd', 'resetpwd', 'mobilelogin']) && !$userinfo) {
  153. //未注册
  154. $this->error(__('手机号未注册'));
  155. }
  156. }
  157. if (!Hook::get('sms_send')) {
  158. $this->error(__('请在后台插件管理安装短信验证插件'));
  159. }
  160. $ret = Smslib::send($mobile, null, $event);
  161. if ($ret) {
  162. $this->success(__('发送成功'));
  163. } else {
  164. $this->error(__('发送失败,请检查短信配置是否正确'));
  165. }
  166. }
  167. /**
  168. * 获取统一验证 token
  169. *
  170. * @return void
  171. */
  172. public function unifiedToken()
  173. {
  174. $user = auth_user();
  175. $token = $this->getUnifiedToken('user:' . $user->id);
  176. $this->success('获取成功', [
  177. 'token' => $token
  178. ]);
  179. }
  180. }