AppletController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace api\applet\controller;
  3. use api\applet\model\UserModel;
  4. use api\common\Http;
  5. use api\common\Sms;
  6. use cmf\controller\RestBaseController;
  7. use think\Db;
  8. class AppletController extends RestBaseController
  9. {
  10. const TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token'; //get
  11. const APPLET_CODE_URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit'; //post
  12. const CODE_URL = 'https://api.weixin.qq.com/sns/jscode2session'; //post
  13. /**
  14. * 根据code登录
  15. * @param $code
  16. */
  17. public function login()
  18. {
  19. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  20. //获取open_id
  21. $code = $this->request->post('code');
  22. $url = self::CODE_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] . '&js_code=' . $code . '&grant_type=authorization_code';
  23. $header = ['content-type: application/json'];
  24. $result = Http::http_post_json($url, [], $header);
  25. if (!empty($result['errcode'])) {
  26. $this->error($result['errmsg']);
  27. }
  28. //获取token
  29. $user = UserModel::where('open_id', $result['openid'])->find();
  30. if (empty($user)) {
  31. $user = UserModel::create([
  32. 'user_pass' => cmf_password('123456'),
  33. 'open_id' => $result['openid'],
  34. 'union_id' => $result['unionid'],
  35. 'user_type' => 2,
  36. 'create_time' => time(),
  37. ]);
  38. }
  39. if (empty($user['unionid'])) {
  40. $user->union_id = $result['unionid'];
  41. $user->save();
  42. }
  43. //写入token
  44. $token = md5(uniqid()) . md5(uniqid());
  45. $currentTime = time();
  46. $expireTime = $currentTime + 24 * 3600 * 180;
  47. Db::name("user_token")->insert([
  48. 'token' => $token,
  49. 'user_id' => $user['id'],
  50. 'expire_time' => $expireTime,
  51. 'create_time' => $currentTime,
  52. 'device_type' => $this->deviceType,
  53. ]);
  54. $this->success('OK',['token' => $token, 'expires_time' => $expireTime]);
  55. }
  56. public function test()
  57. {
  58. //短信
  59. /*$sms = new Sms();
  60. $sms->send('13313826760','talent_appointment_end');*/
  61. $this->success('发送成功');
  62. }
  63. public function t2()
  64. {
  65. $start_time = strtotime(date('Y-m-d H:00:00'));
  66. $end_time = strtotime(date('Y-m-d H:00:00').' +1 hours');
  67. $now = time();
  68. $url = "https://cnapi.sciener.com/v3/keyboardPwd/get";
  69. $data = [
  70. 'clientId' => '3280d286853f4588addae034f855333e',
  71. 'accessToken' => 'd2abf38a91bc0238796043bb02686d34',
  72. 'lockId' => 19406485,
  73. 'keyboardPwdType' => 1,
  74. 'startDate' => $start_time * 1000,
  75. 'endDate' => $end_time * 1000,
  76. 'date' => $now * 1000,
  77. ];
  78. $header = ['Content-Type: application/x-www-form-urlencoded'];
  79. $result = Http::http_post_json($url, $data, $header);
  80. halt($result);
  81. }
  82. public function _getAccessToken()
  83. {
  84. $time = time();
  85. $wx_access_token = cmf_get_option('wx_access_token');
  86. if (empty($wx_access_token) || $wx_access_token['expires_time'] < $time) {
  87. $wxappSettings = cmf_get_option('wxapp_settings')['default'];
  88. $url = self::TOKEN_URL . '?appid=' . $wxappSettings['app_id'] . '&secret=' . $wxappSettings['app_secret'] .'&grant_type=client_credential';
  89. $header = ['content-type: application/json'];
  90. $wx_access_token = Http::http_post_json($url, '', $header);
  91. $wx_access_token['expires_time'] = $time + $wx_access_token['expires_in'];
  92. cmf_set_option('wx_access_token',$wx_access_token);
  93. }
  94. return $wx_access_token['access_token'];
  95. }
  96. }