WeMall微信商城源码插件大转盘代码是用于商业推广的比较有效的方式,分享了部分比较重要的代码,供技术员学习参考
代码详情地址:http://addon.wemallshop.com/Product/addonList/menu_id/1 或 www.wemallshop.com
AdminController.class
- <?php
- namespace Addons\Wheel\Controller;
- class AdminController extends InitController
- {
- // public function __construct()
- // {
- // parent::__construct();
- // }
- public function index()
- {
- $config = M("AddonWheelConfig")->find();
- $this->assign("config", $config);
- $record = D('Addons://Wheel/AddonWheelRecord'); // 实例化User对象
- $count = $record->count();// 查询满足要求的总记录数
- $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数(25)
- $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>");
- $show = $Page->show();// 分页显示输出
- // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
- $record = $record->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->relation(true)->select();
- $this->assign("record", $record);// 赋值数据集
- $this->assign('page', $show);// 赋值分页输出
- $this->display();
- }
- public function addConfig()
- {
- M("AddonWheelConfig")->where(array("id" => "1"))->save($_POST);
- $this->success('设置成功', 'Admin/Admin/index/addon/Wheel');
- }
- }
IndexController.class
- <?php
- /**
- * Created by PhpStorm.
- * User: heqing
- * Date: 15/7/30
- * Time: 09:40
- */
- namespace Addons\Wheel\Controller;
- // class IndexController extends InitController
- // {
- // public function index()
- // {
- // $this->show('SystemInfo Index index');
- // }
- // }
- class IndexController extends InitController
- {
- public $appUrl = "";
- public function __construct()
- {
- parent::__construct();
- $this->appUrl = "http://" . I("server.HTTP_HOST");
- }
- public function init()
- {
- return R("App/Common/init");
- }
- public function oauthRegister($wxuser)
- {
- return R("App/Common/oauthRegister", array($wxuser));
- }
- public function index()
- {
- $user=R("App/Public/oauthLogin");
- // if (!session("userUid")) {
- // $weObj = $this->init();
- // $token = $weObj->getOauthAccessToken();
- // if (!$token) {
- // $weObj = $this->init();
- // $url = $weObj->getOauthRedirect($this->appUrl . u_addons('Wheel://App/Index/index'));
- // header("location: $url");
- // return;
- // } else {
- // $wxuser = $weObj->getOauthUserinfo($token["access_token"], $token["openid"]);
- // session("userUid", $wxuser["openid"]);
- // $this->oauthRegister($wxuser);
- // }
- // }
- $user = M("User")->where(array("uid" => session("userUid")))->find();
- $config = M("AddonWheelConfig")->find();
- $this->assign("config", $config);
- $this->assign("user", $user);
- $record = M("AddonWheelRecord")->where(array("user_id" => session("userId")))->order("id desc")->find();
- $this->assign("record", $record);
- $this->display();
- }
- /**
- * 中奖机率计算
- */
- function lotteryJson()
- {
- $today = date("Y-m-d");
- $where["time"] = array("like", $today . "%");
- $where["user_id"] = session("userId");
- $record = D("Addons://Wheel/AddonWheelRecord")->where($where)->find();
- if ($record) {
- $this->ajaxReturn("-1");
- return;
- }
- $config = M("AddonWheelConfig")->find();
- //奖品概率
- $proArr = array(
- '1' => $config["level1_prob"],
- '2' => $config["level2_prob"],
- '3' => $config["level3_prob"],
- '4' => $config["level4_prob"],
- '5' => $config["level5_prob"],
- '6' => $config["level6_prob"],
- '7' => $config["level7_prob"]
- );
- //奖品库存
- $proCount = array(
- '1' => $config["level1_store"],
- '2' => $config["level2_store"],
- '3' => $config["level3_store"],
- '4' => $config["level4_store"],
- '5' => $config["level5_store"],
- '6' => $config["level6_store"],
- '7' => $config["level7_store"]
- );
- $file = './Data/wheel.txt';
- $data = array(
- '1' => 0, '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0
- );
- if (!file_exists($file)) {
- file_put_contents($file, serialize($data));
- } else {
- $str = file_get_contents($file);
- $data = unserialize($str);
- }
- $rid = $this->getRand($proArr, $proCount);
- if ($rid > 6) {
- $rid = 0;
- } else {
- $rid = $this->returnRid($rid, $file, $data, $proCount, $proArr);
- }
- M("AddonWheelRecord")->add(array("user_id" => session("userId"), "level" => $rid));
- echo $rid;
- }
- function returnRid($rid, $file, $data, $proCount, $proArr)
- {
- $data[$rid] = $data[$rid] + 1;
- $count = $proCount[$rid]; // 总库存
- if ($count < $data[$rid]) {
- // 如果抽取的数据大于总库存时库存清0
- $proCount[$rid] = 0;
- // 然后继续计算一直计算出某个值的库存不为0
- $rid = returnRid($rid, $file, $data, $proCount, $proArr);
- } else {
- // 写入缓存
- file_put_contents($file, serialize($data));
- }
- return $rid;
- }
- /**
- * 中奖概率计算, 能用
- * $proArr = array('1'=>'概率', '2'=>'概率');
- * $proCount = array('1'=>'库存', '2'=>'库存');
- */
- function getRand($proArr, $proCount)
- {
- $result = '';
- $proSum = 0;
- foreach ($proCount as $key => $val) {
- if ($val <= 0) {
- continue;
- } else {
- $proSum = $proSum + $proArr[$key];
- }
- }
- foreach ($proArr as $key => $proCur) {
- if ($proCount[$key] <= 0) {
- continue;
- } else {
- $randNum = mt_rand(1, $proSum);
- if ($randNum <= $proCur) {
- $result = $key;
- break;
- } else {
- $proSum -= $proCur;
- }
- }
- }
- unset($proArr);
- return $result;
- }
- }
InitController.class
- <?php
- /**
- * Created by PhpStorm.
- * User: heqing
- * Date: 15/7/30
- * Time: 12:11
- */
- namespace Addons\Wheel\Controller;
- use Common\Controller\Addon;
- class InitController extends Addon
- {
- public function install()
- {
- $install_sql = './Addons/Wheel/Data/install.sql';
- if (file_exists($install_sql)) {
- execute_sql_file($install_sql);
- }
- $this->success("安装成功", "Admin/Addon/addon");
- }
- public function uninstall()
- {
- $uninstall_sql = './Addons/Wheel/Data/uninstall.sql';
- if (file_exists($uninstall_sql)) {
- execute_sql_file($uninstall_sql);
- }
- $this->success("卸载成功", "Admin/Addon/addon");
- }
- }
前台
Admin_index
- <section class="content-header">
- <h1>
- 大转盘管理
- <small></small>
- </h1>
- </section>
- <section class="content">
- <div class="row">
- <div class="col-md-12">
- <div class="box box-danger">
- <div class="box-header with-border">
- <h3 class="box-title">大转盘设置</h3>
- <!-- /.box-tools -->
- </div>
- <div class="box-body no-padding">
- <div class="mailbox-controls">
- <div class="btn-group">
- <a data-toggle="tab" href="#tab1" class="btn btn-danger "><i class="icon-comments"></i>大转盘设置</a>
- </div>
- <div class="btn-group">
- <a data-toggle="tab" href="#tab2" class="btn btn-danger "><i class="icon-user"></i>大转盘记录</a>
- </div>
- <div class="tab-content padded" id="my-tab-content">
- <div class="tab-pane active" id="tab1">
- <p>
- <form action="{:u_addons('Wheel://Admin/Admin/addConfig')}" id="myForm" method="post" onsubmit="return false;" class="form-horizontal">
- <div class="form-group">
- <label class="control-label col-md-2">大转盘名称</label>
- <div class="col-md-7">
- <input class="form-control" placeholder="" value="{$config.name}" name="name" type="text">
- </div>
- </div>
- <div class="form-group">
- <label class="control-label col-md-2">活动时间</label>
- <div class="col-md-7">
- <input class="form-control" placeholder="" value="{$config.activity_time}" name="activity_time" type="text">
- </div>
- </div>&n