huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
 
namespace app\api\controller;
 
use app\api\model\user\User as UserModel;
use app\api\model\supplier\Supplier as SupplierModel;
use app\api\model\App as AppModel;
use app\common\exception\BaseException;
use app\common\library\easywechat\AppMp;
use app\JjjController;
use think\facade\Env;
use think\facade\Cache;
/**
 * API控制器基类
 */
class Controller extends JjjController
{
 
    // app_id
    protected $app_id;
 
    /**
     * 后台初始化
     */
    public function initialize()
    {
        // 当前小程序id
        $this->app_id = $this->getAppId();
        // 验证当前小程序状态
        $this->checkWxapp();
    }
 
    /**
     * 获取当前应用ID
     */
    private function getAppId()
    {
        if (!$app_id = $this->request->param('app_id')) {
            throw new BaseException(['msg' => '缺少必要的参数:app_id']);
        }
        return $app_id;
    }
 
    /**
     * 验证当前小程序状态
     */
    private function checkWxapp()
    {
        $app = AppModel::detail($this->app_id);
        if (empty($app)) {
            throw new BaseException(['msg' => '当前应用信息不存在']);
        }
        if ($app['is_recycle'] || $app['is_delete']) {
            throw new BaseException(['msg' => '当前应用已暂停使用']);
        }
    }
 
    /**
     * 获取当前用户信息
     */
    protected function getUser($is_force = true)
    {
        if (!$token = $this->request->param('token')) {
            if ($is_force) {
                throw new BaseException(['msg' => '缺少必要的参数:token', 'code' => -1]);
            }
            return false;
        }
        if (!$user = UserModel::getUser($token)) {
            if ($is_force) {
                throw new BaseException(['msg' => '没有找到用户信息', 'code' => -1]);
            }
            return false;
        }
        if ($user['is_delete'] == 1) {
            throw new BaseException(['msg' => '没有找到用户信息', 'code' => -2]);
            Cache::delete($token);
        }
        return $user;
    }
 
    /**
     * 获取当前用户信息
     */
    protected function getSupplierUser($user)
    {
        if (!$user['supplierUser'] && !$user['clerkUser']) {
            throw new BaseException(['msg' => '非法请求', 'code' => -1]);
        }
        if(empty($user['supplierUser']) && !empty($user['clerkUser'])){
            $SupplierModel=new SupplierModel();
            return $SupplierModel->getDetail(['shop_supplier_id'=>$user['clerkUser']['shop_supplier_id']],'');
        }
        return $user['supplierUser'];
    }
 
 
    protected function getShareParams($url, $title = '', $desc = '', $link = '', $imgUrl = '')
    {
        $signPackage = '';
        $shareParams = '';
        if (Env::get('APP_DEBUG')) {
            return [
                'signPackage' => $signPackage,
                'shareParams' => $shareParams
            ];
        }
        if ($url != '') {
            $app = AppMp::getApp($this->app_id);
            $app->jssdk->setUrl($url);
            $signPackage = $app->jssdk->buildConfig(array('updateAppMessageShareData', 'updateTimelineShareData'), false);
            $shareParams = [
                'title' => $title,
                'desc' => $desc,
                'link' => $link,
                'imgUrl' => $imgUrl,
            ];
        }
        return [
            'signPackage' => $signPackage,
            'shareParams' => $shareParams
        ];
    }
 
    protected function getScanParams($url)
    {
        $signPackage = '';
        if (Env::get('APP_DEBUG')) {
            return [
                'signPackage' => $signPackage
            ];
        }
        if ($url != '') {
            $app = AppMp::getApp($this->app_id);
            $app->jssdk->setUrl($url);
            $signPackage = $app->jssdk->buildConfig(array('scanQRCode'), false);
        }
        return [
            'signPackage' => $signPackage
        ];
    }
}