<?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;
|
// }
|
}
|
|
}
|