MainController.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ellisran
  5. * Date: 2016/9/21
  6. * Time: 10:00
  7. */
  8. session_start();
  9. class MainController extends DooController {
  10. public $data;
  11. function __construct() {
  12. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  13. if(isset($_COOKIE['uid'])){
  14. $uid = $this->authcode($_COOKIE['uid']);
  15. $username = $this->authcode ( $_COOKIE['user_auth'] );
  16. $this->data['username'] = $username;
  17. $this->data['uid'] = $uid;
  18. //每次操作都更新cookie时间,12小时没操作重新登录
  19. setcookie ( "user_auth", $_COOKIE['user_auth'], time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  20. setcookie ( "uid", $_COOKIE['uid'], time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  21. }
  22. if(isset($_COOKIE['sso_id'])){
  23. require_once (SITE_PATH . '/protected/class/client.php');
  24. $client = new client ( ZHSSO );
  25. $ssoid = $this->authcode($_COOKIE['sso_id']);
  26. $avatar = $client->ps_getavatar ( $ssoid );
  27. $this->data['useravatar']=$avatar[180];
  28. setcookie ( "sso_id", $_COOKIE['sso_id'], time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  29. }else{
  30. $this->data['useravatar']=Doo::conf()->APP_URL . 'global/images/01.png';
  31. }
  32. if(isset($_SESSION['message'])){
  33. $this->data['message'] = $_SESSION['message'];
  34. }
  35. }
  36. public function index() {
  37. Doo::loadModel('ktclass');
  38. Doo::loadModel('classuser');
  39. $ktclass = new Ktclass();
  40. $classuser = new Classuser();
  41. $class = $ktclass->getRowishowbyId(2);
  42. $usernum = $classuser->getNumbyClassid(2);
  43. $this->data['totalnum'] = sprintf("%.2f", $class['classnum']*$class['classtime']/60);
  44. $this->data['usernum'] = $usernum;
  45. $this->data['class'] = $class;
  46. if(isset($this->data['uid'])){
  47. $isbuy = $classuser->gethasbyuserandclass($this->data['uid'],2);
  48. if(!empty($isbuy)){
  49. $this->data['isbuy'] = true;
  50. }
  51. }
  52. $this->render('index', $this->data, TRUE);
  53. }
  54. public function user_exit(){
  55. setcookie('user_auth', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  56. setcookie('uid', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  57. setcookie('sso_id', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  58. return '/';
  59. }
  60. //检测是否已经填入身份证信息和姓名
  61. function checkidcard($id = ''){
  62. if(isset($this->data['uid'])){
  63. $uid = $this->data['uid'];
  64. }elseif($id != ''){
  65. $uid = $id;
  66. }else{
  67. setcookie('user_auth', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  68. setcookie('uid', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  69. setcookie('sso_id', '', time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  70. return 3;
  71. }
  72. Doo::loadModel('user');
  73. $users = new User();
  74. $user = $users->getuserbyId($uid);
  75. if(isset($user['idcard']) && !empty($user['idcard'])){
  76. return 1;
  77. }else{
  78. return 0;
  79. }
  80. }
  81. public function exitId(){
  82. if(isset($this->data['uid'])){
  83. if(isset($_GET['from']) && $_GET['from'] == 'userinfo'){
  84. $this->data['from'] = 'userinfo';
  85. }elseif(isset($_GET['class'])){
  86. $this->data['from'] = $_GET['class'];
  87. }
  88. $this->render('identity', $this->data, TRUE);
  89. }else{
  90. $_SESSION['message'] = 'cookie过期,请重新登录';
  91. return '/';
  92. }
  93. }
  94. public function do_setid(){
  95. if(!isset($this->data['uid'])){
  96. return '/';
  97. }
  98. if(!isset($_POST['name']) || empty($_POST['name'])){
  99. $this->data['message'] = '请输入您的真实姓名';
  100. $this->render('identity',$this->data,TRUE);die;
  101. }
  102. if(!isset($_POST['idcard']) || empty($_POST['idcard'])){
  103. $this->data['message'] = '请输入您的身份证号';
  104. $this->render('identity',$this->data,TRUE);die;
  105. }
  106. if(!$this->validateIDCard($_POST['idcard'])){
  107. $this->data['message'] = '身份证号格式不正确';
  108. $this->render('identity',$this->data,TRUE);die;
  109. }
  110. Doo::loadModel('user');
  111. $users = new User();
  112. $users->realname = trim($_POST['name']);
  113. $users->idcard = trim($_POST['idcard']);
  114. $users->userid = $this->data['uid'];
  115. $users->update();
  116. if(isset($_POST['from']) && $_POST['from'] == 'userinfo'){
  117. return '/userinfo';
  118. }elseif(isset($_POST['from']) && $_POST['from'] == 0){
  119. return '/';
  120. }else{
  121. return '/classinfo/'.$_POST['from'];
  122. }
  123. }
  124. public function login(){
  125. $username = $this->get_args ( "username" );
  126. $password = $this->get_args ( "password" );
  127. if (empty ( $username ) || empty ( $password )) {
  128. $_SESSION['message'] = "请输入正确的参数";
  129. $src_page = $_SERVER['HTTP_REFERER'];
  130. header("location:".$src_page);die;
  131. // return '/';
  132. }
  133. require_once (SITE_PATH . '/protected/class/client.php');
  134. $client = new client ( ZHSSO );
  135. if(filter_var($username, FILTER_VALIDATE_EMAIL)){
  136. $is_login = $client->zhsso_member_login ( $username, $password ,1);
  137. }else if($this->checkMobile($username)) {
  138. $is_login = $client->zhsso_member_login( $username, $password, 2 );
  139. }else{
  140. $is_login = $client->zhsso_member_login ( $username, $password );
  141. }
  142. $is_login = explode ( "\r", $is_login );
  143. if (is_numeric ( $is_login [0] )) {
  144. if ($is_login [0] == USERNAME_ONFINE) {
  145. $_SESSION['message'] = "您输入的帐号或者密码有误";
  146. $src_page = $_SERVER['HTTP_REFERER'];
  147. header("location:".$src_page);die;
  148. // return '/';
  149. } elseif ($is_login [0] == PASSWORD_ERROR){
  150. $_SESSION['message'] = "您输入的帐号或者密码有误";
  151. $src_page = $_SERVER['HTTP_REFERER'];
  152. header("location:".$src_page);die;
  153. // return '/';
  154. }
  155. elseif ($is_login [0] == - 3) {//USER_NOACTION
  156. $_SESSION['message'] = "用户未激活账号,请前往<a href='http://soo.smartcost.com.cn'>通行账号</a>激活";
  157. return '/';
  158. }
  159. else{
  160. $_SESSION['message'] = "您输入的帐号或者密码有误";
  161. $src_page = $_SERVER['HTTP_REFERER'];
  162. header("location:".$src_page);die;
  163. // return '/';
  164. }
  165. } elseif (! empty ( $is_login [0] )) {
  166. $ts = json_decode ( $is_login [0], true );
  167. if (empty ( $ts )){
  168. $_SESSION['message'] = "通信异常";
  169. $src_page = $_SERVER['HTTP_REFERER'];
  170. header("location:".$src_page);die;
  171. // return '/';
  172. }
  173. $_SESSION['message'] = '';
  174. Doo::loadModel('user');
  175. $users = new User();
  176. $user = $users->getRowByUsername ( $ts [0] ['username'] );
  177. $ssoid = $this->authcode( $ts[0]['id'], "tr");
  178. $userinfo = $this->authcode ( $ts [0] ['username'], "tr" );
  179. setcookie ( "user_auth", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  180. setcookie ( "sso_id", $ssoid, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  181. //同步通行证用户
  182. if (empty ( $user )) {
  183. $users->username = $ts[0]['username'];
  184. $users->useremail = $ts[0]['useremail'];
  185. $users->mobile = $ts[0]['mobile'];
  186. $users->userpasswd = $ts[0]['userpasswd'];
  187. $result = $users->insert();
  188. $userid = $this->authcode ( $result, "tr" );
  189. setcookie ( "uid", $userid, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  190. $src_page = $_SERVER['HTTP_REFERER'];
  191. header("location:".$src_page);
  192. // return '/setId';
  193. }else{
  194. $users->useremail = $ts[0]['useremail'];
  195. $users->mobile = $ts[0]['mobile'];
  196. $users->userpasswd = $ts[0]['userpasswd'];
  197. $users->userid = $user['userid'];
  198. $users->update();
  199. $userid = $this->authcode ( $user['userid'], "tr" );
  200. setcookie ( "uid", $userid, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  201. $src_page = $_SERVER['HTTP_REFERER'];
  202. header("location:".$src_page);
  203. // $hadId = $this->checkidcard($user['userid']);
  204. //
  205. // if($hadId == 0){
  206. // return '/setId';
  207. // }elseif($hadId == 3){
  208. // return '/';
  209. // }
  210. }
  211. } else {
  212. $_SESSION['message'] = "通信异常";
  213. $src_page = $_SERVER['HTTP_REFERER'];
  214. header("location:".$src_page);die;
  215. // return '/';
  216. }
  217. $src_page = $_SERVER['HTTP_REFERER'];
  218. header("location:".$src_page);die;
  219. // return '/';
  220. }
  221. //课程列表
  222. public function classlist(){
  223. // $hadId = $this->checkidcard($this->data['uid']);
  224. // if($hadId == 0){
  225. // return '/setId';
  226. // }elseif($hadId == 3){
  227. // return '/';
  228. // }
  229. if(!isset($this->data['uid'])){
  230. return '/';
  231. }
  232. Doo::loadModel('ktclass');
  233. Doo::loadModel('classuser');
  234. $ktclass = new Ktclass();
  235. $classuser = new Classuser();
  236. $classlist = $classuser->getclassbyuserid($this->data['uid']);
  237. $this->data['classlist'] = $classlist;
  238. if(!empty($classlist)){
  239. foreach($classlist as $k => $v){
  240. $class = $ktclass->getOne(array('where' => 'classid='.$v['classid'], 'asArray' => TRUE));
  241. $this->data['classlist'][$k]['classname'] = $class['classname'];
  242. $this->data['classlist'][$k]['avatar'] = $class['avatar'];
  243. $this->data['classlist'][$k]['classnum'] = $class['classnum'];
  244. $this->data['classlist'][$k]['totalnum'] = sprintf("%.2f", $class['classnum']*$class['classtime']/60);
  245. $this->data['classlist'][$k]['usernum'] = $classuser->getNumbyClassid($v['classid']);
  246. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$v['classid']);
  247. $this->data['classlist'][$k]['user_progress'] = $this->getPersonProgress($class,$hadbuy);
  248. }
  249. }
  250. $this->render('user-class', $this->data, TRUE);
  251. }
  252. public function orderlist(){
  253. // $hadId = $this->checkidcard($this->data['uid']);
  254. // if($hadId == 0){
  255. // return '/setId';
  256. // }elseif($hadId == 3){
  257. // return '/';
  258. // }
  259. if(!isset($this->data['uid'])){
  260. return '/';
  261. }
  262. Doo::loadModel('order');
  263. Doo::loadHelper('DooPager');
  264. $order = new Order();
  265. $totalArchive = $order->count(array('where' => 'userid='.$this->data['uid'], 'asArray' => TRUE));
  266. $pager = new DooPager(Doo::conf()->APP_URL . "orderlist/page", $totalArchive, 5, 10);
  267. if (isset($this->params['pindex']))
  268. $pager->paginate(intval($this->params['pindex']));
  269. else
  270. $pager->paginate(1);
  271. if ($pager->limit != ' -5,5')
  272. $orderlist = $order->getorderlistbyuserid($this->data['uid'],$pager->limit);
  273. if(isset($orderlist) && !empty($orderlist)){
  274. $this->data['orderlist'] = $orderlist;
  275. foreach($orderlist as $k => $v){
  276. $this->data['orderlist'][$k]['createtime'] = date('Y-m-d H:i',$v['createtime']);
  277. }
  278. }
  279. $this->data['pager'] = $pager->output;
  280. $this->render('user-order', $this->data, TRUE);
  281. }
  282. //课程详细页
  283. public function classinfo(){
  284. // $hadId = $this->checkidcard($this->data['uid']);
  285. // if($hadId == 0){
  286. // return '/setId';
  287. // }elseif($hadId == 3){
  288. // return '/';
  289. // }
  290. if(isset($this->params['classid'])){
  291. Doo::loadModel('ktclass');
  292. Doo::loadModel('classuser');
  293. $ktclass = new Ktclass();
  294. $classuser = new Classuser();
  295. $class = $ktclass->getRowishowbyId($this->params['classid']);
  296. if(empty($class)){
  297. return '/';
  298. }
  299. $usernum = $classuser->getNumbyClassid($this->params['classid']);
  300. $this->data['totalnum'] = sprintf("%.2f", $class['classnum']*$class['classtime']/60);
  301. $this->data['classinfo'] = $class;
  302. $this->data['usernum'] = $usernum;
  303. Doo::loadModel('ktsection');
  304. Doo::loadModel('kttype');
  305. $ktype = new Kttype();
  306. $this->data['kttype'] = $ktype->getTypebyClassid($this->params['classid']);
  307. foreach($this->data['kttype'] as $k => $v){
  308. Doo::loadModel('ktsection');
  309. $ktsection = new Ktsection();
  310. $section = $ktsection->getSecbyTypeid($v['typeid']);
  311. $this->data['kttype'][$k]['section'] = $section;
  312. foreach($section as $sk => $sv){
  313. Doo::loadModel('ktvideo');
  314. $ktvideo = new Ktvideo();
  315. $video = $ktvideo->getVideobySecAndShow($sv['seid']);
  316. $this->data['kttype'][$k]['section'][$sk]['video'] = $video;
  317. }
  318. }
  319. if(isset($this->data['uid'])){
  320. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$this->params['classid']);
  321. if(!empty($hadbuy)){
  322. $this->data['isbuy'] = TRUE;
  323. }
  324. }
  325. $this->render('class-info', $this->data, TRUE);
  326. }
  327. }
  328. //跳转到支付宝支付页面
  329. public function paydetail(){
  330. $hadId = $this->checkidcard($this->data['uid']);
  331. if($hadId == 0){
  332. return '/setId?class='.$this->params['classid'];
  333. }elseif($hadId == 3){
  334. return '/';
  335. }
  336. if(isset($this->params['classid'])){
  337. Doo::loadModel('ktclass');
  338. Doo::loadModel('classuser');
  339. $ktclass = new Ktclass();
  340. $classuser = new Classuser();
  341. $class = $ktclass->getRowishowbyId($this->params['classid']);
  342. $usernum = $classuser->getNumbyClassid($this->params['classid']);
  343. $this->data['totalnum'] = sprintf("%.2f", $class['classnum']*$class['classtime']/60);
  344. $this->data['classinfo'] = $class;
  345. $this->data['usernum'] = $usernum;
  346. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$this->params['classid']);
  347. if(!empty($hadbuy)){
  348. $this->data['isbuy'] = TRUE;
  349. }
  350. $this->render('shop-detail', $this->data, TRUE);
  351. }
  352. }
  353. //个人信息页
  354. public function userInfo(){
  355. // $hadId = $this->checkidcard($this->data['uid']);
  356. // if($hadId == 0){
  357. // return '/setId';
  358. // }elseif($hadId == 3){
  359. // return '/';
  360. // }
  361. if(!isset($this->data['uid'])){
  362. return '/';
  363. }
  364. Doo::loadModel('user');
  365. $user = new User();
  366. $this->data['ssoUrl'] = ZHSSO;
  367. $this->data['userinfo'] = $user->getuserbyId($this->data['uid']);
  368. $this->render('user-info',$this->data,TRUE);
  369. }
  370. //修改idcard
  371. public function updateId(){
  372. if(!isset($this->data['uid'])){
  373. return '/';
  374. }
  375. if(!isset($_GET['id']) || empty($_GET['id'])){
  376. $data= '请输入您的身份证号';
  377. echo json_encode(array('code' => 404, 'data' => $data));
  378. exit;
  379. }
  380. if(!$this->validateIDCard($_GET['id'])){
  381. $data = '身份证格式出错,请重新输入';
  382. echo json_encode(array('code' => 404, 'data' => $data));
  383. exit;
  384. }
  385. Doo::loadModel('user');
  386. $user = new User();
  387. $user->userid = $this->data['uid'];
  388. $user->idcard = $_GET['id'];
  389. $user->update();
  390. echo json_encode(array('code' => 200));
  391. exit;
  392. }
  393. //更换头像
  394. public function changeAvatar(){
  395. if(!isset($this->data['uid'])){
  396. return '/';
  397. }
  398. require_once (SITE_PATH . '/protected/class/client.php');
  399. $client = new client ( ZHSSO );
  400. $tuid = $this->authcode($_COOKIE['sso_id']);
  401. $this->data['avatar'] = $client->ps_getavatar ( $tuid );
  402. $this->data ['avatar_flash'] = $client->ps_getavatar_upload_html ( $tuid );
  403. $this->render('user-avatar',$this->data,TRUE);
  404. }
  405. //课程学习页
  406. public function videoInfo(){
  407. Doo::loadModel('ktclass');
  408. Doo::loadModel('classuser');
  409. $ktclass = new Ktclass();
  410. $classuser = new Classuser();
  411. $hadId = $this->checkidcard($this->data['uid']);
  412. if($hadId == 0){
  413. return '/setId';
  414. }elseif($hadId == 3){
  415. return '/';
  416. }
  417. $class = $ktclass->getRowishowbyId($this->params['classid']);
  418. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$this->params['classid']);
  419. if(empty($class)){
  420. return '/';
  421. }
  422. if(empty($hadbuy)){
  423. return '/classinfo/'.$this->params['classid'];
  424. }
  425. $this->data['classinfo'] = $class;
  426. $this->data['totalnum'] = sprintf("%.2f", $class['classnum']*$class['classtime']/60);
  427. Doo::loadModel('ktsection');
  428. Doo::loadModel('kttype');
  429. $ktype = new Kttype();
  430. $type = $ktype->getTypebyClassid($this->params['classid']);
  431. $this->data['kttype'] = $type;
  432. $videolist = array();
  433. foreach($type as $k => $v){
  434. Doo::loadModel('ktsection');
  435. $ktsection = new Ktsection();
  436. $section = $ktsection->getSecbyTypeid($v['typeid']);
  437. $this->data['kttype'][$k]['section'] = $section;
  438. foreach($section as $sk => $sv){
  439. Doo::loadModel('ktvideo');
  440. $ktvideo = new Ktvideo();
  441. $video = $ktvideo->getVideobySecAndShow($sv['seid']);
  442. $this->data['kttype'][$k]['section'][$sk]['video'] = $video;
  443. if(!empty($video)){
  444. foreach($video as $vk => $vv){
  445. $videolist[] = $video[$vk];
  446. }
  447. }
  448. }
  449. }
  450. if(!empty($videolist)){
  451. foreach($videolist as $vk => $vs){
  452. $videolist2[$vk]['videoid'] = $vs['videoid'];
  453. $videolist2[$vk]['wyvideoid'] = $vs['wy_video_id'];
  454. $videolist2[$vk]['videoname'] = $vs['videoname'];
  455. }
  456. $this->data['videolist'] = json_encode($videolist2,TRUE);
  457. }else{
  458. $this->data['videolist'] = '';
  459. }
  460. // var_dump($this->data['videolist']);
  461. // exit;
  462. require_once (Doo::conf()->SITE_PATH . '/protected/class/video.php');
  463. $video = new video ( );
  464. if($hadbuy['watch_wyid'] == 0){
  465. $wyvideoid = $this->data['kttype'][0]['section'][0]['video'][0]['wy_video_id'];
  466. $this->data['play_time'] = 0;
  467. $this->data['videoid'] = $this->data['kttype'][0]['section'][0]['video'][0]['videoid'];
  468. $this->data['wyvideoid'] = $this->data['kttype'][0]['section'][0]['video'][0]['wy_video_id'];
  469. }else{
  470. $wyvideoid = $hadbuy['watch_wyid'];
  471. $this->data['play_time'] = $hadbuy['watch_time'];
  472. $this->data['videoid'] = $hadbuy['watch_videoid'];
  473. $this->data['wyvideoid'] = $hadbuy['watch_wyid'];
  474. }
  475. $result = $video->videoGet($wyvideoid);
  476. $type = '';
  477. if($result['code'] == 200){
  478. $play_url = !empty($result['ret']['shdMp4Url']) ? $result['ret']['shdMp4Url'] : '';
  479. $play_type = substr(strrchr($play_url, '.'), 1);
  480. if ($play_type == 'mp4'){
  481. $type = 'video/mp4';
  482. }
  483. }
  484. if($hadbuy['issuccess'] == 1){
  485. $this->data['successClass'] = 1;
  486. }else{
  487. $this->data['successClass'] = 0;
  488. }
  489. $this->data['user_progress'] = $this->getPersonProgress($class,$hadbuy);
  490. $this->data['play_url'] = isset($play_url) ? $play_url : '';
  491. $this->data['play_type'] = isset($type) ? $type : '';
  492. $this->render('video-play', $this->data, TRUE);
  493. }
  494. //获取个人的学习进度
  495. private function getPersonProgress($class,$classuser){
  496. $totalss = $class['classnum']*$class['classtime']*60; //课程总秒数
  497. $userss = $classuser['seetime']; //已完成的秒数
  498. $nodonehh = sprintf('%.2f',($totalss-$userss)/3600); //未完成的小时数
  499. $donehh = sprintf('%.2f',$classuser['seetime']/3600); //已完成的小时数
  500. $doneclassnum = intval($userss/($class['classtime']*60)); //已完成的课时数,取整
  501. if($doneclassnum<10 && $doneclassnum != 0){
  502. $doneclassnum = '0'.$doneclassnum;
  503. }
  504. $progress = sprintf('%.2f',$userss/$totalss) *100; //已完成占总课程的百分比
  505. $noprogress = 100-$progress; //未完成占总课程的百分比
  506. return array('progress' => $progress, 'noprogress' => $noprogress, 'donehh' => $donehh, 'doness' => $userss,'totalss' => $totalss, 'doneclassnum' => $doneclassnum, 'nodonehh' => $nodonehh);
  507. }
  508. //获取视频播放地址
  509. public function playVideo(){
  510. Doo::loadModel('ktclass');
  511. Doo::loadModel('classuser');
  512. $ktclass = new Ktclass();
  513. $classuser = new Classuser();
  514. $hadId = $this->checkidcard($this->data['uid']);
  515. if($hadId == 0){
  516. return '/setId';
  517. }elseif($hadId == 3){
  518. return '/';
  519. }
  520. $class = $ktclass->getRowishowbyId($_POST['classid']);
  521. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$_POST['classid']);
  522. if(empty($class)){
  523. return '/';
  524. }
  525. if(empty($hadbuy)){
  526. return '/classinfo/'.$_POST['classid'];
  527. }
  528. Doo::loadModel('videouser');
  529. $videouser = new Videouser();
  530. $videotime = $_POST['videotime'];
  531. $thisvideo = $_POST['thisvideo'];
  532. $thiswyvideo = $_POST['thiswyvideo'];
  533. $seetime = $_POST['seetime'];
  534. $duration = $_POST['duration'];
  535. if($hadbuy['issuccess'] == 1){
  536. //保存课堂和视频信息到数据库
  537. $classuser->watch_videoid = $thisvideo;
  538. $classuser->watch_wyid = $thiswyvideo;
  539. $classuser->watch_time = $videotime;
  540. $classuser->lasttime = time();
  541. $classuser->id = $hadbuy['id'];
  542. $classuser->update();
  543. }else{
  544. //保存课堂和视频信息到数据库
  545. $classuser->watch_videoid = $thisvideo;
  546. $classuser->watch_wyid = $thiswyvideo;
  547. $classuser->watch_time = $videotime;
  548. $classuser->lasttime = time();
  549. $classuser->seetime = intval($hadbuy['seetime'])+intval($seetime);
  550. $classuser->id = $hadbuy['id'];
  551. $classuser->update();
  552. }
  553. $hadbuy['seetime'] = intval($hadbuy['seetime'])+intval($seetime);
  554. $vuser = $videouser->getvideoMsg($this->data['uid'],$thisvideo);
  555. if(!empty($vuser)){
  556. $videouser->id = $vuser['id'];
  557. $videouser->file_time = $duration;
  558. $videouser->seetime = $videotime;
  559. $videouser->last_time = time();
  560. $videouser->update();
  561. }else{
  562. $videouser->userid = $this->data['uid'];
  563. $videouser->videoid = $thisvideo;
  564. $videouser->wyvideoid = $thiswyvideo;
  565. $videouser->file_time = $duration;
  566. $videouser->seetime = $videotime;
  567. $videouser->last_time = time();
  568. $videouser->insert();
  569. }
  570. $watchtime = $videouser->gettimebywyvideo($this->data['uid'],$_POST['video']);
  571. $user_progress = $this->getPersonProgress($class,$hadbuy);
  572. require_once (Doo::conf()->SITE_PATH . '/protected/class/video.php');
  573. $video = new video ( );
  574. if(isset($_POST['video'])){
  575. $result = $video->videoGet($_POST['video']);
  576. if($result['code'] == 200){
  577. $play_url = !empty($result['ret']['shdMp4Url']) ? $result['ret']['shdMp4Url'] : '';
  578. $play_type = substr(strrchr($play_url, '.'), 1);
  579. if ($play_type == 'mp4'){
  580. $type = 'video/mp4';
  581. }else {
  582. echo json_encode(array( 'code' => 400));
  583. exit;
  584. }
  585. echo json_encode(array( 'code' => 200, 'url' => $play_url, 'type' => $type, 'seetime' => $watchtime, 'user_progress' => $user_progress));
  586. exit;
  587. }
  588. echo json_encode(array( 'code' => 400));
  589. exit;
  590. }
  591. echo json_encode(array( 'code' => 400));
  592. exit;
  593. }
  594. //保存视频信息
  595. public function saveClassAndVideo(){
  596. Doo::loadModel('ktclass');
  597. Doo::loadModel('classuser');
  598. $ktclass = new Ktclass();
  599. $classuser = new Classuser();
  600. $hadId = $this->checkidcard($this->data['uid']);
  601. if($hadId == 0){
  602. return '/setId';
  603. }elseif($hadId == 3){
  604. return '/';
  605. }
  606. $class = $ktclass->getRowishowbyId($_POST['classid']);
  607. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$_POST['classid']);
  608. if(empty($class)){
  609. return '/';
  610. }
  611. if(empty($hadbuy)){
  612. return '/classinfo/'.$_POST['classid'];
  613. }
  614. Doo::loadModel('videouser');
  615. $videouser = new Videouser();
  616. $videotime = $_POST['videotime'];
  617. $videoid = $_POST['video'];
  618. $wyvideoid = $_POST['wyvideo'];
  619. $seetime = $_POST['seetime'];
  620. $duration = $_POST['duration'];
  621. //判断课程是否已完成,已完成则不再上传课程时间
  622. if($hadbuy['issuccess'] == 0 && intval($hadbuy['seetime'])+intval($seetime) >= $class['classnum']*$class['classtime']*60){
  623. $classuser->issuccess = 1;
  624. $classuser->watch_videoid = $videoid;
  625. $classuser->watch_wyid = $wyvideoid;
  626. $classuser->watch_time = $videotime;
  627. $classuser->lasttime = time();
  628. $classuser->seetime = intval($hadbuy['seetime'])+intval($seetime);
  629. $classuser->id = $hadbuy['id'];
  630. $classuser->update();
  631. }elseif($hadbuy['issuccess'] == 1){
  632. $classuser->watch_videoid = $videoid;
  633. $classuser->watch_wyid = $wyvideoid;
  634. $classuser->watch_time = $videotime;
  635. $classuser->lasttime = time();
  636. $classuser->id = $hadbuy['id'];
  637. $classuser->update();
  638. }else{
  639. //保存课堂和视频信息到数据库
  640. $classuser->watch_videoid = $videoid;
  641. $classuser->watch_wyid = $wyvideoid;
  642. $classuser->watch_time = $videotime;
  643. $classuser->lasttime = time();
  644. $classuser->seetime = intval($hadbuy['seetime'])+intval($seetime);
  645. $classuser->id = $hadbuy['id'];
  646. $classuser->update();
  647. }
  648. $hadbuy['seetime'] = intval($hadbuy['seetime'])+intval($seetime);
  649. $vuser = $videouser->getvideoMsg($this->data['uid'],$videoid);
  650. if(!empty($vuser)){
  651. $videouser->id = $vuser['id'];
  652. $videouser->file_time = $duration;
  653. $videouser->seetime = $videotime;
  654. $videouser->last_time = time();
  655. $videouser->update();
  656. }else{
  657. $videouser->userid = $this->data['uid'];
  658. $videouser->videoid = $videoid;
  659. $videouser->wyvideoid = $wyvideoid;
  660. $videouser->file_time = $duration;
  661. $videouser->seetime = $videotime;
  662. $videouser->last_time = time();
  663. $videouser->insert();
  664. }
  665. $user_progress = $this->getPersonProgress($class,$hadbuy);
  666. echo json_encode(array('user_progress' => $user_progress));
  667. exit;
  668. }
  669. public function alipay(){
  670. Doo::loadModel('ktclass');
  671. Doo::loadModel('classuser');
  672. $ktclass = new Ktclass();
  673. $classuser = new Classuser();
  674. $hadId = $this->checkidcard($this->data['uid']);
  675. if($hadId == 0){
  676. return '/setId';
  677. }elseif($hadId == 3){
  678. return '/';
  679. }
  680. if(isset($this->params['classid'])){
  681. $class = $ktclass->getRowishowbyId($this->params['classid']);
  682. $hadbuy = $classuser->gethasbyuserandclass($this->data['uid'],$this->params['classid']);
  683. if(!empty($class) && empty($hadbuy)){
  684. Doo::loadModel('user');
  685. $users = new User();
  686. $user = $users->getuserbyId($this->data['uid']);
  687. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/alipay.config.php");
  688. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/lib/alipay_submit.class.php");
  689. $alipaySubmit = new AlipaySubmit($alipay_config);
  690. $payment_type = "1";
  691. $notify_url = Doo::conf()->APP_URL."api/smartcost/sso/notify_url";
  692. $return_url = Doo::conf()->APP_URL."api/smartcost/sso/return_url";
  693. //商户订单号
  694. $sn=$this->create_sn();
  695. $out_trade_no = $sn;
  696. $subject = $class['classname'].'费用支付';
  697. $extra_common_param=''; //用户自定义回传参数
  698. //付款金额
  699. $p_amount=trim($class['price']);
  700. $total_fee = $p_amount;
  701. $body = '';
  702. $show_url = '';
  703. //防钓鱼时间戳
  704. $anti_phishing_key = $alipaySubmit->query_timestamp();
  705. $exter_invoke_ip = $this->clientIP();
  706. //构造要请求的参数数组,无需改动
  707. $parameter = array(
  708. "service" => "create_direct_pay_by_user",
  709. "partner" => trim($alipay_config['partner']),
  710. "seller_email" => trim($alipay_config['seller_email']),
  711. "payment_type" => $payment_type,
  712. "notify_url" => $notify_url,
  713. "return_url" => $return_url,
  714. "out_trade_no" => $out_trade_no,
  715. "subject" => $subject,
  716. "total_fee" => $total_fee,
  717. "body" => $body,
  718. "show_url" => $show_url,
  719. "anti_phishing_key" => $anti_phishing_key,
  720. "exter_invoke_ip" => $exter_invoke_ip,
  721. "_input_charset" => trim(strtolower($alipay_config['input_charset'])),
  722. "extra_common_param"=> $extra_common_param
  723. );
  724. //建立请求
  725. $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "加载中");
  726. // 生成订单
  727. Doo::loadModel('order');
  728. $order = new Order();
  729. $array = array('trade_sn' => $sn,
  730. 'userid' => $this->data['uid'],
  731. 'username' => $this->data['username'],
  732. 'useremail' => $user['useremail'],
  733. 'mobile' => $user['mobile'],
  734. 'price' => $p_amount,
  735. 'createtime' => time(),
  736. 'classid' => $this->params['classid'],
  737. 'classname' => $class['classname'],
  738. 'ip' => $this->clientIP(),
  739. 'status' => 2
  740. );
  741. $order->_insertAttributes('order', $array);
  742. header("Content-type: text/html; charset=utf-8");
  743. echo $html_text;
  744. }else{
  745. return '/classinfo/'.$this->params['classid'];
  746. }
  747. }
  748. }
  749. function returnAlipay(){
  750. $hadId = $this->checkidcard($this->data['uid']);
  751. if($hadId == 0){
  752. return '/setId';
  753. }elseif($hadId == 3){
  754. return '/';
  755. }
  756. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/alipay.config.php");
  757. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/lib/alipay_notify.class.php");
  758. $alipayNotify = new AlipayNotify($alipay_config);
  759. $verify_result = $alipayNotify->verifyReturn();
  760. if($verify_result) {
  761. $v_oid = $_GET['out_trade_no']; //商户订单号
  762. $trade_no = $_GET['trade_no']; //支付宝交易号
  763. $trade_status = $_GET['trade_status']; //交易状态
  764. $v_amount=$_GET['total_fee']; //交易金额
  765. if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
  766. $order = Doo::loadModel('order', TRUE);
  767. $callbackData = $order->getRowsBytrade_id($v_oid);
  768. if ($callbackData['status'] == 2) {
  769. if ($callbackData['price'] == $v_amount) {
  770. $order->trade_sn = $v_oid;
  771. $comment = $order->find(array('limit' => 1, 'select' => 'id', 'where' => 'trade_sn='.$v_oid, 'asArray' => TRUE));
  772. $order->id = $comment['id'];
  773. $order->paytime = time();
  774. $order->status = 1;
  775. $succvalue = $order->update();
  776. if ($succvalue) {
  777. Doo::loadModel('classuser');
  778. $classuser = new Classuser();
  779. $classuser->userid = $callbackData['userid'];
  780. $classuser->classid = $callbackData['classid'];
  781. $classuser->price = $callbackData['price'];
  782. $classuser->issuccess = 0;
  783. $result = $classuser->insert();
  784. if ($result) {
  785. $this->data['msg'] = '支付完成';
  786. header('refresh:3;url=/classinfo/'.$callbackData['classid']);
  787. }
  788. }
  789. }
  790. } else {
  791. $this->data['msg'] = '已支付完毕,操作无效';
  792. header('refresh:3;url=/');
  793. }
  794. }
  795. }else {
  796. $this->data['msg'] = '支付故障';
  797. header('refresh:3;url=/');
  798. }
  799. $this->render('pay_msg', $this->data, TRUE);
  800. }
  801. function notifyAlipay(){
  802. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/alipay.config.php");
  803. require_once(Doo::conf()->SITE_PATH."protected/class/alipay/lib/alipay_notify.class.php");
  804. $alipayNotify = new AlipayNotify($alipay_config);
  805. $verify_result = $alipayNotify->verifyNotify();
  806. if($verify_result) {
  807. $v_oid = $_POST['out_trade_no']; //商户订单号
  808. $trade_no = $_POST['trade_no']; //支付宝交易号
  809. $trade_status = $_POST['trade_status']; //交易状态
  810. $v_amount=$_POST['total_fee']; //交易金额
  811. if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
  812. $order = Doo::loadModel('order', TRUE);
  813. $callbackData = $order->getRowsBytrade_id($v_oid);
  814. if ($callbackData['status'] == 2) {
  815. if ($callbackData['price'] == $v_amount) {
  816. $order->trade_sn = $v_oid;
  817. $comment = $order->find(array('limit' => 1, 'select' => 'id', 'where' => 'trade_sn='.$v_oid, 'asArray' => TRUE));
  818. $order->id = $comment['id'];
  819. $order->paytime = time();
  820. $order->status = 1;
  821. $succvalue = $order->update();
  822. if ($succvalue) {
  823. Doo::loadModel('classuser');
  824. $classuser = new Classuser();
  825. $classuser->userid = $callbackData['userid'];
  826. $classuser->classid = $callbackData['classid'];
  827. $classuser->price = $callbackData['price'];
  828. $classuser->issuccess = 0;
  829. $result = $classuser->insert();
  830. if($result) {
  831. echo "success";
  832. die;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. echo "success";
  839. }else {
  840. echo "fail";
  841. }
  842. }
  843. /**
  844. * 生成流水号
  845. */
  846. function create_sn() {
  847. mt_srand((double) microtime() * 1000000);
  848. return date("YmdHis") . str_pad(mt_rand(1, 99999), 5, "0", STR_PAD_LEFT);
  849. }
  850. // 检测手机格式
  851. function checkMobile($mobile) {
  852. if(preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
  853. return TRUE;
  854. } else {
  855. return FALSE;
  856. }
  857. }
  858. //验证身份证是否有效
  859. function validateIDCard($IDCard) {
  860. if (strlen($IDCard) == 18) {
  861. return $this->check18IDCard($IDCard);
  862. } elseif ((strlen($IDCard) == 15)) {
  863. $IDCard = $this->convertIDCard15to18($IDCard);
  864. return $this->check18IDCard($IDCard);
  865. } else {
  866. return false;
  867. }
  868. }
  869. //计算身份证的最后一位验证码,根据国家标准GB 11643-1999
  870. function calcIDCardCode($IDCardBody) {
  871. if (strlen($IDCardBody) != 17) {
  872. return false;
  873. }
  874. //加权因子
  875. $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  876. //校验码对应值
  877. $code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  878. $checksum = 0;
  879. for ($i = 0; $i < strlen($IDCardBody); $i++) {
  880. $checksum += substr($IDCardBody, $i, 1) * $factor[$i];
  881. }
  882. return $code[$checksum % 11];
  883. }
  884. // 将15位身份证升级到18位
  885. function convertIDCard15to18($IDCard) {
  886. if (strlen($IDCard) != 15) {
  887. return false;
  888. } else {
  889. // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
  890. if (array_search(substr($IDCard, 12, 3), array('996', '997', '998', '999')) !== false) {
  891. $IDCard = substr($IDCard, 0, 6) . '18' . substr($IDCard, 6, 9);
  892. } else {
  893. $IDCard = substr($IDCard, 0, 6) . '19' . substr($IDCard, 6, 9);
  894. }
  895. }
  896. $IDCard = $IDCard . $this->calcIDCardCode($IDCard);
  897. return $IDCard;
  898. }
  899. // 18位身份证校验码有效性检查
  900. function check18IDCard($IDCard) {
  901. if (strlen($IDCard) != 18) {
  902. return false;
  903. }
  904. $IDCardBody = substr($IDCard, 0, 17); //身份证主体
  905. $IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码
  906. if ($this->calcIDCardCode($IDCardBody) != $IDCardCode) {
  907. return false;
  908. } else {
  909. return true;
  910. }
  911. }
  912. /**
  913. * 获取get或者POST值
  914. * @param string $name 属性名称
  915. * @return fixed 值
  916. */
  917. function get_args($name) {
  918. if (isset ( $_GET [$name] )) {
  919. if (is_array ( $_GET [$name] ))
  920. return $_GET [$name];
  921. else
  922. return addslashes ( $_GET [$name] );
  923. } elseif (isset ( $_POST [$name] )) {
  924. if (is_array ( $_POST [$name] ))
  925. return $_POST [$name];
  926. else
  927. return addslashes ( $_POST [$name] );
  928. } else {
  929. return false;
  930. }
  931. }
  932. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  933. $ckey_length = 4;
  934. if (! $key) {
  935. $key = "ZHKT";
  936. }
  937. $key = md5 ( $key );
  938. $keya = md5 ( substr ( $key, 0, 16 ) );
  939. $keyb = md5 ( substr ( $key, 16, 16 ) );
  940. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  941. $cryptkey = $keya . md5 ( $keya . $keyc );
  942. $key_length = strlen ( $cryptkey );
  943. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  944. $string_length = strlen ( $string );
  945. $result = '';
  946. $box = range ( 0, 255 );
  947. $rndkey = array ();
  948. for($i = 0; $i <= 255; $i ++) {
  949. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  950. }
  951. for($j = $i = 0; $i < 256; $i ++) {
  952. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  953. $tmp = $box [$i];
  954. $box [$i] = $box [$j];
  955. $box [$j] = $tmp;
  956. }
  957. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  958. $a = ($a + 1) % 256;
  959. $j = ($j + $box [$a]) % 256;
  960. $tmp = $box [$a];
  961. $box [$a] = $box [$j];
  962. $box [$j] = $tmp;
  963. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  964. }
  965. if ($operation == 'DECODE') {
  966. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  967. return substr ( $result, 26 );
  968. } else {
  969. return '';
  970. }
  971. } else {
  972. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  973. }
  974. }
  975. }