Index.php 13 KB

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