| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\wap\controller;
- use app\admin\model\study\Plan;
- use app\admin\model\study\PlanBuy;
- use app\admin\model\study\PlanSteps;
- use app\admin\model\study\PlanItems;
- use app\wap\model\special\Special as SpecialModel;
- use app\wap\model\special\SpecialWatch as SpecialWatch;
- use app\wap\model\special\SpecialTask;
- use app\admin\model\questions\Relation;
- use app\wap\model\topic\ExaminationRecord;
- use app\wap\model\topic\CertificateRelated;
- use app\wap\model\topic\CertificateRecord;
- use app\wap\model\special\SpecialSource;
- use app\wap\model\store\StoreOrder;
- use service\JsonService;
- use think\Url;
- /**商品控制器
- * Class Store
- * @package app\wap\controller
- */
- class Studyplan extends AuthController{
- /*
- * 白名单
- * */
- public static function WhiteList(){
- return [
- 'index',
- 'detail',
- ];
- }
- public function index($page = 1, $limit = 20, $op = ''){
- if ($op == 'list') {
- $model = Plan::alias('p')->where('p.is_del', 0)->page((int)$page, (int)$limit);
- $model = $model->join('PlanBuy pb', 'pb.uid = ' . $this->uid .' and pb.planid = p.id ', 'left');
- $orderList = $model->order('p.shelf_time DESC')->field('p.*, pb.id as isbuy')->select();
- $orderList = count($orderList) > 0 ? $orderList->toArray() : [];
- return JsonService::successful($orderList);
- }
- //微信支付
- $user_level = !$this->uid ? 0 : $this->userInfo;
- $this->assign('is_member', isset($user_level['level']) ? $user_level['level'] : 0);
- return $this->fetch();
- }
- public function details($id = 0) {
- if (!$id) $this->failed('缺少参数,无法访问', Url::build('index/index'));
- $planinfo = Plan::get($id);
- if (!$planinfo) $this->failed('参数错误,无法访问', Url::build('index/index'));
- $planinfo['introduction'] = htmlspecialchars_decode($planinfo['introduction']);
- $steps = PlanSteps::where('is_del', 0)->where('pid', $id)->order('sort asc')->select()->toArray();
- $stepids = [];
- $isPay = 0;
- $buyinfo = PlanBuy::where('planid', $id)->where('uid', $this->uid)->where('is_del', 0)->find();
- if ($buyinfo) {
- $isPay = 1;
- }
- $stepsList = [];
- $itemsList = [];
- foreach($steps as $v){
- $stepsList[$v['id']] = $v;
- $itemsList[$v['id']] = [];
- $stepids[] = $v['id'];
- }
-
-
- $join = [
- ['special s', 's.id = it.cid and s.is_del = 0 and s.status = 1']
- ];
- $items = PlanItems::alias('it')->field('s.*, it.stepsid,it.pid')
- ->join($join)
- ->where('pid', $id)
- ->where('stepsid', 'in', $stepids)
- ->order('sort asc')
- ->select();
- //$specialids = [];
- foreach ($items as $v) {
- $v['count'] = SpecialModel::numberChapters($v['type'], $v['id']);
- $itemsList[$v['stepsid']][] = $v->toArray();
- //$specialids[] = $v['id'];
- }
-
- //标记计划步骤是否完成学习
- $isopen = 1;
- foreach ($itemsList as $key => &$v) {
- $isend = 1;
- foreach($v as $k1 => &$v1) {
- //
- $join = [
- ['SpecialWatch sw', 'sw.special_id = ss.special_id and sw.task_id = ss.source_id and sw.uid = ' . $this->uid, 'left'],
- ];
- $order = 'sort desc,id desc';
- if ($v1['sort_order']) {
- $order = 'sort asc, id asc';
- }
- $specialWatchList = SpecialSource::alias('ss')->field('ss.*, sw.viewing_time,sw.percentage')
- ->join($join)
- ->where('ss.special_id', '=', $v1['id'])
- ->order($order)
- ->select();
- foreach($specialWatchList as $v2){
- $v1['tasklist'][$v2['special_id']][] = $v2->toArray();
- if ($v2['percentage'] < 95 && $isend == 1) $isend = 0;
- }
- //echo $isend . '---' . $v1['id'];
- if ($isend == 1) {
- //判断是否有考试
- $join = [
- ['ExaminationRecord er', 'er.test_id = r.relation_id and er.uid = ' . $this->uid, 'left'],
- ];
- $examlist = Relation::alias('r')->where('r.relationship_id', $v1['id'])->where('r.relationship', 2)
- ->field('r.relation_id as test_id, MAX(er.score) as score')->join($join)->group('r.relation_id')->select();
- if ($examlist) {
- foreach ($examlist as $item) {
- if ($item['score'] < 60) {
- $isend = 0;
- }
- }
- }
- }
- }
- $stepsList[$key]['isopen'] = $isopen;
- if ($isend == 0) {
- $isopen = 0;
- }
- //$stepsList[$key]['isend'] = $isend;
- }
- $stepsList = array_values($stepsList);
- //微信支付相关代码
- $user_level = !$this->uid ? 0 : $this->userInfo;
- $this->assign('is_member', isset($user_level['level']) ? $user_level['level'] : 0);
- $this->assign('isPay', $isPay);
- $this->assign('steps', json_encode($stepsList));
- $this->assign('itemsList', json_encode($itemsList));
- $this->assign('planinfo', json_encode($planinfo));
-
- return $this->fetch();
- }
- /**
- * 创建专题支付订单
- * @param int $special_id 专题id
- * @param int $pay_type 购买类型 1=礼物,2=普通购买,3=开团或者拼团
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function create_order($special_id, $pay_type_num, $payType, $pinkId = 0, $total_num = 1, $link_pay_uid = 0, $from = 'weixin')
- {
- if (!$special_id) return JsonService::fail('缺少购买参数');
- if ($pay_type_num == -1) return JsonService::fail('选择购买方式');
-
- $Plan = Plan::where('is_del', 0)->where('is_show', 1)->find($special_id);
- if (!$Plan) return JsonService::status('ORDER_ERROR', '购买的学习计划不存在');
- $order = StoreOrder::createPlanOrder($Plan, $pinkId, $pay_type_num, $this->uid, $payType, $link_pay_uid, $total_num);
- $orderId = $order['order_id'];
- $info = compact('orderId');
- if ($orderId) {
- $orderInfo = StoreOrder::where('order_id', $orderId)->find();
- if (!$orderInfo || !isset($orderInfo['paid'])) return JsonService::status('pay_error', '支付订单不存在!');
- if ($orderInfo['paid']) return JsonService::status('pay_error', '支付已支付!');
- if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
- if (StoreOrder::jsPayPlanPrice($orderId, $this->uid))
- return JsonService::status('success', '支付成功', $info);
- else
- return JsonService::status('pay_error', StoreOrder::getErrorInfo());
- } else {
- switch ($payType) {
- case 'weixin':
- try {
- if ($from == 'weixinh5') {
- $jsConfig = StoreOrder::h5PlanPay($orderId);
- } else {
- $jsConfig = StoreOrder::jsPlanPay($orderId);
- }
- } catch (\Exception $e) {
- return JsonService::status('pay_error', $e->getMessage(), $info);
- }
- $info['jsConfig'] = $jsConfig;
- if ($from == 'weixinh5') {
- return JsonService::status('wechat_h5_pay', '订单创建成功', $info);
- } else {
- return JsonService::status('wechat_pay', '订单创建成功', $info);
- }
- break;
- case 'yue':
- if (StoreOrder::yuePlanPay($orderId, $this->uid))
- return JsonService::status('success', '余额支付成功', $info);
- else
- return JsonService::status('pay_error', StoreOrder::getErrorInfo());
- break;
- case 'zhifubao':
- $info['pay_price'] = $orderInfo['pay_price'];
- $info['orderName'] = '购买学习计划';
- return JsonService::status('zhifubao_pay', '订单创建成功', base64_encode(json_encode($info)));
- break;
- }
- }
- } else {
- return JsonService::fail(StoreOrder::getErrorInfo('订单生成失败!'));
- }
- }
- public function gotoStudy($id){
- if (!$id) $this->failed('缺少参数,无法访问', Url::build('index/index'));
- $planinfo = Plan::get($id);
- if (!$planinfo) $this->failed('参数错误,无法访问', Url::build('index/index'));
- $buyinfo = PlanBuy::where('planid', $id)->where('uid', $this->uid)->where('is_del', 0)->find();
- if (!$buyinfo) {
- $this->failed('课程尚未购买,非法访问', Url::build('index/index'));
- }
- $planinfo['introduction'] = htmlspecialchars_decode($planinfo['introduction']);
- $steps = PlanSteps::where('is_del', 0)->where('pid', $id)->order('sort asc')->select();
- foreach($steps as $v){
- $stepids[] = $v['id'];
- }
- $join = [
- ['special s', 's.id = it.cid and s.is_del = 0 and s.status = 1'],
- ];
- $items = PlanItems::alias('it')->field('s.*, it.stepsid,it.pid, it.cid')
- ->join($join)
- ->where('pid', $id)
- ->where('stepsid', 'in', $stepids)
- ->order('sort asc')
- ->select();
- //$spids = [];
- $itemsList = [];
- $tasklist = [];
- foreach ($items as $v) {
- $v['count'] = SpecialModel::numberChapters($v['type'], $v['id']);
- $itemsList[$v['stepsid']][] = $v->toArray();
- //$spids[] = $v['cid'];
- $join = [
- ['SpecialWatch sw', 'sw.special_id = ss.special_id and sw.task_id = ss.source_id and sw.uid = ' . $this->uid, 'left'],
- ];
- $order = 'sort desc,id desc';
- if ($v['sort_order']) {
- $order = 'sort asc, id asc';
- }
- $specialWatchList = SpecialSource::alias('ss')->field('ss.*, sw.viewing_time,sw.percentage')
- ->join($join)
- ->where('ss.special_id', '=', $v['cid'])
- ->order($order)
- ->select();
- foreach($specialWatchList as $v){
- $tasklist[$v['special_id']][] = $v->toArray();
- }
- }
-
-
- $nowspid = 0;
- $nowtaskid = 0;
- $viewing_time= 0;
- foreach ($itemsList as $v) {
- foreach ($v as $v1) {
- //判断课程是否学习完成
- if ($tasklist[$v1['id']] && is_array($tasklist[$v1['id']])) {
- foreach ($tasklist[$v1['id']] as $v2) {
- if ($v2['percentage'] < 100) {
- $nowspid = $v1['id'];
- $nowtaskid = $v2['source_id'];
- $viewing_time = $v2['viewing_time'];
- break;
- }
- }
- }
- if ($nowspid) break;
- //判断是否有考试
- $join = [
- ['ExaminationRecord er', 'er.test_id = r.relation_id and er.uid = ' . $this->uid, 'left'],
- ];
- $examlist = Relation::alias('r')->where('r.relationship_id', $v1['id'])->where('r.relationship', 2)
- ->field('r.relation_id as test_id, er.score')->join($join)->select();
- if ($examlist) {
- foreach ($examlist as $item) {
- if ($item['score'] < 60) {
- $this->redirect('wap/special/question_index', ['id' => $item['test_id'], 'planid' => $id, ]);
- }
- }
- }
-
- }
- if ($nowspid) break;
- }
- if (!$nowspid) {
- $nowspid = $items[0]['id'];
- $nowtaskid = $tasklist[$nowspid][0]['source_id'];
- }
- //id=206&specialId=37&viewing_time=0
- $this->redirect('wap/special/task_info', ['id' => $nowtaskid, 'specialId' => $nowspid, 'viewing_time' => $viewing_time, 'planid' => $id, ]);
- }
- /**
- * 购买失败删除订单
- * @param string $orderId 订单id
- * @return json
- * */
- public function del_order($orderId = '') {
- if (StoreOrder::where('order_id', $orderId)->update(['is_del' => 1]))
- return JsonService::successful();
- else
- return JsonService::fail();
- }
- /**
- * 学习计划检测是否达到领取证书标准
- * $plan_id 学习计划ID
- */
- public function inspect($plan_id = 0)
- {
- if (!$this->uid) return JsonService::fail('err');
- if (!$plan_id) $this->failed('缺少参数,无法访问', Url::build('index/index'));
- $planinfo = Plan::get($plan_id);
- if (!$planinfo) $this->failed('参数错误,无法访问', Url::build('index/index'));
- $buyinfo = PlanBuy::where('planid', $plan_id)->where('uid', $this->uid)->where('is_del', 0)->find();
- if (!$buyinfo) {
- $this->failed('课程尚未购买,非法访问', Url::build('index/index'));
- }
- //查询是否关联证书
- $certinfo = CertificateRelated::where(['related' => $plan_id, 'obtain' => 3, 'is_show' => 1])->find();
- if (!$certinfo) $this->failed('该学习计划未绑定证书');
- //查询是否以获得证书
- $certinfo = CertificateRecord::where(['source_id' => $plan_id, 'obtain' => 3, 'status' => 1, 'uid' => $this->uid])->find();
- if ($certinfo) $this->failed('学习计划证书已获取完成');
- $steps = PlanSteps::where('is_del', 0)->where('pid', $plan_id)->order('sort asc')->select();
- foreach($steps as $v){
- $stepids[] = $v['id'];
- }
- $join = [
- ['special s', 's.id = it.cid and s.is_del = 0 and s.status = 1'],
- ];
- $items = PlanItems::alias('it')->field('s.*, it.stepsid,it.pid, it.cid')
- ->join($join)
- ->where('pid', $plan_id)
- ->where('stepsid', 'in', $stepids)
- ->order('sort asc')
- ->select();
- //$spids = [];
- $flag = 1;
- $itemsList = [];
- $tasklist = [];
- foreach ($items as $v) {
- $v['count'] = SpecialModel::numberChapters($v['type'], $v['id']);
- $itemsList[$v['stepsid']][] = $v->toArray();
- //$spids[] = $v['cid'];
- $join = [
- ['SpecialWatch sw', 'sw.special_id = ss.special_id and sw.task_id = ss.source_id and sw.uid = ' . $this->uid, 'left'],
- ];
- $order = 'sort desc,id desc';
- if ($v['sort_order']) {
- $order = 'sort asc, id asc';
- }
- $specialWatchList = SpecialSource::alias('ss')->field('ss.*, sw.viewing_time,sw.percentage')
- ->join($join)
- ->where('ss.special_id', '=', $v['cid'])
- ->order($order)
- ->select();
- foreach($specialWatchList as $v){
- $tasklist[$v['special_id']][] = $v->toArray();
- }
- }
-
-
- $nowspid = 0;
- foreach ($itemsList as $v) {
- foreach ($v as $v1) {
- //判断课程是否学习完成
- if ($tasklist[$v1['id']] && is_array($tasklist[$v1['id']])) {
- foreach ($tasklist[$v1['id']] as $v2) {
- if ($v2['percentage'] < 100) {
- $nowspid = $v1['id'];
- $flag = 0;
- break;
- }
- }
- }
- if ($nowspid) break;
- //判断是否有考试
- $join = [
- ['ExaminationRecord er', 'er.test_id = r.relation_id and er.uid = ' . $this->uid, 'left'],
- ];
- $examlist = Relation::alias('r')->where('r.relationship_id', $v1['id'])->where('r.relationship', 2)
- ->field('r.relation_id as test_id, er.score')->join($join)->select();
- if ($examlist) {
- foreach ($examlist as $item) {
- if ($item['score'] < 60) {
- $flag = 0;
- }
- }
- }
-
- }
- if ($nowspid) break;
- }
- if ($flag) {
- return JsonService::successful('ok');
- } else {
- return JsonService::fail('err');
- }
- }
- /**用户领取证书
- * $special_id 专题ID
- */
- public function getTheCertificate($plan_id)
- {
- $res = CertificateRecord::getUserTheCertificate($plan_id, 3, $this->uid);
- if ($res) return JsonService::successful($res);
- else return JsonService::fail('领取失败');
- }
- }
|