diff --git a/app/api/controller/User.php b/app/api/controller/User.php index 05a47b30..5a752f33 100644 --- a/app/api/controller/User.php +++ b/app/api/controller/User.php @@ -12,20 +12,21 @@ declare (strict_types=1); namespace app\api\controller; +use app\api\model\Agreement as AgreementModel; use app\api\model\User as UserModel; use app\api\model\user\BalanceLog; use app\api\model\user\GoodSource as GoodsSourceModel; use app\api\model\user\InvoiceOrder; -use app\api\model\user\UserInvoice; use app\api\model\user\UserCoupon; +use app\api\model\user\UserInvoice; use app\api\model\UserCoupon as UserCouponModel; -use app\api\model\Agreement as AgreementModel; use app\api\service\Feedback; use app\api\service\User as UserService; -use app\common\enum\dealer\withdraw\PayType; -use app\common\model\Agreement; +use app\common\service\qrcode\InviteUser; use cores\exception\BaseException; -use think\facade\Log; +use think\db\exception\DataNotFoundException; +use think\db\exception\DbException; +use think\db\exception\ModelNotFoundException; use think\response\Json; /** @@ -40,10 +41,12 @@ class User extends Controller * [delete description] * @return [type] [description] */ - public function delete(){ + public function delete() + { $user_id = UserService::getCurrentLoginUserId(); - UserModel::where('user_id', $user_id)->update(['is_delete' => 1,'update_time' => time()]); + UserModel::where('user_id', $user_id)->update(['is_delete' => 1, 'update_time' => time()]); } + /** * 当前用户详情 * @return Json @@ -226,7 +229,7 @@ class User extends Controller public function delInvoicing(): Json { $service = new UserInvoice(); - if(!$service->del($this->request->param())) { + if (!$service->del($this->request->param())) { return $this->renderSuccess($service->getError() ?: '操作失败'); } return $this->renderSuccess('删除成功·'); @@ -272,7 +275,8 @@ class User extends Controller /** * 个人中心协议 */ - public function getAgreement(): Json { + public function getAgreement(): Json + { $params = $this->request->param(); if (empty($params['type'])) { return $this->renderSuccess("参数错误"); @@ -284,7 +288,8 @@ class User extends Controller /** * 添加用户浏览记录 */ - public function addView(): Json { + public function addView(): Json + { $goods_id = $this->request->param('goods_id'); $user_id = UserService::getCurrentLoginUserId(); $userView = new UserCoupon(); @@ -294,4 +299,21 @@ class User extends Controller return $this->renderSuccess($userView->getError() ?: '记录失败'); } + /** + * @notes:邀请好友海报 + * @return Json + * @throws BaseException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + * @author: wanghousheng + */ + public function InviteUserPoster(): Json + { + $userInfo = UserService::getCurrentLoginUser(true); + // 生成二维码 + $Qrcode = new InviteUser($userInfo, 'MP-WEIXIN'); + return $this->renderSuccess(['imageUrl' => $Qrcode->getImage()]); + } + } diff --git a/app/common/library/wechat/Qrcode.php b/app/common/library/wechat/Qrcode.php index 4fffcffc..8f760926 100644 --- a/app/common/library/wechat/Qrcode.php +++ b/app/common/library/wechat/Qrcode.php @@ -38,6 +38,8 @@ class Qrcode extends WxBase $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}"; // 构建请求 $data = compact('scene', 'width'); +// $data['env_version'] = 'trial'; +// $data['check_path'] = false; !is_null($page) && $data['page'] = $page; // 返回结果 $result = $this->post($url, helper::jsonEncode($data)); diff --git a/app/common/service/qrcode/InviteUser.php b/app/common/service/qrcode/InviteUser.php new file mode 100644 index 00000000..d59b78d3 --- /dev/null +++ b/app/common/service/qrcode/InviteUser.php @@ -0,0 +1,118 @@ +userInfo = $userInfo; + // 当前客户端 + $this->channel = $channel; + } + + /** + * 拼接海报图 + * @return string + * @throws BaseException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + * @throws Exception + */ + public function getImage(): string + { + // 判断海报图文件存在则直接返回url + if (file_exists($this->getPosterPath())) { + return $this->getPosterUrl(); + } + // 商城ID + $storeId = $this->userInfo['store_id']; + // 商品海报背景图 + $backdrop = __DIR__ . '/resource/invite_user.png'; + // 小程序码参数 + $scene = "refereeId:" . ($this->userInfo['user_id'] ?: ''); + // 下载小程序码 + $page = 'pages/login/index'; + $qrcode = $this->getQrcode($storeId, $scene, null, $this->channel); + // 拼接海报图 + return $this->savePoster($backdrop, $qrcode); + } + + /** + * 拼接海报图 + * @param string $backdrop 背景图路径 + * @param string $qrcode 二维码图路径 + * @return string + * @throws Exception + */ + private function savePoster(string $backdrop, string $qrcode): string + { + // 实例化图像编辑器 + $editor = Grafika::createEditor(['Gd']); + // 打开海报背景图 + $editor->open($backdropImage, $backdrop); + // 打开小程序码 + $editor->open($qrcodeImage, $qrcode); + // 重设小程序码宽高 + $editor->resizeExact($qrcodeImage, 140, 140); + // 小程序码添加到背景图 + $editor->blend($backdropImage, $qrcodeImage, 'normal', 1.0, 'top-left', 520, 700); + // 保存图片 + $editor->save($backdropImage, $this->getPosterPath()); + return $this->getPosterUrl(); + } + + /** + * 海报图文件路径 + * @return string + */ + private function getPosterPath(): string + { + // 保存路径 + $tempPath = web_path() . "temp/{$this->userInfo['store_id']}/"; + !is_dir($tempPath) && mkdir($tempPath, 0755, true); + return $tempPath . $this->getPosterName(); + } + + /** + * 海报图文件名称 + * @return string + */ + private function getPosterName(): string + { + return 'invite_' . md5("{$this->userInfo['user_id']}_$this->channel") . '.png'; + } + + /** + * 海报图url + * @return string + */ + private function getPosterUrl(): string + { + return base_url() . 'temp/' . $this->userInfo['store_id'] . '/' . $this->getPosterName() . '?t=' . time(); + } +} \ No newline at end of file diff --git a/app/common/service/qrcode/resource/invite_user.png b/app/common/service/qrcode/resource/invite_user.png new file mode 100644 index 00000000..2d586453 Binary files /dev/null and b/app/common/service/qrcode/resource/invite_user.png differ