AppletController.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace api\applet\controller;
  3. use api\applet\model\UserModel;
  4. use api\common\Http;
  5. use cmf\controller\RestBaseController;
  6. use think\Db;
  7. class AppletController extends RestBaseController
  8. {
  9. const TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token'; //get
  10. const APPLET_CODE_URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; //post
  11. const CODE_URL = 'https://api.weixin.qq.com/sns/jscode2session'; //post
  12. /**
  13. * 根据code登录
  14. * @param $code
  15. */
  16. public function login()
  17. {
  18. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  19. //获取open_id
  20. $code = $this->request->post('code');
  21. $url = self::CODE_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] . '&js_code=' . $code . '&grant_type=authorization_code';
  22. $header = ['content-type: application/json'];
  23. $result = Http::http_post_json($url, '', $header);
  24. if (!empty($result['errcode'])) {
  25. $this->error($result['errmsg']);
  26. }
  27. //获取token
  28. $user = UserModel::where('open_id', $result['openid'])->find();
  29. if (empty($user)) {
  30. $user = UserModel::create([
  31. 'user_pass' => cmf_password('123456'),
  32. 'open_id' => $result['openid'],
  33. 'union_id' => $result['unionid'],
  34. 'user_type' => 2,
  35. 'create_time' => time(),
  36. ]);
  37. }
  38. if (empty($user['unionid'])) {
  39. $user->union_id = $result['unionid'];
  40. $user->save();
  41. }
  42. //写入token
  43. $token = md5(uniqid()) . md5(uniqid());
  44. $currentTime = time();
  45. $expireTime = $currentTime + 24 * 3600 * 180;
  46. Db::name("user_token")->insert([
  47. 'token' => $token,
  48. 'user_id' => $user['id'],
  49. 'expire_time' => $expireTime,
  50. 'create_time' => $currentTime,
  51. 'device_type' => $this->deviceType,
  52. ]);
  53. $this->success('OK',['token' => $token, 'expires_time' => $expireTime]);
  54. }
  55. public function test()
  56. {
  57. /*$url = 'https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={$token}';
  58. $data = '{"rid":1}';
  59. $header = ['content-type: application/json'];
  60. $result = Http::http_post_json($url, $data, $header);
  61. halt($result);*/
  62. $token = $this->_getAccessToken();
  63. $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
  64. $data = [
  65. 'touser' => 'oQeNu5TLNQBgPOKBoH4wK8VyAHMQ',
  66. 'msgtype' => 'text',
  67. 'text' => ['content'=>'Hello World']
  68. ];
  69. $header = ['content-type: application/json'];
  70. $result = Http::http_post_json($url, json_encode($data), $header);
  71. halt($result);
  72. }
  73. public function _getAccessToken()
  74. {
  75. $time = time();
  76. $wx_access_token = cmf_get_option('wx_access_token');
  77. if (empty($wx_access_token) || $wx_access_token['expires_time'] < $time) {
  78. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  79. $url = self::TOKEN_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] .'&grant_type=client_credential';
  80. $header = ['content-type: application/json'];
  81. $wx_access_token = Http::http_post_json($url, '', $header);
  82. $wx_access_token['expires_time'] = $time + $wx_access_token['expires_in'];
  83. cmf_set_option('wx_access_token',$wx_access_token);
  84. }
  85. return $wx_access_token['access_token'];
  86. }
  87. }