where([ // 'is_del' => $db::$is_del_normal, // 'mobile' => $param['mobile'] // ]) // ->findOrEmpty() // ->toArray(); // // if (empty($res)) return json_show(1004, '该账号不存在'); // if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用'); // // $password = get_encryption_password($param['password'], $res['salt']); // if ($password['password'] != $res['password']) return json_show(1004, '密码错误'); // // //更新token // $token = Str::random(40, -1); // // $expire_int = mt_rand(3600, 7200); // $expire_time = date('Y-m-d H:i:s', time() + $expire_int); // // $rs = $db // ->where($db->getPk(), $res[$db->getPk()]) // ->save(['token' => $token, 'expire_time' => $expire_time]); // // if (!$rs) return json_show(1005, '更新账号token信息失败'); // // return json_show(0, '登录成功', ['token' => $token, 'expire_time' => $expire_time]); // // // } //获取用户信息 // public static function getUserInfo(string $token = '') // { // $db = new SupplierUserModel(); // // $res = $db // ->where(['is_del' => $db::$is_del_normal, 'status' => $db::$status_normal, 'token' => $token]) // ->where('expire_time', '>=', date('Y-m-d H:i:s')) // ->findOrEmpty() // ->toArray(); // // if (empty($res)) return json_show(1004, '该账号不存在'); // if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用'); // // $list = SupplierRelationUserModel::field('id,supplierNo,supplierName') // ->where(['is_del' => $db::$is_del_normal, 'uid' => $res['uid']]) // ->select() // ->toArray(); // // //获取这些供应商的状态 // $status = Db::name('supplier') // ->where('is_del', 0) // ->whereIn('code', array_column($list, 'supplierNo')) // ->column('status', 'code'); // // foreach ($list as &$value) { // $value['status'] = $status[$value['supplierNo']]; // } // // $info = [ // 'uid' => $res['uid'], // 'nickname' => $res['nickname'], // 'mobile' => $res['mobile'], // 'email' => $res['email'], // 'token' => $res['token'], // 'expire_time' => $res['expire_time'], // 'supplier_list' => $list, // ]; // // return json_show(0, '获取用户信息成功', $info); // // // } //获取供应商账号列表 public static function getAccountList(array $param = []) { $db = new SupplierUserModel(); $rs = $db ->where('is_del', $db::$is_del_normal); if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%'); if ($param['status'] != '') $rs->where('status', $param['status']); if ($param['supplierNo'] != '') { $uids = SupplierRelationUserModel::where([ 'is_del' => $db::$is_del_normal, 'supplierNo' => $param['supplierNo'] ])->column('uid'); $rs->whereIn('uid', $uids); } $count = $rs->count('uid'); $list = $rs ->field('uid,nickname,mobile,email,status,addtime,creater') ->order('addtime', 'desc') ->page($param['page'], $param['size']) ->append(['supplier_list']) ->withAttr('supplier_list', function ($val, $data) use ($db) { return SupplierRelationUserModel::where([ 'is_del' => $db::$is_del_normal, 'uid' => $data['uid'] ])->field('supplierNo,supplierName,status') ->select() ->toArray(); }) ->select() ->toArray(); return json_show(0, '获取成功', ['count' => $count, 'list' => $list]); } //修改供应商账号密码 public static function changePassword(array $param = []) { $db = new SupplierUserModel(); $res = $db ->where([ 'uid' => $param['uid'], 'is_del' => $db::$is_del_normal, ]) ->field('uid,status') ->findOrEmpty() ->toArray(); if (empty($res)) return json_show(1004, '该账号不存在'); if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用'); //更新密码 $password = get_encryption_password($param['password']); $rs = $db ->where('uid', $res['uid']) ->save(['password' => $password['password'], 'salt' => $password['salt']]); return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败'); } //获取供应商账号账号信息 public static function readAccount(array $param = []) { $res = SupplierUser::field('uid,nickname,mobile,email') ->where([ 'uid' => $param['uid'], 'is_del' => SupplierUser::$is_del_normal, ]) ->append(['supplier_list']) ->withAttr('supplier_list', function ($val, $data) { return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime') ->where([ 'is_del' => SupplierUser::$is_del_normal, 'uid' => $data['uid'] ]) ->select() ->toArray(); }) ->findOrEmpty() ->toArray(); return json_show(0, '获取账号信息成功', $res); } //添加供应商账号 public static function addAccount(array $param = [], string $token = '') { $user = GetUserInfo($token); //查找供应商名称 $supplierName = Db::name('supplier') ->whereIn('code', $param['supplierNo']) ->column('name', 'code'); if (empty($supplierName)) return json_show(1004, '供应商不存在'); Db::connect('mysql_sys')->startTrans(); try { $db = new SupplierUserModel(); $res = $db ->field('uid') ->where([ 'is_del' => $db::$is_del_normal, 'mobile' => $param['mobile'], ])->findOrEmpty(); if (!$res->isEmpty()) throw new Exception('该手机号已存在'); //新增账号 //默认密码存在app配置文件中,由于入口在admin应用中,所以如果将配置项放在abutment/config下面是读取不到的 $password = get_encryption_password(Config::get('app.default_password')); $uid = $db->insertGetId([ 'nickname' => $param['nickname'],//姓名 'mobile' => $param['mobile'],//手机号 'email' => $param['email'],//邮箱 'password' => $password['password'],//密码密文 'salt' => $password['salt'],//盐值 'status' => $db::$status_normal, 'is_del' => $db::$is_del_normal, 'createrid' => $user['data']['user_id'] ?? 0, 'creater' => $user['data']['nickname'] ?? '', ]); $insert_data = []; foreach ($param['supplierNo'] as $supplierNo) { $insert_data[] = [ 'uid' => $uid, 'supplierNo' => $supplierNo, 'supplierName' => $supplierName[$supplierNo] ?? '', 'status' => $db::$status_normal, 'is_del' => $db::$is_del_normal, 'createrid' => $user['data']['user_id'] ?? 0, 'creater' => $user['data']['nickname'] ?? '', ]; } if ($insert_data) Db::connect('mysql_sys') ->name('supplier_relation_user') ->insertAll($insert_data); Db::connect('mysql_sys')->commit(); return json_show(0, '操作成功'); } catch (Exception $exception) { Db::connect('mysql_sys')->rollback(); return json_show(1005, '操作失败' . $exception->getMessage()); } } //修改供应商账号 public static function editAccount(array $param = []) { $user = GetUserInfo($param['token']); Db::connect('mysql_sys')->startTrans(); try { $db = new SupplierUserModel(); $res = $db ->field('uid,mobile') ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal]) ->findOrEmpty() ->toArray(); if (empty($res)) return json_show(1004, '该账号不存在'); if ($res['mobile'] != $param['mobile']) { $temp = $db ->field('uid') ->where(['mobile' => $param['mobile'], 'is_del' => $db::$is_del_normal]) ->where('uid', '<>', $param['uid']) ->findOrEmpty() ->isEmpty(); if (!$temp) throw new Exception('要修改的手机号已存在'); } $db ->where('uid', $param['uid']) ->strict(false) ->save($param); $relation_db = new SupplierRelationUserModel(); $insert = $retain = []; foreach ($param['supplier_list'] as $supplier) { if (isset($supplier['id']) && $supplier['id'] != 0) $retain[] = $supplier['id']; else $insert[] = [ 'uid' => $param['uid'], 'supplierNo' => $supplier['supplierNo'], 'supplierName' => $supplier['supplierName'], 'status' => $db::$status_normal, 'is_del' => $db::$is_del_normal, 'createrid' => $user['data']['user_id'] ?? 0, 'creater' => $user['data']['nickname'] ?? 0, ]; } //除了保留id,其余删除 $delete_where = [['is_del', '=', $db::$is_del_normal], ['uid', '=', $param['uid']]]; if (!empty($retain)) $delete_where[] = ['id', 'not in', $retain]; $relation_db->where($delete_where)->save(['is_del' => $db::$is_del_deleted]); if ($insert) $relation_db->insertAll($insert); Db::connect('mysql_sys')->commit(); return json_show(0, '操作成功'); } catch (Exception $exception) { Db::connect('mysql_sys')->rollback(); return json_show(1005, '操作失败,' . $exception->getMessage()); } } //修改供应商账号状态 public static function statusAccount(array $param = []) { Db::connect('mysql_sys')->startTrans(); try { $db = new SupplierUserModel(); $res = $db ->field('uid,status') ->where([ 'uid' => $param['uid'], 'is_del' => $db::$is_del_normal, ])->findOrEmpty(); if ($res->isEmpty()) throw new Exception('该账号不存在'); if ($res->status == $param['status']) throw new Exception('不能重复操作'); $db->where([ 'uid' => $param['uid'], 'is_del' => $db::$is_del_normal, ])->where('status', '<>', $param['status']) ->save(['status' => $param['status']]); SupplierRelationUserModel::where([ 'uid' => $param['uid'], 'is_del' => $db::$is_del_normal, ])->where('status', '<>', $param['status']) ->save(['status' => $param['status']]); Db::connect('mysql_sys')->commit(); return json_show(0, '操作成功'); } catch (Exception $exception) { Db::connect('mysql_sys')->rollback(); return json_show(1005, '操作失败,' . $exception->getMessage()); } } //删除供应商账号 public static function deleteAccount(int $uid = 0) { Db::connect('mysql_sys')->startTrans(); try { $db = new SupplierUserModel(); $res = $db ->field('uid') ->where([ 'uid' => $uid, 'is_del' => $db::$is_del_normal, ])->findOrEmpty(); if ($res->isEmpty()) throw new Exception('该账号不存在或已删除'); $db->where('uid', $uid) ->where('is_del', $db::$is_del_normal) ->save(['is_del' => $db::$is_del_deleted]); SupplierRelationUserModel::where('uid', $uid) ->where('is_del', $db::$is_del_normal) ->save(['is_del' => $db::$is_del_deleted]); Db::connect('mysql_sys')->commit(); return json_show(0, '删除成功'); } catch (Exception $exception) { Db::connect('mysql_sys')->rollback(); return json_show(1005, '删除失败,' . $exception->getMessage()); } } }