1.注意创建会员卡时参数,导入型会员卡和指定code 不一样
# 创建会员卡 此创建为在微信公众平台创建一类卡public function createCard($value=''){$access_token = $this->getAccessToken();$data = $this->request->param();$url = "https://api.weixin.qq.com/card/create?access_token=".$access_token['access_token'];$str = '{"card": {"card_type": "MEMBER_CARD","member_card": {"background_pic_url":"https://mmbiz.qlogo.cn/mmbiz_jpg/dpoqAWiPAut0iaNkgH9csTRrD3lJGMrHU1Z8LpORzbev51FaiaCu8d13uoBLzF2CXiatUEPukaENUw/0","base_info": {"logo_url": "http://dd.cc.com/static/images/20190812134157.jpg","brand_name": "某某超市","code_type": "CODE_TYPE_BARCODE","title": "会员卡","color": "Color010","notice": "使用时向服务员出示此卡","service_phone": "0438-55555555","description": "不可与其他优惠同享","date_info": {"type": "DATE_TYPE_PERMANENT"},"sku": {"quantity": 0},"get_limit": 1,"use_custom_code": true,"get_custom_code_mode": "GET_CUSTOM_CODE_MODE_DEPOSIT"},"supply_bonus": false,"supply_balance": false,"prerogative": "超市会员卡","auto_activate": true,"custom_field1": {"name": "积分","url": "http://dd.cc.com/portal/card/bonus"},"custom_field2": {"name": "等级","url": "http://dd.cc.com/portal/card/myCards"},"custom_field3": {"name": "优惠","url": "http://dd.cc.com/portal/card/activity"},"activate_url": "","custom_cell1": {"name": "会员信息","url": "http://dd.cc.com/portal/card/index"},"custom_cell2": {"name": "会员等级","url": "http://dd.cc.com/portal/card/myCards"},"discount": "2","wx_activate": true,"wx_activate_after_submit": true,"wx_activate_after_submit_url": "http://dd.cc.com/portal/card/myCard"}}}';$res = json_decode(cmf_curl_post($url,$str),true);if ($res['errcode']==0) {Db::name('card_type')->insert(['card_id'=>$res['card_id'],'create_time'=>time()]);$this->success("创建成功!");} else {$this->error("创建失败!");}}
导入卡号-》设置库存
// 导入codepublic function inputCode($code){$user = session('user');$us = Db::name('card')->where(['user_id'=>$user['id']])->find();$access_token = $this->getAccessToken();$url = "http://api.weixin.qq.com/card/code/deposit?access_token=".$access_token['access_token'];$data = '{"card_id": "'.$us['card_id'].'","code": "'.$code.'"}';$re = json_decode(cmf_curl_post($url,$data),true);if ($re['errcode']==0) {$stock = $this->codeStock();} if ($stock['errcode']==0) {$info = $this->infoCard();}if ($info['errcode']==0) {print_r($info['url']);$this->redirect($info['url']);} }// 设置库存public function codeStock($value=''){$user = session('user');$us = Db::name('card')->where(['user_id'=>$user['id']])->find();$access_token = $this->getAccessToken();$url = "http://api.weixin.qq.com/card/modifystock?access_token=".$access_token['access_token'];$data = '{"card_id":"'.$us['card_id'].'","increase_stock_value":1}';$re = json_decode(cmf_curl_post($url,$data),true);return $re;}
生成激活链接
//生成激活链接public function infoCard(){$access_token = $this->getAccessToken();$user = session('user');$us = Db::name('card')->where(['user_id'=>$user['id']])->find();$card_id = $us['card_id']; # 设置开卡字段$kurl = "https://api.weixin.qq.com/card/membercard/activateuserform/set?access_token={$access_token['access_token']}";$kstr = '{"card_id":"'.$card_id.'","service_statement":{"name":"会员守则","url":"https://www.qq.com"},"required_form":{"common_field_id_list":["USER_FORM_INFO_FLAG_NAME","USER_FORM_INFO_FLAG_SEX","USER_FORM_INFO_FLAG_MOBILE","USER_FORM_INFO_FLAG_BIRTHDAY"]}}';$isKai = json_decode(cmf_curl_post($kurl,$kstr),true);//print_r($isKai);die;if ($isKai['errcode'] == 0) {$url = "https://api.weixin.qq.com/card/membercard/activate/geturl?access_token=".$access_token['access_token'];$str = '{"card_id":"'.$card_id.'","outer_str":"123"}';//print_r($str);die;$res = json_decode(cmf_curl_post($url,$str),true);return $res;}}
#1、获取access_token access_token 的有效时间是7200s,故可以采用文件存储的方法进行保存,避免多次请求;public function getAccessToken(){$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";$res = cmf_curl_get($url);$res = json_decode($res,1);return $res; }
card表: