123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\xmwechat\miniprogram\MpPayService;
- use app\common\library\xmwechat\miniprogram\MpService;
- use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
- use EasyWeChat\Kernel\Exceptions\RuntimeException;
- use think\db\exception\PDOException;
- use think\exception\ValidateException;
- use app\admin\model\oauth\Log;
- use think\Request;
- use app\admin\model\AdminLog;
- use think\facade\Db;
- use app\admin\model\Admin;
- /**
- * 小程序
- */
- class Miniprogram extends Backend
- {
- protected array $noNeedLogin = ['miniprogramLogin', 'miniprogramBind', 'checkText', 'checkMedia', 'messageServe','getTokens'];
- protected array $noNeedPermission = ['index'];
- public function initialize(): void
- {
- $this->model = new \app\admin\model\oauth\Log;
- parent::initialize();
- }
- //获取token
- public function getTokens()
- {
- return $this->auth->getToken();
- }
-
- /**
- * 微信小程序获一键登录
- * @param Request $request
- * @return void
- * @author wb
- */
- public function miniprogramLogin(Request $request): void
- {
- if ($request->isPost()) {
- // code 前端获取,传递给接口
- $params = $request->only(['code']);
- $result = MpService::getInstance()->getOpenidAndUnionid($params);
- $user_id= Log::where('uuid', $result['unionid'])->Value('user_id');
- if (empty($user_id)) {
- $this->error(__('当前微信未绑定'));
-
- }
- $user_info=Admin::where('id',$user_id)->find();
- $username=$user_info['username'];
- $password=$user_info['password'];
- $res = $this->auth->wxlogin($username, $password);
- AdminLog::setTitle(__('Login'));
- if ($res) {
- // 这里获取到Openid、Unionid
- $this->success('success', ['userInfo' => $this->auth->getInfo()] );
- } else {
- $this->error('error');
- }
- }
- }
- /**
- * 微信小程序账号绑定
- * @param Request $request
- * @return void
- * @author wb
- */
- public function miniprogramBind(Request $request): void
- {
- if ($request->isPost()) {
- // code 前端获取,传递给接口
-
-
- $params['code'] = $request->only(['code']);
- $params['uid'] = $request->only(['uid']);
-
- $result = MpService::getInstance()->getOpenidAndUnionid($params['code']);
- $res=Log::create([
- 'user_id' => $params['uid']['uid'],
- 'source' => 'wechat_scan',
- 'uuid' => $result['unionid'],
- 'extend' => 'null',
- ]);
-
- if ($res) {
-
- $this->success('success', $res );
- } else {
- $this->error('error');
- }
- }
- }
-
- /**
- * 微信小程序获取手机号示例
- * @param Request $request
- * @return void
- * @author wb
- * 3
- */
- public function getPhoneNumber(Request $request): void
- {
- if ($request->isPost()) {
- // code 前端获取,传递给接口
- $params = $request->only(['code']);
- $result = MpService::getInstance()->getPhoneNumber($params);
- if (!empty($result)) {
- // 这里获取到手机号
- $this->success('success', $result);
- } else {
- $this->error('error');
- }
- }
- }
-
- }
|