Miniprogram.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\xmwechat\miniprogram\MpPayService;
  5. use app\common\library\xmwechat\miniprogram\MpService;
  6. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  7. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  8. use think\db\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use app\admin\model\oauth\Log;
  11. use think\Request;
  12. use app\admin\model\AdminLog;
  13. use think\facade\Db;
  14. use app\admin\model\Admin;
  15. /**
  16. * 小程序
  17. */
  18. class Miniprogram extends Backend
  19. {
  20. protected array $noNeedLogin = ['miniprogramLogin', 'miniprogramBind', 'checkText', 'checkMedia', 'messageServe','getTokens'];
  21. protected array $noNeedPermission = ['index'];
  22. public function initialize(): void
  23. {
  24. $this->model = new \app\admin\model\oauth\Log;
  25. parent::initialize();
  26. }
  27. //获取token
  28. public function getTokens()
  29. {
  30. return $this->auth->getToken();
  31. }
  32. /**
  33. * 微信小程序获一键登录
  34. * @param Request $request
  35. * @return void
  36. * @author wb
  37. */
  38. public function miniprogramLogin(Request $request): void
  39. {
  40. if ($request->isPost()) {
  41. // code 前端获取,传递给接口
  42. $params = $request->only(['code']);
  43. $result = MpService::getInstance()->getOpenidAndUnionid($params);
  44. $user_id= Log::where('uuid', $result['unionid'])->Value('user_id');
  45. if (empty($user_id)) {
  46. $this->error(__('当前微信未绑定'));
  47. }
  48. $user_info=Admin::where('id',$user_id)->find();
  49. $username=$user_info['username'];
  50. $password=$user_info['password'];
  51. $res = $this->auth->wxlogin($username, $password);
  52. AdminLog::setTitle(__('Login'));
  53. if ($res) {
  54. // 这里获取到Openid、Unionid
  55. $this->success('success', ['userInfo' => $this->auth->getInfo()] );
  56. } else {
  57. $this->error('error');
  58. }
  59. }
  60. }
  61. /**
  62. * 微信小程序账号绑定
  63. * @param Request $request
  64. * @return void
  65. * @author wb
  66. */
  67. public function miniprogramBind(Request $request): void
  68. {
  69. if ($request->isPost()) {
  70. // code 前端获取,传递给接口
  71. $params['code'] = $request->only(['code']);
  72. $params['uid'] = $request->only(['uid']);
  73. $result = MpService::getInstance()->getOpenidAndUnionid($params['code']);
  74. $res=Log::create([
  75. 'user_id' => $params['uid']['uid'],
  76. 'source' => 'wechat_scan',
  77. 'uuid' => $result['unionid'],
  78. 'extend' => 'null',
  79. ]);
  80. if ($res) {
  81. $this->success('success', $res );
  82. } else {
  83. $this->error('error');
  84. }
  85. }
  86. }
  87. /**
  88. * 微信小程序获取手机号示例
  89. * @param Request $request
  90. * @return void
  91. * @author wb
  92. * 3
  93. */
  94. public function getPhoneNumber(Request $request): void
  95. {
  96. if ($request->isPost()) {
  97. // code 前端获取,传递给接口
  98. $params = $request->only(['code']);
  99. $result = MpService::getInstance()->getPhoneNumber($params);
  100. if (!empty($result)) {
  101. // 这里获取到手机号
  102. $this->success('success', $result);
  103. } else {
  104. $this->error('error');
  105. }
  106. }
  107. }
  108. }