Files
liche/home/controllers/h5/Hongbao.php
T
2021-09-01 09:49:40 +08:00

131 lines
5.7 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hongbao extends CI_Controller
{
private $uid = 0;
public function __construct()
{
parent::__construct();
$this->load->model('bobing/bobing_logs_model', 'mdBobingLogs');
}
/**
* Notes:
* https://liche-dev.xiaoyu.com/h5/hongbao
* https://www.liche.cn/h5/hongbao
* Created on: 2021/8/26 11:19
* Created by: dengbw
*/
public function index()
{
$params = $this->input->get();
// $params['uid'] = 14;
// $params['id'] = 828932;
$data = array('code' => 400, 'title' => '领取失败', 'msg' => '');
if (!$params['id'] || !$params['uid']) {
$data['msg'] = '参数错误!';
} else {
$this->uid = intval($params['uid']);
$id = intval($params['id']);
$re_l = $this->mdBobingLogs->get(array('id' => $id));
if (!$re_l || $re_l['status'] == 0 || $re_l['hong_bao'] <= 0) {
$data['msg'] = '条件不符合!';
}
if ($re_l['status'] == 2) {
$data['msg'] = '您已领取过了!';
}
if ($re_l['cf_uid'] != $params['uid']) {
$data['msg'] = 'uid错误!';
}
if (!$data['msg']) {
$url = http_host_com('home') . "/h5/hongbao?id={$id}&uid={$this->uid}";
$params['app_id'] = $re_l['app_id'];
$params['cf_uid'] = $re_l['cf_uid'];
$user = $this->set_auth($url, $params);
//小红榜发红包
if ($user['openid_kzh']) {
$sid = $this->create_order_no(350200);
//企业付款到零钱
//$re_l['hong_bao'] = 0.1;
$this->load->library('Transfers', array('app_id' => 3));//小红榜支付
$result = $this->transfers->sendMoney(array('partner_trade_no' => $sid, 'amount' => $re_l['hong_bao']
, 'openid' => $user['openid_kzh'], 'desc' => '狸车博饼红包', 're_user_name' => $user['nickname']));
$result['log_id'] = $id;
$result['log_uid'] = $this->uid;
debug_log("[info] " . __FUNCTION__ . "# result:" . json_encode($result, JSON_UNESCAPED_UNICODE), 'Hongbao.log');
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {//转帐成功
$this->mdBobingLogs->update(array('status' => 2), array('id' => $id));//设为已领取
$data['title'] = '领取成功';
$data['code'] = 200;
$data['msg'] = '¥' . $re_l['hong_bao'] . '元';
} else {
$data['msg'] = '转帐失败!';
}
}
}
}
$this->load->view('/h5/hongbao/index', $data);
}
/**
* Notes:授权获取openid
* Created on: 2021/8/26 14:26
* Created by: dengbw
* @param string $url
* @param array $params
* @return mixed
*/
private function set_auth($url = '', $params = array())
{
if ("wx" == checkua()) {
$this->load->service("app/user_service", array("app_id" => $params['app_id']));
$re = $this->user_service->get(array('id' => $params['cf_uid']));
if ($re['openid_kzh']) {//已经授权获取openid了
return array('openid_kzh' => $re['openid_kzh'], 'nickname' => $re['nickname']);
}
$this->load->helper('url');
$config['appid'] = 'wx0d3d21aec5001a8e';//小红榜授权
$config['appSecret'] = '41f0721c343e00f284a60332fff991a3';
$code = $params['code'];
$auth = $params['auth'];//是否信息授权
$auth = 0;
$auth && $url .= "&auth={$auth}";
if ($code) {//授权码获取微信信息
$auth_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$config['appid']}&secret={$config['appSecret']}&code={$code}&grant_type=authorization_code";
$res = file_get_contents($auth_url);
$ret = json_decode($res, true);
$openid = $ret['openid'];
$unionid = $ret['unionid'];
if ($openid) {
$this->user_service->update(array('openid_kzh' => $openid), array('id' => $params['cf_uid']));
return array('openid_kzh' => $openid, 'nickname' => $re['nickname']);
}
} elseif ($auth) {//信息授权获取用户微信昵称/头像
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
redirect($auth_url);
} else {//静默授权获取用户openid
$redirect_uri = urlencode($url);
$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['appid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
redirect($auth_url);
}
}
}
private function create_order_no($city_id = 350200, $source = '', $pay_type = 1, $order_type = 1)
{
$source_id = sprintf("%02d", 0);
return $city_id . $source_id . $pay_type . $order_type . date('Ymd') . sprintf("%06d", rand(1, 999999));
}
private function show_json($code, $msg)
{
$data['code'] = $code;
$data['msg'] = $msg;
die(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}