<?php
/**
* juchuang_dinosaur模块小程序接口定义
*
* @author 优呀精品
* @url bbs.uuuya.com
*/
defined('IN_IA') or exit('Access Denied');
defined("MODULE_ROOT") or define("MODULE_ROOT", IA_ROOT . "/addons/juchuang_dinosaur");
class Juchuang_dinosaurModuleWxapp extends WeModuleWxapp {
public $fans;
public function __construct() {
global $_W;
require_once MODULE_ROOT . "/func.php";
$this->fans = $_W["fans"];
$this->uniacid = $_W["uniacid"];
}
public function doPageGame() {
$game = h_cache_read("game." . $this->uniacid);
return $this->result(0, "", $game);
tomedia();
}
public function doPageShare() {
global $_W;
$data = pdo_get("juchuang_dinosaur_setting", ["uniacid" => $this->uniacid]);
$data["image"] = $_W['attachurl'] . $data["image"];
return $this->result(0, "", $data);
}
public function doPageScore() {
global $_W, $_GPC;
$score = $_GPC["score"];
$data = $this->setUser(intval($score));
return $this->result(0, "", $data);
}
private function package() {
return pdo_getall("juchuang_dinosaur_package", ["uniacid" => $this->uniacid], "*", "", "money asc");
}
private function setUser($score = 0) {
$openid = $this->fans["openid"];
$user = pdo_get("juchuang_dinosaur_user", ["openid" => $openid]);
$data = [
"score" => $score,
"update_time" => time()
];
if ($score > $user["high_score"]) {
$data["high_score"] = $score;
}
pdo_update("juchuang_dinosaur_user", $data, ["openid" => $openid]);
$user = pdo_get("juchuang_dinosaur_user", ["openid" => $openid]);
//查询排名
$sql = "SELECT COUNT(id) rank FROM " . tablename("juchuang_dinosaur_user") . " WHERE uniacid=" . $this->uniacid . " AND high_score>=" . $user["high_score"];
$rank = pdo_fetch($sql);
$user["rank"] = $rank["rank"];
$sql = "SELECT *,FROM_UNIXTIME(update_time,'%Y-%m-%d') update_time FROM " . tablename("juchuang_dinosaur_user") . " WHERE high_score>0 AND uniacid=" . $this->uniacid . " ORDER BY high_score DESC LIMIT 10";
$list = pdo_fetchall($sql);
return [
"user" => $user,
"list" => $list,
"package" => $this->package()
];
}
public function doPageLogin() {
global $_GPC, $_W;
if (!$this->fans["openid"]) {
return $this->result(-1, "未登录", []);
}
$data = pdo_get("juchuang_dinosaur_user", ["openid" => $this->fans["openid"]]);
if (empty($data)) {
$setting = h_cache_read("payset." . $this->uniacid);
$gold = isset($setting["gold"]) ? intval($setting["gold"]) : 0;
$insert_data = [
"openid" => $this->fans["openid"],
"nickname" => $this->fans["nickname"],
"avatar" => $this->fans["avatar"],
"gold" => isset($setting["gold"]) ? intval($setting["gold"]) : 0,
"update_time" => time(),
"uniacid" => $this->uniacid
];
pdo_insert("juchuang_dinosaur_user", $insert_data);
//订单记录
$order = [
"body" => "新用户赠送",
"openid" => $this->fans["openid"],
"total_fee" => 0,
"status" => 1,
"gold" => $gold,
"pay_time" => time(),
"uniacid" => $this->uniacid
];
pdo_insert("juchuang_dinosaur_order", $order);
}
$user = pdo_get("juchuang_dinosaur_user", ["openid" => $this->fans["openid"]]);
//查询排名
$sql = "SELECT COUNT(id) rank FROM " . tablename("juchuang_dinosaur_user") . " WHERE uniacid=" . $this->uniacid . " AND high_score>=" . $user["high_score"];
$rank = pdo_fetch($sql);
$user["rank"] = $rank["rank"];
//获取用户签到数据
$qiandao = pdo_get("juchuang_dinosaur_qiandao", ["uid" => $user["id"]]);
if (!empty($qiandao)) {
//判断今日是否已签到
if ($qiandao["day"] == date("Ymd", time())) {
$qiandao["today"] = true;
} elseif ($qiandao["day"] != date("Ymd", strtotime("-1 day"))) {
$qiandao["today"] = false;
$qiandao["times"] = 0;
} else {
$qiandao["today"] = false;
}
} else {
$qiandao = [
"today" => false,
"times" => 0
];
}
$user["qiandao"] = $qiandao;
return $this->result(0, "", $user);
}
public function doPageSetting() {
$setting = pdo_get("juchuang_dinosaur_setting", ["uniacid" => $this->uniacid]);
$setting["image"] = tomedia($setting["image"]);
$package = pdo_getall("juchuang_dinosaur_package", ["uniacid" => $this->uniacid]);
$data = h_cache_read("payset." . $this->uniacid);
//广告图片链接补全
foreach ($data["ad_diy"] as $k=>$v){
$data["ad_diy"][$k] = tomedia($v);
}
$data["trade_log"] = explode("\r\n", $data["trade_log"]);
$help = h_cache_read("help." . $this->uniacid);
$game = h_cache_read("game." . $this->uniacid);
$qiandao = h_cache_read("qiandao." . $this->uniacid);
$user = pdo_get("juchuang_dinosaur_user", ["openid" => $this->fans["openid"]]);
$user_qiandao = pdo_get("juchuang_dinosaur_qiandao", ["uid" => $user["id"]]);
if (!empty($user_qiandao)) {
//判断今日是否已签到
if ($user_qiandao["day"] == date("Ymd", time())) {
$user_qiandao["today"] = true;
} elseif ($user_qiandao["day"] != date("Ymd", strtotime("-1 day"))) {
$user_qiandao["today"] = false;
$user_qiandao["times"] = 0;
} else {
$user_qiandao["today"] = false;
}
} else {
$user_qiandao = [
"today" => false,
"times" => 0
];
}
if (isset($game["img"])) {
foreach ($game["img"] as $k => $v) {
$game["img"][$k] = tomedia($v);
}
}
$return = [
"share" => $setting,
"package" => $package,
"data" => $data,
"help" => $help["content"],
"game" => $game,
"qiandao" => $qiandao,
"user_qiandao" => $user_qiandao
];
return $this->result(0, "", $return);
}
public function doPageCheck() {
$check = h_cache_read("check." . $this->uniacid);
return $this->result(0, "", $check);
}
public function doPagePay() {
global $_GPC, $_W;
//查询充值套餐
$package_id = $_GPC["id"];
$package = pdo_get("juchuang_dinosaur_package", ["id" => $package_id]);
if ($package) {
$orderid = self::makeOutTradeNo();
$order = [
'tid' => $orderid,
'user' => $_W['openid'],
'fee' => floatval($package["money"]),
'title' => '金币充值',
];
//插入订单
$data = [
"out_trade_no" => $orderid,
"body" => $order["title"],
"openid" => $_W["openid"],
"total_fee" => $order["fee"],
"transaction_id" => "",
"gold" => $package["gold"],
//未支付
"status" => 0,
"uniacid" => $this->uniacid
];
pdo_insert("juchuang_dinosaur_order", $data);
//生成支付参数,返回给小程序端
$pay_params = $this->pay($order);
if (is_error($pay_params)) {
return $this->result(1, '支付失败,请重试');
}
return $this->result(0, [
"gold" => $package["gold"],
"money" => $package["money"]
], $pay_params);
} else {
return $this->result(-1, '充值套餐不存在', $package);
}
}
public function payResult($log) {
/**
* $log数据格式
* array (
* 'weid' => NULL,
* 'uniacid' => '6',
* 'acid' => '6',
* 'result' => 'success',
* 'type' => 'wxapp',
* 'from' => 'notify',
* 'tid' => '80002018072915544489335',
* 'uniontid' => '2018072915544400004194329742',
* 'transaction_id' => NULL,
* 'trade_type' => 'JSAPI',
* 'follow' => 0,
* 'user' => 'owFUr5CMrQxzIcHckaeymME7I3UM',
* 'fee' => '0.01',
* 'tag' =>array (
* 'acid' => '6',
* 'uid' => '3',
* 'transaction_id' => '4200000120201807296150023972',
* ),
* 'is_usecard' => '0',
* 'card_type' => '0',
* 'card_fee' => '0.01',
* 'card_id' => '',
* 'paytime' => 1532850898,
* )
*/
if (isset($log["tid"])) {
$order =