WeMall微信商城源码插件大转盘代码详情


发布者 wemallshop  发布时间 1475742158372
关键字 Html5 

WeMall微信商城源码插件大转盘代码是用于商业推广的比较有效的方式,分享了部分比较重要的代码,供技术员学习参考

 

代码详情地址:http://addon.wemallshop.com/Product/addonList/menu_id/1 或 www.wemallshop.com

AdminController.class

Php代码  
  1. <?php  
  2. namespace Addons\Wheel\Controller;  
  3.   
  4. class AdminController extends InitController  
  5. {  
  6.   
  7.   
  8.     // public function __construct()  
  9.     // {  
  10.     //     parent::__construct();  
  11.     // }  
  12.   
  13.     public function index()  
  14.     {  
  15.         $config = M("AddonWheelConfig")->find();  
  16.         $this->assign("config", $config);  
  17.   
  18.         $record = D('Addons://Wheel/AddonWheelRecord'); // 实例化User对象  
  19.         $count = $record->count();// 查询满足要求的总记录数  
  20.         $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数(25)  
  21.         $Page->setConfig('theme', "<div class='widget-content padded text-center'><ul class='pagination'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul></div>");  
  22.         $show = $Page->show();// 分页显示输出  
  23.         // 进行分页数据查询 注意limit方法的参数要使用Page类的属性  
  24.         $record = $record->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->relation(true)->select();  
  25.   
  26.         $this->assign("record", $record);// 赋值数据集  
  27.         $this->assign('page', $show);// 赋值分页输出  
  28.   
  29.         $this->display();  
  30.     }  
  31.   
  32.     public function addConfig()  
  33.     {  
  34.         M("AddonWheelConfig")->where(array("id" => "1"))->save($_POST);  
  35.         $this->success('设置成功', 'Admin/Admin/index/addon/Wheel');  
  36.     }  
  37. }  

 

IndexController.class

Php代码  
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: heqing 
  5.  * Date: 15/7/30 
  6.  * Time: 09:40 
  7.  */  
  8.   
  9. namespace Addons\Wheel\Controller;  
  10.   
  11. // class IndexController extends InitController  
  12. // {  
  13. //     public function index()  
  14. //     {  
  15. //         $this->show('SystemInfo Index index');  
  16. //     }  
  17.   
  18.   
  19. // }  
  20.   
  21. class IndexController extends InitController  
  22. {  
  23.     public $appUrl = "";  
  24.     public function __construct()  
  25.     {  
  26.         parent::__construct();  
  27.         $this->appUrl = "http://" . I("server.HTTP_HOST");  
  28.     }  
  29.   
  30.     public function init()  
  31.     {  
  32.         return R("App/Common/init");  
  33.     }  
  34.   
  35.     public function oauthRegister($wxuser)  
  36.     {  
  37.         return R("App/Common/oauthRegister", array($wxuser));  
  38.     }  
  39.   
  40.     public function index()  
  41.     {  
  42.         $user=R("App/Public/oauthLogin");  
  43.   
  44.         // if (!session("userUid")) {  
  45.         //     $weObj = $this->init();  
  46.         //     $token = $weObj->getOauthAccessToken();  
  47.         //     if (!$token) {  
  48.         //         $weObj = $this->init();  
  49.         //         $url = $weObj->getOauthRedirect($this->appUrl . u_addons('Wheel://App/Index/index'));  
  50.         //         header("location: $url");  
  51.         //         return;  
  52.         //     } else {  
  53.         //         $wxuser = $weObj->getOauthUserinfo($token["access_token"], $token["openid"]);  
  54.         //         session("userUid", $wxuser["openid"]);  
  55.         //         $this->oauthRegister($wxuser);  
  56.         //     }  
  57.         // }  
  58.   
  59.         $user = M("User")->where(array("uid" => session("userUid")))->find();  
  60.   
  61.         $config = M("AddonWheelConfig")->find();  
  62.         $this->assign("config", $config);  
  63.         $this->assign("user", $user);  
  64.   
  65.         $record = M("AddonWheelRecord")->where(array("user_id" => session("userId")))->order("id desc")->find();  
  66.         $this->assign("record", $record);  
  67.         $this->display();  
  68.     }  
  69.   
  70.     /** 
  71.      * 中奖机率计算 
  72.      */  
  73.     function lotteryJson()  
  74.     {  
  75.         $today = date("Y-m-d");  
  76.         $where["time"] = array("like", $today . "%");  
  77.         $where["user_id"] = session("userId");  
  78.         $record = D("Addons://Wheel/AddonWheelRecord")->where($where)->find();  
  79.         if ($record) {  
  80.             $this->ajaxReturn("-1");  
  81.             return;  
  82.         }  
  83.   
  84.         $config = M("AddonWheelConfig")->find();  
  85.         //奖品概率  
  86.         $proArr = array(  
  87.             '1' => $config["level1_prob"],   
  88.             '2' => $config["level2_prob"],   
  89.             '3' => $config["level3_prob"],   
  90.             '4' => $config["level4_prob"],   
  91.             '5' => $config["level5_prob"],   
  92.             '6' => $config["level6_prob"],   
  93.             '7' => $config["level7_prob"]  
  94.         );  
  95.         //奖品库存  
  96.         $proCount = array(  
  97.             '1' => $config["level1_store"],  
  98.             '2' => $config["level2_store"],  
  99.             '3' => $config["level3_store"],  
  100.             '4' => $config["level4_store"],  
  101.             '5' => $config["level5_store"],  
  102.             '6' => $config["level6_store"],  
  103.             '7' => $config["level7_store"]  
  104.         );  
  105.         $file = './Data/wheel.txt';  
  106.         $data = array(  
  107.             '1' => 0, '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0  
  108.         );  
  109.         if (!file_exists($file)) {  
  110.             file_put_contents($file, serialize($data));  
  111.         } else {  
  112.             $str = file_get_contents($file);  
  113.             $data = unserialize($str);  
  114.         }  
  115.         $rid = $this->getRand($proArr, $proCount);  
  116.   
  117.         if ($rid > 6) {  
  118.             $rid = 0;  
  119.         } else {  
  120.             $rid = $this->returnRid($rid, $file, $data, $proCount, $proArr);  
  121.         }  
  122.   
  123.         M("AddonWheelRecord")->add(array("user_id" => session("userId"), "level" => $rid));  
  124.         echo $rid;  
  125.     }  
  126.   
  127.     function returnRid($rid, $file, $data, $proCount, $proArr)  
  128.     {  
  129.         $data[$rid] = $data[$rid] + 1;  
  130.         $count = $proCount[$rid]; // 总库存  
  131.         if ($count < $data[$rid]) {  
  132.             // 如果抽取的数据大于总库存时库存清0  
  133.             $proCount[$rid] = 0;  
  134.             // 然后继续计算一直计算出某个值的库存不为0  
  135.             $rid = returnRid($rid, $file, $data, $proCount, $proArr);  
  136.         } else {  
  137.             // 写入缓存  
  138.             file_put_contents($file, serialize($data));  
  139.         }  
  140.         return $rid;  
  141.     }  
  142.   
  143.     /** 
  144.      * 中奖概率计算, 能用 
  145.      * $proArr = array('1'=>'概率', '2'=>'概率'); 
  146.      * $proCount = array('1'=>'库存', '2'=>'库存'); 
  147.      */  
  148.     function getRand($proArr, $proCount)  
  149.     {  
  150.         $result = '';  
  151.         $proSum = 0;  
  152.         foreach ($proCount as $key => $val) {  
  153.             if ($val <= 0) {  
  154.                 continue;  
  155.             } else {  
  156.                 $proSum = $proSum + $proArr[$key];  
  157.             }  
  158.         }  
  159.         foreach ($proArr as $key => $proCur) {  
  160.             if ($proCount[$key] <= 0) {  
  161.                 continue;  
  162.             } else {  
  163.                 $randNum = mt_rand(1, $proSum);  
  164.                 if ($randNum <= $proCur) {  
  165.                     $result = $key;  
  166.                     break;  
  167.                 } else {  
  168.                     $proSum -= $proCur;  
  169.                 }  
  170.             }  
  171.         }  
  172.         unset($proArr);  
  173.         return $result;  
  174.     }  
  175. }  

 

InitController.class

Php代码  
  1. <?php  
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: heqing 
  5.  * Date: 15/7/30 
  6.  * Time: 12:11 
  7.  */  
  8.   
  9. namespace Addons\Wheel\Controller;  
  10.   
  11.   
  12. use Common\Controller\Addon;  
  13.   
  14. class InitController extends Addon  
  15. {  
  16.   
  17.     public function install()  
  18.     {  
  19.         $install_sql = './Addons/Wheel/Data/install.sql';  
  20.         if (file_exists($install_sql)) {  
  21.             execute_sql_file($install_sql);  
  22.         }  
  23.          $this->success("安装成功", "Admin/Addon/addon");  
  24.     }  
  25.   
  26.     public function uninstall()  
  27.     {  
  28.         $uninstall_sql = './Addons/Wheel/Data/uninstall.sql';  
  29.         if (file_exists($uninstall_sql)) {  
  30.             execute_sql_file($uninstall_sql);  
  31.         }  
  32.         $this->success("卸载成功", "Admin/Addon/addon");  
  33.     }  
  34. }  

 

前台

Admin_index

Html代码  
  1.  <section class="content-header">  
  2.     <h1>  
  3.         大转盘管理  
  4.         <small></small>  
  5.     </h1>  
  6. </section>   
  7.  <section class="content">  
  8.     <div class="row">  
  9.         <div class="col-md-12">  
  10.             <div class="box box-danger">  
  11.                 <div class="box-header with-border">  
  12.                     <h3 class="box-title">大转盘设置</h3>    
  13. <!-- /.box-tools -->  
  14.   </div>  
  15.                   
  16.           
  17.                             <div class="box-body no-padding">  
  18.                                 <div class="mailbox-controls">  
  19.                                     <div class="btn-group">  
  20.                             <a data-toggle="tab" href="#tab1" class="btn btn-danger "><i class="icon-comments"></i>大转盘设置</a>  
  21.                         </div>      
  22.                         <div class="btn-group">  
  23.                             <a data-toggle="tab" href="#tab2" class="btn btn-danger "><i class="icon-user"></i>大转盘记录</a>  
  24.                         </div>  
  25.                                  
  26.                                 <div class="tab-content padded" id="my-tab-content">  
  27.                                     <div class="tab-pane active" id="tab1">  
  28.                                         <p>  
  29.                                             <form action="{:u_addons('Wheel://Admin/Admin/addConfig')}" id="myForm" method="post" onsubmit="return false;" class="form-horizontal">  
  30.                                                 <div class="form-group">  
  31.                                                     <label class="control-label col-md-2">大转盘名称</label>  
  32.                                                     <div class="col-md-7">  
  33.                                                         <input class="form-control" placeholder="" value="{$config.name}" name="name" type="text">  
  34.                                                     </div>  
  35.                                                 </div>  
  36.                                                 <div class="form-group">  
  37.                                                     <label class="control-label col-md-2">活动时间</label>  
  38.                                                     <div class="col-md-7">  
  39.                                                         <input class="form-control" placeholder="" value="{$config.activity_time}" name="activity_time" type="text">  
  40.                                                     </div>  
  41.                                                 </div>&n