文件
mashangyoujiu/Application/SmallProgramApi/Controller/VipController.class.php

1215 行
48 KiB
PHP
可执行文件

<?php
namespace SmallProgramApi\Controller;
use Common\Model\InstallAccountModel;
use Common\Model\RetailMessageModel;
use Common\Model\RetailPlusVipModel;
use Common\Model\RetailSmallProgramUserModel;
use Common\Model\RetailStoreModel;
use Common\Model\RetailVipModel;
use LittleGift\Business\Vip;
//[1.8.50]
class VipController extends BaseController
{
public function _initialize()
{
parent::_initialize();
$path = CONTROLLER_NAME . '/' . ACTION_NAME;
if (!in_array($path, array('Vip/getPhone'))) {
if (empty(self::$store_id)) {
$res['status'] = 0;
$res['msg'] = '缺少门店参数';
$this->ajaxReturn($res);
}
if (empty(self::$uid) || self::$user['login_state'] != 1) {
$res['status'] = -1;
$res['msg'] = '用户未登录';
$this->ajaxReturn($res);
}
}
}
/*
* 获取用户绑定的会员卡
*/
public function defaultCard()
{
if (IS_POST) {
$uid = self::$uid; //用户id
$merchant_id = self::$merchant_id;
$RSPUModel = D('RetailSmallProgramUser');
$where[$RSPUModel::MERCHANT_ID] = $merchant_id; //商户id
$user = $RSPUModel->getFind(array($RSPUModel::ID => $uid));
if ($user[$RSPUModel::VID]) {
//绑定了会员卡
$VipModel = D('RetailVip');
$card = $VipModel->getFind(array($VipModel::ID => $user[$RSPUModel::VID]));
$PlusVipModel = D('RetailPlusVip');
$end_time = $PlusVipModel->getFindField(array($PlusVipModel::VIP_ID => $user[$RSPUModel::VID]), $PlusVipModel::END_TIME);
if (!empty($card) && $card[$VipModel::STATUS] == 1) {
$VipIntegralModel = D('RetailVipIntegral');
$vip_card = $VipIntegralModel->getOrderFind(array($VipIntegralModel::MERCHANT_ID => $merchant_id, $VipIntegralModel::MIN_INTEGRAL => array('ELT', $card[$VipModel::GRADE_INTEGRAL])), '*', $VipIntegralModel::MIN_INTEGRAL . ' desc');
$map = array(
$VipIntegralModel::MERCHANT_ID => $merchant_id,
$VipIntegralModel::DISCOUNT => array('gt', 0)
);
if ($user['user_type'] == 2) {
//plus会员
$grade = $VipIntegralModel->getOrderFind($map, '*', $VipIntegralModel::DISCOUNT . ' asc');
$info['grade_name'] = 'PLUS会员';
} else {
//常规会员
$map[$VipIntegralModel::MIN_INTEGRAL] = array('ELT', $card[$VipModel::GRADE_INTEGRAL]);
$grade = $VipIntegralModel->getOrderFind($map, '*', $VipIntegralModel::MIN_INTEGRAL . ' desc');
if (empty($grade[$VipIntegralModel::CARD_NAME])) {
$grade[$VipIntegralModel::CARD_NAME] = '常规会员';
$grade['is_set_grade_level'] = 1; //商户未设置会员等级
$card['grade_integral'] = 0;
}
$info['grade_name'] = $grade[$VipIntegralModel::CARD_NAME];
}
if (!$grade['is_set_grade_level']) {
$next_grade = $VipIntegralModel->getVipNextGradeInfo($merchant_id, $card['grade_integral']);
}
$info['integral'] = $card[$VipModel::INTEGRAL];
$info['card_name'] = $vip_card[$VipIntegralModel::CARD_NAME] ? $vip_card[$VipIntegralModel::CARD_NAME] : '默认会员';
$info['dis'] = $vip_card[$VipIntegralModel::DISCOUNT] ? $vip_card[$VipIntegralModel::DISCOUNT] : '1';
$info['vip_id'] = $card[$VipModel::ID];
$info['balance'] = $card[$VipModel::BALANCE];
$info['plus_type'] = $card['plus_type'];
$info['card_num'] = $card[$VipModel::MOBILE_PHONE] ? $card[$VipModel::MOBILE_PHONE] : '未绑定手机号';
$info['nickname'] = $user[$RSPUModel::NICKNAME];
$info['avatar'] = $user[$RSPUModel::AVATAR];
$info['v_openid'] = $card[$VipModel::OPEN_ID];
$info['grade_integral'] = $card['grade_integral'];
$info['plus_id'] = $card['plus_id'];
if ($grade['is_set_grade_level']) {
$info['next_grade_integral'] = 0;
} else {
$info['next_grade_integral'] = empty($next_grade['min_integral']) ? $grade['min_integral'] : $next_grade['min_integral'];
}
$info['plus_day'] = date('Y-m-d', $end_time);
$res['status'] = 1;
$res['data'] = $info;
} else if ($card[$VipModel::STATUS] == 3) {
$res['status'] = 0;
$res['msg'] = '会员卡已被冻结';
} else if ($card[$VipModel::STATUS] == 4) {
$res['status'] = 0;
$res['msg'] = '会员卡已挂失';
} else {
$res['status'] = 0;
$res['msg'] = '会员卡已被删除';
}
} else {
$res['status'] = 0;
$res['msg'] = '暂未绑定会员卡';
}
$this->ajaxReturn($res);
}
}
/*
* 余额记录
*/
public function BalanceRecord()
{
$type = I('post.type');
$page = I('post.page');
if (empty($type) || empty($page)) {
$result['status'] = 0;
$result['msg'] = '参数有误';
$this->ajaxReturn($result);
}
$RSPUModel = D('RetailSmallProgramUser');
$user = self::$user;
if (empty($user[$RSPUModel::VID])) {
$result['status'] = 0;
$result['msg'] = '您不是会员';
$this->ajaxReturn($result);
}
$ROModel = D('RetailRechargeOrder');
$StoreModel = D('RetailStore');
switch ($type) {
case 1:
//所有
break;
case 2:
//充值
$map['r.' . $ROModel::TYPE] = array('in', '1,3,5,6,7,8');
break;
case 3:
//消费
$map['r.' . $ROModel::TYPE] = array('in', '2,4');
break;
}
$map['r.' . $ROModel::STATUS] = 1;
$map['r.' . $ROModel::VID] = $user[$RSPUModel::VID];
$count = 15;
$order = $ROModel->getBalanceRecord($map, 'r.*,s.' . $StoreModel::STORE_NAME . ' as store_name', $ROModel::ADD_TIME . ' desc', $page . ',' . $count);
if (!empty($order)) {
foreach ($order as &$val) {
$val['add_time'] = date('Y-m-d H:i:s', $val[$ROModel::ADD_TIME]);
switch ($val[$ROModel::TYPE]) {
case '1':
case '3':
case '5':
case '6':
case '7':
case '8':
//充值
$val['type_name'] = '充值';
break;
case '2':
case '4':
//消费
$val['type_name'] = '消费';
break;
case '9':
$val['type_name'] = '充值退款';
break;
}
}
$result['count'] = $count;
$result['data'] = $order;
$result['status'] = 1;
$result['msg'] = '查询成功';
} else {
$result['status'] = 0;
$result['msg'] = '暂无记录';
}
$this->ajaxReturn($result);
}
/**
* 余额记录分期到账信息显示
*/
public function getInstallData()
{
$installAccountModel = new InstallAccountModel();
$RSPUModel = D('RetailSmallProgramUser');
$ROModel = D('RetailRechargeOrder');
$id = I('post.id');
$user = self::$user;
$data = $ROModel->getSelect(array('id' => $id));
$install_data = $installAccountModel->getSelect(array('vip_id' => $user[$RSPUModel::VID], 'order_id' => $id, 'state' => 0));
if (!empty($data)) {
foreach ($data as $key => &$value) {
$value['pay_time'] = date('Y-m-d H:i:s', $value['add_time']);
$value['give_money'] = $value['all_money'] - $value['money'];
}
foreach ($install_data as $k => $val) {
$val['upgrade_name'] = $val['is_upgrade'] == 0 ? '未到账' : '已到账';
$install_data[$k] = $val;
}
$result['install_data'] = $install_data;
$result['direct_data'] = $data;
$result['install_name'] = $data[0]['repay_name'] == 1 ? '分期到账' : '直接到账';
$result['status'] = 1;
} else {
$result['status'] = 0;
$result['msg'] = '暂无记录';
}
$this->ajaxReturn($result);
}
/*
* 会员卡优惠券
*/
public function getCouponData()
{
if (IS_POST) {
$page = I('post.page');
$type = I('post.use_type');
$SPUModel = D('RetailSmallProgramUser');
$user = self::$user;
if (empty($user[$SPUModel::VID])) {
$result['status'] = 0;
$result['msg'] = '您未绑定会员卡';
$this->ajaxReturn($result);
}
$vid = $user[$SPUModel::VID];
$limit = 10;
$time = time();
$merchant_id = self::$merchant_id;
$VipMCoupon = D('RetailVipMCoupon');
$map = array(
$VipMCoupon::STATUS => 2,
$VipMCoupon::VID => $vid,
$VipMCoupon::MERCHANT_ID => $merchant_id
);
if ($type == 1) {
// 可用优惠券
// 未进行的优惠券
$map[$VipMCoupon::IS_USE] = 1;
$map[$VipMCoupon::END_TIME] = array('gt', $time);
} else if ($type == 2) {
$map[$VipMCoupon::IS_USE] = 2;
} else if ($type == 3) {
$map['_string'] = "(" . $VipMCoupon::END_TIME . " < {$time} OR " . $VipMCoupon::IS_USE . " = 3)";
}
$count = $VipMCoupon->getCount($map);
$list = $VipMCoupon->getSelect($map, '*', '', $page . ',' . $limit);
foreach ($list as $k => &$v) {
$MCouponModel = D('RetailMCoupon');
$v['coupon_name'] = $MCouponModel->getFindField(array($MCouponModel::ID => $v[$VipMCoupon::MCOUPON_ID]), $MCouponModel::COUPON_NAME);
$v['add_time'] = date('Y-m-d H:i:s', $v[$VipMCoupon::ADD_TIME]);
if ($type == 1) {
$v['time'] = '有效期:' . date('Y-m-d H:i:s', $v[$VipMCoupon::START_TIME]) . ' - ' . date('Y-m-d H:i:s', $v[$VipMCoupon::END_TIME]);
} else if ($type == 2) {
$v['time'] = '使用时间:' . date('Y-m-d H:i:s', $v[$VipMCoupon::USE_TIME]);
} else if ($type == 3) {
$v['time'] = '过期时间:' . date('Y-m-d H:i:s', $v[$VipMCoupon::END_TIME]);
} else {
$v['time'] = '';
}
}
if ($list) {
$result['status'] = 1;
$result['page'] = $page + 1;
$result['count'] = $count;
$result['data'] = $list;
} else {
$result['status'] = 0;
$result['msg'] = '没有更多了';
$result['page'] = $page + 1;
$result['count'] = $count;
$result['data'] = $list;
}
$this->ajaxReturn($result);
}
}
/**
* 查看优惠券详情
*/
public function getCouponDetail()
{
$coupon_id = I('post.coupon_id');
if (empty($coupon_id)) {
$res['status'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$SPUModel = D('RetailSmallProgramUser');
$user = self::$user;
if (empty($user[$SPUModel::VID])) {
$result['status'] = 0;
$result['msg'] = '您未绑定会员卡';
$this->ajaxReturn($result);
}
$vid = $user[$SPUModel::VID];
$merchant_id = self::$merchant_id;
$VipMCouponModel = D('RetailVipMCoupon');
$map = array(
$VipMCouponModel::ID => $coupon_id,
$VipMCouponModel::VID => $vid,
$VipMCouponModel::MERCHANT_ID => $merchant_id,
);
$fields = array(
$VipMCouponModel::NEED_MONEY . ' as need_money',
$VipMCouponModel::REDUCE_MONEY . ' as reduce_money',
$VipMCouponModel::CAN_USE_STORE_IDS,
$VipMCouponModel::MCOUPON_ID,
$VipMCouponModel::START_TIME,
$VipMCouponModel::END_TIME,
);
$coupon = $VipMCouponModel->getFind($map, $fields);
if (empty($coupon)) {
$res['status'] = 0;
$res['msg'] = '优惠券不存在';
$this->ajaxReturn($res);
}
$MCouponModel = D('RetailMCoupon');
$couponInfo = $MCouponModel->getFind(array($MCouponModel::ID => $coupon[$VipMCouponModel::MCOUPON_ID]));
$coupon['coupon_name'] = $couponInfo[$MCouponModel::COUPON_NAME];
$coupon['detail'] = htmlspecialchars_decode($couponInfo[$MCouponModel::DETAIL]);
$coupon['time'] = '有效期:' . date('Y-m-d H:i:s', $coupon[$MCouponModel::START_TIME]) . ' - ' . date('Y-m-d H:i:s', $coupon[$MCouponModel::END_TIME]);
if ($coupon[$VipMCouponModel::CAN_USE_STORE_IDS] == 0) {
$coupon['store_list'] = array(array('id' => 0, 'store_name' => '所有门店'));
} else {
$StoreModel = D('RetailStore');
$map = array(
$StoreModel::MERCHANT_ID => $merchant_id,
$StoreModel::STORE_STATUS => 0,
$StoreModel::STATE => 0,
);
$fields = array(
$StoreModel::ID,
$StoreModel::STORE_NAME . ' as store_name'
);
$store_list = $StoreModel->getSelect($map, $fields);
$coupon['store_list'] = array();
$coupon['store_ids'] = trim($couponInfo[$MCouponModel::CAN_USE_STORE_IDS], ',');
$store_ids = explode(',', $coupon['store_ids']);
foreach ($store_list as $key => $val) {
if (in_array($val[$StoreModel::ID], $store_ids)) {
$coupon['store_list'][] = $val;
}
}
}
$res['coupon'] = $coupon;
$res['status'] = 1;
$res['msg'] = '查询成功';
$this->ajaxReturn($res);
}
/**
* 绑定会员卡
*/
public function bindCard_v1()
{
$password = I('post.password');
$card = I('post.mobile_phone');
$openid = I('post.openid');
if (empty($password) || empty($card)) {
$res['status'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$VipModel = new RetailVipModel();
$vMap = array(
'mobile_phone|card_key' => strval($card),
'status' => array('neq', 2),
'merchantid' => self::$merchant_id
);
$vipInfo = $VipModel->getFind($vMap);
if (empty($vipInfo)) {
$res['status'] = 0;
$res['msg'] = '会员卡不存在';
$this->ajaxReturn($res);
}
switch ($vipInfo['status']) {
case '1':
if (checkPassword($vipInfo['password'], $password, $vipInfo['addtime'])) {
$VipModel->updateOrder($vMap, ['openid' => $openid]);
$SmallProgramUserModel = new RetailSmallProgramUserModel();
$map = ['id' => self::$uid];
$save = [
'vid' => $vipInfo['id'],
'mobile_phone' => $card
];
if ($vipInfo['plus_type'] == 2) {
$PlusVipModel = new RetailPlusVipModel();
$pMap = ['id' => $vipInfo['plus_id']];
$PlusVipModel->updateOrder($pMap, ['xcx_vip_id' => self::$uid]);
$save['plus_vip_id'] = $vipInfo['plus_id'];
$save['type'] = 2;
} else {
$save['plus_vip_id'] = 0;
$save['type'] = 3;
}
$update = $SmallProgramUserModel->updateOrder($map, $save);
if ($update) {
//查询该会员之前是否被绑定过
$dMap = [
'id' => ['neq', self::$uid],
'vid' => $vipInfo['id'],
];
$ids = $SmallProgramUserModel->getFindField($dMap, 'id', true);
if (!empty($ids)) {
//修改状态并发出通知
$SmallProgramUserModel->updateOrder($dMap, ['vid' => null, 'plus_vip_id' => null, 'type' => 1]);
$MessageModel = new RetailMessageModel();
foreach ($ids as $val) {
$MessageModel->outBindVip([
'uid' => $val,
'nickname' => self::$user['nickname'],
'phone' => $card,
]);
}
}
$res['status'] = 1;
$res['msg'] = '绑定成功';
} else {
$res['status'] = 0;
$res['msg'] = '绑定失败';
}
} else {
$res['status'] = 0;
$res['msg'] = '密码错误';
}
break;
case '3':
$res['status'] = 0;
$res['msg'] = '该会员卡被冻结';
break;
case '4':
$res['status'] = 0;
$res['msg'] = '该会员卡被挂失';
break;
default:
$res['status'] = 0;
$res['msg'] = '会员卡异常';
}
$this->ajaxReturn($res);
}
/**
* 获取用户手机号
*/
public function getPhone()
{
$uid = self::$uid;
$appId = I('post.appId');
$session_key = I('post.session_key');
$iv = I('post.iv');
$encryptedData = I('post.encryptedData');
$verify_vip = I('post.verify_vip');
Vendor('php.wxBizDataCrypt');
$pc = new \wxBizDataCrypt($appId, $session_key);
$data = '';
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode == 0) {
$data = json_decode(json_decode(json_encode($data)), true);
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
$keyItem['phoneNumber'] = $data['phoneNumber'];
S('small_program_key_item_' . $uid, json_encode($keyItem)); //存储密钥
if ($verify_vip == 1) {
//验证该手机号是否是会员
$VipModel = new RetailVipModel();
$vip = $VipModel->getFind(['mobile_phone' => strval($data['phoneNumber']), 'merchantid' => self::$merchant_id],'status');
if (empty($vip)) {
$res['status'] = 0;
$res['msg'] = '该手机号未注册会员';
$this->ajaxReturn($res);
}
switch ($vip['status']){
case '1':
break;
case '3':
$res['status'] = 0;
$res['msg'] = '该会员被冻结';
$this->ajaxReturn($res);
break;
case '4':
$res['status'] = 0;
$res['msg'] = '该会员已挂失';
$this->ajaxReturn($res);
break;
}
}
$res['data'] = $data;
$res['status'] = 1;
$res['msg'] = '获取成功';
} else {
$res['data'] = $errCode;
$res['status'] = 0;
$res['msg'] = '手机号获取失败';
}
$this->ajaxReturn($res);
}
/**
* 获取验证码
*/
public function getCode()
{
$phone = I('post.phone'); //要发送短信的手机号码
if (empty($phone)) {
$res['status'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$uid = self::$uid;
$merchant_id = self::$merchant_id;
$VipModel = D('RetailVip');
$map = array(
$VipModel::MOBILE_PHONE => $phone,
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STATUS => array('neq', 2)
);
$vip = $VipModel->getFind($map);
if (empty($vip)) {
//手机号未注册
$aliDaYu = A('Api/Alidayu');
$result = $aliDaYu->Send($phone, $merchant_id);
if ($result['Code'] == "OK") {
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
$keyItem['verifyCode'] = $result['data']['code'];
$keyItem['codeMobile'] = $phone;
S('small_program_key_item_' . $uid, json_encode($keyItem)); //存储密钥
$res['msg'] = "发送成功";
$res['status'] = 1;
} else {
$res['status'] = 0;
$res['msg'] = "发送失败";
}
} else {
//手机号已注册
$res['success'] = 0;
$res['msg'] = '该手机号已注册会员';
}
$this->ajaxReturn($res);
}
/**
* 获取会员验证码
*/
public function getEditCode()
{
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
if (empty($user[$SmallProgramModel::VID])) {
$res['success'] = 0;
$res['msg'] = '您的请求有误';
$this->ajaxReturn($res);
}
$uid = self::$uid;
$merchant_id = self::$merchant_id;
$VipModel = D('RetailVip');
$map = array(
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::ID => $user[$SmallProgramModel::VID],
$VipModel::STATUS => array('neq', 2)
);
$vip = $VipModel->getFind($map);
if (empty($vip) || empty($vip[$VipModel::MOBILE_PHONE])) {
$res['success'] = 0;
$res['msg'] = '您的请求有误1';
$this->ajaxReturn($res);
} else {
$aliDaYu = A('Api/Alidayu');
$result = $aliDaYu->Send($vip[$VipModel::MOBILE_PHONE], $merchant_id);
if ($result['Code'] == "OK") {
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
$keyItem['verifyCode'] = $result['data']['code'];
$keyItem['codeMobile'] = $vip[$VipModel::MOBILE_PHONE];
S('small_program_key_item_' . $uid, json_encode($keyItem)); //存储密钥
$res['msg'] = "发送成功";
$res['status'] = 1;
} else {
$res['status'] = 0;
$res['msg'] = "发送失败";
}
}
$this->ajaxReturn($res);
}
/**
* 注册会员
*/
public function registerVip()
{
$phone = I('post.phone');
$password = I('post.password');
$code = I('post.code');
if (empty($phone) || empty($password)) {
$res['status'] = 0;
$res['msg'] = '请完善参数';
$this->ajaxReturn($res);
}
if (!verifyMobilePhone($phone)) {
$res['status'] = 0;
$res['msg'] = '手机号有误';
$this->ajaxReturn($res);
}
if (!preg_match('/^[0-9]{6}$/', $password)) {
$res['status'] = 0;
$res['msg'] = '密码只支持6位数字';
$this->ajaxReturn($res);
}
$uid = self::$uid;
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
if (empty($code)) {
if ($phone != $keyItem['phoneNumber']) {
$res['status'] = 0;
$res['msg'] = '请求有误';
$this->ajaxReturn($res);
}
} else {
if ($code != $keyItem['verifyCode']) {
$res['status'] = 0;
$res['msg'] = '验证码不正确';
$this->ajaxReturn($res);
}
}
$merchant_id = self::$merchant_id;
$store_id = self::$store_id;
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
$share_uid = I('post.share_uid');
if($share_uid){
$whereStoreId = [
'id' => $share_uid
];
$uidStoreId = $SmallProgramModel->getFindField($whereStoreId, 'store_id');//查询分享的用户所属门店ID
if (!empty($uidStoreId)) {
//如果分享人有所属门店,则新用户所属门店被替换。
$store_id = $uidStoreId;
$StoreModel = new RetailStoreModel();
$third_code = $StoreModel->getFindField([
'id'=>$store_id
],'third_code');
}
}else{
$third_code = self::$store['third_code'];
}
$VipModel = D('RetailVip');
$map = array(
$VipModel::MOBILE_PHONE => $phone,
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STATUS => array('neq', 2)
);
$vip = $VipModel->getFind($map);
if (!empty($vip)) {
$res['success'] = 0;
$res['msg'] = '该手机号已注册会员';
$this->ajaxReturn($res);
}
$time = $_SERVER['REQUEST_TIME'];
$password = createPassword($password, $time);
$item = array(
$VipModel::MOBILE_PHONE => $phone,
$VipModel::NICK_NAME => $user[$SmallProgramModel::NICKNAME],
$VipModel::PASSWORD => $password,
$VipModel::BALANCE => 0,
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STORE_ID => $store_id,
$VipModel::AVATAR => $user[$SmallProgramModel::AVATAR],
$VipModel::ADD_TIME => $time,
);
if (self::$merchant['platform_type'] == 2) {
//第三方平台
$third_vip = [
'Code' => $phone,
'Source' => '商城注册',
'Customer_Code' => $third_code,
'Name' => $user[$SmallProgramModel::NICKNAME] ? $user[$SmallProgramModel::NICKNAME] : $phone,
];
$ThirdVipClass = new Vip();
$thirdResult = $ThirdVipClass->vipInsert([
'merchant_id' => self::$merchant_id,
'vip_info' => json_encode($third_vip)
]);
if ($thirdResult['status'] == 0) {
$res['status'] = 0;
$res['msg'] = 'Q3 ERP平台会员新增失败' . $thirdResult['message'];
$this->ajaxReturn($res);
}
if (!empty($thirdResult['data']) && !empty($thirdResult['data']['Code'])) {
//Q3已存在更新Q3的会员信息
$third_vip = $thirdResult['data'];
if (!empty($third_vip['Source'])) {
$item['source_channel'] = $third_vip['Source'];
}
if (!empty($third_vip['Name'])) {
$item['name'] = $third_vip['Name'];
}
if (!empty($third_vip['DateOfBirth'])) {
$item['birthday'] = date('Y-m-d', substr($third_vip['DateOfBirth'], 6, 10));
}
if (self::$store['third_code'] != $third_vip['Customer_Code']) {
$StoreModel = new RetailStoreModel();
$new_store_id = $StoreModel->getFindField([
'third_code' => $third_vip['Customer_Code'],
'merchantid' => self::$merchant_id,
'state' => 0
], 'id');
if (!empty($new_store_id)) {
$item['storeid'] = $new_store_id;
}
}
}
}
$add_vip = $VipModel->addOrder($item);
if ($add_vip) {
$bind = $SmallProgramModel->updateOrder(array($SmallProgramModel::ID => $uid), array($SmallProgramModel::VID => $add_vip, $SmallProgramModel::USER_TYPE => 3));
if ($bind) {
$res['coupon_res'] = A('PayApi/Vip')->newVipWelfare($add_vip, $merchant_id, 2, 3);
$res['status'] = 1;
$res['msg'] = '激活成功';
$res['add_vip'] = $add_vip;
} else {
$res['status'] = 0;
$res['msg'] = '会员注册成功,绑定失败,请进行绑定';
$res['add_vip'] = $add_vip;
}
} else {
$res['status'] = 0;
$res['msg'] = '数据库发生错误,请重试';
}
$this->ajaxReturn($res);
}
/**
* 获取会员编辑资料信息
*/
public function getVipData()
{
$merchant_id = self::$merchant_id;
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
$VipModel = D('RetailVip');
$map = array(
$VipModel::ID => $user[$SmallProgramModel::VID],
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STATUS => array('in', '1,3,4'),
);
$fields = array(
$VipModel::ID,
$VipModel::NAME,
$VipModel::MOBILE_PHONE,
$VipModel::SEX,
$VipModel::BIRTHDAY,
);
$vip = $VipModel->getFind($map, $fields);
if (empty($vip)) {
$result['status'] = 0;
$result['msg'] = '未查询到该会员';
} else {
$result['status'] = 1;
$result['msg'] = '查询成功';
$result['data'] = $vip;
}
$this->ajaxReturn($result);
}
/**
* 修改会员资料
*/
public function editVipData()
{
$platform_type = self::$merchant['platform_type'] ? self::$merchant['platform_type'] : 1; //商户平台类型 1默认系统 2微礼平台
$name = I('post.name');
$mobile = I('post.mobile');
$sex = I('post.sex');
$birthday = I('post.birthday');
$code = I('post.code');
if (empty($name) || empty($mobile) || empty($sex) || empty($birthday)) {
$result['status'] = 0;
$result['msg'] = '未查询到该会员';
$this->ajaxReturn($result);
}
if (!verifyMobilePhone($mobile)) {
$result['status'] = 0;
$result['msg'] = '手机号有误';
$this->ajaxReturn($result);
}
if (!verifyName($name)) {
$result['status'] = 0;
$result['msg'] = '姓名有误';
$this->ajaxReturn($result);
}
if (!in_array($sex, array('1', '2'))) {
$result['status'] = 0;
$result['msg'] = '性别有误';
$this->ajaxReturn($result);
}
if (!strtotime($birthday) || strtotime($birthday) > time()) {
$result['status'] = 0;
$result['msg'] = '生日有误';
$this->ajaxReturn($result);
}
$merchant_id = self::$merchant_id;
$uid = self::$uid;
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
$VipModel = D('RetailVip');
$map = array(
$VipModel::ID => $user[$SmallProgramModel::VID],
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STATUS => array('in', '1,3,4'),
);
$fields = array(
$VipModel::ID,
$VipModel::MOBILE_PHONE,
$VipModel::IS_PERFECT,
$VipModel::MERCHANT_ID,
$VipModel::PLUS_TYPE
);
$vip = $VipModel->getFind($map, $fields);
if (empty($vip)) {
$result['status'] = 0;
$result['msg'] = '未查询到该会员';
} else {
//用户修改了手机号
if ($vip[$VipModel::MOBILE_PHONE] != $mobile) {
if (empty($code)) {
$result['status'] = 0;
$result['msg'] = '请输入验证码';
$this->ajaxReturn($result);
} else {
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
if ($mobile != $keyItem['codeMobile']) {
$result['status'] = 0;
$result['msg'] = '请输入获取验证码的手机号';
$this->ajaxReturn($result);
}
if ($code != $keyItem['verifyCode']) {
$result['status'] = 0;
$result['msg'] = '验证码不正确';
$this->ajaxReturn($result);
}
unset($keyItem['verifyCode']);
S('small_program_key_item_' . $uid, json_encode($keyItem)); //存储密钥
}
$oMap = array(
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::STATUS => array('in', '1,3,4'),
$VipModel::MOBILE_PHONE => $mobile
);
$oldVip = $VipModel->getFind($oMap);
if (!empty($oldVip)) {
$result['status'] = 0;
$result['msg'] = '该手机号已被使用';
$this->ajaxReturn($result);
}
}
$save = array(
$VipModel::NAME => $name,
$VipModel::MOBILE_PHONE => $mobile,
$VipModel::SEX => $sex,
$VipModel::BIRTHDAY => $birthday,
);
if ($vip[$VipModel::IS_PERFECT] == 0) {
$save[$VipModel::IS_PERFECT] = 1;
//第一次完善资料
$vipConfig = M('vip_config')->where(array('merchantid' => $merchant_id))->find();
if (!empty($vipConfig)) {
$perfect = true;
if ($vipConfig['plus_infoscore'] > 0) {
if ($vip[$VipModel::PLUS_TYPE] == 2) { //plus会员
$perfect = false;
$integral = $vipConfig['plus_infoscore'];
}
}
if ($perfect && $vipConfig['infoscore'] > 0) {
$integral = $vipConfig['infoscore'];
}
if (intval($integral) > 0) {
$VipModel->toSetInc($map, 'integral', $integral);
$VipModel->toSetInc($map, 'grade_integral', $integral);
$record = array(
'vid' => $vip['id'],
'type' => 6,
'integral' => $integral,
'remark' => '通过完善资料获得' . $integral . '积分',
'storeid' => self::$store_id,
'addtime' => time()
);
D('IntegralOrder')->addOrder($record);
}
}
}
if ($platform_type == 2) {
//Q3会员编辑
$VipClass = new Vip();
$thirdResult = $VipClass->vipModify([
'merchant_id' => self::$merchant_id,
'phone' => $vip['mobile_phone'],
'vip_info' => json_encode([
'mobile_phone' => $mobile,
'sex' => $sex,
'name' => $name,
'birthday' => $birthday
])
]);
if ($thirdResult['status'] == 0) {
$result['msg'] = 'Q3 会员编辑失败' . $thirdResult['message'];
$this->ajaxReturn($result);
}
}
$update = $VipModel->updateOrder($map, $save);
if ($update) {
$result['status'] = 1;
$result['msg'] = '保存成功';
} else {
$result['status'] = 0;
$result['msg'] = '您未修改资料';
}
}
$this->ajaxReturn($result);
}
/**
* 会员修改密码
*/
public function reSetPassword()
{
$code = I('post.code');
$password = I('post.password');
if (empty($code) || empty($password)) {
$res['success'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
if (empty($user[$SmallProgramModel::VID])) {
$res['success'] = 0;
$res['msg'] = '您的请求有误';
$this->ajaxReturn($res);
}
$uid = self::$uid;
$merchant_id = self::$merchant_id;
$VipModel = D('RetailVip');
$map = array(
$VipModel::MERCHANT_ID => $merchant_id,
$VipModel::ID => $user[$SmallProgramModel::VID],
$VipModel::STATUS => array('neq', 2)
);
$vip = $VipModel->getFind($map);
if (empty($vip) || empty($vip[$VipModel::MOBILE_PHONE])) {
$res['success'] = 0;
$res['msg'] = '您的请求有误1';
$this->ajaxReturn($res);
}
$keyItem = S('small_program_key_item_' . $uid); //登录存储的数据
$keyItem = json_decode($keyItem, true);
if ($code != $keyItem['verifyCode']) {
$res['status'] = 0;
$res['msg'] = '验证码不正确';
$this->ajaxReturn($res);
}
unset($keyItem['verifyCode']);
S('small_program_key_item_' . $uid, json_encode($keyItem)); //存储密钥
$password = createPassword($password, $vip[$VipModel::ADD_TIME]);
$update = $VipModel->updateOrder($map, array($VipModel::PASSWORD => $password));
if ($update) {
$res['status'] = 1;
$res['msg'] = '修改成功';
} else {
$res['status'] = 0;
$res['msg'] = '修改失败';
}
$this->ajaxReturn($res);
}
/**
* 获取积分汇总
*/
public function getSumIntegral()
{
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
$vid = $user[$SmallProgramModel::VID];
$IntegralOrderModel = D('RetailIntegralOrder');
$VipModel = D('RetailVip');
$less_integral = $VipModel->getFindField(array($VipModel::ID => $vid), $VipModel::INTEGRAL);
$map = array(
$IntegralOrderModel::VID => $vid,
$IntegralOrderModel::TYPE => array('in', '3,5,9'),
);
$use_integral = $IntegralOrderModel->getSum($map, $IntegralOrderModel::INTEGRAL);
$refund_use_integral = $IntegralOrderModel->getSum($map, $IntegralOrderModel::REFUND_INTEGRAL);
$use_integral = round($use_integral - $refund_use_integral);
$res['status'] = 1;
$res['msg'] = '获取成功';
$res['less_integral'] = $less_integral;
$res['use_integral'] = $use_integral;
$this->ajaxReturn($res);
}
/**
* 获取积分列表
*/
public function getIntegral()
{
$page = I('post.page');
if (empty($page)) {
$res['status'] = 0;
$res['msg'] = '缺少参数';
$this->ajaxReturn($res);
}
$user = self::$user;
$count = 20;
$SmallProgramModel = D('RetailSmallProgramUser');
$vid = $user[$SmallProgramModel::VID];
$IntegralOrderModel = D('RetailIntegralOrder');
$Order = $IntegralOrderModel->getSelect(array($IntegralOrderModel::VID => $vid), '*', $IntegralOrderModel::ADD_TIME . ' desc', $page . ',' . $count);
//1签到获取 2余额消费获取 3使用积分 4后台充值 5后台扣除 6完善资料获取 7积分购买商品
if (!empty($Order)) {
foreach ($Order as &$val) {
if (in_array($val[$IntegralOrderModel::TYPE], array('1', '2', '4', '6', '7', '8'))) {
$val['i_type'] = 1;
} else {
$val['i_type'] = 2;
}
$val['add_time'] = date('Y-m-d H:i:s', $val[$IntegralOrderModel::ADD_TIME]);
}
} else {
$Order = array();
}
$res['status'] = 1;
$res['count'] = $count;
$res['msg'] = '获取成功';
$res['data'] = $Order;
$this->ajaxReturn($res);
}
/**
* 获取动态码
*/
public function getDynamicCode()
{
$user = self::$user;
$SmallProgramModel = D('RetailSmallProgramUser');
$vid = $user[$SmallProgramModel::VID];
if (empty($vid)) {
$res['status'] = 0;
$res['msg'] = '您不是会员';
$this->ajaxReturn($res);
}
$isRefresh = I('post.isRefresh');
if (!empty($vid)) {
$VipDynamicCodeModel = D('RetailVipDynamicCode');
$map = array(
$VipDynamicCodeModel::STATUS => 0,
$VipDynamicCodeModel::STATE => 0,
$VipDynamicCodeModel::VID => $vid,
);
$old = $VipDynamicCodeModel->getOrderFind($map, '*', $VipDynamicCodeModel::ID . ' desc');
$time = time();
$merchant_id = self::$merchant_id;
if (empty($old) || $old[$VipDynamicCodeModel::FAILURE_TIME] <= $time || $isRefresh == 1) {
$code = '';
$flag = true;
while ($flag) {
$code = '8' . rand(600000, 999999) . rand(100000, 999999) . rand(10000, 99999);
$map = array(
$VipDynamicCodeModel::MERCHANT_ID => $merchant_id,
$VipDynamicCodeModel::STATUS => 0,
$VipDynamicCodeModel::STATE => 0,
$VipDynamicCodeModel::CODE => $code
);
$old = $VipDynamicCodeModel->getFind($map);
if (empty($old)) {
$flag = false;
}
}
$add = array(
$VipDynamicCodeModel::VID => $vid,
$VipDynamicCodeModel::MERCHANT_ID => $merchant_id,
$VipDynamicCodeModel::ADD_TIME => $time,
$VipDynamicCodeModel::FAILURE_TIME => round($time + 60),
$VipDynamicCodeModel::STATUS => 0,
$VipDynamicCodeModel::STATE => 0,
$VipDynamicCodeModel::CODE => $code
);
$id = $VipDynamicCodeModel->addOrder($add);
if ($id) {
$map = array(
$VipDynamicCodeModel::VID => $vid,
$VipDynamicCodeModel::MERCHANT_ID => $merchant_id,
$VipDynamicCodeModel::ID => array('neq', $id),
$VipDynamicCodeModel::STATUS => array('neq', 0),
$VipDynamicCodeModel::STATE => array('neq', 1),
);
$VipDynamicCodeModel->updateOrder($map, array($VipDynamicCodeModel::STATE => 1));
$res['status'] = 1;
$res['code'] = $code;
$res['time'] = round($add[$VipDynamicCodeModel::FAILURE_TIME] - $time);
} else {
$res['status'] = 0;
$res['msg'] = '获取超时';
}
} else {
$res['status'] = 1;
$res['code'] = $old[$VipDynamicCodeModel::CODE];
$res['time'] = round($old[$VipDynamicCodeModel::FAILURE_TIME] - $time);
}
} else {
$res['status'] = 0;
$res['msg'] = '登录超时';
}
$this->ajaxReturn($res);
}
/*
* 会员卡优惠券 2019-12-23
*/
public function getCouponData_v1()
{
if (IS_POST) {
$page = I('post.page');
$type = I('post.use_type');
$SPUModel = D('RetailSmallProgramUser');
$user = self::$user;
if (empty($user[$SPUModel::VID])) {
$result['status'] = 0;
$result['msg'] = '您未绑定会员卡';
$this->ajaxReturn($result);
}
$vid = $user[$SPUModel::VID];
$limit = 10;
$time = time();
$merchant_id = self::$merchant_id;
$VipMCoupon = D('RetailVipMCoupon');
$map = array(
$VipMCoupon::STATUS => 2,
$VipMCoupon::VID => $vid,
$VipMCoupon::MERCHANT_ID => $merchant_id
);
if ($type == 1) {
// 可用优惠券
// 未进行的优惠券
$map[$VipMCoupon::IS_USE] = 1;
$map[$VipMCoupon::END_TIME] = array('gt', $time);
} else if ($type == 2) {
$map[$VipMCoupon::IS_USE] = 2;
} else if ($type == 3) {
$map['_string'] = "(" . $VipMCoupon::END_TIME . " < {$time} OR " . $VipMCoupon::IS_USE . " = 3)";
}
$list = $VipMCoupon->getSelect($map, '*', '', $page . ',' . $limit);
foreach ($list as $k => &$v) {
$MCouponModel = D('RetailMCoupon');
$v['reducemoney'] = floatval($v['reducemoney']);
$v['needmoney'] = floatval($v['needmoney']);
$v['coupon_name'] = $MCouponModel->getFindField(array($MCouponModel::ID => $v[$VipMCoupon::MCOUPON_ID]), $MCouponModel::COUPON_NAME);
$v['add_time'] = date('Y.m.d H:i:s', $v[$VipMCoupon::ADD_TIME]);
if ($type == 1) {
$v['time'] = date('Y.m.d H:i:s', $v[$VipMCoupon::START_TIME]) . '~' . date('Y.m.d H:i:s', $v[$VipMCoupon::END_TIME]);
} else if ($type == 2) {
$v['time'] = date('Y.m.d H:i:s', $v[$VipMCoupon::USE_TIME]);
} else if ($type == 3) {
$v['time'] = date('Y.m.d H:i:s', $v[$VipMCoupon::END_TIME]);
} else {
$v['time'] = '';
}
}
$result['limit'] = $limit;
if ($list) {
$result['status'] = 1;
$result['msg'] = '获取成功';
$result['data'] = $list;
} else {
$result['status'] = 1;
$result['msg'] = '没有更多了';
$result['data'] = $list;
}
$this->ajaxReturn($result);
}
}
}