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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
 
namespace app\openapi\model\app;
 
use app\admin\model\page\Page as PageModel;
use app\admin\model\Shop as ShopUser;
use app\common\model\app\App as AppModel;
use app\admin\model\user\Grade as GradeModel;
use app\common\model\shop\User as UserModel;
use app\admin\model\Setting as SettingModel;
use app\agent\model\Client as ClientModel;
use app\common\model\shop\Role as RoleModel;
use app\common\model\shop\UserRole as UserRoleModel;
class App extends AppModel
{
    /**
     * 获取列表
     */
    public function getList($limit)
    {
        return $this->alias('app')->field(['app.*,user.user_name'])
            ->join('shop_user user', 'user.app_id = app.app_id','left')
            ->where('user.is_super', '=', 1)
            ->where('app.is_delete', '=', 0)
            ->order(['create_time' => 'asc'])
            ->paginate($limit);
    }
 
    /**
     * 新增记录
     */
    public function add($data,$client_id=0)
    {
        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
 
        if (ShopUser::checkExist($data['user_name'])) {
            $this->error = '商家用户名已存在';
            return false;
        }
 
        if($data['no_expire'] == 'true'){
            $data['expire_time'] = 0;
        }else{
            $data['expire_time'] = strtotime($data['expire_time']);
        }
 
        if($data['weixin_service'] == 'true'){
            $data['weixin_service'] = 1;
        }else{
            $data['weixin_service'] = 0;
        }
 
        $this->startTrans();
        try {
            // 添加记录
            $this->save($data);
 
            // 新增商家用户信息
            $ShopUser = new ShopUser;
            if (!$ShopUser->add($this['app_id'], $data)) {
                $this->error = $ShopUser->error;
                return false;
            }
 
            // 新增应用diy配置
            (new PageModel)->insertDefault($this['app_id']);
            // 默认等级
            (new GradeModel)->insertDefault($this['app_id']);
            (new SettingModel)->insertDefault($this['app_id'],$this['app_name']);
            //新增代理商管理员
            if($client_id>0){
                $this->saveClient($data,$this['app_id'],$client_id);
            }
 
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 修改记录
     */
    public function edit($data)
    {
        $this->startTrans();
        try {
            $save_data = [
                'app_name' => $data['app_name'],
                'application_id' => $data['application_id'],
            ];
 
            if($data['no_expire'] == 'true'){
                $save_data['expire_time'] = 0;
            }else{
                $save_data['expire_time'] = strtotime($data['expire_time_text']);
            }
 
            if($data['weixin_service'] == 'true'){
                $save_data['weixin_service'] = 1;
            }else{
                $save_data['weixin_service'] = 0;
            }
 
            $this->where(["app_id"=>$data['app_id']])->update($save_data);
          //  $this->save($save_data);
            /*$user_data = [
                'user_name' => $data['user_name']
            ];
 
            if (!empty($data['password'])) {
                $user_data['password'] = salt_hash($data['password']);
                $user_data['real_password'] =$data['password'];
            }
 
            $shop_user = (new ShopUser())->where('app_id', '=', $this['app_id'])->where('is_super', '=', 1)->find();
            if($shop_user['user_name'] != $data['user_name']){
                if (ShopUser::checkExist($data['user_name'])) {
                    $this->error = '商家用户名已存在';
                    return false;
                }
            }
            $shop_user->save($user_data);*/
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
 
    /**
     * 重置账号密码
     */
    public function editUser($data)
    {
        $this->startTrans();
        try {
            $user_data = [
                'user_name' => $data['user_name']
            ];
 
            if (!empty($data['password'])) {
                $user_data['password'] = salt_hash($data['password']);
                $user_data['real_password'] =$data['password'];
            }
 
            $shop_user = (new ShopUser())->where('app_id', '=', $this['app_id'])->where('is_super', '=', 1)->find();
            if($shop_user['user_name'] != $data['user_name']){
                if (ShopUser::checkExist($data['user_name'])) {
                    $this->error = '商家用户名已存在';
                    return false;
                }
            }
            $shop_user->save($user_data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 软删除
     */
    public function setDelete()
    {
        //删除商城,商城下面的所有用户都会被删除
        (new ShopUser())->where('app_id', '=', $this['app_id'])->save(['is_delete' => 1]);
        return $this->save(['is_delete' => 1]);
    }
 
 
    /**
     * 服务商支付开启关闭
     */
    public function updateWxStatus()
    {
        return $this->save([
            'weixin_service' => !$this['weixin_service'],
        ]);
    }
 
    public function saveClient($data,$app_id,$client_id){
        $ClientModel = new ClientModel();
        $Client=$ClientModel->where('client_id','=',$client_id)->find();
        if(empty($Client)){
            return false;
        }
        $Client->save(['app_id' => $app_id]);
 
        //新增角色
        $RoleModel=new RoleModel();
        $arrRole = [
            'role_name' => '商城管理员',
            'sort' => 1,
            'app_id' => $app_id,
            'client_id'=> $client_id,
        ];
        $res = $RoleModel->create($arrRole);
 
        // 新增管理员
        $arr = [
            'user_name' => $this->make_coupon_card(),
            'password' => $this->make_coupon_card(),
            'real_name' => $data['user_name'],
            'role_id' => $res['role_id'],
            'client_id' => $data['client_id'],
            'app_id' => $app_id
        ];
 
        if ($this->addDlShop($arr)) {
            return true;
        }
 
    }
 
    //生成8位随机码 :
    public function make_coupon_card() {
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0,25)]
            .strtoupper(dechex(date('m')))
            .date('d').substr(time(),-5)
            .substr(microtime(),2,5)
            .sprintf('%02d',rand(0,99));
        for(
            $a = md5( $rand, true ),
            $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
            $d = '',
            $f = 0;
            $f < 8;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
            $f++
        );
        return  $d;
 
    }
 
    public function addDlShop($data)
    {
        // $this->startTrans();
        // try {
        $arr = [
            'user_name' => trim($data['user_name']),
            'password' => salt_hash($data['password']),
            'real_password' => trim($data['password']),
            'real_name' => trim($data['real_name']),
            //'role_id' => $data['role_id'],
            'app_id' => $data['app_id'],
            'client_id' => $data['client_id'],
        ];
        $UserModel = new UserModel();
        $res = $UserModel->create($arr);
 
        $add_arr = [];
        $model = new UserRoleModel();
        $add_arr[] = [
            'shop_user_id' => $res['shop_user_id'],
            'role_id' => $data['role_id'],
            'app_id' => $data['app_id'],
        ];
 
        $model->saveAll($add_arr);
        // 事务提交
        //     $this->commit();
        //     return true;
        // } catch (\Exception $e) {
        //     $this->error = $e->getMessage();
        //     $this->rollback();
        //     return false;
        // }
    }
 
}