WxController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\web\controller;
  12. use service\DistributorService;
  13. use service\UserService;
  14. use think\Db;
  15. use app\common\plugin\Jwt;
  16. use app\common\plugin\WxBizDataCrypt;
  17. use app\common\plugin\Jssdk;
  18. //wx类
  19. class WxController extends MainController
  20. {
  21. public $app_id,$app_secret,$accessToken;//公众号
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $wxconfig=config()['config']['wxconfig'];
  26. $this->app_id=$wxconfig['appid'];
  27. $this->app_secret=$wxconfig['secret'];
  28. }
  29. public function login(){
  30. //微信小程序
  31. $code = input("code", '', 'htmlspecialchars_decode');
  32. $avatar=$this->request->param('avatar','');
  33. $wx_nickname=$this->request->param('nickname','匿名用户');
  34. $url = 'https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token='.$this->getAccess_token().'&js_code='.$code.'&grant_type=authorization_code';
  35. //通过code换取openid session _key
  36. $res = $this->curl_get($url);
  37. if(!$res)
  38. $this->api_return_json(['msg'=>'请求微信服务器失败']);
  39. $res=(array)$res;
  40. if (!isset($res['session_key'])) {
  41. $this->api_return_json(['msg'=>'requestTokenFailed']);
  42. }
  43. $user_data['wx_openid']=$res['userid'];
  44. $user_data['corpid']=$res['corpid'];
  45. $user_data['reg_time'] = time();
  46. $user_data['ip']=ip2long($this->ip);
  47. $rand_str = md5($user_data['reg_time']);
  48. $user_data['username'] = 'wx' . $user_data['reg_time'] . substr($rand_str, 0, 4);
  49. $user_data['major']=$this->major;
  50. if(!empty($avatar)){
  51. $user_data['avatar'] =$avatar;
  52. $user_data['wx_nickname']=$wx_nickname;
  53. }
  54. // $res['userid']='1254565412212';$user_data=array();//测试数据
  55. $user= Db::name('wx_user')->where('wx_openid',$res['userid'])->find();
  56. if(empty($user)){
  57. $user_id=Db::name('wx_user')->insertGetId($user_data);
  58. }else{
  59. $user_id=$user['user_id'];
  60. $update_date=array('ip'=>ip2long($this->ip),'major'=>$this->major);
  61. if(!empty($avatar)&&!empty($wx_nickname)){
  62. $update_date['avatar']=$avatar;
  63. $update_date['wx_nickname']=$wx_nickname;
  64. }
  65. db('wx_user')->where('user_id',$user['user_id'])->update($update_date);
  66. }
  67. $token=JWT::getToken($user_id,$this->major);
  68. //
  69. // $distributor_id=$this->request->get('distributor_id');
  70. // $app_id=$this->request->get('app_id');
  71. // if(!empty($distributor_id)){
  72. // $distributor=DistributorService::getOne([['id','=',$distributor_id],['status','=',1]]);
  73. // $result=UserService::update([['user_id','=',$user_id]],['distributor_bind_id'=>$distributor_id]);
  74. // $distributor->setInc('share_num');
  75. // }
  76. // if(!empty($app_id)){
  77. // $distributor=DistributorService::getOne([['app_id','=',$distributor_id],['status','=',1]]);
  78. // $result=UserService::update([['user_id','=',$user_id]],['distributor_bind_id'=>$distributor['id']]);
  79. // $distributor->setInc('share_num');
  80. // }
  81. $this->api_return_json(array('user_data'=>$user_data,'session_id'=>$token,'is_audit'=>config('config.is_audit')),1);
  82. }
  83. public function getRunData(){
  84. $code = $this->request->param('code');
  85. $encryptedData = $this->request->param('encryptedData');
  86. $iv = $this->request->param('iv');
  87. if(!isset($code)||empty($code)){
  88. $this->api_return_json(array('msg'=>'code异常'));
  89. }
  90. if(!isset($encryptedData)||empty($encryptedData)){
  91. $this->api_return_json(array('msg'=>'encryptedData异常'));
  92. }
  93. if(!isset($iv)||empty($iv)){
  94. $this->api_return_json(array('msg'=>'iv异常'));
  95. }
  96. $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->app_id . '&secret=' . $this->app_secret . '&js_code='.$code.'&grant_type=authorization_code';
  97. //通过code换取openid session _key
  98. $res = $this->curl_get($url);
  99. if(!$res)
  100. $this->api_return_json(['msg'=>'请求微信服务器失败']);
  101. $res=(array)$res;
  102. if (!isset($res['session_key'])) {
  103. $this->api_return_json(['msg'=>'requestTokenFailed']);
  104. }
  105. $pc=new WXBizDataCrypt($this->app_id, $res['session_key']);
  106. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  107. if ($errCode == 0||true) {
  108. $res = json_decode($data, true);
  109. $stepInfoList=$res['stepInfoList'];
  110. $time=array();
  111. $data=array();
  112. foreach ($stepInfoList as &$val){
  113. $val['timestamp']=date('Y-m-d',$val['timestamp']);
  114. array_push($time, $val['timestamp']);
  115. array_push($data, $val['step']);
  116. }
  117. $result=array('time'=>$time,'data'=>$data);
  118. $this->api_return_json(array('stepInfoList'=>$result),1);
  119. } else {
  120. $this->api_return_json(array('msg'=>'解密失败'));
  121. }
  122. }
  123. public function getPhone(){
  124. $code = $this->request->param('code');
  125. $encryptedData = $this->request->param('encryptedData');
  126. $iv = $this->request->param('iv');
  127. if(!isset($code)||empty($code)){
  128. $this->api_return_json(array('msg'=>'code异常'));
  129. }
  130. if(!isset($encryptedData)||empty($encryptedData)){
  131. $this->api_return_json(array('msg'=>'encryptedData异常'));
  132. }
  133. if(!isset($iv)||empty($iv)){
  134. $this->api_return_json(array('msg'=>'iv异常'));
  135. }
  136. $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->app_id . '&secret=' . $this->app_secret . '&js_code='.$code.'&grant_type=authorization_code';
  137. //通过code换取openid session _key
  138. $res = $this->curl_get($url);
  139. if(!$res)
  140. $this->api_return_json(['msg'=>'请求微信服务器失败']);
  141. $res=(array)$res;
  142. if (!isset($res['session_key'])) {
  143. $this->api_return_json(['msg'=>'requestTokenFailed']);
  144. }
  145. $pc=new WXBizDataCrypt($this->app_id, $res['session_key']);
  146. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  147. if ($errCode == 0||true) {
  148. $res = json_decode($data, true);
  149. if(!isset($res['phoneNumber'])||empty($res['phoneNumber'])){
  150. $this->api_return_json(array('msg'=>'手机号获取失败'));
  151. }
  152. //查询该小程序用户是否已经存在
  153. $user=Db::name('wx_user')->where('user_id',$this->userid)->find();
  154. if ($user) {
  155. //存在就更新登录时间,检查是否登录过期
  156. db('wx_user')->where('user_id',$this->userid)->update(array('phone'=>$res['phoneNumber'],'is_tied'=>1));
  157. $this->api_return_json(array('phone'=>$res['phoneNumber']),1);
  158. }
  159. $this->api_return_json(array('msg'=>'用户不存在'));
  160. } else {
  161. $this->api_return_json(array('msg'=>'解密失败'));
  162. }
  163. }
  164. //小程序卡券
  165. function cardCreate(){
  166. $jssdk=new Jssdk();
  167. $token=$jssdk->getAccessToken();//测试公众号的token
  168. $package=$jssdk->GetSignPackage();//测试公众号的token
  169. $url='https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token='.$token;
  170. //log图
  171. $data=array('buffer'=>new \CURLFile(realpath(CMF_ROOT.'public/static/images/headicon.png')));
  172. $image_url_log=$this->curl_post($url, $data);
  173. $image_url_log=json_decode($image_url_log)->url;
  174. //某一测试素材图
  175. $data=array('buffer'=>new \CURLFile(realpath(CMF_ROOT.'public/static/images/bg.jpg')));
  176. $image_url_one=$this->curl_post($url, $data);
  177. $image_url_one=json_decode($image_url_one)->url;
  178. //起止截至时间
  179. $begin_timestamp=time()-30;
  180. $end_timestamp=time()+30*24*3600;
  181. //创建卡券json数据 团购券
  182. $type='member_card';
  183. switch ($type){
  184. case 'groupon':
  185. $des='"deal_detail":"以下锅底2选1(有菌王锅、麻辣锅、大骨锅、番茄锅、清补 凉锅、酸菜鱼锅可选):\n大锅1份 12元\n小锅2份 16元 "';
  186. $memdes='';
  187. break;
  188. case 'cash':
  189. $des='"least_cost": 1000,"reduce_cost": 100';
  190. $memdes='';
  191. break;
  192. case 'discount':
  193. $des='"discount": 30';
  194. $memdes='';
  195. break;
  196. case 'gift':
  197. $des='"gift":"可兑换白色bra一个"';
  198. $memdes='';
  199. break;
  200. case 'general_coupon':
  201. $des='"default_detail":"优惠券专用,填写优惠详情"';
  202. $memdes='';
  203. break;
  204. case 'member_card':
  205. $des='"discount": 10';
  206. $memdes='"supply_bonus": true,
  207. "supply_balance": false,
  208. "prerogative": "test_prerogative",
  209. "auto_activate": true,
  210. "custom_field1": {
  211. "name_type": "FIELD_NAME_TYPE_LEVEL",
  212. "url": "http://www.qq.com"
  213. },
  214. "activate_url": "http://www.qq.com",
  215. "custom_cell1": {
  216. "name": "使用入口2",
  217. "tips": "激活后显示",
  218. "url": "http://www.qq.com"
  219. },
  220. "bonus_rule": {
  221. "cost_money_unit": 100,
  222. "increase_bonus": 1,
  223. "max_increase_bonus": 200,
  224. "init_increase_bonus": 10,
  225. "cost_bonus_unit": 5,
  226. "reduce_money": 100,
  227. "least_money_to_use_bonus": 1000,
  228. "max_reduce_bonus": 50
  229. },';
  230. break;
  231. default:
  232. $des='';
  233. $memdes='';
  234. break;
  235. }
  236. $card_res=$this->card_type($type);
  237. $card_type=$card_res['card_type'];
  238. $card_name=$card_res['card_name'];
  239. $data='{
  240. "card": {
  241. "card_type": "'.$card_type.'",
  242. "'.$card_name.'": {
  243. "base_info": {
  244. "logo_url": "'.$image_url_log.'",
  245. "brand_name": "微信餐厅",
  246. "code_type": "CODE_TYPE_TEXT",
  247. "title": "132元双人火锅套餐",
  248. "color": "Color010",
  249. "notice": "使用时向服务员出示此券",
  250. "service_phone": "020-88888888",
  251. "description": "不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食",
  252. "date_info": {
  253. "type": "DATE_TYPE_FIX_TIME_RANGE",
  254. "begin_timestamp": "'.$begin_timestamp.'",
  255. "end_timestamp": "'.$end_timestamp.'"
  256. },
  257. "sku": {
  258. "quantity": 500000
  259. },
  260. "use_limit":100,
  261. "get_limit": 3,
  262. "use_custom_code": false,
  263. "bind_openid": false,
  264. "can_share": true,
  265. "can_give_friend": true,
  266. "location_id_list": [
  267. 123,
  268. 12321,
  269. 345345
  270. ],
  271. "center_title": "云货舱",
  272. "center_sub_title": "立享优惠",
  273. "center_app_brand_user_name": "gh_83c581f00e36@app",
  274. "center_app_brand_pass":"pages/template/tabbar/tabbar",
  275. "custom_url_name": "立即使用",
  276. "custom_app_brand_user_name": "gh_83c581f00e36@app",
  277. "custom_app_brand_pass":"pages/template/tabbar/tabbar",
  278. "custom_url_sub_title": "前往使用",
  279. "promotion_url_name": "更多优惠",
  280. "promotion_url": "http://www.qq.com",
  281. "source": "大众点评"
  282. },
  283. "advanced_info": {
  284. "use_condition": {
  285. "accept_category": "鞋类",
  286. "reject_category": "阿迪达斯",
  287. "can_use_with_other_discount": true
  288. },
  289. "abstract": {
  290. "abstract": "微信餐厅推出多种新季菜品,期待您的光临",
  291. "icon_url_list": [
  292. "'.$image_url_log.'"
  293. ]
  294. },
  295. "text_image_list": [
  296. {
  297. "image_url": "'.$image_url_one.'",
  298. "text": "此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味蕾"
  299. },
  300. {
  301. "image_url": "'.$image_url_one.'",
  302. "text": "此菜品迎合大众口味,老少皆宜,营养均衡"
  303. }
  304. ],
  305. "time_limit": [
  306. {
  307. "type": "MONDAY",
  308. "begin_hour":0,
  309. "end_hour":10,
  310. "begin_minute":10,
  311. "end_minute":59
  312. },
  313. {
  314. "type": "HOLIDAY"
  315. }
  316. ],
  317. "business_service": [
  318. "BIZ_SERVICE_FREE_WIFI",
  319. "BIZ_SERVICE_WITH_PET",
  320. "BIZ_SERVICE_FREE_PARK",
  321. "BIZ_SERVICE_DELIVER"
  322. ]
  323. },'.$memdes.'
  324. '.$des.'
  325. }
  326. }
  327. }';
  328. $url='https://api.weixin.qq.com/card/create?access_token='.$token;
  329. $res=$this->curl_post($url, $data);
  330. $card_id=json_decode($res)->card_id;
  331. $ret=array();
  332. $ret['cardId']=$card_id;
  333. $ret['time']=time();
  334. //$arr['code'] = 1434008071;
  335. $arr['timestamp'] = $ret['time'];
  336. $arr['ticket'] = $package['ticket'];
  337. $arr['nonce_str'] = '123';
  338. $arr['card_id'] = $card_id;
  339. sort($arr,SORT_STRING);
  340. $str = '';
  341. foreach ($arr as $v) {
  342. $str .= $v;
  343. }
  344. $sign = sha1($str);
  345. $ret['signature']=$sign;
  346. $ret['ticket']=$package['ticket'];
  347. $this->api_return_json(array('url'=>$ret),1);
  348. $url='https://api.weixin.qq.com/card/qrcode/create?access_token='.$token;
  349. $data='{
  350. "action_name": "QR_CARD",
  351. "expire_seconds": 1800,
  352. "action_info": {
  353. "card": {
  354. "card_id": "'.$card_id.'",
  355. "is_unique_code": false ,
  356. "outer_str":"12b"
  357. }
  358. }
  359. }';
  360. $res=$this->curl_post($url, $data);
  361. $show_qrcode_url=json_decode($res)->show_qrcode_url;
  362. //设置体验白名单,用于开发者调试用
  363. $url='https://api.weixin.qq.com/card/testwhitelist/set?access_token='.$token;
  364. $data='{
  365. "username": [
  366. "fcy736337824",
  367. "empcity"
  368. ]
  369. }';
  370. $this->curl_post($url, $data);
  371. //白名单ending...
  372. if(!isset($show_qrcode_url)||empty($show_qrcode_url)){
  373. exit('二维码生成失败');
  374. }
  375. $this->api_return_json(array('url'=>$show_qrcode_url),1);
  376. }
  377. function card_type($type){
  378. //团购券
  379. $data=array('groupon'=>array('card_type'=>'GROUPON','card_name'=>'groupon'),
  380. 'cash'=>array('card_type'=>'CASH','card_name'=>'cash'),
  381. 'discount'=>array('card_type'=>'DISCOUNT','card_name'=>'discount'),
  382. 'gift'=>array('card_type'=>'GIFT','card_name'=>'gift'),
  383. 'general_coupon'=>array('card_type'=>'GENERAL_COUPON','card_name'=>'general_coupon'),
  384. 'member_card'=>array('card_type'=>'MEMBER_CARD','card_name'=>'member_card')
  385. );
  386. return $data[$type];
  387. }
  388. }