123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\utility\Rsa;
- use app\index\controller\Fee;
- use app\main\model\object\UserObject;
- use app\main\service\UserService;
- use think\Cookie;
- /**
- * 消费书币
- */
- class Consumer extends Api{
- public function index(){
- $user_id = $this->request->param('user_id');
- $book_id = $this->request->param('book_id');
- $chapter_id = $this->request->param('chapter_id');
- $time = $this->request->param('time');
- $sign = $this->request->param('sign');
- $str = $user_id.$book_id.$chapter_id.$time;
- if(!$this->checkSign($str,$sign)){
- $this->error("签名校验失败");
- }
- if(!$user_id || !$book_id){
- $this->error("参数错误");
- }
- //获取用户信息
- Cookie::set('user_id',$user_id);
- if(!$user_info = UserService::instance()->getUserModel()->getUserInfo($user_id)){
- $this->error("获取用户信息失败");
- }
- //绑定用户信息到UserService
- UserService::instance()->getRunTimeObject()->user = (new UserObject())->bind($user_info);
- //检查书籍是否存在
- if(!$book_info = model('Book')->BookInfo($book_id)){
- $this->error("书籍获取失败");
- }
- //检查书籍是否下架
- if($book_info['state'] == 0){
- $this->error("此书籍已下架");
- }
- //章节为空时,取用户阅读记录
- if (!$chapter_id) {
- if($user_read_log = model('UserRecentlyRead')->getone($user_id, $book_id)){
- $chapter_id = $user_read_log['chapter_id'];
- }
- }
- //章节为空时,取书籍第一张
- if(empty($chapter_id) && isset($book_info['first_chapter_id'])){
- $chapter_id = $book_info['first_chapter_id'];
- }
- //章节为空时返回错误信息
- if(empty($chapter_id)){
- $this->error("获取章节ID失败");
- }
- //检查章节信息
- $chapter = model('Book')->getChapterInfo($book_id, $chapter_id);
- if( $chapter['code'] || !isset($chapter['data']['idx']) || !isset($chapter['data']['name']) ){
- $this->error("章节信息获取失败");
- }
- //检查下一章的章节ID
- if(!isset($chapter['data']['next_id'])){
- $this->error("获取下一章索引失败");
- }
- $fee = new Fee();
- $billing_result = $fee->bookfee($user_info['channel_id'], $book_info, $chapter['data']['name'], $chapter['data']['idx'], $chapter_id, $user_info);
- //当前章节是否是付费章节
- if (array_key_exists('isRecharge', $billing_result) && $billing_result['isRecharge'] == 1) {
- $is_pay_chapter = true;
- }else{
- $is_pay_chapter = false;
- }
- //付费是否成功
- if($billing_result['type'] == 2 || $billing_result['type'] == 7){
- //书币扣除失败
- $this->error($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
- }elseif($billing_result['type'] == 1){
- //书币扣除成功
- $this->success($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
- }else{
- //其他错误
- $this->error($billing_result['msg'],['next_id'=>$chapter['data']['next_id'],'is_pay'=>$is_pay_chapter]);
- }
- }
- public function checkSign($str,$sign){
- if(md5($str) == $sign){
- return true;
- }else{
- return false;
- }
- }
- }
|