Consumer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\utility\Rsa;
  5. use app\index\controller\Fee;
  6. use app\main\model\object\UserObject;
  7. use app\main\service\UserService;
  8. use think\Cookie;
  9. /**
  10. * 消费书币
  11. */
  12. class Consumer extends Api{
  13. public function index(){
  14. $user_id = $this->request->param('user_id');
  15. $book_id = $this->request->param('book_id');
  16. $chapter_id = $this->request->param('chapter_id');
  17. $time = $this->request->param('time');
  18. $sign = $this->request->param('sign');
  19. $str = $user_id.$book_id.$chapter_id.$time;
  20. if(!$this->checkSign($str,$sign)){
  21. $this->error("签名校验失败");
  22. }
  23. if(!$user_id || !$book_id){
  24. $this->error("参数错误");
  25. }
  26. //获取用户信息
  27. Cookie::set('user_id',$user_id);
  28. if(!$user_info = UserService::instance()->getUserModel()->getUserInfo($user_id)){
  29. $this->error("获取用户信息失败");
  30. }
  31. //绑定用户信息到UserService
  32. UserService::instance()->getRunTimeObject()->user = (new UserObject())->bind($user_info);
  33. //检查书籍是否存在
  34. if(!$book_info = model('Book')->BookInfo($book_id)){
  35. $this->error("书籍获取失败");
  36. }
  37. //检查书籍是否下架
  38. if($book_info['state'] == 0){
  39. $this->error("此书籍已下架");
  40. }
  41. //章节为空时,取用户阅读记录
  42. if (!$chapter_id) {
  43. if($user_read_log = model('UserRecentlyRead')->getone($user_id, $book_id)){
  44. $chapter_id = $user_read_log['chapter_id'];
  45. }
  46. }
  47. //章节为空时,取书籍第一张
  48. if(empty($chapter_id) && isset($book_info['first_chapter_id'])){
  49. $chapter_id = $book_info['first_chapter_id'];
  50. }
  51. //章节为空时返回错误信息
  52. if(empty($chapter_id)){
  53. $this->error("获取章节ID失败");
  54. }
  55. //检查章节信息
  56. $chapter = model('Book')->getChapterInfo($book_id, $chapter_id);
  57. if( $chapter['code'] || !isset($chapter['data']['idx']) || !isset($chapter['data']['name']) ){
  58. $this->error("章节信息获取失败");
  59. }
  60. //检查下一章的章节ID
  61. if(!isset($chapter['data']['next_id'])){
  62. $this->error("获取下一章索引失败");
  63. }
  64. $fee = new Fee();
  65. $billing_result = $fee->bookfee($user_info['channel_id'], $book_info, $chapter['data']['name'], $chapter['data']['idx'], $chapter_id, $user_info);
  66. //当前章节是否是付费章节
  67. if (array_key_exists('isRecharge', $billing_result) && $billing_result['isRecharge'] == 1) {
  68. $is_pay_chapter = true;
  69. }else{
  70. $is_pay_chapter = false;
  71. }
  72. //付费是否成功
  73. if($billing_result['type'] == 2 || $billing_result['type'] == 7){
  74. //书币扣除失败
  75. $this->error($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
  76. }elseif($billing_result['type'] == 1){
  77. //书币扣除成功
  78. $this->success($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
  79. }else{
  80. //其他错误
  81. $this->error($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
  82. }
  83. }
  84. public function checkSign($str,$sign){
  85. if(md5($str) == $sign){
  86. return true;
  87. }else{
  88. return false;
  89. }
  90. }
  91. }