<?php
if (!defined('ROCKPHP')) die('Access Denied');
class ShowAction extends BaseAction
{
public function _initialize()
{
// // dump(ACTION_NAME);
// if (mb_strpos(ACTION_NAME, '_') !== false) {
// $id = intval($_REQUEST['id']);
// $id || die;
// $this->assign('id', $id);
// $this->display();
// }
$this->assign('getip', getIp());
$menuon = '';
if (mb_strpos(ACTION_NAME, 'p_') === 0) {
$menuon = 'product';
} else if (mb_strpos(ACTION_NAME, 't_') === 0) {
$menuon = 'travel';
} else if (mb_strpos(ACTION_NAME, 'c_') === 0) {
$menuon = 'car';
}
$this->assign('menuon', $menuon);
}
public function index()
{
die;
}
// 通用方法
// function c_rent
// function p_chanpin
// function t_diy_order
// function t_diy
// function t_lists
// function t_guide
public function _empty($method, $args)
{
if (!file_exists(THEME_PATH . MODULE_NAME . '_' . $method . '.html')) {
_404(L('_ERROR_ACTION_') . ':' . ACTION_NAME);
}
$id = intval($_REQUEST['id']);
$id || die;
$this->assign('id', $id);
if ($method == 't_diy') {
$data['con'] = '线路详情';
$data['p_con'] = '费用说明';
$data['n_con'] = '注意事项';
$this->assign('data', $data);
}
$catid = intval($_REQUEST['catid']);
if ($catid) {
$cat_info = M('category')->where(array('id' => $catid))->find();
$cat = M('category')->where(array('id' => $cat_info['parentid']))->find();
// dump($cat);
$this->assign('cat', $cat);
}
if ($method == 'p_chanpin') {
session('buy', null);
// dump($_SESSION);
$shopcar = session('shopcar');
$shopcar = is_array($shopcar) ? $shopcar : array();
$this->assign('scc', count($shopcar));
}
if ($method == 't_diy_order') {
$info = M('t_diy')->where(['id' => $id])->find();
$this->assign('info', $info);
}
$this->display();
}
// 首页
public function home()
{
// 轮播图
$banner = M('slide_data')->where(array('fid' => 2))->select();
$banner = is_array($banner) ? $banner : array();
$banner = count($banner) ? $banner : array('');
$this->assign('banner', $banner);
$this->display();
}
// 优选特产
public function product()
{
// 轮播图
$banner = M('slide_data')->where(array('fid' => 3))->select();
$banner = is_array($banner) ? $banner : array();
$banner = count($banner) ? $banner : array('');
$this->assign('banner', $banner);
$catid = intval($_REQUEST['catid']);
$this->assign('catid', $catid);
session('buy', null);
$this->display();
}
// 优选特产 订单查询
public function p_olists()
{
$this->assign('id', 1);
$this->display();
}
// 优选特产 订单支付成功
public function p_odone()
{
// $this->assign('id', 1);
// $this->assign('menuon', 'product');
$this->assign('sys_phone', db_config('sys_phone'));
$id = intval($_REQUEST['id']);
$this->assign('id', $id);
$this->display();
}
// 旅游服务
public function travel()
{
// 轮播图
$banner = M('slide_data')->where(array('fid' => 4))->select();
$banner = is_array($banner) ? $banner : array();
$banner = count($banner) ? $banner : array('');
$this->assign('banner', $banner);
$type_lists = array(
't_diy' => '线路定制',
't_lists' => '景点展示',
't_guide' => '旅游指南',
);
$this->assign('type_lists', $type_lists);
$type_item = trim($_REQUEST['type_item']);
reset($type_lists);
$type_item = $type_item ? $type_item : key($type_lists);
$this->assign('type_item', $type_item);
$this->display();
}
// 租车服务
public function car()
{
// 轮播图
$banner = M('slide_data')->where(array('fid' => 5))->select();
$banner = is_array($banner) ? $banner : array();
$banner = count($banner) ? $banner : array('');
$this->assign('banner', $banner);
$this->display();
}
// 便民服务
public function cs()
{
// 轮播图
$banner = M('slide_data')->where(array('fid' => 6))->select();
$banner = is_array($banner) ? $banner : array();
$banner = count($banner) ? $banner : array('');
$this->assign('banner', $banner);
$type_lists = array(
'周边医院',
'周边超市',
'周边银行',
'周边药店',
'周边小吃',
);
$this->assign('type_lists', $type_lists);
$type_item = trim($_REQUEST['type_item']);
reset($type_lists);
$type_item = $type_item ? $type_item : key($type_lists);
$this->assign('type_item', $type_item);
$this->display();
}
// 删除选中的产品
public function todel()
{
// dump($_SESSION);
$pid = intval($_REQUEST['pid']);
$ggid = intval($_REQUEST['ggid']);
$type = trim($_REQUEST['type']);
$type_str = $type ? 'buy' : 'shopcar';
$p_chanpin = session($type_str);
$p_chanpin = is_array($p_chanpin) ? $p_chanpin : array();
unset($p_chanpin[$pid . ':' . $ggid]);
session($type_str, $p_chanpin);
$res['status'] = 200;
die(json_encode($res));
}
// 购物车和结算列表中写入
public function p_order_to()
{
$res['status'] = 301;
$id = intval($_REQUEST['id']);
$id || die(json_encode($res));
// dump($id);
$data = json2arr($_REQUEST['data']);
count($data) || die(json_encode($res));
// dump($data);
$type = trim($_REQUEST['type']);
$type_str = $type ? 'buy' : 'shopcar';
// $_SESSION[$id][$data['guige']] = $data;
// // dump($_SESSION);
// // die;
// $p_chanpin = session('p_chanpin');
$p_chanpin = session($type_str);
$p_chanpin = is_array($p_chanpin) ? $p_chanpin : array();
// $p_chanpin[$id][$data['guige']] = $data;
if ($type_str == 'shopcar') {
// file_put_contents('test.log', date(DATE_RFC3339) . '=p_order_to=data=' . print_r($data, true) . "\n", FILE_APPEND);
if (isset($p_chanpin[$id . ':' . $data['guige']])) {
$temp = $p_chanpin[$id . ':' . $data['guige']];
$data['num'] += $temp['num'];
// file_put_contents('test.log', date(DATE_RFC3339) . '=p_order_to=temp=' . print_r($temp, true) . "\n", FILE_APPEND);
}
}
$p_chanpin[$id . ':' . $data['guige']] = $data;
// session('p_chanpin', $p_chanpin);
session($type_str, $p_chanpin);
$res['status'] = 200;
die(json_encode($res));
}
// 购物车和结算页展示
public function p_order()
{
// dump($_SESSION);
$type = trim($_REQUEST['type']);
$this->assign('type', $type);
$type_str = $type ? 'buy' : 'shopcar';
// dump($_SESSION);
// dump(session('p_chanpin'));
$lists = array();
// $p_chanpin = session('p_chanpin');
$p_chanpin = session($type_str);
// dump($p_chanpin);
$lists = $this->p_info($p_chanpin);
// dump($p_chanpin);
// dump($lists);
// die;
$this->assign('lists', $lists);
$province = $this->to_area(0);
$this->assign('province', $province['lists']);
$this->display();
}
// 跳转到结算页
public function p_order_all()
{
$data = $this->p_data();
session('buy', null);
session('buy', $data);
$res['status'] = 401;
$res['url'] = U('Show/p_order') . '&type=scp';
die(json_encode($res));
}
// 产品创建订单
public function p_order_done()
{
$data = $this->p_data();
$data = $this->p_info($data);
$peisongs = intval($_REQUEST['peisongs']);
$type_get = trim($_REQUEST['type_get']);
$userid = $type_get == 'ed' ? get_uid() : 1;
// $userid = get_uid();
$name = trim($_REQUEST['name']);
$phone = trim($_REQUEST['phone']);
$province = intval($_REQUEST['province']);
$city = intval($_REQUEST['city']);
$area = intval($_REQUEST['area']);
$address = trim($_REQUEST['address']);
// dump($userid);
if ($type_get != 'ed') {
$name = '智能终端';
$address = '智能货柜取货';
}
$sum_price = 0;
foreach ($data as $key => &$value) {
$sum_price += $value['arr']['price'];
}
$order['sn'] = build_order_no();
$order['userid'] = $userid;
$order['consignee'] = $name;
$order['mobile'] = $phone;
$order['province'] = $province;
$order['city'] = $city;
$order['area'] = $area;
$order['address'] = $address;
$order['type'] = 1;
$order['pay_fee'] = $sum_price;
$order['order_amount'] = $sum_price;
$order['add_time'] = time();
// $order['discount'] = $youhui;
// $order['yhqid'] = $shiyongyhqid;
// $order['pstype'] = $sendtype;
$order['peisongs'] = $peisongs;
// $order['postmessage'] = $beizhu;
$order['type_get'] = $type_get;
$doing = M('order')->add($order);
// Log::write('edit=' . print_r($edit, true), Log::INFO);
// Log::write('edit=' . print_r(M()->getLastSql(), true), Log::SQL);
// 判断订单是否添加失败
if (!$doing) {
// 如果添加失败阻止继续执行
$res['code'] = 300;
die(json_encode($res));
}
$order_amount = 0;
$productInfo = array();
foreach ($data as $key => &$value) {
$productInfo[$key] = array();
$productInfo[$key]['userid'] = $userid;
$productInfo[$key]['order_id'] = $doing;
$productInfo[$key]['product_id'] = $value['chanpin']['id'];
$productInfo[$key]['ggid'] = $value['guige']['id'];
$productInfo[$key]['product_thumb'] = $value['chanpin']['thumb'];
// if ($value['chanpin']['title'] != '') {
$productInfo[$key]['product_name'] = $value['chanpin']['title'] . ' ' . $value['guige']['name'];
// } else {
// $productInfo[$key]['product_name'] = $value['chanpin']['shopname'];
// }
$productInfo[$key]['product_price'] = $value['guige']['price'];
// if ($youhui) {
// $productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']) - $youhui;
// }
// $productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']);
$productInfo[$key]['number'] = $value['arr']['num'];
$value['leixin'] = 1;
$productInfo[$key]['type'] = $value['leixin'];
$shangpinid = $value['chanpin']['id'];
// 查找商家id
$model = hqLeixingYu($value['leixin']);
$shops_id = M($model)->where(array('id' => $shangpinid))->find();
if ($value['leixin'] == 1) {
if ($shops_id['business_id'] > 0) {
$shops_idone = $shops_id['business_id'];
} else {
$shops_idone = $shops_id['userid'];
}
} else {
$shops_idone = $shops_id['userid'];
}
$productInfo[$key]['shops_id'] = $shops_idone;
// 优选特产直配区别
if ($value['leixin'] == 1) {
if ($value['business_id'] == 0) {
$user_groupid = get_groupid($value['userids']);
if ($user_groupid == 1) {
$productInfo[$key]['zyzptype'] = 1;
// $zongprice = $value['guige']['price'] * $value['arr']['num'];
// if ($shiyongyhqid == 1 && $isyh == 0 && $zongprice > $youhui) {
// $productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']) - $youhui;
// $isyh = 1;
$productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']);
// } else {
// }
// 如果有配送费减去配送费
if ($peisongs > 0 && $isps == 0) {
// $productInfo[$key]['price'] = $productInfo[$key]['price'] + $peisongs;
$isps = 1;
}
} else {
$productInfo[$key]['zyzptype'] = 2;
$productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']);
}
} else {
$productInfo[$key]['zyzptype'] = 2;
$productInfo[$key]['price'] = ($value['guige']['price'] * $value['arr']['num']);
}
} else {
$productInfo[$key]['price'] = $value['guige']['price'] * $value['arr']['num'];
}
$productInfo[$key]['zyzptype'] = 1;
$order_amount += $productInfo[$key]['price'];
// $gouwuche = M('Gouwuche')->where(array('userid' => $userid, 'chanpin_id' => $shangpinid))->delete();
// Log::write('gouwuche=' . print_r($gouwuche, true), Log::INFO);
// Log::write('gouwuche=' . print_r(M()->getLastSql(), true), Log::SQL);
$order_data = M('Order_data')->add($productInfo[$key]);
// Log::write('order_data=' . print_r($order_data, true), Log::INFO);
// Log::write('order_data=' . print_r(M()->getLastSql(), true), Log::SQL);
}
$res['url'] = U('Show/to_wxpay') . '&id=' . $doing;
$res['doing'] = $doing;
$res['wxpay'] = $this->to_wxpay($doing);
$res['status'] = 200;
die(json_encode($res));
}
// 获取地址列表
public function to_area($parentid = '')
{
$lists = array();
$l_first['id'] = '';
$l_first['name'] = '请选择';
$res['status'] = 200;
$res['lists'] = &$lists;
// $name = $name ? $name : trim($_REQUEST['name']);
$parentid = is_numeric($parentid) ? $parentid : trim($_REQUEST['parentid']);
$parentid = is_numeric($parentid) ? intval($parentid) : $parentid;
// if (!$name) {
// if (IS_AJAX) {
// die(json_encode($res));
// }
// return $res;
// }
if (!is_numeric($parentid)) {
array_unshift($lists, $l_first);
if (IS_AJAX) {
die(json_encode($res));
}
return $res;
}
// $res['name'] = $name;
$lists = M('area')
->where(array('parentid' => $parentid))
->order('id')
->select();
// dump(M()->getLastSql());
$lists = is_array($lists) ? $lists : array();
array_unshift($lists, $l_first);
if (IS_AJAX) {
die(json_encode($res));
}
return $res;
}
// 获取产品和规格信息
private function p_info($p_chanpin)
{
$lists = array();
// foreach ($p_chanpin as $key => &$value) {
// foreach ($value as $key2 => &$value2) {
// $lists_vo = array();
// $chanpin = M('chanpin')->where(array('id' => $value2['id']))->find();
// $label = &$chanpin['label'];
// $label = explode("\n", $label);
// $label = preg_replace('/\s+/i', '', $label);
// $lists_vo['chanpin'] = $chanpin;
// $guige = M('guige')->where(array('id' => $value2['guige']))->find();
// $lists_vo['guige'] = $guige;
// $value2['price'] = $value2['num'] * $guige['price'];
// $value2['price'] = sprintf('%.2f', $value2['price']);
// $lists_vo['arr'] = $value2;
// $lists[] = $lists_vo;
// }
// }
foreach ($p_chanpin as $key => &$value) {
$lists_vo = array();
$chanpin = M('chanpin')->where(array('id' => $value['id']))->find();
$label = &$chanpin['label'];
$label = explode("\n", $label);
$label = preg_replace('/\s+/i', '', $label);
$lists_vo['chanpin'] = $chanpin;
$guige = M('guige')->where(array('id' => $value['guige']))->find();
$lists_vo['guige'] = $guige;
$value['price'] = $value['num'] * $guige['price'];
$value['price'] = sprintf('%.2f', $value['price']);
$lists_vo['arr'] = $value;
$lists[] = $lists_vo;
}
return $lists;
}
// 产品和规格信息处理为可以识别的数组
private function p_data()
{
$data = array();
$temp = array();
$arr = $_REQUEST;
foreach ($arr as $key => &$value) {
if (!is_array($value)) {
continue;
}
foreach ($value as $key2 => &$value2) {
$temp[$key2][$key] = $value2;
}
}
foreach ($temp as $key => &$value) {
$value = $value['item'] ? $value : null;
}
$temp = array_filter($temp);
// dump($temp);
if (!count($temp)) {
$res['status'] = 302;
$res['msg'] = '请选择产品';
die(json_encode($res));
}
foreach ($temp as $key => &$value) {
// $data[$value['id']][$value['guige']] = $value;
$data[$value['id'] . ':' . $value['guige']] = $value;
}
return $data;
}
// 查询是否支付成功
public function pay_success()
{
$res['status'] = 301;
$oid = intval($_REQUEST['oid']);
$oid || die(json_encode($res));
$pay_return = M('order')->where(array('id' => $oid))->getField('pay_return');
$info = $pay_return ? 'info' : '';
$res['info'] = $info;
$res['oid'] = $oid;
$res['status'] = 200;
die(json_encode($res));
}
// 微信支付 通知
public function wxpay_notify()
{
file_put_contents('order.log', date(DATE_RFC3339) . ' wxpay_notify ' . print_r(1111, true) . "\n\n", FILE_APPEND);
$input = file_get_contents('php://input');
file_put_contents('order.log', date(DATE_RFC3339) . ' wxpay_notify input ' . print_r($input, true) . "\n\n", FILE_APPEND);
// file_put_contents('order.log', date(DATE_RFC3339) . ' wxpay_notify GLOBALS HTTP_RAW_POST_DATA '
// . print_r($GLOBALS['HTTP_RAW_POST_DATA'], true) . "\n\n", FILE_APPEND);
$input_arr = $this->fromXml($input);
if (
$input_arr['result_code'] == 'SUCCESS'
&& $input_arr['return_code'] == 'SUCCESS'
) {
file_put_contents('order.log', date(DATE_RFC3339) . ' wxpay_notify SUCCESS ' . print_r(2222, true) . "\n\n", FILE_APPEND);
$order = M('order')->where(array('sn' => $input_arr['out_trade_no']))->find();
$type_get = $order['type_get'];
if ($type_get == 'ed') {
$user = M('user')->where(array('wx_open_id' => $input_arr['openid']))->find();
$address = M('address')->where(array('userid' => $user['id']))->order('`default` desc')->find();
if (
is_array($user) && count($user)
&& !$order['userid']
) {
$data['userid'] = $user['id'];
$data['consignee'] = $user['realname'];
$data['mobile'] = $user['mobile'];
}
if (
is_array($address) && count($address)
&& !$order['province']
&& !$order['address']
) {
$data['province'] = $address['province'];
$data['city'] = $address['city'];
$data['area'] = $address['area'];
$data['address'] = $address['address'];
}
}
$data['status'] = 1;
if ($type_get == 'scp') {
$data['status'] = 3;
}
$data['pay_id'] = 5;
$data['pay_name'] = '微信支付';
$data['pay_code'] = 'Wxpay';
$data['pay_time'] = time();
$data['pay_status'] = 1;
$data['pay_return'] = $input;
$doing = M('order')->where(array('sn' => $input_arr['out_trade_no']))->save($data);
$jifen = get_jifen($order['order_amount']);
M('user')->where(['wx_open_id' => $order['userid']])->setInc('all_point', $jifen);
if ($doing && $type_get == 'scp') {
$obox = M('order')->where(['id' => $order['id']])->getField('obox');
if ($obox) {
return false;
}
$order_data = M('order_data')->where(array('order_id' => $order['id']))->getField('product_id', true);
$order_data[] = 0;
$where = array();
$where['id'] = array('in', $order_data);
$chanpin = M('chanpin')->where($where)->select();
foreach ($chanpin as $key => &$value) {
if (
!$value['box_imei'] ||
!$value['box_no']
) {
continue;
}
$url_arr = array();
$url_arr['type'] = 'OpenPort';
$url_arr['imei'] = $value['box_imei'];
$url_arr['channel'] = $value['box_no'];
httpRequest('http://xpmall.qhjurenwl.com/index.php?m=Mp&a=mpub&' . http_build_query($url_arr));
}
M('order')->where(['id' => $order['id']])->setField('obox', 1);
}
}
}
// 微信支付
public function to_wxpay($id)
{
$img = '';
if (!$id) {
return $img;
}
$info = M('order')->where(array('id' => $id))->find();
$name_arr = M('order_data')->where(array('order_id' => $id))->getField('product_name', true);
$name_arr = array_filter($name_arr);
$name = implode(',', $name_arr);
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$pay_config['appid'] = C('APP_ID');
$pay_config['mch_id'] = C('MCH_ID');
$pay_config['nonce_str'] = $this->randomString(32);
$pay_config['out_trade_no'] = $info['sn'];
// 描述
$pay_config['body'] = '在线购物 ' . $name;
// 金额
$pay_config['total_fee'] = $info['order_amount'] * 100;
$pay_config['total_fee'] = 1;
$pay_config['spbill_create_ip'] = getIp();
// 通知url
$pay_config['notify_url'] = scheme() . '://' . $_SERVER['HTTP_HOST'] . '/Show/wxpay_notify';
$pay_config['trade_type'] = 'NATIVE';
$pay_config['sign'] = $this->to_sign($pay_config);
// dump($pay_config);
// file_put_contents('order.log', date(DATE_RFC3339) . ' to_wxpay pay_config ' . print_r($pay_config, true) . "\n\n", FILE_APPEND);
$xmlData = $this->toXml($pay_config);
file_put_contents('order.log', date(DATE_RFC3339) . ' to_wxpay xmlData ' . print_r($xmlData, true) . "\n\n", FILE_APPEND);
M('order')->where(array('id' => $id))->setField('pay_data', $xmlData);
$response = $this->requestXml($url, $xmlData);
// dump($response);
file_put_contents('order.log', date(DATE_RFC3339) . ' to_wxpay response ' . print_r($response, true) . "\n\n", FILE_APPEND);
M('order')->where(array('id' => $id))->setField('pay_response', $response['info']);
$rp_arr = $this->fromXml($response['info']);
ob_start();
vendor('phpqrcode.phpqrcode');
$data = $rp_arr['code_url'];
$level = 'L';
$size = 4;
echo QRcode::png($data, false, $level, $size, 0);
$qr = ob_get_contents();
ob_end_clean();
$img = qr_edit($qr, './Public/pad/34.png', 36, 36, 292, 292);
// header('Content-Type: text/html; charset=utf-8');
// $this->assign('img', $img);
// $this->display();
return $img;
}
// 生成随机字符串
private function randomString($len = 32)
{
$string = '';
$char = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
for ($i = 0; $i < $len; $i++) {
$string .= $char[mt_rand(0, strlen($char) - 1)];
}
return $string;
}
// 生成签名
private function to_sign($pay_arr)
{
ksort($pay_arr);
$pay_arr['key'] = C('MCH_KEY');
$pay_str = http_build_query($pay_arr);
$pay_str = urldecode($pay_str);
$sign = md5($pay_str);
$sign = strtoupper($sign);
return $sign;
}
// 转换为xml格式
private function toXml($array)
{
$xml = '<xml>';
foreach ($array as $key => $value) {
if (is_numeric($value)) {
$xml .= '<' . $key . '>' . $value . '</' . $key . '>' . "\n";
} else {
$xml .= '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>' . "\n";
}
}
$xml .= '</xml>';
return $xml;
}
// 从xml格式转换
private function fromXml($xml)
{
$xml = str_replace('<![CDATA]>', '', $xml);
$xml = str_replace('&', '{xml}', $xml);
// libxml_disable_entity_loader(true);
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$xml = json_decode(json_encode($xml), true);
$xml = str_replace('{xml}', '&', $xml);
return $xml;
}
// 获取xml参数的接口返回值
private function requestXml($url, $xmlData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/xml; charset=utf-8'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
$result['info'] = curl_exec($ch);
$result['status'] = curl_error($ch);
curl_close($ch);
return $result;
}
// 写入日志
public function to_log()
{
file_put_contents('./request.log', date(DATE_RFC3339) . '=to_log=_REQUEST=' .
print_r($_REQUEST, true) . "\n", FILE_APPEND);
// $res['status'] = 200;
// die(json_encode($res));
}
// 清除日志
public function clean_log()
{
file_put_contents('order.log', '');
}
}
PHP购物车功能和微信支付
最新推荐文章于 2023-06-20 16:25:28 发布