Index.php 6.9 KB

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