DownController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. namespace api\controllers;
  3. use app\common\controllers\AController;
  4. use app\modules\doc\models\DocReal;
  5. use app\modules\doc\models\DocPaylog;
  6. use app\modules\doc\models\DocDowncode;
  7. use app\modules\doc\models\DocMail;
  8. use app\common\components\MessageOne;
  9. use app\common\components\Wallet;
  10. use Yii;
  11. class DownController extends AController
  12. {
  13. public function init()
  14. {
  15. parent::init();
  16. }
  17. public function actionFreedownload()
  18. {
  19. $id = intval($this->post['id']);
  20. $doc = \app\modules\doc\models\DocReal::findOne($id);
  21. if($this->userInfo['is_delete']||$this->userInfo['is_lock'])
  22. {
  23. return [
  24. 'error' => 1,
  25. 'msg' => '账户已被限制,禁止下载',
  26. 'data' => [],
  27. 'code'=>200
  28. ];
  29. }
  30. if(empty($doc)||$doc->is_delete==1)
  31. {
  32. return [
  33. 'error' => 1,
  34. 'msg' => '该'.$this->docname.'不存在',
  35. 'data' => [],
  36. 'code'=>200
  37. ];
  38. }
  39. if(!$doc->realfree)
  40. {
  41. return [
  42. 'error' => 1,
  43. 'msg' => '暂无下载权限',
  44. 'data' => [],
  45. 'code'=>200
  46. ];
  47. }
  48. $existPayLog = DocPaylog::find()->where("user_id=".$this->user_id." and doc_id=$id")->exists();
  49. if(!$existPayLog)
  50. {
  51. $groupRights = $this->userInfo['group_rights'];
  52. $vipSettings = $this->userInfo['vip_settings']?$this->userInfo['vip_settings']:[];
  53. //日期
  54. $timeList = getTimeList(1);
  55. $todayStart = $timeList['dayStart'];
  56. $todayEnd = $timeList['dayEnd'];
  57. //今日免费下载数量
  58. $freeDownNumToday = DocPaylog::find()->where("user_id=".$this->user_id." and (create_time>=".$todayStart." and create_time<=".$todayEnd.") and is_free=1")->count();
  59. //免费总下载数量
  60. $freeDownNumTotal = DocPaylog::find()->where("user_id=".$this->user_id." and is_free=1")->count();
  61. //今日下载数量
  62. $downNumToday = DocPaylog::find()->where("user_id=".$this->user_id." and (create_time>=".$todayStart." and create_time<=".$todayEnd.")")->count();
  63. //总下载数量
  64. $downNumTotal = DocPaylog::find()->where("user_id=".$this->user_id."")->count();
  65. //所在用户组被禁止下载
  66. if($groupRights['doc_downnum_day']==-1||$groupRights['doc_downnum_total']==-1)
  67. {
  68. return [
  69. 'error' => 1,
  70. 'msg' => '您已被禁止下载',
  71. 'data' => 1,
  72. 'code'=>200
  73. ];
  74. }
  75. //VIP下载总量限制检查
  76. if(!empty($vipSettings))
  77. {
  78. if($vipSettings['freedoc_download_num']>0)
  79. {
  80. if($freeDownNumTotal>=$vipSettings['freedoc_download_num'])
  81. {
  82. return [
  83. 'error' => 1,
  84. 'msg' => '您已达到免费'.$this->docname.'下载总量上限('.$vipSettings['freedoc_download_num'].'份),无法继续下载',
  85. 'data' => 1,
  86. 'code'=>200
  87. ];
  88. }
  89. }
  90. }
  91. //如果并非VIP也不是VIP无限下载权限,需要经过用户等级总量限制检查
  92. if(empty($vipSettings)||$vipSettings['freedoc_download_num']!==0)
  93. {
  94. if($groupRights['doc_freedownload_totalnum']>0&&$freeDownNumTotal>=$groupRights['doc_freedownload_totalnum'])
  95. {
  96. return [
  97. 'error' => 1,
  98. 'msg' => '您已达到免费'.$this->docname.'下载总量上限('.$groupRights['doc_freedownload_totalnum'].'份),无法继续下载',
  99. 'data' => 1,
  100. 'code'=>200
  101. ];
  102. }
  103. }
  104. //用户等级单日免费下载量限制检查
  105. if($groupRights['doc_freedownload_num']>0&&$freeDownNumToday>=$groupRights['doc_freedownload_num'])
  106. {
  107. return [
  108. 'error' => 1,
  109. 'msg' => '您已达到今日免费'.$this->docname.'下载总量上限('.$groupRights['doc_freedownload_num'].'份/天),无法继续下载',
  110. 'data' => 1,
  111. 'code'=>200
  112. ];
  113. }
  114. //用户等级下载总量限制
  115. if($groupRights['doc_downnum_total']>0&&$downNumTotal>=$groupRights['doc_downnum_total'])
  116. {
  117. return [
  118. 'error' => 1,
  119. 'msg' => '您已达到'.$this->docname.'下载总量上限('.$groupRights['doc_downnum_total'].'份),无法继续下载',
  120. 'data' => 1,
  121. 'code'=>200
  122. ];
  123. }
  124. //用户等级单日下载量限制
  125. if($groupRights['doc_downnum_day']>0&&$downNumToday>=$groupRights['doc_downnum_day'])
  126. {
  127. return [
  128. 'error' => 1,
  129. 'msg' => '您已达到今日'.$this->docname.'下载总量上限('.$groupRights['doc_downnum_day'].'份/天),无法继续下载',
  130. 'data' => 1,
  131. 'code'=>200
  132. ];
  133. }
  134. //生成支付记录
  135. $paylog = new DocPaylog();
  136. $paylog->user_id = $this->user_id;
  137. $paylog->doc_user_id = $doc->user_id;
  138. $paylog->doc_id = $doc->id;
  139. $paylog->cat_id = $doc->cat_id;
  140. $paylog->cat_ids = $doc->cat_ids;
  141. $paylog->doc_type = $doc->doc_type;
  142. $paylog->is_free = 1;
  143. $paylog->coin_price = $doc->coin_price;
  144. $paylog->coin_num = 0;
  145. $paylog->agent_id = intval($this->userInfo['referer_id']);
  146. $paylog->agent_ids = strval($this->userInfo['referer_ids']);
  147. $paylog->create_time = TIMESTAMP;
  148. if($paylog->save())
  149. {
  150. //更新文档售出次数
  151. $doc->updateCounters(['sales'=>1]);
  152. }
  153. else
  154. {
  155. return [
  156. 'error' => 1,
  157. 'msg' => $paylog->returnFirstError(),
  158. 'data' => [],
  159. 'code'=>200
  160. ];
  161. }
  162. }
  163. //输出下载码
  164. $downCode = new DocDowncode();
  165. $downCode->doc_id = $doc->id;
  166. $downCode->openid = '';
  167. $downCode->downcode = strval(getDowncode($doc->id));
  168. $downCode->user_id = $this->user_id;
  169. $downCode->doc_user_id = $doc->user_id;
  170. $downCode->is_ad = $doc->is_ad;;
  171. $downCode->type = 1;
  172. $downCode->create_time = TIMESTAMP;
  173. $downCode->save();
  174. if(defined('IN_WAP')&&IN_WAP==TRUE)
  175. {
  176. $downurl = WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode.'&direct=1';
  177. }
  178. else
  179. {
  180. $downurl = WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode;
  181. }
  182. return [
  183. 'error' => 0,
  184. 'msg' => '请求下载中,请稍候...',
  185. 'data' => ['downcode'=>$downCode->downcode,'downurl'=>$downurl],
  186. 'code'=>200
  187. ];
  188. }
  189. //直接下载(文档主人或者VIP特权或者购买过)
  190. public function actionDirectdown()
  191. {
  192. $id = intval($this->post['id']);
  193. $doc = \app\modules\doc\models\DocReal::findOne($id);
  194. if($this->userInfo['is_delete']||$this->userInfo['is_lock'])
  195. {
  196. return [
  197. 'error' => 1,
  198. 'msg' => '账户已被限制,禁止下载',
  199. 'data' => [],
  200. 'code'=>200
  201. ];
  202. }
  203. if(empty($doc)||$doc->is_delete==1)
  204. {
  205. return [
  206. 'error' => 1,
  207. 'msg' => '该'.$this->docname.'不存在',
  208. 'data' => [],
  209. 'code'=>200
  210. ];
  211. }
  212. $downFlag = false;
  213. //不是文档主人
  214. if($this->user_id!=$doc->user_id)
  215. {
  216. //判断是否购买记录
  217. $existPayLog = DocPaylog::find()->where("user_id=".$this->user_id." and doc_id=$id")->exists();
  218. if(!$existPayLog)
  219. {
  220. $groupRights = $this->userInfo['group_rights'];
  221. $vipSettings = $this->userInfo['vip_settings']?$this->userInfo['vip_settings']:[];
  222. //所在用户组被禁止下载
  223. if($groupRights['doc_downnum_day']==-1||$groupRights['doc_downnum_total']==-1)
  224. {
  225. return [
  226. 'error' => 1,
  227. 'msg' => '您已被禁止下载',
  228. 'data' => 1,
  229. 'code'=>200
  230. ];
  231. }
  232. //日期
  233. $timeList = getTimeList(1);
  234. $todayStart = $timeList['dayStart'];
  235. $todayEnd = $timeList['dayEnd'];
  236. //总下载数量
  237. $downNumTotal = DocPaylog::find()->where("user_id=".$this->user_id."")->count();
  238. //今日下载数量
  239. $downNumToday = DocPaylog::find()->where("user_id=".$this->user_id." and (create_time>=".$todayStart." and create_time<=".$todayEnd.")")->count();
  240. //VIP免费下载的总量
  241. $vipfreeDownNumTotal = DocPaylog::find()->where("user_id=".$this->user_id." and vip_free=1")->count();
  242. //下载总量限制
  243. if($groupRights['doc_downnum_total']>0&&$downNumTotal>=$groupRights['doc_downnum_total'])
  244. {
  245. return [
  246. 'error' => 1,
  247. 'msg' => '您已达到'.$this->docname.'下载总量上限('.$groupRights['doc_downnum_total'].'份),无法继续下载',
  248. 'data' => 1,
  249. 'code'=>200
  250. ];
  251. }
  252. //今日下载数量限制
  253. if($groupRights['doc_downnum_day']>0&&$downNumToday>=$groupRights['doc_downnum_day'])
  254. {
  255. return [
  256. 'error' => 1,
  257. 'msg' => '您已达到今日'.$this->docname.'下载总量上限('.$groupRights['doc_downnum_day'].'份),无法继续下载',
  258. 'data' => 1,
  259. 'code'=>200
  260. ];
  261. }
  262. //VIP免费下载
  263. if(($doc->vip_free&&!empty($vipSettings))||(!empty($vipSettings)&&$vipSettings['buy_doc_discount']==0))
  264. {
  265. //VIP免费文档数量限制
  266. if(intval($vipSettings['vipfreedoc_download_num'])>0&&$vipfreeDownNumTotal>=$vipSettings['vipfreedoc_download_num'])
  267. {
  268. return [
  269. 'error' => 1,
  270. 'msg' => '您已达到VIP免费'.$this->docname.'下载总量上限('.$vipSettings['vipfreedoc_download_num'].'份),无法继续下载',
  271. 'data' => 1,
  272. 'code'=>200
  273. ];
  274. }
  275. //生成支付记录
  276. $paylog = new DocPaylog();
  277. $paylog->user_id = $this->user_id;
  278. $paylog->doc_user_id = $doc->user_id;
  279. $paylog->doc_id = $doc->id;
  280. $paylog->cat_id = $doc->cat_id;
  281. $paylog->cat_ids = $doc->cat_ids;
  282. $paylog->doc_type = $doc->doc_type;
  283. $paylog->vip_free = 1;
  284. $paylog->coin_price = $doc->coin_price;
  285. $paylog->coin_num = 0;
  286. $paylog->agent_id = intval($this->userInfo['referer_id']);
  287. $paylog->agent_ids = strval($this->userInfo['referer_ids']);
  288. $paylog->create_time = TIMESTAMP;
  289. if($paylog->save())
  290. {
  291. //更新文档售出次数
  292. $doc->updateCounters(['sales'=>1]);
  293. }
  294. else
  295. {
  296. return [
  297. 'error' => 1,
  298. 'msg' => $paylog->returnFirstError(),
  299. 'data' => [],
  300. 'code'=>200
  301. ];
  302. }
  303. $downFlag = true;
  304. }
  305. else
  306. {
  307. return [
  308. 'error' => 1,
  309. 'msg' => '下载失败',
  310. 'data' => [],
  311. 'code'=>200
  312. ];
  313. }
  314. }
  315. else
  316. {
  317. $downFlag = true;
  318. }
  319. }
  320. else
  321. {
  322. $downFlag = true;
  323. }
  324. if($downFlag)
  325. {
  326. //输出下载码
  327. $downCode = new DocDowncode();
  328. $downCode->doc_id = $doc->id;
  329. $downCode->openid = '';
  330. $downCode->downcode = strval(getDowncode($doc->id));
  331. $downCode->user_id = $this->user_id;
  332. $downCode->doc_user_id = $doc->user_id;
  333. $downCode->is_ad = $doc->is_ad;;
  334. $downCode->type = 1;
  335. $downCode->create_time = TIMESTAMP;
  336. $downCode->save();
  337. if(defined('IN_WAP')&&IN_WAP==TRUE)
  338. {
  339. $downurl =WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode."&direct=1";
  340. }
  341. else
  342. {
  343. $downurl = WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode;
  344. }
  345. return [
  346. 'error' => 0,
  347. 'msg' => '请求下载中,请稍候...',
  348. 'data' => ['downcode'=>$downCode->downcode,'downurl'=>$downurl],
  349. 'code'=>200
  350. ];
  351. }
  352. else
  353. {
  354. return [
  355. 'error' => 1,
  356. 'msg' => '下载失败',
  357. 'data' => [],
  358. 'code'=>200
  359. ];
  360. }
  361. }
  362. //金币下载
  363. public function actionCoindown()
  364. {
  365. $id = intval($this->post['id']);
  366. $doc = DocReal::findOne($id);
  367. $coin_num = $doc->coin_price;
  368. $downFlag = false;
  369. if($this->userInfo['is_delete']||$this->userInfo['is_lock'])
  370. {
  371. return [
  372. 'error' => 1,
  373. 'msg' => '账户已被限制,禁止下载',
  374. 'data' => [],
  375. 'code'=>200
  376. ];
  377. }
  378. if(empty($doc)||$doc->is_delete==1)
  379. {
  380. return [
  381. 'error' => 1,
  382. 'msg' => '该'.$this->docname.'不存在',
  383. 'data' => [],
  384. 'code'=>200
  385. ];
  386. }
  387. //判断是否购买记录
  388. $existPayLog = DocPaylog::find()->where("user_id=".$this->user_id." and doc_id=$id")->exists();
  389. if(!$existPayLog)
  390. {
  391. $groupRights = $this->userInfo['group_rights'];
  392. $vipSettings = $this->userInfo['vip_settings']?$this->userInfo['vip_settings']:[];
  393. //所在用户组被禁止下载
  394. if($groupRights['doc_downnum_day']==-1||$groupRights['doc_downnum_total']==-1)
  395. {
  396. return [
  397. 'error' => 1,
  398. 'msg' => '您已被禁止下载',
  399. 'data' => [],
  400. 'code'=>200
  401. ];
  402. }
  403. //下载总量限制(付费不限制)
  404. /* $downNumTotal = DocPaylog::find()->where("user_id=".$this->user_id."")->count();
  405. if($groupRights['doc_downnum_total']>0&&$downNumTotal>=$groupRights['doc_downnum_total'])
  406. {
  407. return [
  408. 'error' => 1,
  409. 'msg' => '您已达到'.$this->docname.'下载总量上限('.$groupRights['doc_downnum_total'].'份),无法继续下载',
  410. 'data' => [],
  411. 'code'=>200
  412. ];
  413. }*/
  414. //VIP专享下载限制
  415. if($doc->is_vip)
  416. {
  417. if(empty($vipSettings))
  418. {
  419. return [
  420. 'error' => 2,
  421. 'msg' => '您还不是VIP,无法继续下载',
  422. 'data' => [],
  423. 'code'=>200
  424. ];
  425. }
  426. else
  427. {
  428. //VIP专享下载的总量(付费文档暂不限制)
  429. /*$vipDownNumTotal = DocPaylog::find()->where("user_id=".$this->user_id." and is_vip=1")->count();
  430. if(intval($vipSettings['vipdoc_download_num'])>0&&$vipDownNumTotal>=$vipSettings['vipdoc_download_num'])
  431. {
  432. return [
  433. 'error' => 1,
  434. 'msg' => '您已达到VIP专享'.$this->docname.'下载总量上限('.$vipSettings['vipdoc_download_num'].'份),无法继续下载',
  435. 'data' => [],
  436. 'code'=>200
  437. ];
  438. }*/
  439. }
  440. }
  441. //VIP折扣计价
  442. if($vipSettings['buy_doc_discount']<100)
  443. {
  444. $discount = (intval($vipSettings['buy_doc_discount'])/100);
  445. $coin_num = $coin_num*$discount;
  446. }
  447. //生成支付记录
  448. $paylog = new DocPaylog();
  449. $paylog->user_id = intval($this->user_id);
  450. $paylog->doc_user_id = intval($doc->user_id);
  451. $paylog->doc_id = $doc->id;
  452. $paylog->cat_id = $doc->cat_id;
  453. $paylog->cat_ids = $doc->cat_ids;
  454. $paylog->doc_type = $doc->doc_type;
  455. $paylog->is_vip = ($doc->is_vip||($vipSettings['buy_doc_discount']<100))?1:0;
  456. $paylog->coin_price = $doc->coin_price;
  457. $paylog->coin_num = $coin_num;
  458. $paylog->agent_id = intval($this->userInfo['referer_id']);
  459. $paylog->agent_ids = strval($this->userInfo['referer_ids']);
  460. $paylog->create_time = TIMESTAMP;
  461. if($paylog->save())
  462. {
  463. $doc->updateCounters(['sales'=>1]);
  464. $left_coin = $coin_num;
  465. $point_num = intval($left_coin*Yii::$app->params['point']['coin_prize_rate']);
  466. //购买方
  467. $logTitleBuy = '购买'.DocReal::typeOptions($doc->doc_type);
  468. Wallet::pointChange($this->user_id,$point_num,$logTitleBuy,1,'point',DocReal::shortTableName(),$doc->id);
  469. Wallet::coinChange($this->user_id,$left_coin,$logTitleBuy,2,'coin',DocReal::shortTableName(),$doc->id);
  470. //处理分销
  471. $left_coin = Wallet::docCommission($this->user_id,$doc,$paylog,$left_coin);
  472. //出售方
  473. $logTitleSell = '出售'.DocReal::typeOptions($doc->doc_type);
  474. if($left_coin>0&&$doc->user_id)
  475. {
  476. //金币变化
  477. Wallet::coinChange($doc->user_id,$left_coin,$logTitleSell,1,'coin_income',DocReal::shortTableName(),$doc->id);
  478. //消息通知
  479. MessageOne::soldDoc($logTitleSell,$doc,$left_coin);
  480. }
  481. $downFlag = true;
  482. }
  483. }
  484. else
  485. {
  486. $downFlag = true;
  487. }
  488. if($downFlag)
  489. {
  490. //输出下载码
  491. $downCode = new DocDowncode();
  492. $downCode->doc_id = $doc->id;
  493. $downCode->openid = '';
  494. $downCode->downcode = strval(getDowncode($doc->id));
  495. $downCode->user_id = $this->user_id;
  496. $downCode->doc_user_id = $doc->user_id;
  497. $downCode->is_ad = $doc->is_ad;
  498. $downCode->type = 1;
  499. $downCode->create_time = TIMESTAMP;
  500. $downCode->save();
  501. if(defined('IN_WAP')&&IN_WAP==TRUE)
  502. {
  503. $downurl = WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode."&direct=1";
  504. }
  505. else
  506. {
  507. $downurl = WEB_URL.'ajax/downdoc/?downcode='.$downCode->downcode;
  508. }
  509. return [
  510. 'error' => 0,
  511. 'msg' => '请求下载中,请稍候...',
  512. 'data' => ['downcode'=>$downCode->downcode,'downurl'=>$downurl],
  513. 'code'=>200
  514. ];
  515. }
  516. else
  517. {
  518. return [
  519. 'error' => 1,
  520. 'msg' => '下载失败',
  521. 'data' => [],
  522. 'code'=>200
  523. ];
  524. }
  525. }
  526. //添加邮件转存任务
  527. public function actionSavetomail()
  528. {
  529. extract($this->post);
  530. if(empty($table_name)||empty($data_id))
  531. {
  532. return [
  533. 'error' => 1,
  534. 'msg' => '缺少必要参数',
  535. 'data' => [],
  536. 'code'=>200
  537. ];
  538. }
  539. else
  540. {
  541. //判断是否有购买记录
  542. $existPayLog = DocPaylog::find()->where("user_id=".$this->user_id." and doc_id=$data_id")->exists();
  543. if($existPayLog)
  544. {
  545. //判断是否已存在任务
  546. $mailModel = DocMail::find()->where("doc_id=$data_id and email='".$this->userInfo['email']."' and status=0")->one();
  547. if(empty($mailModel))
  548. {
  549. $mailModel = new DocMail();
  550. $mailModel->doc_id = $data_id;
  551. $mailModel->email = $this->userInfo['email'];
  552. if($mailModel->save())
  553. {
  554. return [
  555. 'error' => 0,
  556. 'msg' => '转存成功,请注意查收邮件!',
  557. 'data' => [],
  558. 'code'=>200
  559. ];
  560. }
  561. else
  562. {
  563. return [
  564. 'error' => 1,
  565. 'msg' => '操作失败',
  566. 'data' => [],
  567. 'code'=>200
  568. ];
  569. }
  570. }
  571. else
  572. {
  573. return [
  574. 'error' => 0,
  575. 'msg' => '转存成功,请注意查收邮件!',
  576. 'data' => [],
  577. 'code'=>200
  578. ];
  579. }
  580. }
  581. else
  582. {
  583. return [
  584. 'error' => 1,
  585. 'msg' => '请购买后再转存邮箱',
  586. 'data' => [],
  587. 'code'=>200
  588. ];
  589. }
  590. }
  591. }
  592. }