Просмотр исходного кода

[智价云] 公众号订阅功能开关

tangyuanwang 1 неделя назад
Родитель
Сommit
0e9844ce82

+ 48 - 0
app/Http/Controllers/Api/Personnel/Subscribe.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App\Http\Controllers\Api\Personnel;
+
+use App\Http\Controllers\Api\Api;
+use App\Http\Requests\Api\Personnel\Subscribe as Request;
+use App\Models\Api\Personnel\Employee as EmployeeModel;
+
+/**
+ * 员工订阅管理
+ * @author 唐远望
+ * @version 1.0
+ * @date 2026-03-03
+ */
+class Subscribe extends Api
+{
+    /**
+     * 订阅开关
+     * @author    唐远望
+     * @version   1.0
+     * @date      2026-03-03
+     * 
+     */
+    public function change_subscribe(Request $request, EmployeeModel $EmployeeModel)
+    {
+
+        $request->scene('change_subscribe')->validate();
+        $user_info = $this->checkLogin();
+        if (!$user_info) return json_send(['code' => 'error', 'msg' => '请先登录']);
+        $company_id = $user_info['company_id'];
+        $uid = $user_info['uid'];
+        $open_subscribe = request('open_subscribe', 0); //订阅状态0=开启1=关闭 (默认开启)
+        // 查询条件
+        $map  = [];
+        // 其他条件
+        $map[] = ['id', '=', $uid];
+        $map[] = ['company_id', '=', $company_id];
+        // 查询数据
+        $result_model = $EmployeeModel->query();
+        $user_info = $result_model->where($map)->first();
+        // 分配数据
+        if (!$user_info)  return json_send(['code' => 'error', 'msg' => '个人信息获取错误']);
+        $user_info->open_subscribe = $open_subscribe;
+        $user_info->save();
+        // 加载模板
+        return        json_send(['code' => 'success', 'msg' => '订阅成功', 'data' => '']);
+    }
+}

+ 64 - 0
app/Http/Requests/Api/Personnel/Subscribe.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Http\Requests\Api\Personnel;
+
+use App\Http\Requests\BaseRequest;
+
+/**
+ * 员工订阅管理-请求类
+ * @author 唐远望
+ * @version 1.0
+ * @date 2026-03-03
+ * 
+ */
+class Subscribe extends BaseRequest
+{
+    /**
+     * 获取应用于请求的规则
+     *
+     * @return array
+     */
+    public function rules()
+    {
+        // 返回结果
+        return      [
+            'name'                 => 'required',
+            'id'                => 'required|integer|gt:0',
+            'status'            => 'required|integer|in:0,1',
+            'page'              => 'integer|min:1',
+            'limit'             => 'integer|min:1',
+            'open_subscribe'    => 'required|integer|in:0,1',
+        ];
+    }
+
+
+    // 场景列表
+    protected   $scenes         = [
+        'change_subscribe'             => ['open_subscribe'],
+    ];
+
+    /**
+     * 获取已定义验证规则的错误消息
+     *
+     * @return array
+     */
+    public function messages()
+    {
+        return [
+            'name.required'     => '姓名必填',
+            'id.required'       => 'ID未知',
+            'id.integer'        => 'ID格式错误',
+            'id.gt'               => 'ID格式错误',
+            'status.required'   => '状态未知',
+            'status.integer'    => '状态格式错误',
+            'status.in'         => '状态格式错误',
+            'page.integer'      => '页码格式错误',
+            'page.min'          => '页码格式错误',
+            'limit.integer'     => '每页数量格式错误',
+            'limit.min'         => '每页数量格式错误',
+            'open_subscribe.required'   => '订阅状态未知',
+            'open_subscribe.integer'    => '订阅状态格式错误',
+            'open_subscribe.in'         => '订阅状态格式错误',
+        ];
+    }
+}

+ 98 - 0
app/Servers/Wechat/Official.php

@@ -0,0 +1,98 @@
+<?php namespace App\Servers\Wechat;
+
+use EasyWeChat\Factory;
+
+/**
+ * 微信公众号
+ * 
+ */
+class Official
+{
+    // 工作列表
+    protected \EasyWeChat\OfficialAccount\Application  $app;
+
+    // 构造函数
+    public function __construct(){
+        // 应用实例
+        $this->app                          = $this->getApp();
+    }
+
+    /**
+     * 获取应用实例
+     * 
+     * @return \EasyWeChat\OfficialAccount\Application
+     * 
+     */
+    public function getApp(){
+        // 获取配置
+		$this->app                          = Factory::officialAccount(config('wechat.official',[]));
+        // 返回结果
+        return                              $this->app;
+    }
+
+    /**
+     * 获取user
+     * @param  string   $code           通过 前端授权code获取用户openid
+     *
+     *
+     */
+    public function userFromCode($code){
+        // 获取手机号
+        $result							    = $this->app->oauth->userFromCode($code);
+        // 判断结果
+        if( !empty($result['errcode']) )    return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
+        // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
+        return                              $result;
+    }
+
+    /**
+     * 获取JSSDK的配置数组
+     * @return array
+     */
+    public function getJssdkConfig($url=''){
+        // 如果指定路径的话
+        if( $url )                          $this->app->jssdk->setUrl($url);
+        // 获取JSSDK的配置对象
+        $config                             = $this->app->jssdk->buildConfig(['updateAppMessageShareData','updateTimelineShareData'],false,false,false);
+        // 获取JSSDK的配置数组
+        return                              $config;
+    }
+    /**
+     * 发送一次性订阅消息
+     * @param array $params
+     *
+     */
+    public function sendSubscription($params){
+        // 获取手机号
+        $result							    = $this->app->template_message->sendSubscription([
+            'touser' => $params['openid'],
+            'template_id' => 'template-id',
+            'url' => $params['url'],
+            'scene' => 1000,
+            'data' => $params['data'],
+        ]);
+        // 判断结果
+        if( !empty($result['errcode']) )    return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
+        // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
+        return                              $result;
+    }
+    /**
+     * 发送模板消息
+     * @param array $params
+     *
+     */
+    public function send($params){
+        // 获取手机号
+        $result							    = $this->app->template_message->send([
+            'touser' => $params['openid'],
+            'template_id' => 'template-id',
+            'url' => $params['url'],
+            'scene' => 1000,
+            'data' => $params['data'],
+        ]);
+        // 判断结果
+        if( !empty($result['errcode']) )    return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
+        // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
+        return                              $result;
+    }
+}

+ 6 - 2
composer.json

@@ -22,7 +22,8 @@
         "laravel/tinker": "^2.5",
         "phpoffice/phpspreadsheet": "^1.12",
         "vinkla/hashids": "^9.1",
-        "wantp/snowflake": "^1.2"
+        "wantp/snowflake": "^1.2",
+        "w7corp/easywechat": "^5.35"
     },
     "require-dev": {
         "facade/ignition": "^2.5",
@@ -70,7 +71,10 @@
     "config": {
         "optimize-autoloader": true,
         "preferred-install": "dist",
-        "sort-packages": true
+        "sort-packages": true,
+        "allow-plugins": {
+            "easywechat-composer/easywechat-composer": true
+        }
     },
     "minimum-stability": "dev",
     "prefer-stable": true

Разница между файлами не показана из-за своего большого размера
+ 370 - 319
composer.lock


+ 5 - 0
config/wechat.php

@@ -20,5 +20,10 @@ return [
         'host_url'             => env('WECHAT_OPENPLAT_HOST_URL', 'https://retrieveapi.dfwy.tech/'),
         'release_host_url'     => env('WECHAT_OPENPLAT_RELEASE_HOST_URL', 'https://retrieveapi.findit.ltd/'),
     ],
+    'official' => [
+        'app_id' => env('OFFICIAL_APP_ID', 'wxb2a449728522cdaf'),
+        'secret' => env('OFFICIAL_SECRET', '734627d9e8c4a77a83cc58021cfc2e09'),
+        'response_type' => 'array',
+    ],
 
 ];

Некоторые файлы не были показаны из-за большого количества измененных файлов