DataDownload.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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\admin\controller\download;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use app\admin\model\download\DataDownloadCategpry;
  15. use app\admin\model\download\DataDownload as DownloadModel;
  16. use app\admin\model\download\DataDownloadRecords;
  17. use service\SystemConfigService;
  18. use app\admin\model\system\Recommend;
  19. use app\admin\model\system\RecommendRelation;
  20. use app\admin\model\system\WebRecommend;
  21. use app\admin\model\system\WebRecommendRelation;
  22. use app\admin\model\merchant\Merchant;
  23. use service\FormBuilder as Form;
  24. use think\Url;
  25. /**资料控制器
  26. * Class DataDownload
  27. * @package app\admin\controller\download
  28. */
  29. class DataDownload extends AuthController
  30. {
  31. /**
  32. * 资料下载列表
  33. */
  34. public function index()
  35. {
  36. $cate_list = DataDownloadCategpry::specialCategoryAll(2);
  37. $mer_list = Merchant::getMerchantList();
  38. $this->assign([
  39. 'cate_list' => $cate_list,
  40. 'mer_list' => $mer_list
  41. ]);
  42. return $this->fetch();
  43. }
  44. /**
  45. * 获取资料下载列表
  46. */
  47. public function data_download_list()
  48. {
  49. $where = parent::getMore([
  50. ['start_time', ''],
  51. ['end_time', ''],
  52. ['status', 1],
  53. ['page', 1],
  54. ['limit', 20],
  55. ['cate_id', 0],
  56. ['mer_id', 0],
  57. ['is_show', ''],
  58. ['title', '']
  59. ]);
  60. return Json::successlayui(DownloadModel::get_download_list($where));
  61. }
  62. /**资料审核
  63. * @return mixed
  64. */
  65. public function examine()
  66. {
  67. $cate_list = DataDownloadCategpry::specialCategoryAll(2);
  68. $mer_list = Merchant::getMerchantList();
  69. $this->assign([
  70. 'cate_list' => $cate_list,
  71. 'mer_list' => $mer_list
  72. ]);
  73. return $this->fetch();
  74. }
  75. /**获得审核资料
  76. * @throws \think\Exception
  77. */
  78. public function data_download_examine_list()
  79. {
  80. $where = parent::getMore([
  81. ['start_time', ''],
  82. ['end_time', ''],
  83. ['status', ''],
  84. ['page', 1],
  85. ['limit', 20],
  86. ['cate_id', 0],
  87. ['mer_id', 0],
  88. ['title', '']
  89. ]);
  90. return Json::successlayui(DownloadModel::get_download_examine_list($where));
  91. }
  92. public function examineDetails($id)
  93. {
  94. if (!$id) return Json::fail('参数错误');
  95. $details = DownloadModel::get($id);
  96. if (!$details) return Json::fail('资料不存在');
  97. $this->assign(['details' => json_encode($details)]);
  98. return $this->fetch('material');
  99. }
  100. /**不通过
  101. * @param $id
  102. * @throws \think\exception\DbException
  103. */
  104. public function fail($id)
  105. {
  106. $fail_msg = parent::postMore([
  107. ['message', ''],
  108. ]);
  109. if (!DownloadModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  110. $special = DownloadModel::get($id);
  111. if (!$special) return Json::fail('操作记录不存!');
  112. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  113. DownloadModel::beginTrans();
  114. $res = DownloadModel::changeFail($id, $special['mer_id'], $fail_msg['message']);
  115. if ($res) {
  116. DownloadModel::commitTrans();
  117. return Json::successful('操作成功!');
  118. } else {
  119. DownloadModel::rollbackTrans();
  120. return Json::fail('操作失败!');
  121. }
  122. }
  123. /**通过
  124. * @param $id
  125. * @throws \think\exception\DbException
  126. */
  127. public function succ($id)
  128. {
  129. if (!DownloadModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  130. $special = DownloadModel::get($id);
  131. if (!$special) return Json::fail('操作记录不存!');
  132. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  133. DownloadModel::beginTrans();
  134. $res = DownloadModel::changeSuccess($id, $special['mer_id']);
  135. if ($res) {
  136. DownloadModel::commitTrans();
  137. return Json::successful('操作成功!');
  138. } else {
  139. DownloadModel::rollbackTrans();
  140. return Json::fail('操作失败!');
  141. }
  142. }
  143. public function get_cate_list()
  144. {
  145. $cate_list = DataDownloadCategpry::specialCategoryAll(2);
  146. return Json::successful($cate_list);
  147. }
  148. /**资料编辑、添加
  149. * @param int $id
  150. */
  151. public function add($id = 0)
  152. {
  153. if ($id) {
  154. $download = DownloadModel::get($id);
  155. if (!$download) return Json::fail('资料不存在');
  156. $this->assign(['download' => json_encode($download)]);
  157. }
  158. $this->assign(['id' => $id]);
  159. return $this->fetch();
  160. }
  161. /**保存、编辑轻专题
  162. * @param int $id
  163. */
  164. public function save_data($id = 0)
  165. {
  166. $data = parent::postMore([
  167. ['title', ''],
  168. ['abstract', ''],
  169. ['cate_id', 0],
  170. ['sales', 0],
  171. ['image', ''],
  172. ['poster_image', ''],
  173. ['money', 0],
  174. ['sort', 0],
  175. ['member_money', 0],
  176. ['member_pay_type', 0],
  177. ['pay_type', 0],//支付方式:免费、付费
  178. ['type', 0],
  179. ['link', ''],
  180. ['network_disk_link', ''],
  181. ['network_disk_pwd', '']
  182. ]);
  183. if (!$data['cate_id']) return Json::fail('请选择分类');
  184. if (!$data['title']) return Json::fail('请输入资料标题');
  185. if (!$data['abstract']) return Json::fail('请输入资料简介');
  186. if (!$data['image']) return Json::fail('请上传资料封面图');
  187. if (!$data['poster_image']) return Json::fail('请上传推广海报');
  188. if ($data['type'] == 1) {
  189. if (!$data['link']) return Json::fail('请上传文件');
  190. $data['network_disk_link'] = '';
  191. $data['network_disk_pwd'] = '';
  192. } else if ($data['type'] == 2) {
  193. if (!$data['network_disk_link']) return Json::fail('请输入百度网盘文件链接');
  194. if (!$data['network_disk_pwd']) return Json::fail('请输入百度网盘文件获取密码');
  195. $data['link'] = '';
  196. } else {
  197. if (!$data['link']) return Json::fail('请上传文件');
  198. if (!$data['network_disk_link']) return Json::fail('请输入百度网盘文件链接');
  199. if (!$data['network_disk_pwd']) return Json::fail('请输入百度网盘文件获取密码');
  200. }
  201. if ($data['pay_type'] == PAY_MONEY && ($data['money'] == '' || $data['money'] == 0.00 || $data['money'] < 0)) return Json::fail('购买金额未填写或者金额非法');
  202. if ($data['member_pay_type'] == MEMBER_PAY_MONEY && ($data['member_money'] == '' || $data['member_money'] == 0.00 || $data['member_money'] < 0)) return Json::fail('会员购买金额未填写或金额非法');
  203. if ($data['pay_type'] != PAY_MONEY) {
  204. $data['money'] = 0;
  205. }
  206. if ($data['member_pay_type'] != MEMBER_PAY_MONEY) {
  207. $data['member_money'] = 0;
  208. }
  209. DownloadModel::beginTrans();
  210. try {
  211. if ($id) {
  212. $res = DownloadModel::edit($data, $id, 'id');
  213. if ($res) {
  214. DownloadModel::commitTrans();
  215. return Json::successful('修改成功');
  216. } else {
  217. DownloadModel::rollbackTrans();
  218. return Json::fail('添加失败');
  219. }
  220. } else {
  221. $data['is_show'] = 1;
  222. $data['add_time'] = time();
  223. if (DownloadModel::be(['title' => $data['title'], 'is_del' => 0])) return Json::fail('资料已存在');
  224. $res = DownloadModel::set($data);
  225. if ($res) {
  226. DownloadModel::commitTrans();
  227. return Json::successful('添加成功');
  228. } else {
  229. DownloadModel::rollbackTrans();
  230. return Json::fail('添加失败');
  231. }
  232. }
  233. } catch (\Exception $e) {
  234. DownloadModel::rollbackTrans();
  235. return Json::fail($e->getMessage());
  236. }
  237. }
  238. public function sliceFileUpload()
  239. {
  240. $aliyunOss = \Api\AliyunOss::instance([
  241. 'AccessKey' => SystemConfigService::get('accessKeyId'),
  242. 'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
  243. 'OssEndpoint' => SystemConfigService::get('end_point'),
  244. 'OssBucket' => SystemConfigService::get('OssBucket'),
  245. 'uploadUrl' => SystemConfigService::get('uploadUrl'),
  246. ]);
  247. $res = $aliyunOss->sliceFileUpload('file');
  248. if ($res) {
  249. return Json::successful('上传成功', ['url' => $res['url']]);
  250. } else {
  251. return Json::fail('上传失败');
  252. }
  253. }
  254. /**
  255. * 快速编辑
  256. * @param string $field 字段名
  257. * @param int $id 修改的主键
  258. * @param string value 修改后的值
  259. * @return json
  260. */
  261. public function set_value($field = '', $id = '', $value = '')
  262. {
  263. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  264. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  265. if ($field == 'ficti' && bcsub($value, 0, 0) < 0) return Json::fail('虚拟下载量不能为负数');
  266. $res = DownloadModel::where('id', $id)->update([$field => $value]);
  267. if ($res)
  268. return Json::successful('保存成功');
  269. else
  270. return Json::fail('保存失败');
  271. }
  272. /**
  273. * 添加推荐
  274. * @param int $special_id
  275. * @return mixed
  276. * @throws \think\exception\DbException
  277. */
  278. public function recommend($data_id = 0)
  279. {
  280. if (!$data_id) $this->failed('缺少参数');
  281. $download = DownloadModel::get($data_id);
  282. if (!$download) $this->failed('没有查到此资料');
  283. if ($download->is_del) $this->failed('此资料已删除');
  284. $form = Form::create(Url::build('save_recommend', ['data_id' => $data_id]), [
  285. Form::select('recommend_id', '推荐')->setOptions(function () {
  286. $model = Recommend::where(['is_show' => 1, 'is_fixed' => 0, 'type' => 14]);
  287. $list = $model->field('title,id')->order('sort desc,add_time desc')->select();
  288. $menus = [];
  289. foreach ($list as $menu) {
  290. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  291. }
  292. return $menus;
  293. })->filterable(1),
  294. Form::number('sort', '排序'),
  295. ]);
  296. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  297. $this->assign(compact('form'));
  298. return $this->fetch('public/form-builder');
  299. }
  300. /**
  301. * 保存推荐
  302. * @param int $special_id
  303. * @throws \think\exception\DbException
  304. */
  305. public function save_recommend($data_id = 0)
  306. {
  307. if (!$data_id) $this->failed('缺少参数');
  308. $data = parent::postMore([
  309. ['recommend_id', 0],
  310. ['sort', 0],
  311. ]);
  312. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  313. $recommend = Recommend::get($data['recommend_id']);
  314. if (!$recommend) return Json::fail('导航菜单不存在');
  315. $data['add_time'] = time();
  316. $data['type'] = $recommend->type;
  317. $data['link_id'] = $data_id;
  318. if (RecommendRelation::be(['type' => $recommend->type, 'link_id' => $data_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  319. if (RecommendRelation::set($data))
  320. return Json::successful('推荐成功');
  321. else
  322. return Json::fail('推荐失败');
  323. }
  324. /**取消推荐
  325. * @param int $id
  326. */
  327. public function cancel_recommendation($id = 0, $data_id = 0)
  328. {
  329. if (!$id || !$data_id) $this->failed('缺少参数');
  330. if (RecommendRelation::be(['id' => $id, 'link_id' => $data_id])) {
  331. $res = RecommendRelation::where(['id' => $id, 'link_id' => $data_id])->delete();
  332. if ($res)
  333. return Json::successful('取消推荐成功');
  334. else
  335. return Json::fail('取消推荐失败');
  336. } else {
  337. return Json::fail('推荐不存在');
  338. }
  339. }
  340. /**
  341. * 添加推荐
  342. * @param int $data_id
  343. * @return mixed
  344. * @throws \think\exception\DbException
  345. */
  346. public function web_recommend($data_id = 0)
  347. {
  348. if (!$data_id) $this->failed('缺少参数');
  349. $download = DownloadModel::get($data_id);
  350. if (!$download) $this->failed('没有查到此资料');
  351. if ($download->is_del) $this->failed('此资料已删除');
  352. $form = Form::create(Url::build('save_web_recommend', ['data_id' => $data_id]), [
  353. Form::select('recommend_id', '推荐')->setOptions(function () {
  354. $model = WebRecommend::where(['is_show' => 1, 'type' => 3]);
  355. $list = $model->field('title,id')->order('sort desc,add_time desc')->select();
  356. $menus = [];
  357. foreach ($list as $menu) {
  358. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  359. }
  360. return $menus;
  361. })->filterable(1),
  362. Form::number('sort', '排序'),
  363. ]);
  364. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  365. $this->assign(compact('form'));
  366. return $this->fetch('public/form-builder');
  367. }
  368. /**
  369. * 保存推荐
  370. * @param int $special_id
  371. * @throws \think\exception\DbException
  372. */
  373. public function save_web_recommend($data_id = 0)
  374. {
  375. if (!$data_id) $this->failed('缺少参数');
  376. $data = parent::postMore([
  377. ['recommend_id', 0],
  378. ['sort', 0],
  379. ]);
  380. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  381. $recommend = WebRecommend::get($data['recommend_id']);
  382. if (!$recommend) return Json::fail('导航菜单不存在');
  383. $data['add_time'] = time();
  384. $data['type'] = $recommend->type;
  385. $data['link_id'] = $data_id;
  386. if (WebRecommendRelation::be(['type' => $recommend->type, 'link_id' => $data_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  387. if (WebRecommendRelation::set($data))
  388. return Json::successful('推荐成功');
  389. else
  390. return Json::fail('推荐失败');
  391. }
  392. /**取消推荐
  393. * @param int $id
  394. */
  395. public function cancel_web_recommendation($id = 0, $data_id = 0)
  396. {
  397. if (!$id || !$data_id) return Json::fail('缺少参数');
  398. if (WebRecommendRelation::be(['id' => $id, 'link_id' => $data_id])) {
  399. $res = WebRecommendRelation::where(['id' => $id, 'link_id' => $data_id])->delete();
  400. if ($res)
  401. return Json::successful('取消推荐成功');
  402. else
  403. return Json::fail('取消推荐失败');
  404. } else {
  405. return Json::fail('推荐不存在');
  406. }
  407. }
  408. /**资料删除
  409. * @param int $id
  410. */
  411. public function delete($id = 0)
  412. {
  413. if (!$id) $this->failed('缺少参数');
  414. $download = DownloadModel::get($id);
  415. if (!$download) $this->failed('没有查到此资料');
  416. if ($download->is_del) $this->failed('此资料已删除');
  417. $data['is_del'] = 1;
  418. $res = DownloadModel::edit($data, $id);
  419. if ($res)
  420. return Json::successful('删除成功');
  421. else
  422. return Json::fail('删除失败');
  423. }
  424. /**下载记录
  425. * @param int $id
  426. * @throws \think\exception\DbException
  427. */
  428. public function records($id = 0)
  429. {
  430. $this->assign(['id' => $id, 'year' => getMonth('y')]);
  431. return $this->fetch();
  432. }
  433. public function get_download_records_list($id)
  434. {
  435. $where = parent::getMore([
  436. ['id', 0],
  437. ['page', 1],
  438. ['limit', 20],
  439. ['excel', 0],
  440. ['data', '']
  441. ]);
  442. $where['id'] = $where['id'] >= 0 ? $where['id'] : $id;
  443. return Json::successlayui(DataDownloadRecords::specialLearningRecordsLists($where, $where['id']));
  444. }
  445. }