123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- <?php
- namespace app\index\controller;
- use app\index\model\AuthCompanyModel;
- use app\index\service\HelperService;
- use app\index\service\wechat\WxPayConfig;
- use think\Cache;
- use think\Config;
- use think\Controller;
- use think\Validate;
- class WechatJs extends Controller
- {
- public function getConfigJs(){
- $param = $this->request->param();
- $companyCode = $this->_validCompanyCode($param);
- WxPayConfig::$companyCode();
- //判断有没有当前公司的access_token
- $this->_getAccessToken($companyCode);
- }
- public function getApiTicket(){
- $param = $this->request->param();
- $companyCode = $this->_validCompanyCode($param);
- if(Cache::has("{$companyCode}_apiTicket")){
- HelperService::returnJson([
- 'code'=>200,
- 'msg'=>'success',
- 'data'=>Cache::get("{$companyCode}_apiTicket")
- ]);
- }
- WxPayConfig::$companyCode();
- $access_token = $this->_getToken($companyCode);
- $request_token = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=wx_card";
- $res = HelperService::httpPost($request_token,'',true);
- if($res === false){
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>'api_token is error',
- 'data'=>$res
- ]);
- }
- $tokenArr = @json_decode($res,true);
- if($tokenArr['errcode']>0){
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>'api_token is error',
- 'data'=>$res
- ]);
- }
- Cache::set("{$companyCode}_apiTicket","{$tokenArr['ticket']}",7200);
- HelperService::returnJson([
- 'code'=>200,
- 'msg'=>'success',
- 'data'=>"{$res['ticket']}"
- ]);
- }
- public function getToken(){
- $param = $this->request->param();
- $companyCode = $this->_validCompanyCode($param);
- $token = $this->_getToken($companyCode);
- Cache::set('access_token_'.$companyCode,$token,300);
- HelperService::returnJson([
- 'code'=>200,
- 'msg'=>'curl',
- 'data'=>"$token"
- ]);
- }
- private function _getToken($companyCode,$is_force=false){
- WxPayConfig::$companyCode();
- $token = Cache::get('access_token_'.$companyCode);
- if(!empty($token) && $is_force==false){
- HelperService::returnJson([
- 'code'=>200,
- 'msg'=>'cache',
- 'data'=>"$token"
- ]);
- }
- //判断有没有当前公司的access_token
- $request_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WxPayConfig::$APPID."&secret=".WxPayConfig::$APPSECRET;
- $token_json = HelperService::httpPost($request_token,'',true);
- if($token_json === false){
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>'token is error',
- 'data'=>"$token_json"
- ]);
- }
- $tokenArr = json_decode($token_json,true);
- if(!isset($tokenArr['access_token'])){
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>'wechat return',
- 'data'=>"$token_json",
- 'url'=>$request_token
- ]);
- }
- $token = $tokenArr['access_token'];
- return $token;
- }
- public function sendCard(){
- $cardId = $this->request->param('card_id');
- $code = $this->request->param('code');
-
- $companyCode = 'SHYL';
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/card/qrcode/create?access_token=$access_token";
- $data = '{
- "action_name": "QR_CARD",
- "expire_seconds": 1800,
- "action_info": {
- "card": {
- "card_id": "'.$cardId.'",
- "code": "'.$code.'",
- "is_unique_code": true,
- "outer_str":"12b"
- }
- }
- }';
- $res = HelperService::httpPost($url,$data,true);
- var_dump($res);
- exit;
- }
-
- public function updateCardCode(){
- $code = $this->request->param('code');
- $companyCode = 'SHYL';
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/card/code/unavailable?access_token=$access_token";
- $data = '{"code":"'.$code.'","card_id":"pIa9rt9EyR4IvGsWq7VY6HfKQ39U"}';
- echo $data;
- $res = HelperService::httpPost($url,$data,true);
- var_dump($res);
- exit;
- }
- /**
- * 发送白模版接口
- */
- public function sendTemplate(){
- $params = $this->request->param();
- $rule = [
- 'access_token'=>'require',
- 'open_id'=>'require',
- 'template_url'=>'require',
- 'content'=>'require',
- 'template_id'=>'require'
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>$validate->getError(),
- ]);
- return false;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$params['access_token']}";
- $data = [
- "touser"=>$params['open_id'],
- "template_id"=>$params['template_id'],
- "url"=>$params['template_url'],
- "data"=>$params['content']
- ];
- $returnJson = HelperService::httpPost($url,json_encode($data));
- $decodeJson = json_decode($returnJson,true);
- HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$decodeJson]);
- }
- public function _getAccessToken($companyCode){
- HelperService::returnJson(HelperService::getWechatToken($companyCode));
- }
- /**
- * 创建logo
- * 待测试
- */
- public function cardLogo(){
- $params = $this->request->param();
- $rule = [
- 'companyCode'=>'require',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$params]);
- }
- $access_token = $this->_getToken($params['companyCode'],true);
- $filePath = $this->_upload('media1');
- $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}&type=card_logo";
- $data = [
- "type"=>"image",
- "media"=>new \CURLFile($filePath)
- ];
- $returnJson = HelperService::httpPost($url,$data,true,[],false);
- $decodeJson = json_decode($returnJson,true);
- HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$decodeJson]);
- }
- /**
- * 获取门店信息
- */
- public function getStoreList(){
- $companyCode = 'SHYL';
- WxPayConfig::$companyCode();
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={$access_token}";
-
- $res = HelperService::httpPost($url, json_encode(['begin'=>0,'limit'=>50]), true);
- $res = @json_decode($res,true);
-
- if($res['errcode']!=0){
- var_dump($res);exit;
- }
-
- echo "\n=====\n";
- foreach($res['business_list'] as $item){
- echo $item['base_info']['poi_id'].",";
- }
- echo "\n==".count($res['business_list'])."==\n";
- }
-
- /**
- * 获取门店信息
- */
- public function paycell(){
-
- $cardId = $this->request->param('card_id');
- $companyCode = 'SHYL';
- WxPayConfig::$companyCode();
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/card/paycell/set?access_token={$access_token}";
-
- $res = HelperService::httpPost($url, json_encode(['card_id'=>$cardId,'is_open'=>true]), true);
- $res = @json_decode($res,true);
-
- var_dump($res);
- }
- /**
- * 创建卡券
- * 参考连接https://www.w3cschool.cn/weixinkaifawendang/rkoi1qfa.html
- */
- public function createCard(){
- $companyCode = 'SMDD';
- WxPayConfig::$companyCode();
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/card/create?access_token={$access_token}";
- $data = '{
- "card": {
- "card_type": "MEMBER_CARD",
- "member_card": {
- "background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXdfLQNt3n1wdkovicibia49VUEdJbdHw9QRc6L5B1Itib2hKS1ricSFmfeDFegPqTBFpX4MBtnoUyIgmZg/0",
- "base_info": {
- "logo_url": "http://mmbiz.qpic.cn/mmbiz_jpg/tnZicgdFjqL06BkhRVwqU40BWNKziaz2EzhicuIRLfaJuFbEDoMHMKNjrBibLrRqD2iac3JY4nZzOB1BHSXiasfGrWHg/0",
- "brand_name": "客勤八久",
- "code_type": "CODE_TYPE_QRCODE",
- "title": "会员卡",
- "color": "Color010",
- "notice": "使用时向服务员出示此券",
- "service_phone": "021-51651616",
- "description": "会员权益不可与其他优惠同享",
- "date_info": {
- "type": "DATE_TYPE_FIX_TERM",
- "fixed_term":730,
- "fixed_begin_term":0
- },
- "pay_info":{
- "swipe_card":{
- "is_swipe_card":true
- }
-
- },
- "custom_url_name": "会员中心",
- "custom_url": "http://member.keqin89.com",
- "sku": {
- "quantity": 50000000
- },
- "get_limit": 1,
- "use_limit":50000000,
- "use_custom_code": true,
- "can_share":false,
- "can_give_friend": false
- },
- "advanced_info": {
- "abstract": {
- "abstract": "即可享有会员专属优惠,会员消费时优惠券、现金券自动抵扣,同时拥有多种优惠券时,自动按可享受最高折扣的优惠券进行抵扣使用",
- "icon_url_list": [
- "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT00BG9HrZsibOjNm6dfxbjiaYS4BFeNfwAUTv9F7UpAurDCB7ZeUQ7UZiaQ/0"
- ]
- },
- "text_image_list": [
- {
- "image_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT0Cehn0fUmJQamCmsKwabibrkPtLdDodgZ4AB5k3SNjPV6BiadO6ea9fAg/0",
- "text": " 我们一直以顾客的体验为初衷,不断提升门店的舒适度、为让顾客在更舒适、愉悦的心情下美美的用餐。"
- },
- {
- "image_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT0Rsm7nX2zw9dI41It88PQJIicFOUJJsWn0eQ4oNiab5kOTyQp96Z5atFA/0",
- "text": " 我们一直坚持品质、一直努力用心,为的就是让顾客更放心、安心、舒心的感受这舌尖上的美味。"
- }
- ],
- "time_limit": [
- ],
- "business_service": [
- "BIZ_SERVICE_DELIVER"
- ]
- },
- "auto_activate": true,
- "supply_bonus": false,
- "supply_balance": false,
- "prerogative":"注册会员"
- }
- }
- }';
- echo "1url:$url\n";
- $res = HelperService::httpPost($url, $data, true);
- $res = @json_decode($res,true);
- var_dump($res);exit;
- }
- public function updateCard(){
- $companyCode = 'SHYL';
- WxPayConfig::$companyCode();
- $access_token = $this->_getToken($companyCode,true);
- $url = "https://api.weixin.qq.com/card/update?access_token={$access_token}";
- //pIa9rt9EyR4IvGsWq7VY6HfKQ39U
- //pIa9rt417yEFidbNitU1wmcuqn_I
- //"location_id_list":[491473572,491470160,491470156,491470125,491367847,491367829,491367826,491367819,491367814,491215505,491215496,491205223,489947656,489933482,489933422,489933375,489933309,489933238,489933051,489932984,489932971,489932923,489932888,489932845,489932819,480178208,477741160,403959458]
-
- //$data ='{
- // "card_id":"pIa9rt9EyR4IvGsWq7VY6HfKQ39U",
- // "member_card": {
- // "base_info": {
- // "can_give_friend": true,
- // "type":"DATE_TYPE_FIX_TIME_RANGE",
- // "begin_timestamp":1539792000,
- // "end_timestamp":1640966400
- // }
- //}
- //}';
- $data = [
- "card_id"=>"pIa9rt9EyR4IvGsWq7VY6HfKQ39U",
- "member_card"=>[
- "base_info"=>[
- "can_give_friend"=>true,
- "date_info"=>[
- "type"=>"DATE_TYPE_FIX_TIME_RANGE",
- "begin_timestamp"=>1514736000,
- "end_timestamp"=>1640966400
- ]
- ]
- ]
- ];
- echo json_encode($data);
- echo "1url:$url\n";
- $res = HelperService::httpPost($url, json_encode($data), true);
- $res = @json_decode($res,true);
- var_dump($res);exit;
- }
- /**
- * 返回保存路径
- * @param $key
- * @return type
- */
- private function _upload($key){
- // 获取表单上传文件 例如上传了001.jpg
- $file = $this->request->file($key);
- // 移动到框架应用根目录/public/uploads/ 目录下
- if($file){
- $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
- if($info){
- // 成功上传后 获取上传信息
- // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
- return $info->getPath().DS.$info->getFilename();
- }else{
- // 上传失败获取错误信息
- HelperService::returnJson(['code'=>400,'msg'=>$file->getError(),'data'=>[]]);
- }
- }
- HelperService::returnJson(['code'=>400,'msg'=>'没有上传图片','data'=>[]]);
- }
- /**
- * 验证公司是否正确
- * @param $param
- * @return mixed
- */
- private function _validCompanyCode($param){
- if(isset($param['companyCode'])){
- $AuthCompanyModel = new AuthCompanyModel();
- $companyCode = $param['companyCode'];
- $company = $AuthCompanyModel->getInfo(['company_code'=>$companyCode]);
- if(empty($company)){
- die("I don't have this companyCode.");
- }
- session('company_db_json',$company['db_json']);
- session('SmsConfig',json_decode($company['sms_json'],true));
- session('companyCode',$param['companyCode']);
- }
- if(!session('companyCode')){
- die("I don't know companyCode.");
- }
- $companyCode = session('companyCode');
- $this->_loadConfig();
- return $companyCode;
- }
- private function _loadConfig(){
- $db_json = session('company_db_json');
- $db_arr = json_decode($db_json,true);
- if(empty($db_arr)){
- return true;
- }
- foreach($db_arr as $k=>$v){
- $data = [
- 'type' => 'mysql',
- 'hostname' => '127.0.0.1',
- 'database' => 'test',
- 'username' => 'root',
- 'password' => '',
- 'hostport' => '3306',
- 'params' => [],
- 'charset' => 'utf8',
- 'prefix' => '',
- ];
- $data['type'] = $v['type'];
- $data['hostname'] = $v['hostname'];
- $data['database'] = $v['database'];
- $data['username'] = $v['username'];
- $data['password'] = $v['password'];
- Config::set($v['config_name'],$data);
- }
- }
- }
|