Index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\admin\model\special\RecommendBanner;
  13. use app\admin\model\wechat\WechatQrcode;
  14. use app\wap\model\activity\EventRegistration;
  15. use app\wap\model\live\LiveStudio;
  16. use app\wap\model\special\Lecturer;
  17. use app\wap\model\user\SmsCode;
  18. use app\wap\model\recommend\Recommend;
  19. use app\wap\model\special\SpecialSubject;
  20. use app\wap\model\user\PhoneUser;
  21. use app\wap\model\user\User;
  22. use app\wap\model\wap\Search;
  23. use service\GroupDataService;
  24. use service\JsonService;
  25. use service\SystemConfigService;
  26. use service\UploadService as Upload;
  27. use service\UtilService;
  28. use think\cache\driver\Redis;
  29. use think\Config;
  30. use think\Session;
  31. use think\Cookie;
  32. use think\Url;
  33. use think\Db;
  34. /**首页控制器
  35. * Class Index
  36. * @package app\wap\controller
  37. */
  38. class Index extends AuthController
  39. {
  40. /*
  41. * 白名单
  42. * */
  43. public static function WhiteList()
  44. {
  45. return [
  46. 'agree',
  47. 'index',
  48. 'get_recommend',
  49. 'get_content_recommend',
  50. 'get_host_search',
  51. 'go_search',
  52. 'login',
  53. 'user_login',
  54. 'search',
  55. 'get_unifiend_list',
  56. 'get_recommend_info',
  57. 'more_list',
  58. 'get_more_list',
  59. 'unified_list',
  60. 'qcode_login',
  61. 'get_search_history',
  62. 'aboutus',
  63. ];
  64. }
  65. /**
  66. * @return mixed
  67. */
  68. public function agree()
  69. {
  70. $this->assign('title', SystemConfigService::get('site_name') . '用户付费协议');
  71. $this->assign('content', get_config_content('user_agreement'));
  72. return $this->fetch();
  73. }
  74. /**
  75. * 主页
  76. * @return mixed
  77. */
  78. public function index()
  79. {
  80. $keep_on_record = SystemConfigService::get('yd_keep_on_record');//网站备案信息
  81. $live_one_id = Session::get('live_one_id');
  82. $is_show_or_hide = SystemConfigService::get('is_show_or_hide');
  83. $activity = [];
  84. if ($is_show_or_hide == 1) {
  85. $activity = GroupDataService::getData('home_activity');
  86. }
  87. $this->assign([
  88. 'banner' => json_encode(GroupDataService::getData('store_home_banner') ?: []),//首页轮播
  89. 'title' => SystemConfigService::get('site_name'),
  90. 'keep_on_record' => $keep_on_record,
  91. 'activity' => json_encode($activity),
  92. 'liveOne' => json_encode(LiveStudio::getLiveOne($live_one_id)),
  93. ]);
  94. return $this->fetch();
  95. }
  96. /**
  97. * 上传图片
  98. * @return \think\response\Json
  99. */
  100. public function upload($name = 'file', $link = 'master')
  101. {
  102. $res = Upload::image($name, $link);
  103. $thumbPath = Upload::thumb($res->dir);
  104. if ($res->status == 200)
  105. return JsonService::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  106. else
  107. return JsonService::fail($res->error);
  108. }
  109. /**
  110. * @param int $qcode_id
  111. * @throws \think\exception\DbException
  112. */
  113. public function qcode_login($qcode_id = 0)
  114. {
  115. $qcodeInfo = WechatQrcode::get($qcode_id);
  116. if ($qcodeInfo) {
  117. if ($qcodeInfo->scan_id) {
  118. Session::set('loginUid', $qcodeInfo->scan_id, 'wap');
  119. Cookie::set('__login_phone', 1);
  120. Session::set('__login_phone_num' . $qcodeInfo->scan_id, User::where('uid', $qcodeInfo->scan_id)->value('phone'));
  121. return JsonService::successful();
  122. }
  123. }
  124. return JsonService::fail('');
  125. }
  126. /**
  127. * 手机号码登录
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. * @throws \think\exception\DbException
  131. */
  132. public function login()
  133. {
  134. list($phone, $code) = UtilService::postMore([
  135. ['phone', ''], ['code', '']
  136. ], $this->request, true);
  137. if (!$phone) return JsonService::fail('请输入手机号码');
  138. $userphone = User::where(['uid' => $this->uid])->value('phone');
  139. if ($userphone) {
  140. if ($userphone != $phone) return JsonService::fail('当前手机号码尚未绑定此用户');
  141. }
  142. if (!$code) return JsonService::fail('请输入验证码');
  143. $code = md5('is_phone_code' . $code);
  144. if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
  145. SmsCode::setCodeInvalid($phone, $code);
  146. if (!$userphone) {
  147. //检查手机号码不存在时
  148. $phoneUser = PhoneUser::where(['phone' => $phone])->find();
  149. //H5页面有注册过
  150. if ($phoneUser) {
  151. //检测当前用户是否是H5用户
  152. if (User::where('uid', $phoneUser['uid'])->value('is_h5user')) {
  153. $res = User::setUserRelationInfos($phone, $phoneUser['uid'], $this->uid);
  154. if ($res === false) return JsonService::fail(User::getErrorInfo());
  155. }
  156. }
  157. if (!isset($res)) {
  158. if (!user::be(['phone' => $phone])) {
  159. User::update(['phone' => $phone], ['uid' => $this->uid]);
  160. } else {
  161. return JsonService::fail('手机号已被使用');
  162. }
  163. }
  164. }
  165. $name = '__login_phone_number';
  166. Cookie::set('__login_phone', 1);
  167. Cookie::set('is_login', 1);
  168. Session::set('__login_phone_num' . $this->uid, $phone, 'wap');
  169. Session::set($name, $phone, 'wap');
  170. return JsonService::successful('登录成功');
  171. }
  172. /**
  173. * 获取手机号码登录状态
  174. * */
  175. public function user_login()
  176. {
  177. if ($this->phone || $this->force_binding == 2 && $this->isWechat) {
  178. return JsonService::successful('登录中');
  179. } else {
  180. return JsonService::fail('请先登录!');
  181. }
  182. }
  183. public function login_user()
  184. {
  185. if ($this->uid)
  186. return JsonService::successful('登录中');
  187. else
  188. return JsonService::fail('请登录!');
  189. }
  190. /**
  191. * 获取主页导航图标
  192. */
  193. public function get_recommend()
  194. {
  195. return JsonService::successful(Recommend::getRecommend());
  196. }
  197. /**
  198. * 获取主页推荐列表
  199. * @param int $page
  200. * @param int $limit
  201. */
  202. public function get_content_recommend()
  203. {
  204. try {
  205. //获取推荐列表
  206. $exists_recommend_reids = $this->redisModel->HEXISTS($this->subjectUrl . "wap_index_has", "recommend_list");
  207. if (!$exists_recommend_reids) {
  208. $is_member = isset($this->userInfo['level']) ? $this->userInfo['level'] : 0;
  209. $recommend_list = json_encode(Recommend::getContentRecommend($this->uid, $is_member));
  210. $this->redisModel->hset($this->subjectUrl . "wap_index_has", "recommend_list", $recommend_list);
  211. $this->redisModel->expire($this->subjectUrl . "wap_index_has", 120);
  212. } else {
  213. $recommend_list = $this->redisModel->hget($this->subjectUrl . "wap_index_has", "recommend_list");
  214. }
  215. return JsonService::successful(json_decode($recommend_list, true));
  216. } catch (\Exception $e) {
  217. return JsonService::fail(parent::serRedisPwd($e->getMessage()));
  218. }
  219. }
  220. /**
  221. * 获取热搜词
  222. */
  223. public function get_host_search()
  224. {
  225. return JsonService::successful(Search::getHostSearch());
  226. }
  227. /**
  228. * 清除缓存
  229. */
  230. public function del_search_history()
  231. {
  232. if (!$this->uid) return JsonService::successful('ok');
  233. $res = Db::name('search_history')->where('uid', $this->uid)->delete();
  234. try {
  235. $res1 = $this->redisModel->hdel($this->subjectUrl . "wap_index_has", "search_history_" . $this->uid);
  236. } catch (\Exception $e) {
  237. return JsonService::fail(parent::serRedisPwd($e->getMessage()));
  238. }
  239. if ($res && $res1) {
  240. return JsonService::successful('清除成功!');
  241. } else {
  242. return JsonService::fail('清除失败!');
  243. }
  244. }
  245. /**
  246. * 查找搜索历史内容
  247. * */
  248. public function get_search_history($search = '', $limit = 0)
  249. {
  250. if ($this->uid) {
  251. try {
  252. $exists_search_reids = $this->redisModel->HEXISTS($this->subjectUrl . "wap_index_has", "search_history_" . $this->uid);
  253. if (!$exists_search_reids) {
  254. $search_list = Search::userSearchHistory($this->uid);
  255. $search_list = count($search_list) > 0 ? json_encode($search_list) : [];
  256. if ($search_list) {
  257. $this->redisModel->hset($this->subjectUrl . "wap_index_has", "search_history_" . $this->uid, $search_list);
  258. $this->redisModel->expire($this->subjectUrl . "wap_index_has", 120);
  259. } else {
  260. $this->redisModel->hdel($this->subjectUrl . "wap_index_has", "search_history_" . $this->uid);
  261. }
  262. } else {
  263. $search_list = $this->redisModel->hget($this->subjectUrl . "wap_index_has", "search_history_" . $this->uid);
  264. }
  265. } catch (\Exception $e) {
  266. return JsonService::fail(parent::serRedisPwd($e->getMessage()));
  267. }
  268. } else {
  269. $search_list = [];
  270. }
  271. return JsonService::successful($search_list ? json_decode($search_list, true) : []);
  272. }
  273. /**
  274. * 查找搜索内容
  275. * */
  276. public function go_search($search = '', $limit = 0)
  277. {
  278. return JsonService::successful(Search::getSearchContent($search, $limit, $this->uid));
  279. }
  280. /**
  281. * 搜索页面
  282. * */
  283. public function search()
  284. {
  285. return $this->fetch();
  286. }
  287. /**
  288. * 搜索页面查看更多
  289. * */
  290. public function more_list($type = 0, $search = '')
  291. {
  292. if ($search == '') $this->failed('没有查找相关数据,点我返回上一页', Url::build('index/index'));
  293. $this->assign(compact('type', 'search'));
  294. return $this->fetch();
  295. }
  296. /**
  297. * 分页获取搜索更多内容
  298. * */
  299. public function get_more_list()
  300. {
  301. $where = UtilService::getMore([
  302. ['type', 0],
  303. ['search', ''],
  304. ['page', 1],
  305. ['limit', 10],
  306. ]);
  307. return JsonService::successful(Search::getMoerList($where));
  308. }
  309. /**
  310. * @param int $recommend_id
  311. * @throws \think\exception\DbException
  312. */
  313. public function get_recommend_info($recommend_id = 0)
  314. {
  315. return JsonService::successful(Recommend::get($recommend_id));
  316. }
  317. /**
  318. * 主页标签详情
  319. * @param int $type
  320. * @param string $title
  321. * @param int $recommend_id
  322. * @return mixed
  323. * @throws \think\db\exception\DataNotFoundException
  324. * @throws \think\db\exception\ModelNotFoundException
  325. * @throws \think\exception\DbException
  326. */
  327. public function unified_list($type = 0, $title = '', $recommend_id = 0)
  328. {
  329. if (!$recommend_id) $this->failed('您查看的页面走丢了', Url::build('index/index'));
  330. $recommend = Recommend::get($recommend_id);
  331. if (!$recommend) $this->failed('您查看的栏目不存在', Url::build('index/index'));
  332. if ($recommend->is_show == 0) $this->failed('您查看的栏目不存在', Url::build('index/index'));
  333. $banner = RecommendBanner::valiWhere()->where('recommend_id', $recommend_id)->select();
  334. $Recommendlist = SpecialSubject::where('is_show', 1)->where('is_del', 0)->where('grade_id', $recommend['grade_id'])->field(['name as title', 'id'])->order('sort desc')->select();
  335. if ($recommend->typesetting == 4) {
  336. $recommend->typesetting = 3;
  337. }
  338. $this->assign([
  339. 'type' => (int)$type,
  340. 'title' => $title,
  341. 'grade_id' => (int)$recommend->grade_id,
  342. 'image' => $recommend->image,
  343. 'recommend_id' => (int)$recommend_id,
  344. 'typesetting' => (int)$recommend->typesetting,
  345. 'banner' => json_encode($banner),
  346. 'Recommendlist' => json_encode($Recommendlist),
  347. ]);
  348. return $this->fetch();
  349. }
  350. /**
  351. * 标签详情列表获取
  352. * */
  353. public function get_unifiend_list()
  354. {
  355. $where = UtilService::getMore([
  356. ['page', 1],
  357. ['limit', 10],
  358. ['recommend_id', 0],
  359. ['type', 0],
  360. ['typesetting', 0],
  361. ['subject_id', 0],
  362. ]);
  363. $is_member = isset($this->userInfo['level']) ? $this->userInfo['level'] : 0;
  364. return JsonService::successful(Search::getUnifiendList($where, $is_member));
  365. }
  366. public function date_empty()
  367. {
  368. \think\Session::clear();
  369. \think\Cookie::clear();
  370. }
  371. public function aboutus(){
  372. return $this->fetch();
  373. }
  374. }