文件
mashangyoujiu/Application/Retail/Controller/WxMembercardController.class.php

382 行
17 KiB
PHP
可执行文件

<?php
namespace Retail\Controller;
use Think\Controller;
//[1.8.50]
class WxMembercardController extends BaseController
{
//获取用户信息
public function storeid()
{
//获取用户信息
$userinfo = session("ls_userinfo");
$info = M('wxconfig')->where(array('merchantid' => $userinfo['merchantid'], 'status' => 1))->find();
if ($info) {
return $info;
} else {
return false;
}
}
//获取token
public function getToken()
{
$info = $this->storeid();
if ($info['type'] == 1) {
$appid = $info['appid'];
$appserect = $info['appsecret'];
//获取token
$json_token = http_request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appserect);
$escape_token = json_decode($json_token);//转义
$token['token'] = $escape_token->{'access_token'};//2个小时换一次,一天2000次
$token['merchantid'] = $info['merchantid'];
file_put_contents('./taoken.txt', $json_token);
return $token;//返回token值
} else if ($info['type'] == 2) {
$tokens = get_authorizer_access_token($info['merchantid']);
$token['token'] = $tokens;//2个小时换一次,一天2000次
$token['merchantid'] = $info['merchantid'];
file_put_contents('./taoken2.txt', json_encode($tokens));
/* echo $token;
exit;*/
return $token;
}
}
//创建会员卡
public function createCard($type)
{
//商户id
$token_arr = $this->getToken();
$merchantid = $token_arr['merchantid'];
$token = $token_arr['token'];
//参数
//服务
$weekk = $type['business_service'];
$week = implode(',', $weekk);
$httpname = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$url = $_SERVER['HTTP_HOST'];
$arr = array(
'card' => array(
'card_type' => 'MEMBER_CARD',
'member_card' => array(
//'background_pic_url'=>'https://mmbiz.qlogo.cn/mmbiz/',//背景图片
'base_info' => array(
'logo_url' => 'http://' . $_SERVER['HTTP_HOST'] . $type['logo_url'],//头像
'brand_name' => $type['brand_name'],//商户名字,字数上限为12个汉字必须
'code_type' => $type['code_type'],//码类型
'title' => $type['title'],//卡券名必须
"color" => "Color010",//颜色必须
"notice" => $type['notice'],//卡券提醒必须
"service_phone" => $type['servoce_phone'],//电话号
"description" => $type['description'],//卡券使用说明必须
"date_info" => array(
"type" => "DATE_TYPE_PERMANENT"//使用日期必须
),
"sku" => array(
"quantity" => $type['quantity']//库存必须
),
"pay_info" => array(
"swipe_card" => array(
"is_swipe_card" => true
)
),
"get_limit" => 1,//每人可领券的数量限制
"use_custom_code" => false,//自定义code
//"get_custom_code_mode"=>"GET_CUSTOM_CODE_MODE_DEPOSIT",//填入该字段后,自定义code卡券方可进行导入code并投放的动作
"can_give_friend" => false,//可以转增
"can_share" => $type['can_share'] == 2 ? true : false, //可不可以分享1不能分享2可以分享
'location_id_list' => false,//门店位置ID。调用 POI门店管理接口 获取门店位置ID。
"custom_url_name" => "我的钱包",
"custom_url" => $httpname . $url . '/index.php/Vip/Index/index.html?merchantid=' . $merchantid,
"need_push_on_view" => true,
),
"advanced_info" => array(
"business_service" => $week//服务
),
"supply_bonus" => true,//显示积分必须
"supply_balance" => false,//储存必须
"prerogative" => $type['prerogative'],//会员卡特权说明必须
"auto_activate" => true,//自动激活
//微信跳转
"custom_field1" => array(
"name" => "余额", //会员信息类目半自定义名称
"url" => $httpname . $url . '/index.php/Vip/Onlinepay/index.html?merchantid=' . $merchantid
),//跳转链接
)
)
);
//生成card_id
$json_arr = json_encode($arr, JSON_UNESCAPED_UNICODE);
$create_card = http_request('https://api.weixin.qq.com/card/create?access_token=' . $token, $json_arr);
$arr_id = json_decode($create_card, true);
$arr_card = $arr_id['card_id'];
if ($arr_id['errcode'] == 0) {
$id = M('wx_membercard')->add($type);
$save = M('wx_membercard')->where(array('id' => $id))->save(array('card_id' => $arr_card));
if ($save) {
return $arr_id;
} else {
return false;
}
} else {
return $arr_id;
}
}
//更新会员信息
public function member_info($arr_card, $custom_num)
{
$token_arr = $this->getToken();
$token = $token_arr['token'];
//参数
$arr = array(
"code" => $custom_num,
"card_id" => $arr_card,
"record_bonus" => "消费十元获得十积分",
"bonus" => 3000,//全量值
//"add_bonus"=>100,//增加或减少
"notify_optional" => array(
"is_notify_bonus" => true
)
);
$json_arr = json_encode($arr, JSON_UNESCAPED_UNICODE);
$success = http_request('https://api.weixin.qq.com/card/membercard/updateuser?access_token=' . $token, $json_arr);
$custom = json_decode($success);
if ($custom->{'errmsg'} == 'ok') {
return true;
} else {
return false;
}
}
//修改卡券
public function modify($data)
{
$da = $this->look_card($data['hi']);
$token_arr = $this->getToken();
$merchantid = $token_arr['merchantid'];
$map['pm.merchant_id'] = $merchantid;
$map['pm.m_status'] = 1;
$map['pm.status'] = 1;
$map['p.status'] = 1;
$map['p.plug_name'] = '积分商城';
$token = $token_arr['token'];
$xcxuser_name = M('appinfo')->where(array('merchant_id' => $merchantid))->getField('user_name');
$plug = M('plug_merchant as pm')
->join('ls_plug as p on p.id=pm.plug_id AND p.type=1')
->where($map)
->find();
if ($plug) {
$openintegral = true;
} else {
$openintegral = false;
}
$httpurl = C("shop_http");
$arr = array(
'card_id' => $da['card_id'],
'member_card' => array(
'base_info' => array(
'logo_url' => $data['logo_url'],
'code_type' => $data['code_type'],//码类型
'notice' => $data['notice'], //展示显示的卡券
'service_phone' => $data['servoce_phone'],
"description" => $data['description'],//卡券使用说明必须
'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享
"custom_url_name" => "会员支付",
"custom_url" => C('shop_http') . '/index.php/Vip/Mycard/dynamic.html?merchantid=' . session('ls_userinfo')['merchantid'],
//"center_title" => "我的钱包",
//"center_url" => C('shop_http') . '/index.php/Vip/Index/index.html?merchantid=' . session('ls_userinfo')['merchantid'],
"promotion_url_name" => "线上商城",
"promotion_app_brand_user_name" => $xcxuser_name . '@app',
"promotion_app_brand_pass" => 'pages/store/store',
"need_push_on_view" => true,
),
//微信跳转
"custom_field1" => array(
"name" => "积分", //会员信息类目半自定义名称
"url" => $httpurl . "/index.php/Vip/Integral/index.html?merchantid=" . $merchantid
),//跳转链接
"custom_field2" => array(
"name" => "余额", //会员信息类目半自定义名称
"url" => $httpurl . '/index.php/Vip/Onlinepay/index.html?merchantid=' . $merchantid
),
"custom_cell1" => array(
"name" => "积分商城",
"url" => $openintegral ? C('shop_http') . "/index.php/Vip/ScoreMall/index.html?merchantid=" . $merchantid : '',
),
"prerogative" => $data['prerogative']//特权说明
)
);
if (!empty($data['logo_bg_url'])) {
$arr['card']['member_card']['background_pic_url'] = $data['logo_bg_url'];
}
$json_arr = json_encode($arr, JSON_UNESCAPED_UNICODE);
$sucess = http_request('https://api.weixin.qq.com/card/update?access_token=' . $token, $json_arr);
$arr_success = json_decode($sucess);
if ($arr_success->{'errmsg'} == 'ok') {
$save = M('wx_membercard')->where(array('id' => $data['hi']))->save($data);
if ($save) {
return true;
} else {
return false;
}
} else {
return false;
}
}
//根据主键id获取卡券信息
public function look_card($id)
{
$content = M('wx_membercard')->where(array('id' => $id))->find();
if ($content) {
return $content;
} else {
return false;
}
}
//删除卡券
public function delete($id)
{
$token_arr = $this->getToken();
$token = $token_arr['token'];
$content = $this->look_card($id);
$card_id = $content['card_id'];
$arr = array(
"card_id" => $card_id
);
$json_arr = json_encode($arr);
$success = http_request('https://api.weixin.qq.com/card/delete?access_token=' . $token, $json_arr);
$success = json_decode($success);
if ($success->{'errmsg'} == 'ok') {
$delete = M('wx_membercard')->where(array('id' => $id))->delete();
if ($delete) {
return true;
}
} else {
return false;
}
}
//创建会员卡 2019-4-22
public function activationceshi($post)
{
//商户id
$token_arr = $this->getToken();
$merchantid = $token_arr['merchantid'];
$token = $token_arr['token'];
$xcxuser_name = M('appinfo')->where(array('merchant_id' => $merchantid))->getField('user_name');
//参数
//服务
$weekk = $post['business_service'];
$week = implode(',', $weekk);
/* $httpname = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$url = $_SERVER['HTTP_HOST'];*/
$httpurl = C('shop_http');
//判断是否开启积分商城
$map['pm.merchant_id'] = $merchantid;
$map['pm.m_status'] = 1;
$map['pm.status'] = 1;
$map['p.status'] = 1;
$map['p.plug_name'] = '积分商城';
$plug = M('plug_merchant as pm')
->join('ls_plug as p on p.id=pm.plug_id AND p.type=1')
->where($map)
->find();
if ($plug) {
$openintegral = true;
} else {
$openintegral = false;
}
$arr = array(
'card' => array(
'card_type' => 'MEMBER_CARD',
'member_card' => array(
"wx_activate" => true, //一键激活
"wx_activate_after_submit" => true,
"wx_activate_after_submit_url" => "https://qq.com",
'base_info' => array(
'logo_url' => $post['logo_url'],//头像
'brand_name' => $post['brand_name'],//商户名字,字数上限为12个汉字必须
'code_type' => $post['code_type'],//码类型
'title' => $post['title'],//卡券名必须
"color" => "Color010",//颜色必须
"notice" => $post['notice'],//卡券提醒必须
"service_phone" => $post['servoce_phone'],//电话号
"description" => $post['description'],//卡券使用说明必须
"date_info" => array(
"type" => "DATE_TYPE_PERMANENT"//使用日期必须
),
"sku" => array(
"quantity" => $post['quantity']//库存必须
),
"pay_info" => array(
"swipe_card" => array(
"is_swipe_card" => true
)
),
"get_limit" => 1,//每人可领券的数量限制
"use_custom_code" => false,//自定义code
//"get_custom_code_mode"=>"GET_CUSTOM_CODE_MODE_DEPOSIT",//填入该字段后,自定义code卡券方可进行导入code并投放的动作
"can_give_friend" => false,//可以转增
"can_share" => $post['can_share'] == 2 ? true : false, //可不可以分享1不能分享2可以分享
'location_id_list' => false,//门店位置ID。调用 POI门店管理接口 获取门店位置ID。
//"center_title" => "我的钱包",
//"center_url" => $httpurl . '/index.php/Vip/Index/index.html?merchantid=' . $merchantid,
"custom_url_name" => "会员支付",
"custom_url" => $httpurl . '/index.php/Vip/Mycard/dynamic.html?merchantid=' . $merchantid,
"promotion_url_name" => "线上商城",
"promotion_app_brand_user_name" => $xcxuser_name . '@app',
"promotion_app_brand_pass" => 'pages/store/store',
"need_push_on_view" => true,
),
"advanced_info" => array(
"business_service" => $week//服务
),
"supply_bonus" => false,//显示积分必须
"supply_balance" => false,//储存必须
"prerogative" => $post['prerogative'],//会员卡特权说明必须
//微信跳转
//微信跳转
"custom_field1" => array(
"name" => "积分", //会员信息类目半自定义名称
"url" => $httpurl . "/index.php/Vip/Integral/index.html?merchantid=" . $merchantid
),//跳转链接
"custom_field2" => array(
"name" => "余额", //会员信息类目半自定义名称
"url" => $httpurl . '/index.php/Vip/Onlinepay/index.html?merchantid=' . $merchantid
),//跳转链接
"custom_cell1" => array(
"name" => "积分商城",
"url" => $openintegral ? $httpurl . "/index.php/Vip/ScoreMall/index.html?merchantid=" . $merchantid : '',
),
)
)
);
if (!empty($post['logo_bg_url'])) {
$arr['card']['member_card']['background_pic_url'] = $post['logo_bg_url'];
}
// print_r($arr);exit;
//生成card_id
$json_arr = json_encode($arr, JSON_UNESCAPED_UNICODE);
$create_card = http_request('https://api.weixin.qq.com/card/create?access_token=' . $token, $json_arr);
$arr_id = json_decode($create_card, true);
$arr_card = $arr_id['card_id'];
if ($arr_id['errcode'] == 0) {
$id = M('wx_membercard')->add($post);
$save = M('wx_membercard')->where(array('id' => $id))->save(array('card_id' => $arr_card));
if ($save) {
return $arr_id;
} else {
return false;
}
} else {
return $arr_id;
}
}
}