WechatJs.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\model\AuthCompanyModel;
  4. use app\index\service\HelperService;
  5. use app\index\service\wechat\WxPayConfig;
  6. use think\Cache;
  7. use think\Config;
  8. use think\Controller;
  9. use think\Validate;
  10. class WechatJs extends Controller
  11. {
  12. public function getConfigJs(){
  13. $param = $this->request->param();
  14. $companyCode = $this->_validCompanyCode($param);
  15. WxPayConfig::$companyCode();
  16. //判断有没有当前公司的access_token
  17. $this->_getAccessToken($companyCode);
  18. }
  19. public function getApiTicket(){
  20. $param = $this->request->param();
  21. $companyCode = $this->_validCompanyCode($param);
  22. if(Cache::has("{$companyCode}_apiTicket")){
  23. HelperService::returnJson([
  24. 'code'=>200,
  25. 'msg'=>'success',
  26. 'data'=>Cache::get("{$companyCode}_apiTicket")
  27. ]);
  28. }
  29. WxPayConfig::$companyCode();
  30. $access_token = $this->_getToken($companyCode);
  31. $request_token = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=wx_card";
  32. $res = HelperService::httpPost($request_token,'',true);
  33. if($res === false){
  34. HelperService::returnJson([
  35. 'code'=>400,
  36. 'msg'=>'api_token is error',
  37. 'data'=>$res
  38. ]);
  39. }
  40. $tokenArr = @json_decode($res,true);
  41. if($tokenArr['errcode']>0){
  42. HelperService::returnJson([
  43. 'code'=>400,
  44. 'msg'=>'api_token is error',
  45. 'data'=>$res
  46. ]);
  47. }
  48. Cache::set("{$companyCode}_apiTicket","{$tokenArr['ticket']}",7200);
  49. HelperService::returnJson([
  50. 'code'=>200,
  51. 'msg'=>'success',
  52. 'data'=>"{$res['ticket']}"
  53. ]);
  54. }
  55. public function getToken(){
  56. $param = $this->request->param();
  57. $companyCode = $this->_validCompanyCode($param);
  58. $token = $this->_getToken($companyCode);
  59. Cache::set('access_token_'.$companyCode,$token,300);
  60. HelperService::returnJson([
  61. 'code'=>200,
  62. 'msg'=>'curl',
  63. 'data'=>"$token"
  64. ]);
  65. }
  66. private function _getToken($companyCode,$is_force=false){
  67. WxPayConfig::$companyCode();
  68. $token = Cache::get('access_token_'.$companyCode);
  69. if(!empty($token) && $is_force==false){
  70. HelperService::returnJson([
  71. 'code'=>200,
  72. 'msg'=>'cache',
  73. 'data'=>"$token"
  74. ]);
  75. }
  76. //判断有没有当前公司的access_token
  77. $request_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WxPayConfig::$APPID."&secret=".WxPayConfig::$APPSECRET;
  78. $token_json = HelperService::httpPost($request_token,'',true);
  79. if($token_json === false){
  80. HelperService::returnJson([
  81. 'code'=>400,
  82. 'msg'=>'token is error',
  83. 'data'=>"$token_json"
  84. ]);
  85. }
  86. $tokenArr = json_decode($token_json,true);
  87. if(!isset($tokenArr['access_token'])){
  88. HelperService::returnJson([
  89. 'code'=>400,
  90. 'msg'=>'wechat return',
  91. 'data'=>"$token_json",
  92. 'url'=>$request_token
  93. ]);
  94. }
  95. $token = $tokenArr['access_token'];
  96. return $token;
  97. }
  98. public function sendCard(){
  99. $cardId = $this->request->param('card_id');
  100. $code = $this->request->param('code');
  101. $companyCode = 'SHYL';
  102. $access_token = $this->_getToken($companyCode,true);
  103. $url = "https://api.weixin.qq.com/card/qrcode/create?access_token=$access_token";
  104. $data = '{
  105. "action_name": "QR_CARD",
  106. "expire_seconds": 1800,
  107. "action_info": {
  108. "card": {
  109. "card_id": "'.$cardId.'",
  110. "code": "'.$code.'",
  111. "is_unique_code": true,
  112. "outer_str":"12b"
  113. }
  114. }
  115. }';
  116. $res = HelperService::httpPost($url,$data,true);
  117. var_dump($res);
  118. exit;
  119. }
  120. public function updateCardCode(){
  121. $code = $this->request->param('code');
  122. $companyCode = 'SHYL';
  123. $access_token = $this->_getToken($companyCode,true);
  124. $url = "https://api.weixin.qq.com/card/code/unavailable?access_token=$access_token";
  125. $data = '{"code":"'.$code.'","card_id":"pIa9rt9EyR4IvGsWq7VY6HfKQ39U"}';
  126. echo $data;
  127. $res = HelperService::httpPost($url,$data,true);
  128. var_dump($res);
  129. exit;
  130. }
  131. /**
  132. * 发送白模版接口
  133. */
  134. public function sendTemplate(){
  135. $params = $this->request->param();
  136. $rule = [
  137. 'access_token'=>'require',
  138. 'open_id'=>'require',
  139. 'template_url'=>'require',
  140. 'content'=>'require',
  141. 'template_id'=>'require'
  142. ];
  143. $validate = new Validate($rule);
  144. if(!$validate->check($params)){
  145. HelperService::returnJson([
  146. 'code'=>400,
  147. 'msg'=>$validate->getError(),
  148. ]);
  149. return false;
  150. }
  151. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$params['access_token']}";
  152. $data = [
  153. "touser"=>$params['open_id'],
  154. "template_id"=>$params['template_id'],
  155. "url"=>$params['template_url'],
  156. "data"=>$params['content']
  157. ];
  158. $returnJson = HelperService::httpPost($url,json_encode($data));
  159. $decodeJson = json_decode($returnJson,true);
  160. HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$decodeJson]);
  161. }
  162. public function _getAccessToken($companyCode){
  163. HelperService::returnJson(HelperService::getWechatToken($companyCode));
  164. }
  165. /**
  166. * 创建logo
  167. * 待测试
  168. */
  169. public function cardLogo(){
  170. $params = $this->request->param();
  171. $rule = [
  172. 'companyCode'=>'require',
  173. ];
  174. $validate = new Validate($rule);
  175. if(!$validate->check($params)){
  176. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$params]);
  177. }
  178. $access_token = $this->_getToken($params['companyCode'],true);
  179. $filePath = $this->_upload('media1');
  180. $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$access_token}&type=card_logo";
  181. $data = [
  182. "type"=>"image",
  183. "media"=>new \CURLFile($filePath)
  184. ];
  185. $returnJson = HelperService::httpPost($url,$data,true,[],false);
  186. $decodeJson = json_decode($returnJson,true);
  187. HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$decodeJson]);
  188. }
  189. /**
  190. * 获取门店信息
  191. */
  192. public function getStoreList(){
  193. $companyCode = 'SHYL';
  194. WxPayConfig::$companyCode();
  195. $access_token = $this->_getToken($companyCode,true);
  196. $url = "https://api.weixin.qq.com/cgi-bin/poi/getpoilist?access_token={$access_token}";
  197. $res = HelperService::httpPost($url, json_encode(['begin'=>0,'limit'=>50]), true);
  198. $res = @json_decode($res,true);
  199. if($res['errcode']!=0){
  200. var_dump($res);exit;
  201. }
  202. echo "\n=====\n";
  203. foreach($res['business_list'] as $item){
  204. echo $item['base_info']['poi_id'].",";
  205. }
  206. echo "\n==".count($res['business_list'])."==\n";
  207. }
  208. /**
  209. * 获取门店信息
  210. */
  211. public function paycell(){
  212. $cardId = $this->request->param('card_id');
  213. $companyCode = 'SHYL';
  214. WxPayConfig::$companyCode();
  215. $access_token = $this->_getToken($companyCode,true);
  216. $url = "https://api.weixin.qq.com/card/paycell/set?access_token={$access_token}";
  217. $res = HelperService::httpPost($url, json_encode(['card_id'=>$cardId,'is_open'=>true]), true);
  218. $res = @json_decode($res,true);
  219. var_dump($res);
  220. }
  221. /**
  222. * 创建卡券
  223. * 参考连接https://www.w3cschool.cn/weixinkaifawendang/rkoi1qfa.html
  224. */
  225. public function createCard(){
  226. $companyCode = 'SMDD';
  227. WxPayConfig::$companyCode();
  228. $access_token = $this->_getToken($companyCode,true);
  229. $url = "https://api.weixin.qq.com/card/create?access_token={$access_token}";
  230. $data = '{
  231. "card": {
  232. "card_type": "MEMBER_CARD",
  233. "member_card": {
  234. "background_pic_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXdfLQNt3n1wdkovicibia49VUEdJbdHw9QRc6L5B1Itib2hKS1ricSFmfeDFegPqTBFpX4MBtnoUyIgmZg/0",
  235. "base_info": {
  236. "logo_url": "http://mmbiz.qpic.cn/mmbiz_jpg/tnZicgdFjqL06BkhRVwqU40BWNKziaz2EzhicuIRLfaJuFbEDoMHMKNjrBibLrRqD2iac3JY4nZzOB1BHSXiasfGrWHg/0",
  237. "brand_name": "客勤八久",
  238. "code_type": "CODE_TYPE_QRCODE",
  239. "title": "会员卡",
  240. "color": "Color010",
  241. "notice": "使用时向服务员出示此券",
  242. "service_phone": "021-51651616",
  243. "description": "会员权益不可与其他优惠同享",
  244. "date_info": {
  245. "type": "DATE_TYPE_FIX_TERM",
  246. "fixed_term":730,
  247. "fixed_begin_term":0
  248. },
  249. "pay_info":{
  250. "swipe_card":{
  251. "is_swipe_card":true
  252. }
  253. },
  254. "custom_url_name": "会员中心",
  255. "custom_url": "http://member.keqin89.com",
  256. "sku": {
  257. "quantity": 50000000
  258. },
  259. "get_limit": 1,
  260. "use_limit":50000000,
  261. "use_custom_code": true,
  262. "can_share":false,
  263. "can_give_friend": false
  264. },
  265. "advanced_info": {
  266. "abstract": {
  267. "abstract": "即可享有会员专属优惠,会员消费时优惠券、现金券自动抵扣,同时拥有多种优惠券时,自动按可享受最高折扣的优惠券进行抵扣使用",
  268. "icon_url_list": [
  269. "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT00BG9HrZsibOjNm6dfxbjiaYS4BFeNfwAUTv9F7UpAurDCB7ZeUQ7UZiaQ/0"
  270. ]
  271. },
  272. "text_image_list": [
  273. {
  274. "image_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT0Cehn0fUmJQamCmsKwabibrkPtLdDodgZ4AB5k3SNjPV6BiadO6ea9fAg/0",
  275. "text": "  我们一直以顾客的体验为初衷,不断提升门店的舒适度、为让顾客在更舒适、愉悦的心情下美美的用餐。"
  276. },
  277. {
  278. "image_url": "http://mmbiz.qpic.cn/mmbiz_jpg/LzP9djqouXfzLkWajaZfQAKVyZfvsaT0Rsm7nX2zw9dI41It88PQJIicFOUJJsWn0eQ4oNiab5kOTyQp96Z5atFA/0",
  279. "text": "  我们一直坚持品质、一直努力用心,为的就是让顾客更放心、安心、舒心的感受这舌尖上的美味。"
  280. }
  281. ],
  282. "time_limit": [
  283. ],
  284. "business_service": [
  285. "BIZ_SERVICE_DELIVER"
  286. ]
  287. },
  288. "auto_activate": true,
  289. "supply_bonus": false,
  290. "supply_balance": false,
  291. "prerogative":"注册会员"
  292. }
  293. }
  294. }';
  295. echo "1url:$url\n";
  296. $res = HelperService::httpPost($url, $data, true);
  297. $res = @json_decode($res,true);
  298. var_dump($res);exit;
  299. }
  300. public function updateCard(){
  301. $companyCode = 'SHYL';
  302. WxPayConfig::$companyCode();
  303. $access_token = $this->_getToken($companyCode,true);
  304. $url = "https://api.weixin.qq.com/card/update?access_token={$access_token}";
  305. //pIa9rt9EyR4IvGsWq7VY6HfKQ39U
  306. //pIa9rt417yEFidbNitU1wmcuqn_I
  307. //"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]
  308. //$data ='{
  309. // "card_id":"pIa9rt9EyR4IvGsWq7VY6HfKQ39U",
  310. // "member_card": {
  311. // "base_info": {
  312. // "can_give_friend": true,
  313. // "type":"DATE_TYPE_FIX_TIME_RANGE",
  314. // "begin_timestamp":1539792000,
  315. // "end_timestamp":1640966400
  316. // }
  317. //}
  318. //}';
  319. $data = [
  320. "card_id"=>"pIa9rt9EyR4IvGsWq7VY6HfKQ39U",
  321. "member_card"=>[
  322. "base_info"=>[
  323. "can_give_friend"=>true,
  324. "date_info"=>[
  325. "type"=>"DATE_TYPE_FIX_TIME_RANGE",
  326. "begin_timestamp"=>1514736000,
  327. "end_timestamp"=>1640966400
  328. ]
  329. ]
  330. ]
  331. ];
  332. echo json_encode($data);
  333. echo "1url:$url\n";
  334. $res = HelperService::httpPost($url, json_encode($data), true);
  335. $res = @json_decode($res,true);
  336. var_dump($res);exit;
  337. }
  338. /**
  339. * 返回保存路径
  340. * @param $key
  341. * @return type
  342. */
  343. private function _upload($key){
  344. // 获取表单上传文件 例如上传了001.jpg
  345. $file = $this->request->file($key);
  346. // 移动到框架应用根目录/public/uploads/ 目录下
  347. if($file){
  348. $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  349. if($info){
  350. // 成功上传后 获取上传信息
  351. // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
  352. return $info->getPath().DS.$info->getFilename();
  353. }else{
  354. // 上传失败获取错误信息
  355. HelperService::returnJson(['code'=>400,'msg'=>$file->getError(),'data'=>[]]);
  356. }
  357. }
  358. HelperService::returnJson(['code'=>400,'msg'=>'没有上传图片','data'=>[]]);
  359. }
  360. /**
  361. * 验证公司是否正确
  362. * @param $param
  363. * @return mixed
  364. */
  365. private function _validCompanyCode($param){
  366. if(isset($param['companyCode'])){
  367. $AuthCompanyModel = new AuthCompanyModel();
  368. $companyCode = $param['companyCode'];
  369. $company = $AuthCompanyModel->getInfo(['company_code'=>$companyCode]);
  370. if(empty($company)){
  371. die("I don't have this companyCode.");
  372. }
  373. session('company_db_json',$company['db_json']);
  374. session('SmsConfig',json_decode($company['sms_json'],true));
  375. session('companyCode',$param['companyCode']);
  376. }
  377. if(!session('companyCode')){
  378. die("I don't know companyCode.");
  379. }
  380. $companyCode = session('companyCode');
  381. $this->_loadConfig();
  382. return $companyCode;
  383. }
  384. private function _loadConfig(){
  385. $db_json = session('company_db_json');
  386. $db_arr = json_decode($db_json,true);
  387. if(empty($db_arr)){
  388. return true;
  389. }
  390. foreach($db_arr as $k=>$v){
  391. $data = [
  392. 'type' => 'mysql',
  393. 'hostname' => '127.0.0.1',
  394. 'database' => 'test',
  395. 'username' => 'root',
  396. 'password' => '',
  397. 'hostport' => '3306',
  398. 'params' => [],
  399. 'charset' => 'utf8',
  400. 'prefix' => '',
  401. ];
  402. $data['type'] = $v['type'];
  403. $data['hostname'] = $v['hostname'];
  404. $data['database'] = $v['database'];
  405. $data['username'] = $v['username'];
  406. $data['password'] = $v['password'];
  407. Config::set($v['config_name'],$data);
  408. }
  409. }
  410. }