model = Db::name('user'); } /** * 用户换绑 * @return void */ public function userBindCompany($params=[]) { $result = [ 'status' => 1, 'msg' => '操作成功', 'data' => [], ]; try { $userId = isset($params['user_id']) ? $params['user_id'] : 0; $companyId = isset($params['company_id']) ? $params['company_id'] : 0; $where['id'] = $userId; $user = Db::name('user')->where($where['id'])->find(); if (!empty($user)) { if ($user['company_id'] != $companyId) { $userData['company_id'] = $companyId; $userData['updatetime'] = time(); $userRes = Db::name('user')->where($where['id'])->update($userData); if (!$userRes) { throw new Exception('操作失败'); } } } } catch (Exception $e) { $result['status'] = 0; $result['msg'] = $e->getMessage(); } return $result; } /** * 用户绑定门店和钱包 * @return void */ public function userWallet($params=[]) { $result = [ 'status' => 1, 'msg' => '操作成功', 'data' => [], ]; try { $userId = isset($params['user_id']) ? $params['user_id'] : 0; $companyId = isset($params['company_id']) ? $params['company_id'] : 0; $comefrom = isset($params['comefrom']) ? $params['comefrom'] : ''; $bindRes = $this->userBindCompany($params);//绑定门店 if (!$bindRes['status']) { throw new Exception($bindRes['msg']); } $where['user_id'] = $userId; $where['company_id'] = $companyId; $userWallet = Db::name('user_wallet')->where($where['id'])->find(); $time = time(); if (empty($userWallet)) { $userWalletData = [ 'user_id' => $userId, 'company_id' => $companyId, 'money' => 0.00, 'createtime' => $time, 'comefrom' => $comefrom, ]; $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData); if (!$userWalletRes) { throw new Exception('生成钱包失败'); } } } catch (Exception $e) { $result['status'] = 0; $result['msg'] = $e->getMessage(); } return $result; } }